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

import cn.estsh.i3plus.ext.mes.pcn.api.busi.IErrorLineOffService;
import cn.estsh.i3plus.ext.mes.pcn.api.busi.IProduceSnExtService;
import cn.estsh.i3plus.ext.mes.pcn.pojo.bean.MesErrorLineOff;
import cn.estsh.i3plus.ext.mes.pcn.pojo.bean.MesErrorLineOffDetail;
import cn.estsh.i3plus.ext.mes.pcn.pojo.bean.MesProduceSnExt;
import cn.estsh.i3plus.ext.mes.pcn.pojo.model.ProduceSnExtModel;
import cn.estsh.i3plus.ext.mes.pcn.pojo.repository.MesErrorLineOffRepository;
import cn.estsh.i3plus.ext.mes.pcn.pojo.repository.MesProduceSnExtRepository;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.ConvertBeanExt;
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.mes.pcn.util.PojoAttrUtil;
import cn.estsh.i3plus.platform.common.convert.ConvertBean;
import cn.estsh.i3plus.pojo.base.enumutil.CommonEnumUtil;
import cn.estsh.i3plus.pojo.base.enumutil.MesPcnEnumUtil;
import cn.estsh.i3plus.pojo.mes.model.AttrBean;
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 lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
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.ArrayList;
import java.util.List;

/**
 * @Description : 工步业务-半成品异常下线缺件-补件确认工步
 * @Reference :
 * @Author : wangjie
 * @CreateDate : 2019-07-11
 * @Modify:
 **/
@Slf4j
@Service("sxFrePartConfirmStepService")
public class SxFrePartConfirmStepService extends BaseStepService {

    @Autowired
    private IProduceSnExtService produceSnExtService;

    @Autowired
    private MesProduceSnExtRepository produceSnExtRepository;

    @Autowired
    private IErrorLineOffService errorLineOffService;

    @Autowired
    private MesErrorLineOffRepository errorLineOffRepository;

    @Override
    public StepResult execute(StationRequestBean reqBean) {
        log.info("SxFrePartConfirmStepService[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.getNonComplete(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);
        }

        if (StringUtils.isEmpty(reqBean.getSerialNumber())) {
            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);
        }

        MesProduceSnExt produceSnExt = produceSnExtService.getProduceSnExtBySn(reqBean.getOrganizeCode(), reqBean.getSerialNumber());
        if (null == produceSnExt) {
            err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "条码[%s]无效"), reqBean.getSerialNumber());
            sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
            return StepResult.getNonComplete(err);
        }

        produceSnExt.setSnStatus(MesPcnExtEnumUtil.PRODUCE_SN_STATUS.OFFLINE.getValue());
        produceSnExtService.packSnExtProcess(produceSnExt, reqBean.getOrganizeCode(), reqBean.getWorkCenterCode(), reqBean.getWorkCellCode());
        ConvertBean.serviceModelUpdate(produceSnExt, reqBean.getUserInfo());
        produceSnExt.setSystemSyncStatus(MesPcnExtEnumUtil.IF_SYNC_STATUS.NO_SYNC.getValue());
        new ConvertBeanExt(produceSnExt).convertBean(produceSnExt);
        produceSnExtRepository.save(produceSnExt);

        MesErrorLineOff errorLineOff = errorLineOffService.getErrorLineOffBySnAndOrder(reqBean.getOrganizeCode(), reqBean.getWorkOrderNo(), produceSnExt.getSerialNumber());
        if (null != errorLineOff) {
            errorLineOff.setFreStatus(CommonEnumUtil.TRUE_OR_FALSE.TRUE.getValue());
            errorLineOff.setFreUser(reqBean.getUserInfo());
            ConvertBean.serviceModelUpdate(errorLineOff, reqBean.getUserInfo());
            errorLineOff.setFreTime(errorLineOff.getModifyDatetime());
            errorLineOff.setSystemSyncStatus(MesPcnExtEnumUtil.IF_SYNC_STATUS.NO_SYNC.getValue());
            errorLineOffRepository.save(errorLineOff);

            List<MesErrorLineOffDetail> detailList = errorLineOffService.getErrorLineOffDetailListByPid(errorLineOff.getId());
            if (!CollectionUtils.isEmpty(detailList)) {
                List<AttrBean> attrBeanList = new ArrayList<>();
                PojoAttrUtil.loadPojoAttrs(attrBeanList, MesPcnExtConstWords.SERIAL_NUMBER, LocaleUtil.transferLanguage(reqBean.getUserInfo(), "产品条码"));
                PojoAttrUtil.loadPojoAttrs(attrBeanList, MesPcnExtConstWords.ITEM_PART_NO, LocaleUtil.transferLanguage(reqBean.getUserInfo(), "缺件物料代码"));
                PojoAttrUtil.loadPojoAttrs(attrBeanList, MesPcnExtConstWords.ITEM_PART_NAME, LocaleUtil.transferLanguage(reqBean.getUserInfo(), "缺件物料名称"));

                resultBean.setBusiType(MesPcnEnumUtil.STATION_BUSI_TYPE.STEP_CONTENT.getValue());
                resultBean.setDataType(MesPcnEnumUtil.STATION_DATA_TYPE.TABLE.getValue());
                resultBean.setResultList(detailList);
                resultBean.setDataAttrList(attrBeanList);
                this.sendMessage(reqBean, resultBean);
            }
        }

        ProduceSnExtModel snModel = new ProduceSnExtModel();
        BeanUtils.copyProperties(produceSnExt, snModel, MesPcnExtConstWords.ID);
        snModel.setWorkCenterCode(reqBean.getWorkCenterCode());
        snModel.setWorkCellCode(reqBean.getWorkCellCode());
        reqBean.getDataMap().put(MesPcnExtConstWords.PRODUCE_SN, snModel);

        return StepResult.getSuccessComplete();
    }

}
