李林
2023-10-07 658d4927d468c47208fd012d9128b09249c07eff
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package com.chinaztt.mes.quality.utils;
 
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.chinaztt.mes.quality.entity.HwWeightLabelConfig;
import com.chinaztt.mes.quality.entity.QualityHw19Code;
import com.chinaztt.mes.quality.entity.QualityHw19Gross;
import com.chinaztt.mes.quality.mapper.HwWeightLabelConfigMapper;
import com.chinaztt.mes.quality.mapper.QualityHw19CodeMapper;
import com.chinaztt.mes.quality.mapper.QualityHw19GrossMapper;
import com.chinaztt.ztt.common.core.util.R;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
 
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.List;
 
/**
 * @Description :华为称重标签配置参数工具类
 * @Author : sll
 * @Date: 2023-01-14 17:23
 */
@Service
@AllArgsConstructor
public class HwWeightLabelConfigUtils {
    private HwWeightLabelConfigMapper hwWeightLabelConfigMapper;
    private QualityHw19GrossMapper qualityHw19GrossMapper;
    private QualityHw19CodeMapper qualityHw19CodeMapper;
    private static final ScriptEngineManager manager = new ScriptEngineManager();
 
    /**
     * 根据客户物料编码获取称重配置数据
     * @param materialCode
     * @return
     */
    public List<HwWeightLabelConfig> getConfigs(String materialCode) {
        List<HwWeightLabelConfig> hwWeightLabelConfigList = hwWeightLabelConfigMapper.selectList(Wrappers.<HwWeightLabelConfig>lambdaQuery().eq(HwWeightLabelConfig::getMaterialCode,materialCode));
 
        if (CollectionUtils.isEmpty(hwWeightLabelConfigList)){
            throw new RuntimeException("客户物料编码 = 【" + materialCode + "】 -> 查无称重标签配置数据");
        }
 
        return hwWeightLabelConfigList;
    }
 
    public HwWeightLabelConfig getConfigLine(List<HwWeightLabelConfig> hwWeightLabelConfigList, BigDecimal length) throws Exception {
        ScriptEngine se = manager.getEngineByName("js");
        String s;
 
        for (HwWeightLabelConfig hwWeightLabelConfig : hwWeightLabelConfigList) {
            //将公式中的x替换成长度数值
            s = hwWeightLabelConfig.getLengthScope().replace("x", length.toString());
 
            if ((Boolean)se.eval(s)){
                //返回找到的配置记录行
                return hwWeightLabelConfig;
            }
        }
 
        //根据长度配没找到配置记录行
        throw new RuntimeException("客户物料编码 = 【" + hwWeightLabelConfigList.get(0).getMaterialCode() + "】 -> 对应的长度 = 【" + length + "】  -> 查无称重标签配置数据");
    }
 
    /**
     * 打印校验
     * @param id 记录行id
     */
    public R printCheck(Long id) throws Exception {
        QualityHw19Gross qualityHw19Gross = qualityHw19GrossMapper.selectById(id);
        if (null == qualityHw19Gross){
            throw new RuntimeException("根据记录行id = 【" + id + "】 -> 查无数据");
        }
 
        //根据19条码获取客户物料编码
        List<QualityHw19Code> qualityHw19CodeList = qualityHw19CodeMapper.selectList(Wrappers.<QualityHw19Code>lambdaQuery().eq(QualityHw19Code::getBarCode,qualityHw19Gross.getBarCode()));
        if (CollectionUtils.isEmpty(qualityHw19CodeList)){
            throw new RuntimeException("19条码 = 【" + qualityHw19Gross.getBarCode() + "】 -> 查无生码记录");
        }
 
        if (qualityHw19CodeList.size() != 1) {
            throw new RuntimeException("19条码 = 【" + qualityHw19Gross.getBarCode() + "】 -> 存在多条生码记录");
        }
 
        //根据客户物料编码获取称重配置数据
        List<HwWeightLabelConfig> hwWeightLabelConfigList = getConfigs(qualityHw19CodeList.get(0).getMaterialCode());
 
        //根据客户物料编码及长度获取对应的配置记录行
        HwWeightLabelConfig hwWeightLabelConfig = getConfigLine(hwWeightLabelConfigList, qualityHw19CodeList.get(0).getLength());
 
        //判断是否需要打印称重标签
        if (!hwWeightLabelConfig.getIsNeedPrint() || !hwWeightLabelConfig.getActive()){
            throw new RuntimeException("客户物料编码 = 【" + qualityHw19CodeList.get(0).getMaterialCode() + "】 -> 长度 = 【" + qualityHw19CodeList.get(0).getLength() + "】 -> 无需打印称重标签");
        }
 
        //初始化实际净重
        BigDecimal actualNetWeight = BigDecimal.ZERO;
        //初始化理论净重 = 长度 * 米重
        BigDecimal theoryNetWeight = qualityHw19CodeList.get(0).getLength().multiply(hwWeightLabelConfig.getMeterWeight());
 
        BigDecimal a = new BigDecimal("1");
        //防呆范围浮动参数
        BigDecimal b = hwWeightLabelConfig.getFloatParam();
 
        switch (hwWeightLabelConfig.getPackMaterial()) {
            case "纸箱" :
                //实际净重 = 毛重 - 纸箱重量
                actualNetWeight = qualityHw19Gross.getGrossWei().subtract(hwWeightLabelConfig.getCartonWeight());
 
                //校验实际净重
                return checkWeight(actualNetWeight,theoryNetWeight,a,b,qualityHw19Gross);
            case "盘具" :
                //实际净重 = 毛重 - 空盘具重量 + 纸板重量
                actualNetWeight = qualityHw19Gross.getGrossWei().subtract(hwWeightLabelConfig.getEmptyTrayWeight()).add(hwWeightLabelConfig.getPaperboardWeight());
 
                //校验实际净重
                return checkWeight(actualNetWeight,theoryNetWeight,a,b,qualityHw19Gross);
            default:
                throw new RuntimeException("包装材质 = 【" + hwWeightLabelConfig.getPackMaterial() + "】 -> 不合规");
        }
    }
 
    /**
     * 校验实际净重
     * @param actualNetWeight 实际净重
     * @param theoryNetWeight 理论净重
     * @param a 常量 1
     * @param b 防呆范围浮动参数
     * @param qualityHw19Gross 毛重记录行
     * @return
     */
    public R checkWeight(BigDecimal actualNetWeight, BigDecimal theoryNetWeight, BigDecimal a, BigDecimal b, QualityHw19Gross qualityHw19Gross) {
        //更新称重标签记录行中的净重,方便后期用户修改配置数据后,重新计算实际净重
        qualityHw19Gross.setNetWei(actualNetWeight);
        qualityHw19GrossMapper.updateById(qualityHw19Gross);
 
        BigDecimal min = theoryNetWeight.multiply(a.subtract(b));
        BigDecimal max = theoryNetWeight.multiply(a.add(b));
 
        if (actualNetWeight.compareTo(min) >= 0 && actualNetWeight.compareTo(max) <= 0) {
            //满足条件
            return R.ok();
        } else {
            //不满足条件
            DecimalFormat decimalFormatTip = new DecimalFormat("00.00");//报错提示精度
            return R.failed("实际净重 = 【" + decimalFormatTip.format(actualNetWeight) + "】 -> 超出标准范围 -> [" + decimalFormatTip.format(min) + "," + decimalFormatTip.format(max) + "]");
        }
    }
}