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

import cn.estsh.i3plus.ext.mes.pcn.pojo.bean.MesPartExt;
import cn.estsh.i3plus.ext.mes.pcn.pojo.repository.MesPartExtRepository;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtConstWords;
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.pojo.base.bean.DdlPackBean;
import cn.estsh.i3plus.pojo.base.enumutil.MesPcnEnumUtil;
import cn.estsh.i3plus.pojo.base.tool.DdlPreparedPack;
import cn.estsh.i3plus.pojo.mes.bean.MesWorkOrder;
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.repository.MesWorkOrderRepository;
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 : mingliang.li
 * @CreateDate : 2023-01-11
 * @Modify:
 **/
@Slf4j
@Service("sxBuFreOffLineCheckStepService")
public class SxBuFreOffLineCheckStepService extends BaseStepService {


    @Autowired
    private MesWorkOrderRepository mesWorkOrderRDao;

    @Autowired
    private MesPartExtRepository mesPartExtRepository;


    @Override
    public StepResult execute(StationRequestBean reqBean) {
        log.info("sxBuFreOffLineCheckStepService[execute] ws请求参数 {}", reqBean);
        StationResultBean resultBean = new StationResultBean();
        String err;


        if (StringUtils.isEmpty(reqBean.getWorkOrderNo())) {
            err = LocaleUtil.transferLanguage(reqBean.getUserInfo(), "入参缺少工单号");
            this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
            return StepResult.getNonComplete(err);
        }

        DdlPackBean packBean = DdlPackBean.getDdlPackBean(reqBean.getOrganizeCode());
        DdlPreparedPack.getStringEqualPack(reqBean.getWorkOrderNo(), "orderNo", packBean);
        MesWorkOrder mesWorkOrder = mesWorkOrderRDao.getByProperty(packBean);

        DdlPackBean partPackBean = DdlPackBean.getDdlPackBean(reqBean.getOrganizeCode());
        DdlPreparedPack.getStringLikerPack(mesWorkOrder.getPartNo(), MesPcnExtConstWords.PART_NO, partPackBean);
        MesPartExt partExt = mesPartExtRepository.getByProperty(partPackBean);

        if (StringUtils.isEmpty(partExt)) {
            err = LocaleUtil.transferLanguage(reqBean.getUserInfo(), "缺少物料信息");
            this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
            return StepResult.getNonComplete(err);
        }


        //补件是否报工
        boolean flg = false;

        if (!StringUtils.isEmpty(partExt.getIsRepairWorkReport()) && partExt.getIsRepairWorkReport() == MesPcnExtEnumUtil.IS_REPAIR_WORK_REPORT.NO.getValue()) {
            flg = true;
        }

        if (flg) {
            err = LocaleUtil.transferLanguage(reqBean.getUserInfo(), "补件不需要报工！");
            this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.GUIDE, MesPcnEnumUtil.STATION_DATA_TYPE.TEXT);
            return StepResult.getNonComplete(err);
        }

        return StepResult.getSuccessComplete();
    }


}

