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.MesMouldMultiCavity;
import cn.estsh.i3plus.pojo.mes.bean.MesWorkCell;
import cn.estsh.i3plus.pojo.mes.bean.MesWorkOrder;
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.StringUtils;

import java.io.Serializable;

/**
 * 生产过程上下文对象-产出零件信息
 */
@Data
public class MesProductionPartContext implements Serializable {

    private static final long serialVersionUID = 5562937531822977296L;

    @ApiParam(name = "数据关联键")
    private Integer foreignKey;

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

    @ApiParam("生产工单ID")
    private Long id;

    @ApiParam("生产工单号")
    private String workOrderNo;

    @ApiParam("客户代码")
    private String custCode;

    @ApiParam("客户零件号")
    private String custPartNo;

    @ApiParam("裁片工单数量")
    private Double cutQty;

    @ApiParam("工单数量")
    private Double qty;

    @ApiParam("完成数量【不能用于业务计算】")
    private Double completeQty;

    @ApiParam("工单状态")
    private Integer workOrderStatus;

    @ApiParam("上线时间")
    private String onlineTime;

    @ApiParam("下线时间")
    private String offlineTime;

    @ApiParam("生产线代码")
    private String workCenterCode;

    @ApiParam("生产版本")
    private String productVersion;

    @ApiParam("设备代码")
    private String equipmentCode;

    @ApiParam("设备名称")
    private String equipmentName;

    @ApiParam("零件号")
    private String partNo;

    @ApiParam("零件名称")
    private String partName;

    @ApiParam("模具代码")
    private String mouldNo;

    //默认否
    @ApiParam("是否空腔")
    private Integer isFinishCode = CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue();

    @ApiParam("零件生产组代码")
    private String partProdGroupCode;

    @ApiParam("零件生产组名称")
    private String partProdGroupName;

    @ApiParam("工单标识")
    private String orderFlag;

    @ApiParam("车型大类")
    private String carModelCode;

    @ApiParam("信息来源")
    private Integer messageSource;

    @ApiParam("生产序号")
    private Long productSeq;

    @ApiParam("客户订单号")
    private String custOrderNo;

    @ApiParam("vin号【用于发送设备加工参数】")
    private String vinCode;

    @ApiParam("工单序号【用于发送设备加工参数】")
    private String workOrderSeq;

    @ApiParam("工艺强过码")
    private String craftJumpCode;

    //默认否
    @ApiParam("是否顺序防错")
    private Integer isCheckSeq = CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue();

    //默认未验证
    @ApiParam("顺序防错结果")
    private Integer checkSeqResult = MesPcnExtConstWords.ZERO;

    //默认未验证, 目前使用场景【通用汽车顺序防错验证客户订单号的流水号】
    @ApiParam("附加客制化顺序防错结果")
    private Integer customCheckSeqResult = MesPcnExtConstWords.ZERO;

    //默认否
    @ApiParam("业务验证之后需要清除的数据标志")
    private Integer busiCheckToDelete = CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue();

    // 裁片功能
    @ApiParam("裁片方案代码")
    private String cutCode;

    @ApiParam("裁片工单号")
    private String cutWorkOrderNo;
    // 裁片功能

    //根据一模多腔赋值
    public MesProductionPartContext copyMouldMultiCavity(MesMouldMultiCavity mouldMultiCavity) {
        BeanUtils.copyProperties(mouldMultiCavity, this, MesPcnExtConstWords.ID);
        return messageSource(MesExtEnumUtil.CELL_MESSAGE_SOURCE.READ.getValue());
    }

    //根据零件信息赋值
    public MesProductionPartContext copyPartNo(String organizeCode, String partNo, String partName) {
        this.organizeCode = organizeCode;
        this.partNo = partNo;
        this.partName = partName;
        return messageSource(MesExtEnumUtil.CELL_MESSAGE_SOURCE.READ.getValue());
    }

    //根据工单信息赋值
    public MesProductionPartContext copyPartNo(MesWorkOrder workOrder, Integer messageSource) {
        if (null != workOrder) {
            BeanUtils.copyProperties(workOrder, this);
            if (StringUtils.isEmpty(this.completeQty)) this.completeQty = new Double(0);
        } else this.isFinishCode = CommonEnumUtil.TRUE_OR_FALSE.TRUE.getValue();
        return messageSource(messageSource);
    }

    //根据工单信息、裁片工单明细赋值
    public MesProductionPartContext copyPartNo(MesWorkOrder workOrder, Double cutQty, String cutWorkOrderNo, Integer messageSource) {
        if (null != workOrder) {
            BeanUtils.copyProperties(workOrder, this);
            if (StringUtils.isEmpty(this.completeQty)) this.completeQty = new Double(0);
            this.setCutQty(cutQty);
            this.setCutWorkOrderNo(cutWorkOrderNo);
        } else this.isFinishCode = CommonEnumUtil.TRUE_OR_FALSE.TRUE.getValue();
        return messageSource(messageSource);
    }

    public MesProductionPartContext isFinishCode() {
        this.isFinishCode = CommonEnumUtil.TRUE_OR_FALSE.TRUE.getValue();
        return this;
    }

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

    public MesProductionPartContext messageSource(Integer messageSource) {
        this.messageSource = messageSource;
        return this;
    }

    public MesProductionPartContext busiCheckToDelete() {
        this.busiCheckToDelete = CommonEnumUtil.TRUE_OR_FALSE.TRUE.getValue();
        return this;
    }

    public MesProductionPartContext isCheck(MesWorkCell workCell) {
        if(!StringUtils.isEmpty(workCell.getIsCheckSeq()) && workCell.getIsCheckSeq().compareTo(CommonEnumUtil.TRUE_OR_FALSE.TRUE.getValue()) == 0) this.isCheckSeq = workCell.getIsCheckSeq();
        return this;
    }

    public MesProductionPartContext checkSeqResult(Integer checkSeqResult) {
        this.checkSeqResult = checkSeqResult;
        return this;
    }

    public MesProductionPartContext customCheckSeqResult(Integer customCheckSeqResult) {
        this.customCheckSeqResult = customCheckSeqResult;
        return this;
    }

}
