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

import cn.estsh.i3plus.ext.mes.pcn.api.base.IMesCustomerPartService;
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.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.mes.pcn.util.DateUtil;
import cn.estsh.i3plus.platform.common.convert.ConvertBean;
import cn.estsh.i3plus.platform.common.tool.TimeTool;
import cn.estsh.i3plus.pojo.base.codemaker.SnowflakeIdMaker;
import cn.estsh.i3plus.pojo.mes.bean.MesCustomerPart;
import cn.estsh.i3plus.pojo.mes.bean.MesNumberRule;
import cn.estsh.i3plus.pojo.mes.bean.MesPart;
import cn.estsh.i3plus.pojo.mes.bean.MesProduceSn;
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 cn.estsh.i3plus.pojo.mes.util.MesExtEnumUtil;
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.*;

/**
 * @Description : 装配目视单标签打印
 * @Reference :
 * @Author : Castle
 * @CreateDate : 2024/6/17 16:43
 * @Modify:
 **/
@Component
@Slf4j
public class ChengDuSplitFixCharPrintStrategy implements IPrintTemplateStrategyService {
    @Autowired
    private ISyncFuncService syncFuncService;

    @Autowired
    private SnowflakeIdMaker snowflakeIdMaker;

    @Autowired
    private IMesPartService mesPartService;

    @Autowired
    private IMesPrintedSnLogService mesPrintedSnLogService;

    @Autowired
    private IMesCustomerPartService mesCustomerPartService;

    @Override
    public MesProduceSnPrintModel execute(GenSerialNoModel genSerialNoModel, MesProduceSnPrintModel mesProduceSnPrintModel, MesNumberRule numberRule, StepResult stepResult, StationRequestBean reqBean, Boolean isStep) {
        String organizeCode = mesProduceSnPrintModel.getOrganizeCode();
        //物料信息
        MesPart mesPart = mesPartService.getMesPartByPartNo(mesProduceSnPrintModel.getPartNo(), organizeCode);
        MesCustomerPart customerPart = (!Objects.isNull(genSerialNoModel) && !CollectionUtils.isEmpty(genSerialNoModel.getDataMap()) && genSerialNoModel.getDataMap().containsKey(MesCustomerPart.class.getSimpleName())) ? (MesCustomerPart) genSerialNoModel.getDataMap().get(MesCustomerPart.class.getSimpleName()) : mesCustomerPartService.getMesCustomerPart(organizeCode,mesProduceSnPrintModel.getPartNo());
        if (!isStep){
            if (!Objects.isNull(customerPart)) {
                genSerialNoModel.setCustPartNo(customerPart.getCustPartNo());
            }
            for (int i = 0; i < mesProduceSnPrintModel.getPrintQty(); i++) {
                //保存条码信息
                MesProduceSn produceSn = generateMesProduceSn(mesPart, syncFuncService.syncSerialNo(genSerialNoModel.partSnParam(mesPart.getPartSnParam()), mesProduceSnPrintModel.getUserName(), organizeCode, 1).getResultList().get(0).toString(), mesProduceSnPrintModel.getUserName(), mesProduceSnPrintModel.getQty());
                //封装打印信息
                MesProduceSnPrintDataModel printDataModel = getModel(produceSn, customerPart);
                mesProduceSnPrintModel.getMesProduceSnPrintDataModelList().clear();
                mesProduceSnPrintModel.getMesProduceSnList().add(produceSn);
                Map<String, Object> printTemplateData = new HashMap<>(getPrintContextMap(produceSn, customerPart));
                mesProduceSnPrintModel.getPrintContextList().add(printTemplateData);
                //保存打印记录
                mesProduceSnPrintModel.getMesPrintedSnLogList().add(mesPrintedSnLogService.getMesCustomPrintedSnLog(mesProduceSnPrintModel.getUserName(), organizeCode, printDataModel, printTemplateData));
            }
        }else{
            MesProduceSn mesProduceSn = mesProduceSnPrintModel.getMesProduceSnList().get(0);
            //封装打印信息
            MesProduceSnPrintDataModel printDataModel = getModel(mesProduceSn, customerPart);
            mesProduceSnPrintModel.getMesProduceSnPrintDataModelList().clear();
            Map<String, Object> printTemplateData = new HashMap<>(getPrintContextMap(mesProduceSn, customerPart));
            List<Map<String, Object>> printDataMapList = new ArrayList<>();
            printDataMapList.add(printTemplateData);
            mesProduceSnPrintModel.getPrintContextList().add(packResultMap(mesProduceSnPrintModel, printDataMapList));

            //保存打印记录
            mesProduceSnPrintModel.getMesPrintedSnLogList().add(mesPrintedSnLogService.getMesCustomPrintedSnLog(mesProduceSnPrintModel.getUserName(), organizeCode, printDataModel, printTemplateData));
        }
        return mesProduceSnPrintModel;
    }

    private MesProduceSn generateMesProduceSn(MesPart mesPart, String sn, String userName, Double qty) {
        MesProduceSn mesProduceSn = new MesProduceSn();
        mesProduceSn.setSerialNumber(snowflakeIdMaker.nextId() + "");
        mesProduceSn.setProductSn(sn);
        mesProduceSn.setCustSn(sn);
        mesProduceSn.setPartNo(mesPart.getPartNo());
        mesProduceSn.setPartName(mesPart.getPartName());
        mesProduceSn.setProcessLabelTemplate(mesPart.getProcessLabelTemplate());
        mesProduceSn.setCustLabelTemplate(mesPart.getCustLabelTemplate());
        mesProduceSn.setProdLabelTemplate(mesPart.getProductLabelTemplate());
        mesProduceSn.setQty(qty);
        mesProduceSn.setSnStatus(MesExtEnumUtil.PRODUCE_SN_STATUS.CREATE.getValue());
        mesProduceSn.setQcStatus(MesExtEnumUtil.PRODUCE_QC_STATUS.QUALIFIED.getValue());
        mesProduceSn.setLotNo(TimeTool.getToday());
        mesProduceSn.setPrintCount(MesPcnExtConstWords.ONE);
        mesProduceSn.setPrintStatus(MesExtEnumUtil.PRINT_STATUS.PRINTED.getValue());
        mesProduceSn.setOrganizeCode(mesPart.getOrganizeCode());
        ConvertBean.serviceModelInitialize(mesProduceSn, userName);
        return mesProduceSn;
    }

    private MesProduceSnPrintDataModel getModel(MesProduceSn produceSn, MesCustomerPart customerPart) {
        MesProduceSnPrintDataModel mesProduceSnPrintDataModel = new MesProduceSnPrintDataModel();
        mesProduceSnPrintDataModel.setPartNo(produceSn.getPartNo());
        mesProduceSnPrintDataModel.setPartName(produceSn.getPartName());
        if (!Objects.isNull(customerPart)) {
            mesProduceSnPrintDataModel.setCustPartNo(customerPart.getCustPartNo());
        }
        mesProduceSnPrintDataModel.setBarcode(produceSn.getProductSn());
        mesProduceSnPrintDataModel.setPrintDate(TimeTool.getNowTime(true));
        mesProduceSnPrintDataModel.setUserName(produceSn.getCreateUser());
        mesProduceSnPrintDataModel.setProductDate(TimeTool.parseStringFormat(produceSn.getLotNo(), DateUtil.SHORT_FORMAT, "yyyy/MM/dd"));
        return mesProduceSnPrintDataModel;
    }

    private Map<String, Object> getPrintContextMap(MesProduceSn produceSn, MesCustomerPart customerPart) {
        Map<String, Object> result = new HashMap<>();
        String[] splitSn = produceSn.getProductSn().split("#");
        if (splitSn.length < 3) {
            return result;
        }

        result.put(MesPcnExtConstWords.PART_NO, produceSn.getPartNo());
        result.put(MesPcnExtConstWords.PART_NAME, produceSn.getPartName());
        if (!Objects.isNull(customerPart)) {
            result.put(MesPcnExtConstWords.CUST_PART_NO, customerPart.getCustPartNo());
        }
        if (!StringUtils.isEmpty(splitSn[0])) {
            result.put("FIX_PART", splitSn[0].substring(0, 1));
            result.put(MesPcnExtConstWords.CUST_PART_NO, splitSn[0].substring(1));
        }
        if (!StringUtils.isEmpty(splitSn[1])) {
            result.put("FIX_DATE", splitSn[1].substring(0, 1));
            result.put(MesPcnExtConstWords.PRODUCT_DATE, splitSn[1].substring(1));
        }
        if (!StringUtils.isEmpty(splitSn[2])) {
            result.put("FIX_SUPPLIER", splitSn[2].substring(0, 1));
            result.put(MesPcnExtConstWords.SUPPLIER_CODE_L, splitSn[2].substring(1));
        }
        result.put(MesPcnExtConstWords.PRINT_BAR_CODE, produceSn.getProductSn());
        result.put(MesPcnExtConstWords.PRINT_DATE, TimeTool.getNowTime(true));
        result.put(MesPcnExtConstWords.USER_NAME, produceSn.getCreateUser());
        return result;
    }

    private Map<String, Object> packResultMap(MesProduceSnPrintModel printModel, List<Map<String, Object>> printTemplateDateList) {
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put(MesPcnExtConstWords.LABEL_TEMPLATE, printModel.getMesLabelTemplate());
        resultMap.put(MesPcnExtConstWords.TEMPLATE_DATA, printTemplateDateList);
        resultMap.put(MesPcnExtConstWords.TEMPLATE_CODE, printModel.getMesLabelTemplate().getTemplateCode());
        resultMap.put(MesPcnExtConstWords.PRINTER, printModel.getPrinter());
        return resultMap;
    }
}
