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 JxProdBusiStrategyRepairService implements IJxProdBusiStrategyService {

    @Override
    public StepResult checkLineOn(JxProdBusiStrategyParamsBuilder params) {

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

        StepResult stepResult = StepResult.getNonComplete();

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

        return checkLineOn4Ps(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()));

    }

    @Override
    public StepResult execLineOnBindMc(JxProdBusiStrategyParamsBuilder params) { return StepResult.getSuccessComplete(); }

    @Override
    public StepResult checkFinalInspection(JxProdBusiStrategyParamsBuilder params) {

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

        StepResult stepResult = StepResult.getNonComplete();

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

        return checkFinalInspection4Ps(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()));

    }

    @Override
    public StepResult checkThirdPartyPs(JxProdBusiStrategyParamsBuilder params) { return StepResult.getSuccessComplete(); }

}