package cn.estsh.i3plus.ext.mes.pcn.pojo.model;

import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtConstWords;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtEnumUtil;
import cn.estsh.i3plus.pojo.mes.bean.MesDataObject;
import cn.estsh.i3plus.pojo.mes.bean.MesDatasource;
import cn.estsh.i3plus.pojo.mes.bean.MesObjectCfg;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.ApiParam;
import lombok.Data;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Data
public class SxThirdPartyDbCollectDataModel implements Serializable {

    private static final long serialVersionUID = -2147511121082046955L;

    @ApiParam("过程条码拼接字段")
    private String serialNumberArrField;

    @ApiParam("产品条码拼接字段")
    private String productSnArrField;

    @ApiParam("动态反射字段   基于List<SxThirdPartyDbDynamicsFieldModel>的JSON数据")
    private String dynamicsReflectField;

    @ApiParam("动态表名  继承BaseBean的全路径Bean对象类名,同时需创建基于Bean对象的Repository类")
    private String dynamicsTableName;

    @ApiParam("固定字段写值   基于Map<String, Object>的JSON数据")
    private String defaultValueField;

    @ApiParam("业务自定义方法字段   基于Map<String, ISxThirdPartyDbCollectDataMethodService>的JSON数据")
    private String methodField;

    @ApiParam("第三方表名")
    private String otherTableName;

    @ApiParam("对等同步表名")
    private String paritySyncTableName;

    @ApiParam("对等同步数据库类型")
    private String paritySyncDatabase;

    @ApiParam("是否使用自增列")
    private String isSelfAddition;

    @ApiParam("排序字段")
    private String orderField;

    @ApiParam("查询数量")
    private String limitRows;

    @ApiParam("默认策略")
    private Integer defaultStrategy;

    @ApiParam("实现策略")
    private String strategyClass;

    @ApiParam("组织代码")
    private String organizeCode;

    @ApiParam("操作人")
    private String userInfo;

    @ApiParam("JOB配置代码")
    private String cfgCode;

    @ApiParam("对象代码")
    private String objectCode;

    @ApiParam("对象代码配置值")
    private String cfgValue;

    @ApiParam("是否使用自增列")
    private Boolean isSelfAdditionBoo;

    @ApiParam("执行步骤")
    private Integer state;

    @ApiParam("无额外验证")
    private Boolean noExtraValidate;

    @ApiParam("采集结果")
    private Boolean execResult;

    @ApiParam("错误信息")
    private String error;

    @ApiParam("执行数据量")
    private Integer execSize;

    @ApiParam("动态字段集合")
    private List<SxThirdPartyDbDynamicsFieldModel> dynamicsFieldList;

    @ApiParam("地址清单")
    private MesDatasource dataSource;

    @ApiParam("数据对象")
    private MesDataObject dataObject;

    @ApiParam("对象结构")
    private List<MesObjectCfg> objectCfgList;

    @ApiParam("采集数据")
    private List<Map<String, Object>> mapList;

    @ApiParam("预留数据存储")
    private Map<String, Object> dataMap;

    public String checkValid() {
        if (!StringUtils.isEmpty(this.defaultStrategy) && !StringUtils.isEmpty(MesPcnExtEnumUtil.SX_THIRD_PARTY_DB_COLLECT_DATA_STRATEGY.valueOfClazz(this.defaultStrategy)))
            this.strategyClass = MesPcnExtEnumUtil.SX_THIRD_PARTY_DB_COLLECT_DATA_STRATEGY.valueOfClazz(this.defaultStrategy);
        if (StringUtils.isEmpty(this.strategyClass)) return String.format("默认策略[%s]或自定义策略[%s]", MesPcnExtConstWords.DEFAULT_STRATEGY, MesPcnExtConstWords.STRATEGY_CLASS);
        if (StringUtils.isEmpty(this.otherTableName)) return String.format("第三方表名[%s]", MesPcnExtConstWords.OTHER_TABLE_NAME);
        if (StringUtils.isEmpty(this.isSelfAddition)) return String.format("是否使用自增列[%s]", MesPcnExtConstWords.IS_SELF_ADDITION);
        if (StringUtils.isEmpty(this.orderField)) return String.format("排序字段[%s]", MesPcnExtConstWords.ORDER_FIELD);
        return null;
    }

    public SxThirdPartyDbCollectDataModel organizeCode(String organizeCode) {
        this.organizeCode = organizeCode;
        return this;
    }

    public SxThirdPartyDbCollectDataModel userInfo(String userInfo) {
        this.userInfo = userInfo;
        return this;
    }

    public SxThirdPartyDbCollectDataModel cfgCode(String cfgCode) {
        this.cfgCode = cfgCode;
        return this;
    }

    public SxThirdPartyDbCollectDataModel objectCode(String objectCode) {
        this.objectCode = objectCode;
        return this;
    }

    public SxThirdPartyDbCollectDataModel cfgValue(String cfgValue) {
        this.cfgValue = cfgValue;
        return this;
    }

    public SxThirdPartyDbCollectDataModel paritySyncDatabase(String paritySyncDatabase) {
        this.paritySyncDatabase = paritySyncDatabase;
        return this;
    }

    public SxThirdPartyDbCollectDataModel noExtraValidate(Boolean noExtraValidate) {
        this.noExtraValidate = noExtraValidate;
        return this;
    }

    public SxThirdPartyDbCollectDataModel defaultValue() {
        if (StringUtils.isEmpty(this.limitRows)) this.limitRows = MesPcnExtConstWords.LIMIT_ROWS_DEFAULT_100;
        this.execSize = MesPcnExtConstWords.ZERO;
        this.state = MesPcnExtConstWords.STATE_1;
        this.noExtraValidate = true;
        this.execResult = false;
        return this;
    }

    public SxThirdPartyDbCollectDataModel dynamicsFieldList(List<SxThirdPartyDbDynamicsFieldModel> dynamicsFieldList) {
        this.dynamicsFieldList = dynamicsFieldList;
        return this;
    }

    public SxThirdPartyDbCollectDataModel dataMap(String key, Object obj) {
        if (CollectionUtils.isEmpty(this.dataMap)) this.dataMap = new HashMap<>();
        this.dataMap.put(key, obj);
        return this;
    }

    public SxThirdPartyDbCollectDataModel dataSource(MesDatasource dataSource) {
        this.dataSource = dataSource;
        return this;
    }

    public SxThirdPartyDbCollectDataModel dataObject(MesDataObject dataObject) {
        this.dataObject = dataObject;
        return this;
    }

    public SxThirdPartyDbCollectDataModel objectCfgList(List<MesObjectCfg> objectCfgList) {
        this.objectCfgList = objectCfgList;
        return this;
    }

    public SxThirdPartyDbCollectDataModel mapList(List<Map<String, Object>> mapList) {
        this.mapList = mapList;
        return this;
    }

    public String error(String error) {
        this.error = error;
        return error;
    }

    public void noError() {
        this.error = null;
    }

    public SxThirdPartyDbCollectDataModel execSize(Integer execSize) {
        this.execSize = execSize;
        return this;
    }

    public SxThirdPartyDbCollectDataModel state(Integer state) {
        this.state = state;
        return this;
    }

    public SxThirdPartyDbCollectDataModel complete() {
        this.state = MesPcnExtConstWords.ZERO;
        this.execResult = true;
        return this;
    }

    public static void main(String[] args) {
        SxThirdPartyDbCollectDataModel model = new SxThirdPartyDbCollectDataModel();
        model.setSerialNumberArrField("serial_number1");
        model.setProductSnArrField("serial_number1");

        List<SxThirdPartyDbDynamicsFieldModel> dynamicsFieldList = new ArrayList<>();
        SxThirdPartyDbDynamicsFieldModel dynamicsField = new SxThirdPartyDbDynamicsFieldModel();
        dynamicsField.setFieldName("物料编码");
        dynamicsField.setSourceField("part_no1");
        dynamicsField.setTargetField("part_no");
        SxThirdPartyDbDynamicsFieldModel dynamicsField1 = new SxThirdPartyDbDynamicsFieldModel();
        dynamicsField1.setFieldName("物料名称");
        dynamicsField1.setSourceField("part_name_rdd1");
        dynamicsField1.setTargetField("part_name_rdd");
        dynamicsFieldList.add(dynamicsField);
        dynamicsFieldList.add(dynamicsField1);
        model.setDynamicsReflectField(JSONObject.toJSONString(dynamicsFieldList));
        model.setDynamicsTableName("MesProduceSnThirdParty");

//        model.setParitySyncTableName("mes_data_collect_test");
        model.setOtherTableName("mes_data_collect_test0");
        model.setIsSelfAddition("1");
        model.setOrderField("id");
        model.setDefaultStrategy(20);

        Map<String, Object> defaultMap = new HashMap<>();
        defaultMap.put("qcStatus", 10);
        defaultMap.put("snStatus", 20);
        model.setDefaultValueField(JSONObject.toJSONString(defaultMap));

        Map<String, String> methodMap = new HashMap<>();
        methodMap.put("date", "sxThirdPartyDbCollectDataMethodGetDateService");
        model.setMethodField(JSONObject.toJSONString(methodMap));

        System.out.println(JSONObject.toJSONString(model));


    }

}
