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.IMesShippingQueueService;
import cn.estsh.i3plus.ext.mes.pcn.pojo.context.MesProdRuleContext;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtConstWords;
import cn.estsh.i3plus.mes.pcn.serviceimpl.fsm.BaseStepService;
import cn.estsh.i3plus.platform.common.tool.TimeTool;
import cn.estsh.i3plus.pojo.base.enumutil.CommonEnumUtil;
import cn.estsh.i3plus.pojo.base.enumutil.MesPcnEnumUtil;
import cn.estsh.i3plus.pojo.mes.bean.shipping.MesShippingQueue;
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.util.MesExtEnumUtil;
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 : 发运队列消除工步
 * @Author : wangjie
 **/
@Slf4j
@Service("mesShippingQueueRemoveStepService")
public class MesShippingQueueRemoveStepService extends BaseStepService {

    @Autowired
    private IMesProductionDispatchContextStepService productionDispatchContextStepService;

    @Autowired
    private IMesShippingQueueService shippingQueueService;

    @Override
    public StepResult execute(StationRequestBean reqBean) {

        StationResultBean resultBean = new StationResultBean();

        StepResult stepResult = StepResult.getSuccessComplete();

        //保存上下文发运队列信息
        MesShippingQueue shippingQueue = productionDispatchContextStepService.getShippingQueueContext(reqBean);
        if (null == shippingQueue) {
            return stepDynamicsCompleteAndSendMsgReturn(reqBean, resultBean.writeDbLog(), stepResult,
                    true, MesPcnEnumUtil.STATION_BUSI_TYPE.RUNNING_INFO, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT, "当前未获取到缓存中有效的发运队列,消除队列失败!");
        }

        //获取上下文加工规则数据信息集合
        List<MesProdRuleContext> prodRuleContextList = productionDispatchContextStepService.getProdRuleDataContext(reqBean);

        //修改字段数组
        String[] propertyName = new String[]{MesPcnExtConstWords.QUEUE_STATUS, MesPcnExtConstWords.PID, MesPcnExtConstWords.PRODUCTION_RECORD_ID,
                MesPcnExtConstWords.MODIFY_USER, MesPcnExtConstWords.MODIFY_DATE_TIME, MesPcnExtConstWords.SYSTEM_SYNC_STATUS, MesPcnExtConstWords.SYSTEM_SYNC_DATE_TIME};

        //修改值数组
        Object[] propertyValue = new Object[]{MesExtEnumUtil.QUEUE_ORDER_STATUS.FINISH.getValue(), (!StringUtils.isEmpty(shippingQueue.getPidStr()) ? Long.valueOf(shippingQueue.getPidStr()) : 0L),
                ((!CollectionUtils.isEmpty(prodRuleContextList) && !StringUtils.isEmpty(prodRuleContextList.get(0).getProductionRecordId())) ? prodRuleContextList.get(0).getProductionRecordId() : 0L),
                reqBean.getUserInfo(), TimeTool.getNowTime(true), CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), MesPcnExtConstWords.EMPTY};

        //根据ID修改发运队列
        shippingQueueService.saveShippingQueue(reqBean.getOrganizeCode(), shippingQueue.getId(), propertyName, propertyValue);

        return stepSuccessCompleteAndSendMsgReturn(reqBean, resultBean.writeDbLog(), stepResult, String.format("发运单号[%s]已从发运队列中消除成功!", shippingQueue.getShippingCode()));

    }

}
