package cn.estsh.i3plus.ext.mes.pcn.apiservice.serviceimpl.station.function;

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.apiservice.serviceimpl.step.method.MesWorkOrderCheckCompleteQtyStepService;
import cn.estsh.i3plus.ext.mes.pcn.pojo.context.MesProdRuleContext;
import cn.estsh.i3plus.ext.mes.pcn.pojo.context.MesProductionPartContext;
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.BaseSwsService;
import cn.estsh.i3plus.mes.pcn.serviceimpl.fsm.IShippingDispatchService;
import cn.estsh.i3plus.mes.pcn.serviceimpl.fsm.function.IFsmModuleFunctionService;
import cn.estsh.i3plus.platform.common.util.MesPcnConstWords;
import cn.estsh.i3plus.pojo.base.enumutil.MesPcnEnumUtil;
import cn.estsh.i3plus.pojo.mes.model.ButtonDynamicModel;
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 org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description : 弹框输入工单数量接口实现【输入工单数量弹框】
 **/
@Service
public class MesFunctionDialogInputOrderQtyService extends BaseSwsService implements IFsmModuleFunctionService {

    @Autowired
    private IMesProductionProcessContextStepService productionProcessContextStepService;

    @Autowired
    private IMesProductionDispatchContextStepService productionDispatchContextStepService;

    @Autowired
    private MesWorkOrderCheckCompleteQtyStepService workOrderCheckCompleteQtyStepService;

    @Autowired
    private IShippingDispatchService shippingDispatchService;

    @Override
    public Boolean doFunction(StationRequestBean reqBean, StationResultBean resultBean, ButtonDynamicModel buttonDynamicModel) {

        this.sendMessage(reqBean, resultBean.writeDbLog(MesPcnEnumUtil.WORK_CELL_SCAN_MONITOR_LOG_TYPE.PICK.getValue()).scanInfo(buttonDynamicModel.getFunctionValue()),
                String.format("生产线[%s]工位[%s]工单数量弹框提交成功,请等待验证!提交信息[%s]", reqBean.getWorkCenterCode(), reqBean.getWorkCellCode(), buttonDynamicModel.getFunctionValue()),
                MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.TEXT);

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

        //获取上下文产出零件信息
        List<MesProductionPartContext> productionPartContextList = productionDispatchContextStepService.getProductionPartContext(reqBean);

        //用弹框输入的工单数量 虚拟产出零件信息
        List<MesProductionPsOutContext> productionPsOutContextList = new ArrayList<>();
        productionPartContextList.forEach(o -> {
            MesProductionPsOutContext productionPsOutContext = new MesProductionPsOutContext().foreignKey(o.getForeignKey());
            productionPsOutContext.setQty(new Double(buttonDynamicModel.getFunctionValue()));
            productionPsOutContextList.add(productionPsOutContext);
        });

        StepResult stepResult = StepResult.getSuccessComplete();
        //验证加工单完成数量
        workOrderCheckCompleteQtyStepService.dispatchWorkOrderCompleteQtyContext(reqBean, resultBean, stepResult, productionProcessContext, productionPartContextList, null, productionPsOutContextList);

        reqBean.setClientInfo(shippingDispatchService.getActorClientInfo(reqBean));
        reqBean.setInterfaceType(MesPcnConstWords.SHIPPING);
        reqBean.setBusiType(MesPcnEnumUtil.ACTOR_RECEIVE_STRATEGY.WS_CMD_DO_SCAN.getCode());

        if (!stepResult.isCompleted()) reqBean.setTriggerAutoFsm(true);
        else {

            //获取上下文产品加工规则数据信息集合
            List<MesProdRuleContext> prodRuleContextList = productionDispatchContextStepService.getProdRuleDataContext(reqBean);
            prodRuleContextList.stream().forEach(o -> o.eachCavityQty(buttonDynamicModel.getFunctionValue()));

            productionDispatchContextStepService.dispatchProdRuleDataContext(reqBean, prodRuleContextList);

            reqBean.setButtonCode(buttonDynamicModel.getButtonCode());
            reqBean.setStepDialogStatus(true);

        }

        shippingDispatchService.sendScanQueueNextExec(reqBean);

        return true;

    }

}
