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

import cn.estsh.i3plus.ext.mes.pcn.api.base.ISxWorkOrderExtService;
import cn.estsh.i3plus.ext.mes.pcn.api.busi.IPackageExtService;
import cn.estsh.i3plus.ext.mes.pcn.api.busi.IProduceSnExtService;
import cn.estsh.i3plus.ext.mes.pcn.api.busi.bu.IOuterPackageSnService;
import cn.estsh.i3plus.ext.mes.pcn.pojo.bean.MesOuterPackage;
import cn.estsh.i3plus.ext.mes.pcn.pojo.bean.MesOuterPackageDetail;
import cn.estsh.i3plus.ext.mes.pcn.pojo.bean.MesProduceSnExt;
import cn.estsh.i3plus.ext.mes.pcn.pojo.bean.MesWorkOrderExt;
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.tool.MathOperation;
import cn.estsh.i3plus.pojo.base.enumutil.MesPcnEnumUtil;
import cn.estsh.i3plus.pojo.mes.bean.MesPackageDetail;
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.StringUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description : 工步业务-净水器返工校验条码有效性工步
 * @Reference :
 * @Author : wangjie
 * @CreateDate : 2019-07-11
 * @Modify:
 **/
@Slf4j
@Service("sxOuterPackageRepairCheckSnStepService")
public class SxOuterPackageRepairCheckSnStepService extends BaseStepService {

    @Autowired
    private IProduceSnExtService produceSnExtService;

    @Autowired
    private ISxWorkOrderExtService workOrderExtService;

    @Autowired
    private IOuterPackageSnService iOuterPackageSnService;

    @Autowired
    private IPackageExtService iPackageExtService;

    @Override
    public StepResult guide(StationRequestBean reqBean) {
        StationResultBean resultBean = new StationResultBean();
        String guide = LocaleUtil.transferLanguage(reqBean.getUserInfo(), "请扫描大包装条码");
        this.sendMessage(reqBean, resultBean, guide, MesPcnEnumUtil.STATION_BUSI_TYPE.GUIDE, MesPcnEnumUtil.STATION_DATA_TYPE.TEXT);
        return super.guide(reqBean);
    }

    @Override
    public StepResult execute(StationRequestBean reqBean) {
        log.info("sxOuterPackageRepairCheckSnStepService[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.getScanInfo())) {
            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<MesOuterPackage> mesOuterPackages = iOuterPackageSnService.getOuterPackageListByPackageNo(reqBean.getOrganizeCode(),reqBean.getScanInfo());
        if(null == mesOuterPackages || mesOuterPackages.size() == 0){
            err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "扫描的大包装条码[%s]不存在"), reqBean.getScanInfo());
            sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
            return StepResult.getNonCompleteStop(err);
        }
        if(!(mesOuterPackages.get(0).getIsSealed() == MesPcnExtEnumUtil.PACKAGE_IS_SEALED.SEALED.getValue())){
            err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "扫描的大包装条码[%s]状态不为已封箱"), reqBean.getScanInfo());
            sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
            return StepResult.getNonCompleteStop(err);
        }
        // 校验工单
        MesWorkOrderExt workOrderExt = workOrderExtService.getWorkOrderExtByOrderNo(reqBean.getOrganizeCode(), reqBean.getWorkOrderNo());
        if (null == workOrderExt) {
            err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "工单[%s]无效,不能操作"), reqBean.getWorkOrderNo());
            sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
            return StepResult.getNonComplete(err);
        }
        if (MesPcnExtEnumUtil.WORK_ORDER_TYPE.REWORK_ORDER.getValue() != workOrderExt.getWorkOrderType()) {
            err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "工单[%s]类型为[%s],不能操作"),
                    reqBean.getWorkOrderNo(), MesPcnExtEnumUtil.WORK_ORDER_TYPE.valueOfDescription(workOrderExt.getWorkOrderType()));
            sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
            return StepResult.getNonComplete(err);
        }
        if (MathOperation.compareTo(workOrderExt.getQty(), new Double(0)) <= 0) {
            err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "工单[%s]计划数量未[0],不能操作"), reqBean.getWorkOrderNo());
            sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
            return StepResult.getNonComplete(err);

        }
        if (MesPcnExtEnumUtil.WORK_ORDER_STATUS.OPEN.getValue() != workOrderExt.getWorkOrderStatus()) {
            err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "工单[%s]状态为[%s],不能操作"),
                    reqBean.getWorkOrderNo(), MesPcnExtEnumUtil.WORK_ORDER_STATUS.valueOfDescription(workOrderExt.getWorkOrderStatus()));
            sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
            return StepResult.getNonComplete(err);
        }
        if (MesPcnExtEnumUtil.WORK_ORDER_SN_CREATE_STATUS.CREATED.getValue() == workOrderExt.getSnCreateStatus()) {
            err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "工单[%s]条码生成状态为[%s],不能操作"),
                    reqBean.getWorkOrderNo(), MesPcnExtEnumUtil.WORK_ORDER_SN_CREATE_STATUS.valueOfDescription(workOrderExt.getSnCreateStatus()));
            sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
            return StepResult.getNonComplete(err);
        }

        // 通过大包装条码获取小包装条码列表
        List<MesOuterPackageDetail> mesOuterPackageDetails = iOuterPackageSnService.getOuterPackageDetailListByOuterPackageNo(reqBean.getOrganizeCode(),reqBean.getScanInfo());
        // 得到小包装条码集合
        List<String> innerPackageNoList = mesOuterPackageDetails.stream().filter(o -> (null != o && !StringUtils.isEmpty(o.getInnerPackageNo()))).map(MesOuterPackageDetail::getInnerPackageNo).collect(Collectors.toList());

        for (int i = 0; i < innerPackageNoList.size(); i++) {

            // 通过小包装条码去包装表中查询得到本体条码（一对一的关系）
            MesPackageDetail mesPackageDetail = iPackageExtService.getPackageDetailByPackNo(reqBean.getOrganizeCode(), innerPackageNoList.get(i));
            if (null == mesPackageDetail || StringUtils.isEmpty(mesPackageDetail.getSerialNumber())) {
                String errInfo = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "大包装条码[%s]对应的小包装条码[%s]无效"), innerPackageNoList.get(i));
                sendMessage(reqBean, new StationResultBean(), errInfo, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
                return StepResult.getNonComplete(errInfo);
            }

            MesProduceSnExt produceSnExt = produceSnExtService.getProduceSnExtBySn(reqBean.getOrganizeCode(), mesPackageDetail.getSerialNumber());
            if (null == produceSnExt || StringUtils.isEmpty(produceSnExt.getSerialNumber())) {
                String errInfo = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "大包装条码[%s]对应的小包装条码[%s]查询不到对应的本体条码"), innerPackageNoList.get(i));
                sendMessage(reqBean, new StationResultBean(), errInfo, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
                return StepResult.getNonComplete(errInfo);
            }
            if (!produceSnExt.getPartNo().equals(workOrderExt.getPartNo())) {
                err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "工单[%s]关联的物料编码[%s]与产品条码[%s]关联的物料编码[%s]不一致,不能操作"),
                        workOrderExt.getOrderNo(), workOrderExt.getPartNo(), produceSnExt.getSerialNumber(), produceSnExt.getPartNo());
                sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
                return StepResult.getNonComplete(err);
            }
            if (MesPcnExtEnumUtil.PRODUCE_SN_QC_STATUS.QUALIFIED.getValue() != produceSnExt.getQcStatus()) {
                err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "条码[%s]质量状态为[%s],不能操作"),
                        reqBean.getSerialNumber(), MesPcnExtEnumUtil.PRODUCE_SN_QC_STATUS.valueOfDescription(produceSnExt.getQcStatus()));
                sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
                return StepResult.getNonComplete(err);
            }
        }

        return StepResult.getSuccessComplete();
    }

}
