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.MesAssemblyNosortCfg;
import cn.estsh.i3plus.pojo.mes.bean.MesProductionAssembly;
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 MesProductionAssemblyNosortContext extends MesProductionAssemblyContext implements Serializable {

    private static final long serialVersionUID = -6507517107073405709L;

    @ApiParam(value = "BOM明细ID")
    private String bomDetailId;

    @ApiParam(name = "任意装配件扫描错误判断工位isResetScan后是否重新扫描")
    public Integer isResetScan = CommonEnumUtil.TRUE_OR_FALSE.TRUE.getValue();

    @ApiParam(value = "是否忽略回车")
    private Integer isIgnoreEntry;

    @ApiParam(name = "产成品图")
    private String outPartNoPic;

    @ApiParam(value = "截取的流水号长度")
    private Integer serialLength;

    public MesProductionAssemblyNosortContext() {}

    public MesProductionAssemblyNosortContext copy(MesProdRuleContext prodRuleContext, MesAssemblyNosortCfg assemblyNosortCfg) {

        BeanUtils.copyProperties(assemblyNosortCfg, this);

        BeanUtils.copyProperties(prodRuleContext, this);

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

        this.sourceId = assemblyNosortCfg.getId();

        this.partNo = prodRuleContext.getOutPartNo();

        if (StringUtils.isEmpty(this.routeSeq)) this.routeSeq = MesPcnExtConstWords.ZERO;
        if (StringUtils.isEmpty(this.isRepeat) || this.isRepeat.compareTo(CommonEnumUtil.TRUE_OR_FALSE.TRUE.getValue()) != 0) this.isRepeat = CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue();

        this.assemblyStatus = MesExtEnumUtil.ASSEMBLY_STATUS.ASSEMBLY_STATUS_30.getValue();

        return this;
    }

    public MesProductionAssemblyNosortContext overrideAssemblyStatus(String repeatAssemblySn) {

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

        if (!StringUtils.isEmpty(repeatAssemblySn)) {
            this.assemblySn = repeatAssemblySn;
            this.isRepeat2Cache = CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue();
            this.assemblyStatus = MesExtEnumUtil.ASSEMBLY_STATUS.ASSEMBLY_STATUS_10.getValue();
        }

        if (this.assemblyStatus.compareTo(MesExtEnumUtil.ASSEMBLY_STATUS.ASSEMBLY_STATUS_30.getValue()) != 0) this.isResetScan = CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue();
        return this;
    }

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

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

    public MesProductionAssemblyNosortContext assemblySn(String assemblySn) {
        this.assemblySn = assemblySn;
        return this;
    }

    public MesProductionAssemblyNosortContext qty(Double qty) {
        this.qty = qty;
        return this;
    }

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

    public MesProductionAssemblyNosortContext assemblyStatusReset() {
        this.assemblyStatus = MesExtEnumUtil.ASSEMBLY_STATUS.ASSEMBLY_STATUS_30.getValue();
        this.assemblySn = null;
        this.matchDatetime = null;
        this.isSkip = CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue();
        return this;
    }

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

    public MesProductionAssemblyNosortContext copy(MesAssemblyNosortCfg assemblyNosortCfg) {
        BeanUtils.copyProperties(assemblyNosortCfg, this);
        this.sourceId = assemblyNosortCfg.getId();
        if (StringUtils.isEmpty(this.routeSeq)) this.routeSeq = MesPcnExtConstWords.ZERO;
        this.assemblyStatus = MesExtEnumUtil.ASSEMBLY_STATUS.ASSEMBLY_STATUS_30.getValue();
        if (!MesExtEnumUtil.ASSEMBLY_MATCH_TYPE.checkIsNeedScan(this.matchType)) this.assemblyStatus = MesExtEnumUtil.ASSEMBLY_STATUS.ASSEMBLY_STATUS_10.getValue();
        return this;
    }

    public MesProductionAssemblyNosortContext 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 MesProductionAssemblyNosortContext fileIsFinished() {
        this.fileIsFinished = CommonEnumUtil.TRUE_OR_FALSE.TRUE.getValue();
        return this;
    }

    public MesProductionAssemblyNosortContext copy(MesProductionAssembly productionAssembly) {
        BeanUtils.copyProperties(productionAssembly, this);
        this.sourceId = productionAssembly.getSourceId();
        if (StringUtils.isEmpty(this.routeSeq)) this.routeSeq = MesPcnExtConstWords.ZERO;
        this.assemblyStatus = MesExtEnumUtil.ASSEMBLY_STATUS.ASSEMBLY_STATUS_30.getValue();
        if (!MesExtEnumUtil.ASSEMBLY_MATCH_TYPE.checkIsNeedScan(this.matchType)) this.assemblyStatus = MesExtEnumUtil.ASSEMBLY_STATUS.ASSEMBLY_STATUS_10.getValue();
        return this;
    }

    public MesProductionAssemblyNosortContext 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()); }

}
