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.pojo.model.MesWorkOrderCutDetailModel;
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.StationCustomDialogBean;
import cn.estsh.i3plus.pojo.mes.model.StationRequestBean;
import cn.estsh.i3plus.pojo.mes.model.StationResultBean;
import com.alibaba.fastjson.JSONObject;
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;

/**
 * @Description : 裁片工单弹框接口实现【裁片工单打印弹框】
 **/
@Service
@Slf4j
public class MesFunctionDialogCutOrderPrintService extends BaseSwsService implements IFsmModuleFunctionService {

    @Autowired
    private IMesProductionDispatchContextStepService productionDispatchContextStepService;

    @Autowired
    private IShippingDispatchService shippingDispatchService;

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

        if (StringUtils.isEmpty(buttonDynamicModel.getFunctionValue())) {
            this.sendMessage(reqBean, resultBean.writeDbLog(MesPcnEnumUtil.WORK_CELL_SCAN_MONITOR_LOG_TYPE.PICK.getValue()).scanInfo(buttonDynamicModel.getFunctionValue()),
                    String.format("生产线[%s]工位[%s]裁片工单打印弹框提交信息有误!", reqBean.getWorkCenterCode(), reqBean.getWorkCellCode()),
                    MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
            return false;
        }

        this.sendMessage(reqBean, resultBean, String.format("生产线[%s]工位[%s]裁片工单打印弹框提交信息成功!", reqBean.getWorkCenterCode(), reqBean.getWorkCellCode()), MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.TEXT);
        this.sendMessage(reqBean, new StationResultBean().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.RUNNING_INFO, MesPcnEnumUtil.STATION_DATA_TYPE.TEXT);

        productionDispatchContextStepService.dispatchMesWorkOrderCutFgDataContext(reqBean, JSONObject.parseArray(buttonDynamicModel.getFunctionValue(), MesWorkOrderCutDetailModel.class));

        reqBean.setClientInfo(shippingDispatchService.getActorClientInfo(reqBean));
        reqBean.setInterfaceType(MesPcnConstWords.SHIPPING);
        reqBean.setBusiType(MesPcnEnumUtil.ACTOR_RECEIVE_STRATEGY.WS_CMD_DO_SCAN.getCode());
        reqBean.setButtonCode(buttonDynamicModel.getButtonCode());
        reqBean.setStepDialogStatus(true);
        shippingDispatchService.sendScanQueueNextExec(reqBean);

        return true;

    }

    @Override
    public StationCustomDialogBean stepDialogDispatch(StationRequestBean reqBean, StationCustomDialogBean dialogBean) {

        List<MesWorkOrderCutDetailModel> mesWorkOrderCutFgDataContextList = productionDispatchContextStepService.getMesWorkOrderCutFgDataContext(reqBean);

        return CollectionUtils.isEmpty(mesWorkOrderCutFgDataContextList) ? dialogBean.unDialog() : dialogBean.obj(mesWorkOrderCutFgDataContextList);
    }
}
