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.apiservice.serviceimpl.print.IPrintTemplateStrategyService;
import cn.estsh.i3plus.ext.mes.pcn.apiservice.util.MesPcnException;
import cn.estsh.i3plus.ext.mes.pcn.pojo.model.MesProduceSnPrintModel;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtConstWords;
import cn.estsh.i3plus.platform.common.convert.ConvertBean;
import cn.estsh.i3plus.pojo.mes.bean.MesCustomerPart;
import cn.estsh.i3plus.pojo.mes.bean.MesNumberRule;
import cn.estsh.i3plus.pojo.mes.bean.MesPackage;
import cn.estsh.i3plus.pojo.mes.bean.MesPrintedSnLog;
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 com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.*;

/**
 * @Description : 包装条码打印
 * @Reference :
 * @Author : wangjie
 * @CreateDate : 2024/9/29 16:43
 * @Modify:
 **/
@Component
@Slf4j
public class YfaiPackageNoPrintStrategy implements IPrintTemplateStrategyService {

    @Autowired
    private IMesCustomerPartService mesCustomerPartService;

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

        MesCustomerPart customerPart = (!Objects.isNull(genSerialNoModel) &&
                !CollectionUtils.isEmpty(genSerialNoModel.getDataMap()) && genSerialNoModel.getDataMap().containsKey(MesCustomerPart.class.getSimpleName()))
                ? (MesCustomerPart) genSerialNoModel.getDataMap().get(MesCustomerPart.class.getSimpleName())
                : mesCustomerPartService.getMesCustomerPart(model.getOrganizeCode(), model.getPartNo());
        if (null == customerPart) MesPcnException.throwBusiException("零件号[%s]未维护客户物料信息", model.getPartNo());

        // 返回的结果集合
        List<Map<String, Object>> printDataMapList = new ArrayList<>();
        if (!isStep) {
            model.getPackageList().forEach(o -> printDataMapList.add(getPrintMap(model, o, customerPart)));
            model.setPrintContextList(printDataMapList);
        } else {
            model.getPackageList().forEach(o -> printDataMapList.add(getPrintMap(model, o, customerPart)));
            model.getPrintContextList().add(packResultMap(model, printDataMapList));
        }

        return model;
    }

    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;
    }

    private MesPrintedSnLog getMesPrintedSnLog(MesPackage packageDb, String userName, String printData) {
        MesPrintedSnLog snLog = new MesPrintedSnLog();
        snLog.setBarcode(packageDb.getPackageNo());
        snLog.setPartNo(packageDb.getPartNo());
        snLog.setPartName(packageDb.getPartName());
        snLog.setPrintData(printData);
        snLog.setOrganizeCode(packageDb.getOrganizeCode());
        ConvertBean.serviceModelInitialize(snLog, userName);
        return snLog;
    }

    private Map<String, Object> getPrintMap(MesProduceSnPrintModel model, MesPackage packageDb, MesCustomerPart customerPart) {
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put(MesPcnExtConstWords.PART_NO, packageDb.getPartNo());
        resultMap.put(MesPcnExtConstWords.PART_NAME, packageDb.getPartName());
        resultMap.put(MesPcnExtConstWords.CUST_PART_NO, customerPart.getCustPartNo());
        resultMap.put(MesPcnExtConstWords.QR_CODE, packageDb.getPackageNo());
        resultMap.put(MesPcnExtConstWords.QTY, packageDb.getQty().intValue());
        resultMap.put(MesPcnExtConstWords.UNIT, packageDb.getUnit());
        resultMap.put(MesPcnExtConstWords.PACK_SPEC_QTY, packageDb.getPackSpecQty().intValue());
        resultMap.put(MesPcnExtConstWords.LOT_NO, packageDb.getLotNo().replaceAll(MesPcnExtConstWords.SEPARATOR, MesPcnExtConstWords.SLANT_R));
        resultMap.put(MesPcnExtConstWords.BAR_CODE, packageDb.getPackageOneCode());
        resultMap.put(MesPcnExtConstWords.MODIFY_DATE_TIME, packageDb.getModifyDatetime().replaceAll(MesPcnExtConstWords.SEPARATOR, MesPcnExtConstWords.SLANT_R));
        model.getMesPrintedSnLogList().add(getMesPrintedSnLog(packageDb, model.getUserName(), JSONObject.toJSONString(resultMap)));
        return resultMap;
    }

}
