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

import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtConstWords;
import cn.estsh.i3plus.pojo.base.enumutil.CommonEnumUtil;
import cn.estsh.i3plus.pojo.mes.bean.MesProductionAssembly;
import cn.estsh.i3plus.pojo.mes.bean.MesWorkCell;
import cn.estsh.i3plus.pojo.mes.bean.MesWorkOrderAssembly;
import cn.estsh.i3plus.pojo.mes.util.MesExtEnumUtil;
import io.swagger.annotations.ApiParam;
import lombok.Data;
import org.springframework.beans.BeanUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

/**
 * 生产过程上下文对象-装配件清单信息（排序）
 */
@Data
public class MesProductionAssemblySortContext extends MesProductionAssemblyContext implements Serializable {

    private static final long serialVersionUID = -1492128619505737538L;

    @ApiParam(value = "零件条码")
    private String productSn;

    @ApiParam(value = "工艺顺序号")
    private Integer processSeq;

    @ApiParam(value = "生产零件号")
    private String productPartNo;

    @ApiParam(value = "汇报零件号")
    private String reportPartNo;

    @ApiParam(value = "零件类型")
    private String partTypeCode;

    @ApiParam(value = "VPPS码")
    private String vppsCode;

    @ApiParam(value = "车型代码")
    private String vehicleNo;

    @ApiParam(value = "车系")
    private String vehicleClassNo;

    @ApiParam(value = "显示顺序")
    private Integer displaySeq;

    @ApiParam(value = "显示内容")
    private String displayValue;

    @ApiParam(value = "显示规则")
    private String displayRule;

    @ApiParam(value = "动作代码")
    private String actionCode;

    @ApiParam(value = "动作名称")
    private String actionName;

    @ApiParam(value = "汇报方式")
    private Integer reportType;

    @ApiParam(value = "SAP来源库位")
    private String srcErpLocation;

    @ApiParam(value = "SAP目标库位")
    private String destErpLocation;

    @ApiParam(value = "条码打印方式")
    private Integer printType;

    @ApiParam(value = "条码打印机")
    private String printer;

    @ApiParam(value = "条码打印模板")
    private String printTemplate;

    @ApiParam(value = "是否启动组合条码")
    private String isCombineSn;

    @ApiParam(value = "逻辑数量")
    private Double logicNum;

    @ApiParam(value = "是否启用逻辑数量")
    private Integer isLogicNum;

    @ApiParam(value = "显示颜色")
    private String showColor;

    @ApiParam(value = "是否保存过程工艺参数")
    private Integer isSaveProcessParam ;

    @ApiParam(value = "是否保存过程工艺参数类型")
    private Integer isSaveProcessParamType;

    @ApiParam(value = "扫描条码载取符")
    private String delimiter;

    @ApiParam(value = "生产组代码")
    private String productGroupCode;

    @ApiParam(value = "装配件记录表ID")
    public Long id;

    @ApiParam("开模记录ID")
    private Long mouldRecordId;

    @ApiParam("加工记录ID")
    private Long productionRecordId;

    @ApiParam(value = "工单装配件信息备注")
    public String woaRemark;

    @ApiParam(value = "装配件记录信息备注")
    public String paRemark;

    @ApiParam(value = "装配件记录表创建时间")
    public String createDatetime;

    @ApiParam(value = "装配件记录表创建人")
    public String createUser;

    @ApiParam(name = "是否顺序扫描")
    private Integer isCheckBindSeq;

    @ApiParam(name = "是否保存到DB")
    private Integer isSaveDb = CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue();

    @ApiParam(name = "是否已经存在于DB")
    private Integer isExistDb = CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue();

    public MesProductionAssemblySortContext() {}

    //前道工位
    public MesProductionAssemblySortContext copy(MesWorkOrderAssembly workOrderAssembly) {

        this.dataSource = MesExtEnumUtil.WORK_CENTER_TYPE.SORT.getValue();

        if (null != workOrderAssembly) {
            BeanUtils.copyProperties(workOrderAssembly, this, MesPcnExtConstWords.BASE_BEAN_FIELDS);
            this.sourceId = workOrderAssembly.getId();
        }

        if (StringUtils.isEmpty(this.routeSeq)) this.routeSeq = MesPcnExtConstWords.ZERO;
        if (StringUtils.isEmpty(this.processSeq)) this.processSeq = MesPcnExtConstWords.ZERO;

        if (StringUtils.isEmpty(this.assemblyStatus) || this.assemblyStatus.compareTo(MesPcnExtConstWords.ZERO) == 0 ||
                this.assemblyStatus.compareTo(MesExtEnumUtil.ASSEMBLY_STATUS.ASSEMBLY_STATUS_20.getValue()) == 0) {
            this.assemblyStatus = MesExtEnumUtil.ASSEMBLY_STATUS.ASSEMBLY_STATUS_30.getValue();
        }

        if (this.assemblyStatus.compareTo(MesExtEnumUtil.ASSEMBLY_STATUS.ASSEMBLY_STATUS_10.getValue()) == 0 ||
                this.assemblyStatus.compareTo(MesExtEnumUtil.ASSEMBLY_STATUS.ASSEMBLY_STATUS_40.getValue()) == 0) {
            this.fileIsFinished().isSaveDb().setIsExistDb(CommonEnumUtil.TRUE_OR_FALSE.TRUE.getValue());
        }

        if (this.assemblyStatus.compareTo(MesExtEnumUtil.ASSEMBLY_STATUS.ASSEMBLY_STATUS_30.getValue()) == 0 &&
                !MesExtEnumUtil.ASSEMBLY_MATCH_TYPE.checkIsNeedScan(this.matchType)) {
            this.assemblyStatus = MesExtEnumUtil.ASSEMBLY_STATUS.ASSEMBLY_STATUS_10.getValue();
        }

        return this;
    }

    //末道工位
    public MesProductionAssemblySortContext copy(MesWorkOrderAssembly workOrderAssembly, MesProductionAssembly productionAssembly, Integer isCheckBindSeq) {

        if (null != workOrderAssembly) {
            BeanUtils.copyProperties(workOrderAssembly, this, MesPcnExtConstWords.BASE_BEAN_FIELDS);
            this.sourceId = workOrderAssembly.getId();
            this.woaRemark = workOrderAssembly.getRemark();
        }

        if (null != productionAssembly) {
            this.id = productionAssembly.getId();
            this.mouldRecordId = productionAssembly.getMouldRecordId();
            this.productionRecordId = productionAssembly.getProductionRecordId();
            this.createDatetime = productionAssembly.getCreateDatetime();
            this.createUser = productionAssembly.getCreateUser();
            this.paRemark = productionAssembly.getRemark();
        }

        this.isCheckBindSeq = isCheckBindSeq;

        if (StringUtils.isEmpty(this.routeSeq)) this.routeSeq = MesPcnExtConstWords.ZERO;
        if (StringUtils.isEmpty(this.processSeq)) this.processSeq = MesPcnExtConstWords.ZERO;

        if (StringUtils.isEmpty(this.assemblyStatus) || this.assemblyStatus.compareTo(MesPcnExtConstWords.ZERO) == 0 ||
                this.assemblyStatus.compareTo(MesExtEnumUtil.ASSEMBLY_STATUS.ASSEMBLY_STATUS_20.getValue()) == 0) this.assemblyStatus = MesExtEnumUtil.ASSEMBLY_STATUS.ASSEMBLY_STATUS_30.getValue();

        if (this.assemblyStatus.compareTo(MesExtEnumUtil.ASSEMBLY_STATUS.ASSEMBLY_STATUS_10.getValue()) == 0) this.isSaveDb().setIsExistDb(CommonEnumUtil.TRUE_OR_FALSE.TRUE.getValue());

        if (this.assemblyStatus.compareTo(MesExtEnumUtil.ASSEMBLY_STATUS.ASSEMBLY_STATUS_10.getValue()) != 0 && !MesExtEnumUtil.ASSEMBLY_MATCH_TYPE.checkIsNeedScan(this.matchType))
            this.assemblyStatus = MesExtEnumUtil.ASSEMBLY_STATUS.ASSEMBLY_STATUS_10.getValue();

        if (this.assemblyStatus.compareTo(MesExtEnumUtil.ASSEMBLY_STATUS.ASSEMBLY_STATUS_40.getValue()) == 0)
            this.assemblyStatus = MesExtEnumUtil.ASSEMBLY_STATUS.ASSEMBLY_STATUS_30.getValue();

        return this;
    }

    public MesProductionAssemblySortContext foreignKey(Integer foreignKey) {
        this.foreignKey = foreignKey;
        return this;
    }

    public MesProductionAssemblySortContext assemblyStatus(String assemblySn) {
        this.assemblyStatus = MesExtEnumUtil.ASSEMBLY_STATUS.ASSEMBLY_STATUS_10.getValue();
        this.assemblySn = assemblySn;
        matchDatetime();
        return this;
    }

    public MesProductionAssemblySortContext assemblySkip() {
        this.assemblyStatus = MesExtEnumUtil.ASSEMBLY_STATUS.ASSEMBLY_STATUS_40.getValue();
        this.isSkip = CommonEnumUtil.TRUE_OR_FALSE.TRUE.getValue();
        matchDatetime();
        return this;
    }

    public MesProductionAssemblySortContext productSnId(Long productSnId) {
        this.productSnId = productSnId;
        return this;
    }

    public MesProductionAssemblySortContext fileUrl(Map<Long, String> fileMap) {
        if (CollectionUtils.isEmpty(fileMap)) return this;
        if (!StringUtils.isEmpty(this.imageFileId)) this.imageFileUrl= fileMap.get(this.imageFileId);
        if (!StringUtils.isEmpty(this.audioFileId)) this.audioFileUrl= fileMap.get(this.audioFileId);
        return this;
    }

    public MesProductionAssemblySortContext fileIsFinished() {
        this.fileIsFinished = CommonEnumUtil.TRUE_OR_FALSE.TRUE.getValue();
        return this;
    }

    public MesProductionAssemblySortContext isSaveDb() {
        this.isSaveDb = CommonEnumUtil.TRUE_OR_FALSE.TRUE.getValue();
        return this;
    }

    public MesProductionAssemblySortContext copy(MesProductionAssembly productionAssembly) {

        if (null != productionAssembly) BeanUtils.copyProperties(productionAssembly, this, MesPcnExtConstWords.BASE_BEAN_FIELDS);

        if (StringUtils.isEmpty(this.routeSeq)) this.routeSeq = MesPcnExtConstWords.ZERO;

        if (this.assemblyStatus.compareTo(MesExtEnumUtil.ASSEMBLY_STATUS.ASSEMBLY_STATUS_30.getValue()) == 0 && !MesExtEnumUtil.ASSEMBLY_MATCH_TYPE.checkIsNeedScan(this.matchType))
            this.assemblyStatus = MesExtEnumUtil.ASSEMBLY_STATUS.ASSEMBLY_STATUS_10.getValue();

        return this;
    }

    public MesProductionAssemblySortContext prodRuleIgnoreCfg(String prodRuleIgnoreCfg) {
        if (!StringUtils.isEmpty(prodRuleIgnoreCfg)) this.prodRuleIgnoreCfg = prodRuleIgnoreCfg;
        return this;
    }

    private void matchDatetime() { this.matchDatetime = (new SimpleDateFormat(MesPcnExtConstWords.DATE_FORMAT_SSS)).format(new Date()); }

}
