package com.bohuikeji.delivery.backstage.entity;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.bohuikeji.frame.module.admin.annotation.TableNewField;
import com.bohuikeji.frame.module.admin.annotation.TableQueryField;
import com.bohuikeji.frame.module.admin.annotation.TableShowField;
import com.bohuikeji.frame.module.admin.base.EntityBase;
import com.bohuikeji.frame.module.admin.entity.sys.SysConfig;
import com.bohuikeji.frame.module.admin.utils.StringUtils;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;

import java.time.LocalDateTime;

/**
 * 加盟商配置信息表
 * @author 10552
 */
@Data
@EqualsAndHashCode(callSuper = true)
@Accessors(chain = true)
@ApiModel(value="加盟商配置信息表", description="加盟商配置信息表")
@TableName("t_config")
public class TConfig extends EntityBase<TConfig> {

    @ApiModelProperty(value="配置项ID")
    @TableId(value = "id", type = IdType.AUTO)
    @TableShowField(value = "ID",id = true)
    @TableNewField(show = false)
    private Integer id;

    @ApiModelProperty(value = "创建时间")
    @TableField(exist = false)
    @TableQueryField(show = false)
    @TableShowField(value = "创建时间")
    private LocalDateTime createTime;

    @ApiModelProperty(value = "更新时间")
    @TableField(exist = false)
    @TableQueryField(show = false)
    @TableShowField(value = "更新时间")
    private LocalDateTime updateTime;

    @ApiModelProperty(value = "加盟商")
    @TableField(value = "fran_id")
    @TableQueryField(show = false)
    @TableShowField(value = "加盟商")
    private Integer franId;

    @ApiModelProperty(value = "所属加盟商")
    @TableField(value = "fran_name")
    @TableQueryField(show = false)
    @TableShowField(value = "配置项名称")
    private String franName;

    @ApiModelProperty(value = "账号")
    @TableField(value = "user_id")
    @TableQueryField(show = false)
    @TableShowField(value = "账号")
    private Integer userId;

    @ApiModelProperty(value = "配置项名称")
    @TableField(value = "config_name")
    @TableQueryField(show = false)
    @TableShowField(value = "配置项名称")
    private String configName;

    @ApiModelProperty(value = "配置项KEY")
    @TableField(value = "config_key")
    @TableQueryField(show = false)
    @TableShowField(value = "配置项KEY")
    private String configKey;

    @ApiModelProperty(value = "配置项值")
    @TableField(value = "config_value")
    @TableQueryField(show = false)
    @TableShowField(value = "配置项值")
    private JSONObject configValue;

    @ApiModelProperty(value = "配置组")
    @TableField(value = "config_group")
    @TableQueryField(show = false)
    @TableShowField(value = "配置组")
    private String configGroup;

    @ApiModelProperty(value = "默认值")
    @TableField(value = "config_default")
    @TableQueryField(show = false)
    @TableShowField(value = "默认值")
    private JSONObject configDefault;

    @ApiModelProperty(value = "排序值")
    @TableField(value = "config_sort")
    @TableQueryField(show = false)
    @TableShowField(value = "排序值")
    private double configSort;

    @ApiModelProperty(value = "显示类型")
    @TableField(value = "show_type")
    @TableQueryField(show = false)
    @TableShowField(value = "显示类型")
    private double showType;


    @ApiModelProperty(value = "配置项状态")
    @TableField(value = "config_status")
    @TableQueryField(show = false)
    @TableShowField(value = "配置项状态")
    private String configStatus;

    @ApiModelProperty(value = "配置项备注")
    @TableField("config_rmk")
    @TableShowField(value = "配置项备注",show = false)
    @TableNewField(show = false)
    private String configRmk;

    @ApiModelProperty(value = "车辆记号")
    @TableField("config_mark")
    @TableShowField(value = "车辆记号",show = false)
    @TableNewField(show = false)
    private String configMark;

    /**
     * 删除策略
     * 删除策略(-1:不允许删除,0:可删除,>1:至少保留条数)
     */
    @TableField("delete_policy")
    private Integer deletePolicy;

    public SysConfig toOldConfig(){
        SysConfig sysConfig=new SysConfig();
        sysConfig.setConfigId(this.id);
        sysConfig.setConfigKey(this.configKey);
        sysConfig.setConfigGroup(this.configGroup);
        sysConfig.setConfigName(this.getConfigName());
        if(this.configValue!=null){
            if("service_type".equals(this.configGroup)){
                sysConfig.setConfigDataOne(this.configValue.getString("默认距离(公里)"));
                sysConfig.setConfigDataTwo(this.configValue.getString("续重(元/KG)"));
                sysConfig.setConfigValue(this.configValue.getString("超出默认距离续费(元/公里)"));
                sysConfig.setConfigDefault(this.configValue.getString("默认费用(元)"));
                sysConfig.setConfigMark(this.configValue.getString("说明"));
            }
            if("cargo_type".equals(this.configGroup)){
                sysConfig.setConfigValue(this.configValue.getString("加价(元)"));
                sysConfig.setConfigMark(this.configValue.getString("说明"));
            }
            else {
                String showName=this.configValue.getString("显示名称");
                if(StringUtils.isNoneBlank(showName)){
                    sysConfig.setConfigName(showName);
                }
                sysConfig.setConfigDataOne(this.configValue.getString("首重(KG)"));
                sysConfig.setConfigDataTwo(this.configValue.getString("续重(元/KG)"));
                sysConfig.setConfigValue(this.configValue.getString("距离(元/公里)"));
                sysConfig.setConfigMark(this.configValue.getString("说明"));
            }

        }
        return sysConfig;

    }
}
