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 : 武汉 油漆线 条码打印2（批量打印）
 * @Reference :
 * @Author : wangjie
 * @CreateDate 2024/9/27 18:25
 * @Modify:
 **/
@Component
@Slf4j
public class WuHanPaintCenterPrint2Strategy 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 today = TimeTool.getToday();
        for (int i = 0; i < mesProduceSnPrintModel.getPrintQty(); i++) {
            Map<String, Object> printTemplateData = new HashMap<>();
            printTemplateData.put(MesPcnExtConstWords.QR_CODE, mesPart.getPartSnParam());
            printTemplateData.put(MesPcnExtConstWords.PART_SN_PARAM, mesPart.getPartSnParam());
            printTemplateData.put(MesPcnExtConstWords.CREATE_DATE_TIME, today.substring(2));
            printTemplateData.put(MesPcnExtConstWords.PART_NAME, mesPart.getPartName());
            mesProduceSnPrintModel.getPrintContextList().add(printTemplateData);
        }

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

        return mesProduceSnPrintModel;
    }

}
