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.model.MesWorkOrderCutDetailModel;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtConstWords;
import cn.estsh.i3plus.pojo.mes.bean.*;
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 org.springframework.util.StringUtils;

import java.util.*;

/**
 * @Description : 裁片工单包装标签打印
 * @Reference :
 * @Author : Castle
 * @CreateDate : 2024/6/17 16:43
 * @Modify:
 **/
@Component
@Slf4j
public class CutWorkOrderPackageSnPrintStrategy implements IPrintTemplateStrategyService {

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

        List<MesWorkOrderCutDetailModel> workOrderCutDetailModelList = (List<MesWorkOrderCutDetailModel>) mesProduceSnPrintModel.getSourceData();

        List<Map<String, Object>> printDataMapList = new ArrayList<>();

        for (MesWorkOrderCutDetailModel model : workOrderCutDetailModelList) {
            Map<String, Object> resultMap = new HashMap<>();
            resultMap.put(MesPcnExtConstWords.PART_NO, model.getPartNo());
            resultMap.put(MesPcnExtConstWords.PART_NAME, model.getPartName());
            resultMap.put(MesPcnExtConstWords.CUST_PART_NO, model.getCustPartNo());
            String lotNo = "";

            if (!StringUtils.isEmpty(model.getLotNo())) {
                String[] split = model.getLotNo().split("-");
                lotNo = new StringJoiner(MesPcnExtConstWords.SLANT_R).add(split[0]).add(split[1]).add(split[2]).toString();
            }

            resultMap.put(MesPcnExtConstWords.LOT_NO, lotNo);
            resultMap.put(MesPcnExtConstWords.TIME, model.getCreateDatetime());
            resultMap.put(MesPcnExtConstWords.PACKAGE_QTY, model.getPackageQty());
            resultMap.put(MesPcnExtConstWords.QTY, model.getQty());
            resultMap.put(MesPcnExtConstWords.CUT_WORK_ORDER_NO, model.getCutWorkOrderNo());
            resultMap.put(MesPcnExtConstWords.PACKAGE_SN, model.getPackageNo());
            // 一维码
            resultMap.put(MesPcnExtConstWords.BAR_CODE, model.getPackageNo());
            // 二维码
            resultMap.put(MesPcnExtConstWords.QR_CODE, new StringJoiner(MesPcnExtConstWords.VERTICAL_BAR).add("YFWHU")
                    .add(model.getPackageNo()).add(model.getOrganizeCode()).add(model.getPartNo()).add(String.valueOf(model.getQty().intValue()))
                    .add("EA").toString());
            printDataMapList.add(resultMap);
        }

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

        return mesProduceSnPrintModel;
    }


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

}
