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

import cn.estsh.i3plus.ext.mes.pcn.apiservice.serviceimpl.print.IPrintTemplateStrategyService;
import cn.estsh.i3plus.ext.mes.pcn.pojo.model.MesProduceSnPrintModel;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtConstWords;
import cn.estsh.i3plus.pojo.mes.bean.MesNumberRule;
import cn.estsh.i3plus.pojo.mes.bean.MesWorkOrder;
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.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description : 客户标签打印
 * @Reference :
 * @Author : wangjie
 * @CreateDate : 2024/9/29 16:43
 * @Modify:
 **/
@Component
@Slf4j
public class CommonCustSnPrintStrategy implements IPrintTemplateStrategyService {

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

        MesWorkOrder workOrder = (MesWorkOrder) model.getSourceData();

        if (workOrder == null) {
            log.info("CommonCustSnPrintStrategy --- execute --- 工单信息为空");
            return null;
        }

        // 返回的结果集合
        List<Map<String, Object>> printDataMapList = new ArrayList<>();
        // 单个标签参数值
        Map<String, Object> resultMap = new HashMap<>();
        // 物料名称
        resultMap.put(MesPcnExtConstWords.PART_NAME, workOrder.getPartName());
        // 物料名称
        resultMap.put(MesPcnExtConstWords.CUST_PART_NO, workOrder.getCustPartNo());
        // 客户条码
        resultMap.put(MesPcnExtConstWords.CUST_SN, workOrder.getCustSn());

        printDataMapList.add(resultMap);

        model.setPrintContextList(packResultMapList(model, printDataMapList));

        return model;
    }

    private List<Map<String, Object>> packResultMapList(MesProduceSnPrintModel printModel, List<Map<String, Object>> printTemplateDateList) {
        List<Map<String, Object>> resultMapList = new ArrayList<>();
        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());
        resultMapList.add(resultMap);
        return resultMapList;
    }

}
