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

import cn.estsh.i3plus.ext.mes.pcn.api.base.ISxWorkOrderExtService;
import cn.estsh.i3plus.ext.mes.pcn.api.busi.IErrorLineOffService;
import cn.estsh.i3plus.ext.mes.pcn.api.busi.IWorkOrderLogExtService;
import cn.estsh.i3plus.ext.mes.pcn.pojo.bean.MesErrorLineOff;
import cn.estsh.i3plus.ext.mes.pcn.pojo.bean.MesWorkOrderExt;
import cn.estsh.i3plus.ext.mes.pcn.pojo.repository.MesWorkOrderExtRepository;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.ConvertBeanExt;
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.CommonEnumUtil;
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 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;
import java.util.stream.Collectors;

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

    @Autowired
    private IErrorLineOffService errorLineOffService;

    @Autowired
    private ISxWorkOrderExtService workOrderExtService;

    @Autowired
    private MesWorkOrderExtRepository workOrderExtRepository;

    @Autowired
    private IWorkOrderLogExtService workOrderLogExtService;

    @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);
        }

        List<MesErrorLineOff> errorLineOffList = errorLineOffService.getErrorLineOffListByWorkOrderNo(reqBean.getOrganizeCode(), reqBean.getWorkOrderNo());
        List<MesErrorLineOff> unFreList = CollectionUtils.isEmpty(errorLineOffList) ? null :
                errorLineOffList.stream().filter(o -> (null != o && CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue() == o.getFreStatus())).collect(Collectors.toList());
        if (!CollectionUtils.isEmpty(unFreList)) {
            err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "工单[%s]剩余确认补件数量[%s]"), reqBean.getWorkOrderNo(), unFreList.size());
            this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.TEXT);
            return StepResult.getNonComplete(err);
        }

        MesWorkOrderExt workOrderExt = workOrderExtService.getWorkOrderExtByOrderNo(reqBean.getOrganizeCode(), reqBean.getWorkOrderNo());
        if (null != workOrderExt) {
            workOrderExt.setMissingQty(new Double(0));
            ConvertBean.serviceModelUpdate(workOrderExt, reqBean.getUserInfo());
            workOrderExt.setSystemSyncStatus(MesPcnExtEnumUtil.IF_SYNC_STATUS.NO_SYNC.getValue());
            new ConvertBeanExt(workOrderExt).convertBean(workOrderExt);
            try {
                workOrderExtRepository.save(workOrderExt);
                workOrderLogExtService.insertMesWorkOrderLogExt(workOrderExt);
            } catch (Exception e) {
                err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "保存工单缺件数量及工单日志异常,请重新扫描条码[%s]"), reqBean.getSerialNumber());
                this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
                foundExThrowNoShowMsg();
            }
        }

        return StepResult.getSuccessComplete();
    }

}
