package cn.estsh.i3plus.ext.mes.pcn.apiservice.serviceimpl.print.strategy;

import cn.estsh.i3plus.ext.mes.pcn.api.base.IMesPartService;
import cn.estsh.i3plus.ext.mes.pcn.api.busi.IMesPrintedSnLogService;
import cn.estsh.i3plus.ext.mes.pcn.apiservice.serviceimpl.print.IPrintTemplateStrategyService;
import cn.estsh.i3plus.ext.mes.pcn.apiservice.util.MesPcnException;
import cn.estsh.i3plus.ext.mes.pcn.pojo.model.MesProduceSnPrintDataModel;
import cn.estsh.i3plus.ext.mes.pcn.pojo.model.MesProduceSnPrintModel;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtConstWords;
import cn.estsh.i3plus.mes.pcn.api.iservice.busi.ISyncFuncService;
import cn.estsh.i3plus.platform.common.tool.TimeTool;
import cn.estsh.i3plus.pojo.base.codemaker.SnowflakeIdMaker;
import cn.estsh.i3plus.pojo.mes.bean.MesNumberRule;
import cn.estsh.i3plus.pojo.mes.bean.MesPart;
import cn.estsh.i3plus.pojo.mes.model.GenSerialNoModel;
import cn.estsh.i3plus.pojo.mes.model.StationRequestBean;
import cn.estsh.i3plus.pojo.mes.model.StepResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * @Description : 武汉 油漆线 条码打印（批量打印）
 * @Reference :
 * @Author : wangjie
 * @CreateDate 2024/9/27 18:25
 * @Modify:
 **/
@Component
@Slf4j
public class WuHanPaintCenterPrintStrategy implements IPrintTemplateStrategyService {

    @Autowired
    private ISyncFuncService syncFuncService;

    @Autowired
    private SnowflakeIdMaker snowflakeIdMaker;

    @Autowired
    private IMesPartService mesPartService;

    @Autowired
    private IMesPrintedSnLogService mesPrintedSnLogService;

    private static final String SUPPLIER_CODE_DEFAULT = "V280156";

    @Override
    public MesProduceSnPrintModel execute(GenSerialNoModel genSerialNoModel, MesProduceSnPrintModel mesProduceSnPrintModel, MesNumberRule numberRule, StepResult stepResult, StationRequestBean reqBean, Boolean isStep) {

        if (isStep) return mesProduceSnPrintModel;

        String organizeCode = mesProduceSnPrintModel.getOrganizeCode();

        MesPart mesPart = (!CollectionUtils.isEmpty(genSerialNoModel.getDataMap()) && genSerialNoModel.getDataMap().containsKey(MesPart.class.getSimpleName())) ?
                (MesPart) genSerialNoModel.getDataMap().get(MesPart.class.getSimpleName()) : mesPartService.getMesPartByPartNo(mesProduceSnPrintModel.getPartNo(), organizeCode);

        if (null == mesPart) MesPcnException.throwBusiException("物料[%s]信息不存在!", mesProduceSnPrintModel.getPartNo());
        if (StringUtils.isEmpty(mesPart.getPartSnParam())) MesPcnException.throwBusiException("物料[%s]信息未维护[零件条码参数]!", mesProduceSnPrintModel.getPartNo());

        String nowTime = TimeTool.getNowTime(true);

        //{PREFIX}{SPILTRULE}{PARTSNPARAM}{SPILTRULE}{YEAR}{}{MONTH}{DAY}{SPILTRULE}   prefix字段维护成:WuHanPaint      spiltRule字段维护成:  P,#T,#V280156#
        genSerialNoModel.partSnParam(mesPart.getPartSnParam()).year(nowTime.substring(2, 4));
        String qrCode = syncFuncService.syncSerialNo(genSerialNoModel, mesProduceSnPrintModel.getUserName(), organizeCode, 1).getResultList().get(0).toString();

        if (StringUtils.isEmpty(qrCode)) MesPcnException.throwBusiException("根据编码规则[%s]生成条码失败!", numberRule.getRuleCode());
        if (!StringUtils.isEmpty(numberRule.getPrefix())) qrCode = qrCode.replace(numberRule.getPrefix(), MesPcnExtConstWords.EMPTY);

        String[] barCodeArr = qrCode.split(MesPcnExtConstWords.WELL_NO);

        for (int i = 0; i < mesProduceSnPrintModel.getPrintQty(); i++) {
            Map<String, Object> printTemplateData = new HashMap<>();
            printTemplateData.put(MesPcnExtConstWords.QR_CODE, qrCode);
            if (barCodeArr.length >= 1) printTemplateData.put(MesPcnExtConstWords.PART_SN_PARAM, barCodeArr[0] + MesPcnExtConstWords.WELL_NO);
            if (barCodeArr.length >= 2) printTemplateData.put(MesPcnExtConstWords.CREATE_DATE_TIME, barCodeArr[1] + MesPcnExtConstWords.WELL_NO);
            if (barCodeArr.length >= 3) printTemplateData.put(MesPcnExtConstWords.SUPPLIER_CODE_L, barCodeArr[2] + MesPcnExtConstWords.WELL_NO);
            mesProduceSnPrintModel.getPrintContextList().add(printTemplateData);
        }

        //保存打印记录
        MesProduceSnPrintDataModel printDataModel = new MesProduceSnPrintDataModel();
        printDataModel.setBarcode(qrCode);
        printDataModel.setPartNo(mesPart.getPartNo());
        printDataModel.setPartName(mesPart.getPartName());
        printDataModel.setPrintDate(nowTime);
        printDataModel.setProductDate(TimeTool.getToday());
        printDataModel.setUserName(mesProduceSnPrintModel.getUserName());
        printDataModel.setPrintData(String.format("{%s:%s, %s:%s}", MesPcnExtConstWords.QR_CODE, qrCode, MesPcnExtConstWords.QTY, mesProduceSnPrintModel.getPrintQty()));
        mesProduceSnPrintModel.getMesPrintedSnLogList().add(mesPrintedSnLogService.getMesPrintedSnLog(mesProduceSnPrintModel.getUserName(), organizeCode, printDataModel));

        return mesProduceSnPrintModel;
    }

}
