package cn.estsh.i3plus.ext.mes.pcn.apiservice.serviceimpl.busi.jx;

import cn.estsh.i3plus.ext.mes.pcn.api.busi.jx.IJxProdBusiStrategyService;
import cn.estsh.i3plus.ext.mes.pcn.pojo.model.jx.JxProdBusiStrategyParamsBuilder;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtEnumUtil;
import cn.estsh.i3plus.pojo.mes.model.StepResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * @Author: wangjie
 * @CreateDate: 2021/01/18 11:41 上午
 * @Description: 试制工单
 **/
@Slf4j
@Service
public class JxProdBusiStrategyAttemptService implements IJxProdBusiStrategyService {

    @Override
    public StepResult checkLineOn(JxProdBusiStrategyParamsBuilder params) {

        ((IJxProdBusiStrategyService) params.getStrategyService()).checkLineOn(params);

        StepResult stepResult = StepResult.getNonComplete();

        if (null == params.getManageCodeDb() && null == params.getProduceSnExtDb()) return stepResult.msg(String.format("%s[%s]未匹配到有效的管理编码或产品条码信息,验证失败!", params.getScanName(), params.getScanInfo()));

        if (null != params.getProduceSnExtDb()) return checkLineOn4Ps(params, stepResult);

        return checkLineOn4Mc(params, stepResult);

    }

    private StepResult checkLineOn4Ps(JxProdBusiStrategyParamsBuilder params, StepResult stepResult) {

        if (!params.getProduceSnExtDb().getLastWorkOrderNo().equals(params.getOrderModel().getOrderNo()))
            return stepResult.msg(String.format("%s[%s]对应的产品条码信息关联的工单号[%s]与当前生产的工单号[%s]不一致,验证失败!", params.getScanName(), params.getScanInfo(), params.getProduceSnExtDb().getLastWorkOrderNo(), params.getOrderModel().getOrderNo()));

        if (MesPcnExtEnumUtil.PRODUCE_SN_STATUS.PROCESS.getValue() <= params.getProduceSnExtDb().getSnStatus())
            return stepResult.msg(String.format("%s[%s]对应的产品条码信息当前条码状态[%s],验证失败!", params.getScanName(), params.getScanInfo(), MesPcnExtEnumUtil.PRODUCE_SN_STATUS.valueOfDescription(params.getProduceSnExtDb().getSnStatus())));

        if (MesPcnExtEnumUtil.SN_OPERATE_TYPE.REPAIR.getValue() == params.getProduceSnExtDb().getOperateType())
            return stepResult.msg(String.format("%s[%s]对应的产品条码信息当前业务类型[返修],验证失败!", params.getScanName(), params.getScanInfo()));

        return stepResult.isCompleted(true).msg(String.format("%s[%s]已匹配产品条码信息,验证成功!", params.getScanName(), params.getScanInfo()));

    }

    private StepResult checkLineOn4Mc(JxProdBusiStrategyParamsBuilder params, StepResult stepResult) {

        if (null != params.getProduceSnMcRecordDb()) {
            if (!params.getProduceSnMcRecordDb().getWorkOrderNo().equals(params.getOrderModel().getOrderNo()))
                return stepResult.msg(String.format("%s[%s]已绑定其他生产工单[%s],验证失败!", params.getScanName(), params.getScanInfo(), params.getProduceSnMcRecordDb().getWorkOrderNo()));

            if (MesPcnExtEnumUtil.MC_RECORD_LINE_ON_STATUS.TRUE.getValue() == params.getProduceSnMcRecordDb().getLineOnStatus())
                return stepResult.msg(String.format("%s[%s]对应的管理编码与工单条码关系信息当前状态[已上线],验证失败!", params.getScanName(), params.getScanInfo()));
            else
                return stepResult.isCompleted(true).msg(String.format("%s[%s]此前上线未完成,验证成功!", params.getScanName(), params.getScanInfo()));
        } else {
            Integer count = ((IJxProdBusiStrategyService) params.getStrategyService()).queryOrderBindedQty(params);
            if (count >= params.getOrderModel().getQty())
                return stepResult.msg(String.format("生产工单[%s]计划数量[%s]上线数量已达标,请切换生产工单!", params.getOrderModel().getOrderNo(), params.getOrderModel().getQty()));

            return stepResult.isCompleted(true).msg(String.format("%s[%s]验证成功!", params.getScanName(), params.getScanInfo()));
        }

    }

    @Override
    public StepResult execLineOnBindMc(JxProdBusiStrategyParamsBuilder params) {

        ((IJxProdBusiStrategyService) params.getStrategyService()).insertProduceSnMcRecord(params);

        return StepResult.getSuccessComplete().data(params.getProduceSnMcRecordDb());

    }

    @Override
    public StepResult checkFinalInspection(JxProdBusiStrategyParamsBuilder params) {

        ((IJxProdBusiStrategyService) params.getStrategyService()).checkFinalInspection(params);

        StepResult stepResult = StepResult.getNonComplete();

        if (null == params.getProduceSnMcRecordDb() && null == params.getProduceSnExtDb()) return stepResult.msg(String.format("%s[%s]未匹配到有效的管理编码与工单条码关系信息或产品条码信息,验证失败!", params.getScanName(), params.getScanInfo()));

        if (null != params.getProduceSnExtDb()) return checkFinalInspection4Ps(params, stepResult);

        return checkFinalInspection4Mc(params, stepResult);

    }

    private StepResult checkFinalInspection4Ps(JxProdBusiStrategyParamsBuilder params, StepResult stepResult) {

        if (!params.getProduceSnExtDb().getLastWorkOrderNo().equals(params.getOrderModel().getOrderNo()))
            return stepResult.msg(String.format("%s[%s]对应的产品条码信息关联的工单号[%s]与当前生产的工单号[%s]不一致,验证失败!", params.getScanName(), params.getScanInfo(), params.getProduceSnExtDb().getLastWorkOrderNo(), params.getOrderModel().getOrderNo()));

        if (MesPcnExtEnumUtil.PRODUCE_SN_STATUS.PROCESS.getValue() != params.getProduceSnExtDb().getSnStatus())
            return stepResult.msg(String.format("%s[%s]对应的产品条码信息当前条码状态[%s],验证失败!", params.getScanName(), params.getScanInfo(), MesPcnExtEnumUtil.PRODUCE_SN_STATUS.valueOfDescription(params.getProduceSnExtDb().getSnStatus())));

        if (!StringUtils.isEmpty(params.getProduceSnExtDb().getProcessCode()) && params.getProduceSnExtDb().getProcessCode().equals(params.getProcessCode()))
            return stepResult.msg(String.format("%s[%s]对应的产品条码信息已完成当前工序,验证失败!", params.getScanName(), params.getScanInfo()));

        if (MesPcnExtEnumUtil.SN_OPERATE_TYPE.REPAIR.getValue() == params.getProduceSnExtDb().getOperateType())
            return stepResult.msg(String.format("%s[%s]对应的产品条码信息当前业务类型[返修],验证失败!", params.getScanName(), params.getScanInfo()));

        if (MesPcnExtEnumUtil.PRODUCE_SN_QC_STATUS.DEFECTED.getValue() == params.getProduceSnExtDb().getQcStatus())
            return stepResult.msg(String.format("%s[%s]对应的产品条码信息当前质量状态[不合格],验证失败!", params.getScanName(), params.getScanInfo()));

        return stepResult.isCompleted(true).msg(String.format("%s[%s]已匹配产品条码信息,验证成功!", params.getScanName(), params.getScanInfo()));

    }

    private StepResult checkFinalInspection4Mc(JxProdBusiStrategyParamsBuilder params, StepResult stepResult) {

        if (!params.getProduceSnMcRecordDb().getWorkOrderNo().equals(params.getOrderModel().getOrderNo()))
            return stepResult.msg(String.format("%s[%s]对应的管理编码与工单条码关系信息关联的工单号[%s]与当前生产的工单号[%s]不一致,验证失败!", params.getScanName(), params.getScanInfo(), params.getProduceSnMcRecordDb().getWorkOrderNo(), params.getOrderModel().getOrderNo()));

        if (MesPcnExtEnumUtil.SN_OPERATE_TYPE.REPAIR.getValue() == params.getProduceSnMcRecordDb().getOperateType())
            return stepResult.msg(String.format("%s[%s]对应的管理编码与工单条码关系信息当前业务类型[返修],验证失败!", params.getScanName(), params.getScanInfo()));

        if (MesPcnExtEnumUtil.MC_RECORD_QC_STATUS.FALSE.getValue() == params.getProduceSnMcRecordDb().getQcStatus())
            return stepResult.msg(String.format("%s[%s]对应的管理编码与工单条码关系信息当前质量状态[不合格],验证失败!", params.getScanName(), params.getScanInfo()));

        if (MesPcnExtEnumUtil.MC_RECORD_LINE_ON_STATUS.FALSE.getValue() == params.getProduceSnMcRecordDb().getLineOnStatus())
            return stepResult.msg(String.format("%s[%s]对应的管理编码与工单条码关系信息当前状态[上线未完成],验证失败!", params.getScanName(), params.getScanInfo()));

        return stepResult.isCompleted(true).msg(String.format("%s[%s]验证成功!", params.getScanName(), params.getScanInfo()));

    }

    @Override
    public StepResult checkThirdPartyPs(JxProdBusiStrategyParamsBuilder params) {

        StepResult stepResult = StepResult.getNonComplete();

        ((IJxProdBusiStrategyService) params.getStrategyService()).queryProduceSnInfo(params.productSn(params.getScanInfo()));

        if (null == params.getProduceSnExtDb())
            return stepResult.msg(String.format("产品条码[%s]未关联试制工单[%s],操作失败!", params.getScanInfo(), params.getOrderModel().getOrderNo()));

        if (!params.getProduceSnExtDb().getLastWorkOrderNo().equals(params.getOrderModel().getOrderNo()))
            return stepResult.msg(String.format("产品条码[%s]信息关联的工单号[%s]与当前生产的工单号[%s]不一致,验证失败!", params.getScanInfo(), params.getProduceSnExtDb().getLastWorkOrderNo(), params.getOrderModel().getOrderNo()));

        if (!StringUtils.isEmpty(params.getProduceSnExtDb().getTrayNo()))
            return stepResult.msg(String.format("产品条码[%s]已经过终检工序,验证失败!", params.getScanInfo()));

        if (MesPcnExtEnumUtil.PRODUCE_SN_STATUS.CREATE.getValue() != params.getProduceSnExtDb().getSnStatus())
            return stepResult.msg(String.format("产品条码[%s]信息当前状态[%s],验证失败!", params.getScanInfo(), MesPcnExtEnumUtil.PRODUCE_SN_STATUS.valueOfDescription(params.getProduceSnExtDb().getSnStatus())));

        if (MesPcnExtEnumUtil.SN_OPERATE_TYPE.REPAIR.getValue() == params.getProduceSnExtDb().getOperateType())
            return stepResult.msg(String.format("产品条码[%s]信息当前业务类型[返修],验证失败!", params.getScanInfo()));

        if (MesPcnExtEnumUtil.PRODUCE_SN_QC_STATUS.DEFECTED.getValue() == params.getProduceSnExtDb().getQcStatus())
            return stepResult.msg(String.format("产品条码[%s]信息当前质量状态[不合格],验证失败!", params.getScanInfo()));

        return stepResult.isCompleted(true);
    }

}