package com.ghy.web.pojo.vo;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.ghy.common.annotation.SysConstants;
import com.ghy.common.enums.SysGlobalConfigDataTypeEnum;
import com.ghy.common.enums.SysGlobalConfigEnum;
import com.ghy.common.utils.ObjectVerifyUtils;
import com.ghy.system.domain.SysGlobalConfig;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
import lombok.experimental.Accessors;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author 但星霖
 * @Date 2023-03-20 20：37：16
 * 说明：系统配置返回vo
 */
@Data
@ToString
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
@Schema(description = "系统配置返回vo")
public class SysGlobalConfigResponseVO {

    @Schema(description = "配置项主键ID")
    private Long id;

    @Schema(description = "配置项名称")
    private String name;

    @Schema(description = "配置项内容json串")
    private String content;

    @Schema(description = "排序号")
    private Integer sort;

    @Schema(description = "描述")
    private String describe;

    @Schema(description = "配置类型")
    private Integer type;

    @Schema(description = "配置类型枚举")
    private SysGlobalConfigEnum typeEnum;

    @Schema(description = "配置类型Desc")
    private String typeEnumDesc;

    @Schema(description = "数据内容类型")
    private Integer dataType;

    @Schema(description = "数据内容类型")
    private SysGlobalConfigDataTypeEnum dataTypeEnum;

    @Schema(description = "数据内容类型")
    private String dataTypeEnumDesc;

    @Schema(description = "单位")
    private String configUnit;

    @JsonInclude(value = JsonInclude.Include.NON_NULL)
    @JsonFormat(pattern = SysConstants.DATE_TIME_FORMAT, locale = "zh", timezone = "GMT+8")
    @Schema(description = "创建时间")
    private Date createTime;

    @JsonInclude(value = JsonInclude.Include.NON_NULL)
    @JsonFormat(pattern = SysConstants.DATE_TIME_FORMAT, locale = "zh", timezone = "GMT+8")
    @Schema(description = "更新时间")
    private Date updateTime;

    public static SysGlobalConfigResponseVO toVo(SysGlobalConfig model) {
        if (ObjectVerifyUtils.isNull(model)) {
            return null;
        }
        SysGlobalConfigResponseVO vo = new SysGlobalConfigResponseVO();
        BeanUtils.copyProperties(model, vo);
        // 配置类型
        if (ObjectVerifyUtils.isNotNull(model.getType())) {
            SysGlobalConfigEnum configEnum = SysGlobalConfigEnum.parse(model.getType());
            if (ObjectVerifyUtils.isNotNull(configEnum)) {
                vo.setTypeEnum(configEnum);
                vo.setTypeEnumDesc(configEnum.desc());
            }
        }
        // 配置类型数据类型
        if (ObjectVerifyUtils.isNotNull(model.getDataType())) {
            SysGlobalConfigDataTypeEnum sysConfigDataTypeEnum = SysGlobalConfigDataTypeEnum.parse(model.getDataType());
            if (ObjectVerifyUtils.isNotNull(sysConfigDataTypeEnum)) {
                vo.setDataTypeEnum(sysConfigDataTypeEnum);
                vo.setDataTypeEnumDesc(sysConfigDataTypeEnum.desc());
            }
        }
        // 时间类单独处理
        vo.setCreateTime(ObjectVerifyUtils.isNotNull(model.getCreateTime()) ? new Date(model.getCreateTime()) : null);
        vo.setUpdateTime(ObjectVerifyUtils.isNotNull(model.getUpdateTime()) ? new Date(model.getUpdateTime()) : null);
        return vo;
    }

    public static List<SysGlobalConfigResponseVO> toVoList(List<SysGlobalConfig> sysConfigList) {
        List<SysGlobalConfigResponseVO> voList = new ArrayList<>();
        if (ObjectVerifyUtils.isNotEmpty(sysConfigList)) {
            sysConfigList.forEach(sysConfig -> voList.add(toVo(sysConfig)));
        }
        return voList;
    }
}
