LogicOperation.java
6.5 KB
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
package com.sy.logic;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.sy.model.GatherInfo;
import com.sy.model.LandBusinessTypeList;
import com.sy.model.LandRoadVe;
import com.sy.service.LandBusListService;
import com.sy.service.LandRoadVeService;
import com.sy.socket.CommandClient;
import com.sy.utils.FileTool;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigDecimal;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.text.NumberFormat;
import java.text.ParseException;
import java.util.List;
import java.util.Map;
@Component
public class LogicOperation {
private static final Logger logger = Logger.getLogger(LogicOperation.class);
@Autowired
private LandBusListService listService;
@Autowired
private LandRoadVeService veService;
private static LogicOperation logic;
//读取配置文件里的载重与称重的可控范围
private static String checkWt = FileTool.readProperties("grossWt");
//逻辑判断后的结果定义
private static String PERMITTHOUGH = "直接放行";
private static String GROWSSEXCETION = "禁止通行,重量不在可控范围";
private static String NORECORD = "车辆未备案";
private static String INPUTSTATION = "此车辆未做进站申请";
private static String ENTERSTATION = "此车辆未做出站申请";
@PostConstruct
public void init() {
logic = this;
logic.listService = this.listService;
logic.veService = this.veService;
}
/**
* @Param info 卡口采集数据
* @Result 计算卡口采集数据并发送报文
*/
public static boolean operation(GatherInfo info) {
boolean result = false;
BigDecimal grosswt = info.getGrosswt();
//转为double类型
double growssWt = grosswt.doubleValue();
String ietype = info.getIetype();
List<LandBusinessTypeList> lists = null;
String vaName = info.getVename();
LandRoadVe ve = logic.veService.selectByFrameNo(vaName);
if(ve ==null) return result;
LandBusinessTypeList list = logic.listService.selectForOne(info.getVename(),info.getBarcode(),info.getAreaid
(),info.getChnlno(),info.getIetype());
double goodsWt = 0.0;
if (list.getMasterList().length() > 0) {
goodsWt = GoodsWt(list.getMasterList(), info.getIetype());
}
double selfWt = Double.parseDouble(ve.getSelfWt());
if (sendBw(info, growssWt, selfWt, goodsWt)) {
if ("I".equals(info.getIetype())) {
list.setContrastflag("已进站");
} else {
list.setContrastflag("已出站");
}
logic.listService.updateById(list);
result = true;
}
return result;
}
//将获取的checkWt进行小数转化
public static double valueDob() {
NumberFormat nf = NumberFormat.getPercentInstance();
Number m = null;
try {
m = nf.parse(checkWt);//将百分数转换成Number类型
} catch (ParseException e) {
e.printStackTrace();
logger.info(e.getMessage());
}
return m.doubleValue();
}
/**
* @Param grossWt 地磅称重
* @Param wt 车辆自重
* @Param goodsWt 货物总重
* @Result 获取运单重量
*/
//校验载重和称重是否在合理的范围
public static boolean checkResult(double grossWt, double wt, double goodsWt) {
boolean flag = false;
double result = 0.0;
if (goodsWt > 0) {
result = (grossWt - wt) / goodsWt;
if (result > 1) {
result = result - 1;
} else {
result = 1 - result;
}
} else {
result = (grossWt - wt);
}
BigDecimal bd = new BigDecimal(result);
if (bd.setScale(3, BigDecimal.ROUND_HALF_UP).doubleValue() <= valueDob()) {
flag = true;
}
return flag;
}
/**
* @Param mainifast 主单列表
* @Param ietype 进出标志
* 获取货物总重
*/
public static double GoodsWt(String mainifast, String ietype) {
Double sum = 0.0;
if (mainifast.length() > 0) {
String[] mainifastList = mainifast.split(",");
for (String mainBill : mainifastList) {
sum = sum += getGrossWt(mainBill, ietype);
}
}
return sum;
}
/**
* @Param waybill 主单号
* @Param imp 进出港标识
* @Result 获取运单重量
*/
public static double getGrossWt(String waybill, String imp) {
String url = "http://tjfx.15miaoo.com:8003/tj/orig/orig?waybill=" + waybill + "&imp=" + imp;
StringBuilder json = new StringBuilder();
Map map = null;
double bg = 0;
try {
URL Url = new URL(url);
URLConnection yc = Url.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream()));
String inputLine = null;
while ((inputLine = in.readLine()) != null) {
json.append(inputLine);
}
JSONArray array = JSONArray.parseArray(json.toString());
for (int i = 0; i < array.size(); i++) {
map = JSON.parseObject(array.getString(i));
if (map.get("receiptinformation").toString().startsWith("41301") || map.get("receiptinformation")
.toString().startsWith("41106")) {
bg = Double.parseDouble((String) map.get("totalweight"));
return bg;
}
}
in.close();
} catch (MalformedURLException e) {
e.getMessage();
} catch (IOException e) {
e.getMessage();
}
return bg;
}
/**
* 校验重量并发送报文
*/
public static boolean sendBw(GatherInfo info, double growsswWt, double selfWt, double goodsWt) {
boolean flag = false;
if (checkResult(growsswWt, selfWt, goodsWt)) {
CommandClient.Client(info, PERMITTHOUGH);
flag = true;
} else {
CommandClient.Client(info, GROWSSEXCETION);
}
return flag;
}
}