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

import cn.estsh.i3plus.ext.mes.pcn.api.busi.IMesPartShippingGroupService;
import cn.estsh.i3plus.ext.mes.pcn.api.busi.IMesProductionCustomContextStepService;
import cn.estsh.i3plus.ext.mes.pcn.api.busi.IMesProductionProcessContextStepService;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtConstWords;
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.mes.pcn.util.StationKvBeanUtil;
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.StationKvBean;
import cn.estsh.i3plus.pojo.mes.model.StationRequestBean;
import cn.estsh.i3plus.pojo.mes.model.StationResultBean;
import cn.estsh.i3plus.pojo.mes.util.MesExtEnumUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

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

/**
 * @Description : 工位参数按钮事件接口实现【选择发运组】
 **/
@Service
public class MesFunctionChooseShippingGroupService extends BaseSwsService implements IFsmModuleFunctionService {

    @Autowired
    private IMesProductionCustomContextStepService productionCustomContextStepService;

    @Autowired
    private IShippingDispatchService shippingDispatchService;

    @Autowired
    private IMesPartShippingGroupService partShippingGroupService;

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

        //验证发运组信息是否有效
        List<StationKvBean> resultList = checkShippingGroupIsValid(reqBean.getOrganizeCode(), buttonDynamicModel.getFunctionValue());
        if (CollectionUtils.isEmpty(resultList)) {
            this.sendMessage(reqBean, resultBean.writeDbLog(MesPcnEnumUtil.WORK_CELL_SCAN_MONITOR_LOG_TYPE.PICK.getValue()).scanInfo(buttonDynamicModel.getFunctionValue()),
                    String.format("生产线[%s]工位[%s]%s失败,请检查选择的发运组信息[%s]的有效性!", reqBean.getWorkCenterCode(), reqBean.getWorkCellCode(), buttonDynamicModel.getButtonName(), buttonDynamicModel.getFunctionValue()),
                    MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
            return false;
        }

        //保存选择的零件发运组信息上下文
        productionCustomContextStepService.dispatchFunctionChooseShippingGroupContext(reqBean, resultList);

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

        reqBean.setClientInfo(shippingDispatchService.getActorClientInfo(reqBean));
        reqBean.setInterfaceType(MesPcnConstWords.SHIPPING);
        reqBean.setBusiType(MesPcnEnumUtil.ACTOR_RECEIVE_STRATEGY.WS_CMD_DO_SCAN.getCode());
        reqBean.setTriggerAutoFsm(true);
        shippingDispatchService.sendScanQueueIfNoQueue(reqBean, false, this.getClass().getSimpleName());

        return true;

    }

    //验证发运组信息是否有效
    public List<StationKvBean> checkShippingGroupIsValid(String organizeCode, String functionValue) {
        //查询发运组数据
        if (null == partShippingGroupService.getMesPartShippingGroup(organizeCode, functionValue)) return null;
        return StationKvBeanUtil.addStationKvBeanList(new ArrayList<>(),
                new StationKvBean(MesPcnExtConstWords.SHIPPING_GROUP_CODE, "零件发运组代码", functionValue),
                new StationKvBean(MesPcnExtConstWords.CELL_MESSAGE_SOURCE, "工位信息来源", MesExtEnumUtil.CELL_MESSAGE_SOURCE.READ.getValueStr()));
    }

}
