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.MesWorkOrderCutPrintModel;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtConstWords;
import cn.estsh.i3plus.platform.common.tool.TimeTool;
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.CollectionUtils;

import java.math.BigDecimal;
import java.util.*;

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

    public static final int EQUIP_NUM = 1;
    public static final int EQUIP_PARAM_TITLE_NUM = 4;

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

        List<MesWorkOrderCutPrintModel> workOrderCutPrintModelList = (List<MesWorkOrderCutPrintModel>)model.getSourceData();
        List<Map<String, Object>> printDataMapList = new ArrayList<>();

        for (MesWorkOrderCutPrintModel workOrderCutPrintModel : workOrderCutPrintModelList) {

            List<MesCutSchemeFg> mesCutSchemeFgList = workOrderCutPrintModel.getCutSchemeFgList();

            List<MesCutSchemeMaterial> mesCutSchemeMaterialList = workOrderCutPrintModel.getCutSchemeMaterialList();

            Map<Long, List<MesCutSchemeEquipmentDetail>> cutSchemeEquipmentDetailMapByPid = workOrderCutPrintModel.getCutSchemeEquipmentDetailMapByPid();

            MesCutSchemeMaterial cutSchemeMaterial = CollectionUtils.isEmpty(mesCutSchemeMaterialList) ? null : mesCutSchemeMaterialList.stream().findFirst().get();

            List<MesCutSchemeEquipment> cutSchemeEquipmentListByCutCode = workOrderCutPrintModel.getCutSchemeEquipmentList();

            if (!CollectionUtils.isEmpty(cutSchemeEquipmentListByCutCode)) cutSchemeEquipmentListByCutCode.sort(Comparator.comparing(MesCutSchemeEquipment::getSeq));

            Map<String, Object> resultMap = new HashMap<>();

            resultMap.put(MesPcnExtConstWords.CUT_WORK_ORDER_NO, workOrderCutPrintModel.getCutWorkOrderNo());
            resultMap.put(MesPcnExtConstWords.CUT_NAME, workOrderCutPrintModel.getCutName());
            // 原材料零件号
            resultMap.put(MesPcnExtConstWords.RAW_PART_NO, cutSchemeMaterial == null ? "" : cutSchemeMaterial.getPartNo());

            // 拉料信息 START
            resultMap.put(MesPcnExtConstWords.PULLING_MATERIAL_LENGTH, cutSchemeMaterial == null ? "" : getValByDouble(cutSchemeMaterial.getPullingMaterialLength()));
            resultMap.put(MesPcnExtConstWords.PULLING_MATERIAL_LAYER_NUMBER, cutSchemeMaterial == null ? "" : getValByDouble(cutSchemeMaterial.getPullingMaterialLayerNumber()));
            resultMap.put(MesPcnExtConstWords.PER_LAYER_NUMBER, cutSchemeMaterial == null ? "" : getValByDouble(cutSchemeMaterial.getPerLayerNumber()));
            resultMap.put(MesPcnExtConstWords.MATERIAL_DOSAGE, cutSchemeMaterial == null ? "" : getValByDouble(cutSchemeMaterial.getMaterialDosage()));
            resultMap.put(MesPcnExtConstWords.USING_LENGTH, cutSchemeMaterial == null ? "" : getValByDouble(cutSchemeMaterial.getUsingLength()));
            resultMap.put(MesPcnExtConstWords.LAYER_NUMBER, cutSchemeMaterial == null ? "" : getValByDouble(cutSchemeMaterial.getLayerNumber()));
            resultMap.put(MesPcnExtConstWords.TENSION_PARAM, cutSchemeMaterial == null ? "" : getValByDouble(cutSchemeMaterial.getTensionParam()));
            resultMap.put(MesPcnExtConstWords.PRINT_TIME, TimeTool.getNowTime("yyyy-M-d H:mm:ss"));
            // 拉料信息 END

            MesCutSchemeEquipment mesCutSchemeEquipment = CollectionUtils.isEmpty(cutSchemeEquipmentListByCutCode) ? null : cutSchemeEquipmentListByCutCode.get(0);

            List<MesCutSchemeEquipmentDetail> cutSchemeEquipmentDetails = mesCutSchemeEquipment == null || CollectionUtils.isEmpty(cutSchemeEquipmentDetailMapByPid) ? new ArrayList<>() : cutSchemeEquipmentDetailMapByPid.get(mesCutSchemeEquipment.getId());

            for (int i = 0; i < EQUIP_NUM; i++) {
                //设备参数值标题
                resultMap.put(MesPcnExtConstWords.EQUIPMENT_NAME+(i+1), mesCutSchemeEquipment == null ? "" : mesCutSchemeEquipment.getEquipmentName());
                for (int l = 0; l < EQUIP_PARAM_TITLE_NUM; l++) {
                    if (cutSchemeEquipmentDetails.size() > l) {
                        //设备参数值标题1
                        resultMap.put(MesPcnExtConstWords.EQUIP_PARAM_NAME+(l+1), cutSchemeEquipmentDetails.get(l).getName());
                        //设备参数值1
                        resultMap.put(MesPcnExtConstWords.EQUIP_PARAM_VALUE+(l+1), cutSchemeEquipmentDetails.get(l).getValue());
                    } else {
                        //设备参数值标题1
                        resultMap.put(MesPcnExtConstWords.EQUIP_PARAM_NAME+(l+1), "");
                        //设备参数值1
                        resultMap.put(MesPcnExtConstWords.EQUIP_PARAM_VALUE+(l+1), "");
                    }
                }
            }

            String partValue = "";
            if (!CollectionUtils.isEmpty(mesCutSchemeFgList)) {
                BigDecimal[] bigDecimals = divideAndRemainder(mesCutSchemeFgList.get(0).getQty(), mesCutSchemeFgList.get(0).getPackageQty());
                BigDecimal remainder = bigDecimals[1];
                Double boxQty = remainder.doubleValue() > 0 ? bigDecimals[0].add(new BigDecimal("1")).doubleValue() : bigDecimals[0].doubleValue();
                partValue = mesCutSchemeFgList.get(0).getPartNo() + "    " + boxQty.intValue() +"箱";
            }

            //产品信息
            resultMap.put(MesPcnExtConstWords.FG_PART_NO, partValue);

            printDataMapList.add(resultMap);

        }

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

        return model;
    }

    private String getValByDouble(Double val) {
        return val == null ? "" : val.toString();
    }

    private BigDecimal[] divideAndRemainder(Double qty, Double packageQty) {
        BigDecimal dividend  = new BigDecimal(qty.toString());
        BigDecimal divisor = new BigDecimal(packageQty.toString());
        return dividend.divideAndRemainder(divisor);
    }

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

}
