李林
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
package com.chinaztt.mes.quality.service.impl;
 
import cn.hutool.http.HttpRequest;
import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chinaztt.mes.quality.dto.ZxWeightLabelDTO;
import com.chinaztt.mes.quality.entity.StockBean;
import com.chinaztt.mes.quality.entity.ZxLabelBindRelation;
import com.chinaztt.mes.quality.entity.ZxWeightLabel;
import com.chinaztt.mes.quality.entity.ZxWeightLabelConfig;
import com.chinaztt.mes.quality.mapper.ZxLabelBindRelationMapper;
import com.chinaztt.mes.quality.mapper.ZxWeightLabelConfigMapper;
import com.chinaztt.mes.quality.mapper.ZxWeightLabelMapper;
import com.chinaztt.mes.quality.service.ZxWeightLabelService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
 
/**
 * @Description : 中兴称重标签
 * @ClassName : ZxWeightLabelServiceImpl
 * @Author : sll
 * @Date: 2022-12-01  23:26
 */
@Service
@AllArgsConstructor
@Component
@Transactional(rollbackFor = Exception.class)
public class ZxWeightLabelServiceImpl extends ServiceImpl<ZxWeightLabelMapper, ZxWeightLabel> implements ZxWeightLabelService{
    private ZxWeightLabelConfigMapper zxWeightLabelConfigMapper;
    private ZxLabelBindRelationMapper zxLabelBindRelationMapper;
    private StockBean stockBean;
    private static final ScriptEngineManager manager = new ScriptEngineManager();
 
    @Override
    public ZxWeightLabel addWeightLabel(ZxWeightLabelDTO zxWeightLabelDTO) throws Exception{
        if(null == zxWeightLabelDTO.getShopCode() || !zxWeightLabelDTO.getShopCode().equals("GD")){
            throw new RuntimeException("车间代码异常");
        }
 
        if(null == zxWeightLabelDTO.getPkgCode() || null == zxWeightLabelDTO.getGrossWeight()){
            throw new RuntimeException("PKG码及毛重数据不可为空");
        }
 
        //解析pkg码
        try{
            List<String> stringList = Arrays.asList(zxWeightLabelDTO.getPkgCode().split("&"));
            if(stringList.size() < 3){
                //正常的按&切割后是8段,但只有前3段对我们是有用的
                throw new RuntimeException("PKG码格式错误");
            }else{
                ZxWeightLabelDTO.PkgNode pkgNode = new ZxWeightLabelDTO.PkgNode();
                pkgNode.setPkgId(stringList.get(0));//pkgid
                pkgNode.setMaterialCode(stringList.get(1));//物料编码
                pkgNode.setQuality(new BigDecimal(stringList.get(2)));//数量
                zxWeightLabelDTO.setPkgNode(pkgNode);
            }
        }catch(Exception e){
            throw new RuntimeException("解析pkg码异常 -> " + e.getMessage());
        }
 
        //校验
        //1.pkgid是否绑定
        ZxLabelBindRelation zxLabelBindRelation = zxLabelBindRelationMapper.selectOne(Wrappers.<ZxLabelBindRelation>lambdaQuery().eq(ZxLabelBindRelation::getPkgId,zxWeightLabelDTO.getPkgNode().getPkgId()).last("limit 1"));
        if(null == zxLabelBindRelation){
            throw new RuntimeException("该PKG码未进行标签关系绑定 -> PKGID = 【" + zxWeightLabelDTO.getPkgNode().getPkgId() + "】");
        }
 
        //2.pkgid是否已经生产过称重标签
        ZxWeightLabel zxWeightLabel = baseMapper.selectOne(Wrappers.<ZxWeightLabel>lambdaQuery().eq(ZxWeightLabel::getPkgId,zxWeightLabelDTO.getPkgNode().getPkgId()).last("limit 1"));
        if(null != zxWeightLabel){
            throw new RuntimeException("该PKGID = 【" + zxWeightLabelDTO.getPkgNode().getPkgId() + "】 -> 已生成过称重记录");
        }
 
        //3.根据客户物料编码去配置表中取配置数据
        List<ZxWeightLabelConfig> zxWeightLabelConfigList = zxWeightLabelConfigMapper.selectList(Wrappers.<ZxWeightLabelConfig>lambdaQuery().eq(ZxWeightLabelConfig::getMaterialCode,zxWeightLabelDTO.getPkgNode().getMaterialCode()));
        if(null == zxWeightLabelConfigList || zxWeightLabelConfigList.size() == 0){
            throw new RuntimeException("客户物料编码 = 【" + zxWeightLabelDTO.getPkgNode().getMaterialCode() + "】 -> 未找到包装配置数据");
        }
 
        ScriptEngine se = manager.getEngineByName("js");
        Boolean door = false;//标记
        for(ZxWeightLabelConfig zxWeightLabelConfig : zxWeightLabelConfigList){
            zxWeightLabelDTO.setMeterWeight(zxWeightLabelConfig.getMeterWeight());//米重
            zxWeightLabelDTO.setFloatParam(zxWeightLabelConfig.getFloatParam());//防呆范围浮动参数
            String s = zxWeightLabelConfig.getMeterScope().replace("x",zxWeightLabelDTO.getPkgNode().getQuality().toString());//将公式中的x替换成pkg码中解析出来的数量
            if((Boolean)se.eval(s)){
                //判断对应的包装参数是否需要人工手动填写
                if(!zxWeightLabelConfig.getIsAuto()){
                    //判断前端是否传入包装参数
                    if(null == zxWeightLabelDTO.getPackMaterial() || null == zxWeightLabelDTO.getPackSize() || null == zxWeightLabelDTO.getPackWeight()){
                        //前端没传,则需要前端弹出窗体,让用户填写
                        zxWeightLabelDTO.setPopWindow(true);
                        return zxWeightLabelDTO;
                    }else{
                        //检验一下包装尺寸格式是否合规
                        List<String> stringList = Arrays.asList(zxWeightLabelDTO.getPackSize().split("\\*"));
                        if(null == stringList || stringList.size() != 3){
                            throw new RuntimeException("包尺寸格式不合规");
                        }
                        for(String cc : stringList){
                            if(!StringUtils.isNumber(cc) || Double.parseDouble(cc) <= 0){
                                throw new RuntimeException("包装尺寸 -> 长、宽、高必须填写数值且不可小于等于0");
                            }
                        }
                        door = true;
                        break;
                    }
                }else{
                    zxWeightLabelDTO.setPackMaterial(zxWeightLabelConfig.getPackMaterial());//包装材料
                    zxWeightLabelDTO.setPackSize(zxWeightLabelConfig.getPackSize());//包装尺寸
                    zxWeightLabelDTO.setPackWeight(zxWeightLabelConfig.getPackWeight());//包装重量
                    door = true;//获取配置成功
                    break;
                }
            }
        }
 
        //4.分割米标是否合规
        if(!door){
            throw new RuntimeException("根据客户物料编码 = 【" + zxWeightLabelDTO.getPkgNode().getMaterialCode() + "】的分割计米 = 【" + zxWeightLabelDTO.getPkgNode().getQuality() + "】 -> 不合规");
        }
 
        //5.校验重量范围 净重*(1-防呆范围浮动参数)<=理论重量<=净重*(1+防呆范围浮动参数)
        zxWeightLabelDTO.setNetWeight(zxWeightLabelDTO.getGrossWeight().subtract(zxWeightLabelDTO.getPackWeight()));//净重 = 毛重 - 包装重量
        zxWeightLabelDTO.setTheoryWeight(zxWeightLabelDTO.getPkgNode().getQuality().multiply(zxWeightLabelDTO.getMeterWeight()));//理论重量 = pkg码中数量 * 米重
        BigDecimal a = new BigDecimal("1");
        BigDecimal b = zxWeightLabelDTO.getFloatParam();//防呆范围浮动参数改成从配置表中获取
        if(zxWeightLabelDTO.getTheoryWeight().compareTo(zxWeightLabelDTO.getNetWeight().multiply(a.subtract(b))) >= 0 && zxWeightLabelDTO.getTheoryWeight().compareTo(zxWeightLabelDTO.getNetWeight().multiply(a.add(b))) <= 0){
            //创建数据
            zxWeightLabelDTO.setPkgId(zxWeightLabelDTO.getPkgNode().getPkgId());//pkgid
            zxWeightLabelDTO.setQuantity(zxWeightLabelDTO.getPkgNode().getQuality());//数量
            zxWeightLabelDTO.setMaterialCode(zxWeightLabelDTO.getPkgNode().getMaterialCode());//客户物料编码
            baseMapper.insert(zxWeightLabelDTO);
 
            return zxWeightLabelDTO;
        }else{
            DecimalFormat decimalFormatTip = new DecimalFormat("00.00");//报错提示精度
            throw new RuntimeException("理论净重 = 【" + decimalFormatTip.format(zxWeightLabelDTO.getTheoryWeight()) + "】 -> 不在[" + decimalFormatTip.format(zxWeightLabelDTO.getNetWeight().multiply(a.subtract(b))) + "," + decimalFormatTip.format(zxWeightLabelDTO.getNetWeight().multiply(a.add(b))) + "]范围内");
        }
 
    }
 
    @Override
    public String weighing(){
        //从地磅获取称重
        String ok = "OK";
        String key = "result";
        String weight = "message";
 
        JSONObject jsonObject;
 
        try {
            //从地磅获取称重
            Map<String, Object> paramMap = new HashMap<>(16);
            String result = HttpRequest.get(stockBean.getGd_1()).form(paramMap).execute().body();
            //String result = "[{\"result\":\"OK\",\"message\":\"0.5\"}]";//测试用
            JSONArray jsonArray = JSONObject.parseArray(result);
            jsonObject = jsonArray.getJSONObject(0);
 
        } catch (Exception e) {
            throw new RuntimeException("调用接口失败");
        }
 
        if (jsonObject.getString(key).equals(ok)){
            return jsonObject.getString(weight);
        }else{
            throw new RuntimeException("获取称重失败 -> " + jsonObject.getString(weight));
        }
 
 
 
//        String xmlres = "<ArrayOfWeightModel xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"http://schemas.datacontract.org/2004/07/Models\"><WeightModel><message>10</message><result>OK</result></WeightModel></ArrayOfWeightModel>";
//        String res = "0";
//        Map<String, Object> paramMap = new HashMap<>(16);
//        if(true){
//            xmlres = HttpRequest.get(stockBean.getGd_1()).form(paramMap).execute().body();
//        }
//
//        Pattern p = Pattern.compile("\r|\n");
//        Matcher m = p.matcher(xmlres);
//        xmlres = m.replaceAll("");
//
//        // 创建一个新的字符串
//        StringReader read = new StringReader(xmlres);
//        // 创建新的输入源SAX 解析器将使用 InputSource 对象来确定如何读取 XML 输入
//        InputSource source = new InputSource(read);
//        // 创建一个新的SAXBuilder
//        SAXBuilder sb = new SAXBuilder();
//        // 通过输入源构造一个Document
//        Document doc = sb.build(source);
//        // 取的根元素
//        Element root = doc.getRootElement();
//        System.out.println(root.getName());// 输出根元素的名称(测试)ArrayOfWeightModel
//        // 得到根元素所有子元素的集合
//        List jiedian = root.getChildren();
//        // 获得XML中的命名空间(XML中未定义可不写)
//        Namespace name = root.getNamespace();
//        Element et = null;
//
//        for (int i = 0; i < jiedian.size(); i++) {
//            et = (Element) jiedian.get(i);// 循环依次得到子元素
//            if (i == 0) {//获取参数
//                if (et.getChild("result", name).getText().equals("OK")){
//                    res = et.getChild("message", name).getText();
//                }else{
//                    throw new RuntimeException("获取称重数据失败 -> " + et.getChild("message", name).getText());
//                }
//            }
//        }
//        return res;
    }
}