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

import cn.estsh.i3plus.ext.mes.pcn.api.base.IPartExtService;
import cn.estsh.i3plus.ext.mes.pcn.api.base.IProdPackExtService;
import cn.estsh.i3plus.ext.mes.pcn.api.base.IWorkCenterExtService;
import cn.estsh.i3plus.ext.mes.pcn.api.busi.IPackageExtService;
import cn.estsh.i3plus.ext.mes.pcn.api.busi.IProductEncodeCfgExtService;
import cn.estsh.i3plus.ext.mes.pcn.pojo.bean.MesPackageExt;
import cn.estsh.i3plus.ext.mes.pcn.pojo.bean.MesPartExt;
import cn.estsh.i3plus.ext.mes.pcn.pojo.bean.MesProdPackExt;
import cn.estsh.i3plus.ext.mes.pcn.pojo.bean.MesWorkCenterExt;
import cn.estsh.i3plus.ext.mes.pcn.pojo.model.PackageExtModel;
import cn.estsh.i3plus.ext.mes.pcn.pojo.model.WorkCenterExtModel;
import cn.estsh.i3plus.ext.mes.pcn.pojo.repository.MesPackageExtRepository;
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.api.iservice.busi.ISyncFuncService;
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.bean.BaseResultBean;
import cn.estsh.i3plus.pojo.base.enumutil.MesPcnEnumUtil;
import cn.estsh.i3plus.pojo.mes.bean.MesPackageDetail;
import cn.estsh.i3plus.pojo.mes.model.GenSerialNoModel;
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.MesPackageDetailRepository;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.BeanUtils;
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("sxNoumenonPackageStepService")
public class SxNoumenonPackageStepService extends BaseStepService {

    @Autowired
    private IPartExtService partExtService;

    @Autowired
    private IProdPackExtService prodPackExtService;

    @Autowired
    private IProductEncodeCfgExtService productEncodeCfgExtService;

    @Autowired
    private ISyncFuncService syncFuncService;

    @Autowired
    private IPackageExtService packageExtService;

    @Autowired
    private MesPackageExtRepository packageExtRepository;

    @Autowired
    private MesPackageDetailRepository packageDetailRepository;

    @Autowired
    private IWorkCenterExtService workCenterExtService;

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

        MesWorkCenterExt workCenterExt = workCenterExtService.getWorkCenterExtByWorkCenterCode(reqBean.getOrganizeCode(), reqBean.getWorkCenterCode());
        if (null == workCenterExt) {
            err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "生产线代码[%s]无效"), reqBean.getWorkCenterCode());
            this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
            foundExThrowNoShowMsg();
        }
        if (StringUtils.isEmpty(workCenterExt.getShiftCode()) || StringUtils.isEmpty(workCenterExt.getWorkTime())) {
            err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "生产线代码[%s]未启动班次"), reqBean.getWorkCenterCode());
            this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
            foundExThrowNoShowMsg();
        }

        MesPartExt partDb = partExtService.getPartExt(reqBean.getOrganizeCode(), reqBean.getPartNo());
        if (null == partDb) {
            err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "物料编码[%s]无效"), reqBean.getPartNo());
            this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
            return StepResult.getNonComplete(err);
        }

        MesPackageExt pack;
        MesPackageDetail packageDetail = packageExtService.getPackageDetailBySn(reqBean.getOrganizeCode(), reqBean.getSerialNumber());
        if (null != packageDetail) {
            pack = packageExtService.getPackageByPackNo(reqBean.getOrganizeCode(), packageDetail.getPackageNo());
            if (null == pack) {
                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();
            }

            PackageExtModel packModel = new PackageExtModel();
            BeanUtils.copyProperties(pack, packModel, MesPcnExtConstWords.ID);
            reqBean.getDataMap().put(MesPcnExtConstWords.PACK_SN, packModel);

            reqBean.setPackageNo(pack.getPackageNo());
            return StepResult.getSuccessComplete();
        }

        GenSerialNoModel genSerialNoModel = new GenSerialNoModel();
        try {
            genSerialNoModel.setRuleCode(productEncodeCfgExtService.getRuleCodeByMatchType(
                    reqBean.getOrganizeCode(), MesPcnExtEnumUtil.ENCODE_CODE_TYPE.PACK_SN.getValue(), partDb, reqBean.getWorkCenterCode()));
        } catch (Exception e) {
            err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "%s"), e.getMessage());
            sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
            foundExThrowNoShowMsg();
        }
        genSerialNoModel.setSerialNumber(reqBean.getSerialNumber());
        BaseResultBean<String> rb = syncFuncService.syncSerialNo(genSerialNoModel, reqBean.getUserInfo(), reqBean.getOrganizeCode(), 1);
        if (rb != null && rb.isSuccess()) {
            if (CollectionUtils.isEmpty(rb.getResultList())) {
                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);
            }
        } else {
            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);
        }
        MesProdPackExt prodPackDb = prodPackExtService.getProdPackExt(reqBean.getOrganizeCode(), reqBean.getPartNo(), partDb.getPptCode());

        pack = new MesPackageExt();
        pack.setPackageNo(rb.getResultList().get(0));
        pack.setPartNo(reqBean.getPartNo());
        pack.setPartNameRdd(reqBean.getPartName());
        pack.setQty(new Double(1));
        pack.setPackSpecQty(new Double(1));
        pack.setUnit(partDb.getUnit());
        pack.setIsSealed(MesPcnEnumUtil.PACKAGE_IS_SEALED.SEALED.getValue());
        pack.setPrintStatus(MesPcnEnumUtil.PACKAGE_PRINT_STATUS.PRINTED.getValue());
        pack.setPackLevel(MesPcnEnumUtil.PACKAGE_PACKLEVEL.FIRST.getValue());
        pack.setWorkOrderNo(reqBean.getWorkOrderNo());
        pack.setLastWorkOrderNo(reqBean.getWorkOrderNo());
        pack.setWorkCenterCode(reqBean.getWorkCenterCode());
        pack.setWorkCellCode(reqBean.getWorkCellCode());
        pack.setPackageLabelTemplate(null != prodPackDb ? prodPackDb.getPackLabelTemplate() : null);
        pack.setOrganizeCode(reqBean.getOrganizeCode());
        ConvertBean.serviceModelInitialize(pack, reqBean.getUserInfo());
        pack.setSystemSyncStatus(MesPcnExtEnumUtil.IF_SYNC_STATUS.NO_SYNC.getValue());
        new ConvertBeanExt(pack).convertBean(pack);
        packageExtRepository.insert(pack);

        packageDetail = new MesPackageDetail();
        packageDetail.setPackageNo(pack.getPackageNo());
        packageDetail.setSerialNumber(reqBean.getSerialNumber());
        packageDetail.setPartNo(reqBean.getPartNo());
        packageDetail.setPartNameRdd(reqBean.getPartName());
        packageDetail.setOrganizeCode(reqBean.getOrganizeCode());
        packageDetail.setSystemSyncStatus(MesPcnExtEnumUtil.IF_SYNC_STATUS.NO_SYNC.getValue());
        ConvertBean.serviceModelInitialize(packageDetail, reqBean.getUserInfo());
        packageDetailRepository.insert(packageDetail);

        PackageExtModel packModel = new PackageExtModel();
        BeanUtils.copyProperties(pack, packModel, MesPcnExtConstWords.ID);
        reqBean.getDataMap().put(MesPcnExtConstWords.PACK_SN, packModel);

        WorkCenterExtModel centerModel = new WorkCenterExtModel();
        BeanUtils.copyProperties(workCenterExt, centerModel, MesPcnExtConstWords.ID);
        reqBean.getDataMap().put(MesPcnExtConstWords.WORK_CENTER, centerModel);

        reqBean.setPackageNo(pack.getPackageNo());
        err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "产品条码[%s]已经生成包装条码[%s]"), reqBean.getSerialNumber(), pack.getPackageNo());
        sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.TEXT);
        return StepResult.getSuccessComplete();
    }

}
