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

import cn.estsh.i3plus.ext.mes.pcn.api.busi.IProduceSnRepairService;
import cn.estsh.i3plus.ext.mes.pcn.api.busi.jx.IJxProdBusiStrategyService;
import cn.estsh.i3plus.ext.mes.pcn.pojo.bean.MesProduceSnRepair;
import cn.estsh.i3plus.ext.mes.pcn.pojo.model.jx.JxProdBusiStrategyParamsBuilder;
import cn.estsh.i3plus.ext.mes.pcn.pojo.repository.MesProduceSnRepairRepository;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtConstWords;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtEnumUtil;
import cn.estsh.i3plus.platform.common.convert.ConvertBean;
import cn.estsh.i3plus.pojo.mes.model.StepResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
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 {

    @Autowired
    private IProduceSnRepairService produceSnRepairService;

    @Autowired
    private MesProduceSnRepairRepository produceSnRepairRepository;

    @Override
    public StepResult checkLineOn(JxProdBusiStrategyParamsBuilder params) {

        StepResult stepResult = ((IJxProdBusiStrategyService) params.getStrategyService()).checkLineOn(params);

        if (!stepResult.isCompleted()) return stepResult;

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

        return checkLineOn4Ps(params, stepResult);

    }

    private StepResult checkLineOn4Ps(JxProdBusiStrategyParamsBuilder params, StepResult stepResult) {

        if (!StringUtils.isEmpty(params.getProduceSnExtDb().getProcessCode()))
            return stepResult.isCompleted(false).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.isCompleted(false).msg(String.format("%s[%s]对应的产品条码信息当前业务类型[返修],验证失败!", params.getScanName(), params.getScanInfo()));

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

        if (!params.getProduceSnExtDb().getLastWorkOrderNo().equals(params.getOrderModel().getOrderNo()))
            return stepResult.nextTriggerEvent(MesPcnExtConstWords.TMP_ORDER).msg(String.format("%s[%s]已匹配产品条码信息[已被标记为返修品],即将切换到临时工单[%s]!", params.getScanName(), params.getScanInfo(), params.getProduceSnExtDb().getLastWorkOrderNo()));
        else
            return stepResult.msg(String.format("%s[%s]已匹配产品条码信息,验证成功!", params.getScanName(), params.getScanInfo()));

    }

    @Override
    public StepResult execLineOff(JxProdBusiStrategyParamsBuilder params) {

        StepResult stepResult = ((IJxProdBusiStrategyService) params.getStrategyService()).execLineOff(params);

        if (!stepResult.isCompleted()) return stepResult;

        saveProduceSnRepair(params);

        return stepResult;
    }

    private void saveProduceSnRepair(JxProdBusiStrategyParamsBuilder params) {
        MesProduceSnRepair produceSnRepairDb = produceSnRepairService.getProduceSnRepairBySnAndOrderNo(params.getOrganizeCode(), params.getProduceSnExtDb().getSerialNumber(), params.getOrderModel().getOrderNo());
        ConvertBean.serviceModelUpdate(produceSnRepairDb, params.getUserInfo());
        produceSnRepairDb.setOutWorkCenterTime(produceSnRepairDb.getModifyDatetime());
        produceSnRepairDb.setWorkTime(params.getWorkCenterExtDb().getWorkTime());
        produceSnRepairDb.setShiftCode(params.getWorkCenterExtDb().getShiftCode());
        produceSnRepairDb.setLotNo(produceSnRepairDb.getWorkTime());
        produceSnRepairDb.setSystemSyncStatus(MesPcnExtEnumUtil.IF_SYNC_STATUS.NO_SYNC.getValue());
        produceSnRepairRepository.save(produceSnRepairDb);
    }

}