package cn.estsh.i3plus.ext.mes.pcn.apiservice.serviceimpl.step;

import cn.estsh.i3plus.ext.mes.pcn.api.busi.IMesProductionDispatchContextStepService;
import cn.estsh.i3plus.ext.mes.pcn.api.busi.IMesProductionProcessContextStepService;
import cn.estsh.i3plus.ext.mes.pcn.pojo.context.MesProdRuleContext;
import cn.estsh.i3plus.ext.mes.pcn.pojo.context.MesProductionProcessContext;
import cn.estsh.i3plus.ext.mes.pcn.pojo.context.MesProductionPsOutContext;
import cn.estsh.i3plus.mes.pcn.serviceimpl.fsm.BaseStepService;
import cn.estsh.i3plus.platform.common.convert.ConvertBean;
import cn.estsh.i3plus.pojo.base.enumutil.CommonEnumUtil;
import cn.estsh.i3plus.pojo.base.enumutil.MesPcnEnumUtil;
import cn.estsh.i3plus.pojo.mes.bean.MesWorkOrderReductionSource;
import cn.estsh.i3plus.pojo.mes.model.StationRequestBean;
import cn.estsh.i3plus.pojo.mes.model.StationResultBean;
import cn.estsh.i3plus.pojo.mes.model.StepResult;
import cn.estsh.i3plus.pojo.mes.repository.MesWorkOrderReductionSourceRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @Description : 保存条码与工单对应关系工步
 * @Author : wangjie
 **/
@Slf4j
@Service("mesBarcodeWithWorkOrderRelationSaveStepService")
public class MesBarcodeWithWorkOrderRelationSaveStepService extends BaseStepService {

    @Autowired
    private IMesProductionProcessContextStepService productionProcessContextStepService;

    @Autowired
    private IMesProductionDispatchContextStepService productionDispatchContextStepService;

    @Autowired
    private MesWorkOrderReductionSourceRepository workOrderReductionSourceRepository;

    @Override
    public StepResult execute(StationRequestBean reqBean) {

        StationResultBean resultBean = new StationResultBean();

        StepResult stepResult = StepResult.getSuccessComplete();

        //获取上下文信息
        MesProductionProcessContext productionProcessContext = productionProcessContextStepService.dispatchCurCellEquipment(reqBean);

        //配置错误 抛出异常
        if (!productionProcessContext.getSuccess()) stepExpSendMsgAndThrowEx(reqBean, resultBean.writeDbLog(), productionProcessContext.getMessage());

        //存储生产过程上下文对象
        productionProcessContextStepService.dispatchProductionProcessContext(reqBean, productionProcessContext);

        //获取上下文产品加工规则数据信息集合
        List<MesProdRuleContext> prodRuleContextList = productionDispatchContextStepService.getProdRuleDataContext(reqBean);
        if (CollectionUtils.isEmpty(prodRuleContextList)) stepExpSendMsgAndThrowEx(reqBean, resultBean.writeDbLog(), "当前不存在非排序加工规则数据,请重置工序!");

        //获取上下文产出条码数据信息集合
        List<MesProductionPsOutContext> productionPsOutContextList = productionDispatchContextStepService.getProductionPsOutContext(reqBean);
        if (CollectionUtils.isEmpty(productionPsOutContextList)) stepExpSendMsgAndThrowEx(reqBean, resultBean.writeDbLog(), "当前不存在产出零件条码信息,请重置工序解决!");

        //验证产出条码是否匹配工单
        Optional<MesProductionPsOutContext> optional = productionPsOutContextList.stream().filter(o -> (null != o && StringUtils.isEmpty(o.getWorkOrderNo()))).findFirst();
        if (null != optional && optional.isPresent()) stepExpSendMsgAndThrowEx(reqBean, resultBean.writeDbLog(), "当前产出零件条码信息未匹配工单,请重置工序解决!");

        Map<Integer, MesProdRuleContext> prMap = prodRuleContextList.stream().filter(o -> (null != o && !StringUtils.isEmpty(o.getForeignKey()))).collect(Collectors.toMap(MesProdRuleContext::getForeignKey, o -> o));

        productionPsOutContextList.stream().filter(o -> null != o).forEach(o -> saveBarcodeWithWorkOrderRelation(reqBean, o, prMap.get(o.getForeignKey())));

        return stepSuccessCompleteAndSendMsgReturn(reqBean, resultBean.writeDbLog(MesPcnEnumUtil.WORK_CELL_SCAN_MONITOR_LOG_TYPE.PROCESS.getValue()), stepResult, "保存条码与工单对应关系成功!");

    }

    private void saveBarcodeWithWorkOrderRelation(StationRequestBean reqBean, MesProductionPsOutContext productionPsOutContext, MesProdRuleContext prodRuleContext) {
        MesWorkOrderReductionSource workOrderReductionSource = new MesWorkOrderReductionSource();
        workOrderReductionSource.setWorkOrderNo(productionPsOutContext.getWorkOrderNo());
        workOrderReductionSource.setProductSn(productionPsOutContext.getProductSn());
        workOrderReductionSource.setPartNo(productionPsOutContext.getPartNo());
        workOrderReductionSource.setPartName(productionPsOutContext.getPartName());
        workOrderReductionSource.setAreaCode(reqBean.getAreaCode());
        workOrderReductionSource.setWorkCenterCode(reqBean.getWorkCenterCode());
        workOrderReductionSource.setProductionRecordId(prodRuleContext.getProductionRecordId());
        workOrderReductionSource.setQty(new Double(1));
        workOrderReductionSource.setSourceType(10);
        workOrderReductionSource.setSourceStatus(CommonEnumUtil.TRUE_OR_FALSE.TRUE.getValue());
        workOrderReductionSource.setOrganizeCode(reqBean.getOrganizeCode());
        ConvertBean.serviceModelInitialize(workOrderReductionSource, reqBean.getUserInfo());
        workOrderReductionSourceRepository.insert(workOrderReductionSource);
    }

}
