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

import cn.estsh.i3plus.ext.mes.pcn.pojo.bean.MesWorkCellPrintQueue;
import cn.estsh.i3plus.ext.mes.pcn.pojo.repository.MesWorkCellPrintQueueRepository;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtEnumUtil;
import cn.estsh.i3plus.mes.pcn.serviceimpl.base.BaseStepService;
import cn.estsh.i3plus.mes.pcn.util.LocaleUtil;
import cn.estsh.i3plus.platform.common.convert.ConvertBean;
import cn.estsh.i3plus.pojo.base.enumutil.MesPcnEnumUtil;
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 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.StringUtils;

/**
 * @Description : 工步业务-打印队列执行打印工步
 * @Reference :
 * @Author : wangjie
 * @CreateDate : 2019-07-11
 * @Modify:
 **/
@Slf4j
@Service("sxWorkCellPrintQueueExecStepService")
public class SxWorkCellPrintQueueExecStepService extends BaseStepService {

    @Autowired
    private MesWorkCellPrintQueueRepository workCellPrintQueueRepository;

    @Override
    public StepResult execute(StationRequestBean reqBean) {
        log.info("SxWorkCellPrintQueueExecStepService[execute] 参数：{}", reqBean);

        StationResultBean resultBean = new StationResultBean();
        String err;

        if (StringUtils.isEmpty(reqBean.getOrganizeCode())) {
            err = LocaleUtil.transferLanguage(reqBean.getUserInfo(), "入参缺少工厂代码");
            this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
            return StepResult.getSuccessComplete();
        }

        if (StringUtils.isEmpty(reqBean.getScanInfo())) {
            err = LocaleUtil.transferLanguage(reqBean.getUserInfo(), "入参缺少打印数据ID");
            this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
            return StepResult.getSuccessComplete();
        }

        try {
            MesWorkCellPrintQueue workCellPrintQueue = workCellPrintQueueRepository.getById(Long.valueOf(reqBean.getScanInfo()));
            if (null != workCellPrintQueue && !StringUtils.isEmpty(workCellPrintQueue.getPrintContent()) &&
                    MesPcnEnumUtil.PACKAGE_PRINT_STATUS.UNPRINTED.getValue() == workCellPrintQueue.getPrintStatus()) {

                resultBean.setBusiType(MesPcnEnumUtil.STATION_BUSI_TYPE.CUSTOM_COMPONENT.getValue());
                if (MesPcnExtEnumUtil.PRINT_QUEUE_TYPE.QUALITY_ASSURE.getValue() == workCellPrintQueue.getPrintType()) {
                    resultBean.setDataType("QUALITY_ASSURE");
                }
                resultBean.setResultObj(JSONObject.parse(workCellPrintQueue.getPrintContent()));
                this.sendMessage(reqBean, resultBean);

                workCellPrintQueue.setPrintStatus(MesPcnEnumUtil.PACKAGE_PRINT_STATUS.PRINTED.getValue());
                ConvertBean.serviceModelUpdate(workCellPrintQueue, reqBean.getUserInfo());
                workCellPrintQueueRepository.save(workCellPrintQueue);

                err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "产品条码[%s]对应的[%s] -- 打印成功"),
                        workCellPrintQueue.getSerialNumber(), MesPcnExtEnumUtil.PRINT_QUEUE_TYPE.valueOfDescription(workCellPrintQueue.getPrintType()));
                this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.TEXT);
            }
        } catch (Exception e) {
        }

        return StepResult.getSuccessComplete();
    }

}
