FFMTest.java
16.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
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
import com.sun.org.apache.bcel.internal.generic.IF_ACMPEQ;
import com.tianbo.analysis.NmmsAdminApplication;
import com.tianbo.analysis.exception.FFMResolveException;
import com.tianbo.util.Date.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.junit.platform.commons.util.StringUtils;
import org.springframework.boot.test.context.SpringBootTest;
import javax.validation.constraints.NotNull;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.StringReader;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@SpringBootTest(classes = NmmsAdminApplication.class)
@Slf4j
public class FFMTest {
private String ffm = "ZCZC\n" +
"QD SELKTCR SELKTCR ICNKO5X \n" +
".CGOFD1E 101732\n" +
"FFM/5 \n" +
"5/5X173/10SEP/CGO/N328UP \n" +
"ICN/13SEP2249/13SEP0712 \n" +
"406-66666666CGOICN/T1K1MC0.01/CONSOLIDATED/EXP \n" +
"ULD/AAY569135X \n" +
"406-91567416CGOICN/T1K1MC0.01/CONSOLIDATED/EXP \n" +
"ULD/AAZ298885X \n" +
"406-77806864CGOONT/T34K286MC2.12/CONSOLIDATED \n" +
"406-77806890CGOONT/T1K1MC0.01/CONSOLIDATED \n" +
"406-77807472CGOONT/T11K125MC0.99/CONSOLIDATED \n" +
"406-81801020CGOONT/T45K17MC0.14/CONSOLIDATED \n" +
"406-81804962CGOONT/T1K11.8MC0.08/CONSOLIDATED \n" +
"406-87761446CGOONT/T2K73.5MC0.42/CONSOLIDATED \n" +
"406-87762021CGOONT/T1K11.3MC0.03/CONSOLIDATED \n" +
"406-87762824CGOONT/T1K9MC0.07/CONSOLIDATED \n" +
"406-92753124CGOONT/S10K145.7MC0.94T33/CONSOLIDATED \n" +
"406-92753275CGOONT/T1K1MC0.01/CONSOLIDATED \n" +
"ULD/AMJ522515X \n" +
"406-91567383CGOSDF/S180K2635.5MC2.31T309/CONSOLIDATED/EXP \n" +
"ULD/AMJ586525X \n" +
"406-77804355CGOSDF/T9K202MC0.86/CONSOLIDATED \n" +
"406-77806341CGOSDF/T2K25MC0.17/CONSOLIDATED \n" +
"406-77807100CGOSDF/T2K3MC0.01/CONSOLIDATED \n" +
"406-81376315CGOSDF/T12K176.5MC0.90/CONSOLIDATED \n" +
"406-81804951CGOSDF/T6K98.3MC0.45/CONSOLIDATED \n" +
"406-87762732CGOSDF/T1K2.5MC0.08/CONSOLIDATED \n" +
"PVG/12SEP0250 \n" +
"ULD/AMJ609925X \n" +
"406-77806842CGOSDF/T3K18MC0.03/CONSOLIDATED \n" +
"406-77806853CGOSDF/T6K17MC0.06/CONSOLIDATED \n" +
"406-77806960CGOSDF/T1K4MC0.02/CONSOLIDATED \n" +
"406-77807133CGOSDF/T3K45MC0.28/CONSOLIDATED \n" +
"406-77807273CGOSDF/T3K13MC0.18/CONSOLIDATED \n" +
"406-77807516CGOSDF/T8K13MC0.19/CONSOLIDATED \n" +
"406-91567383CGOSDF/S129K1568MC1.65T309/CONSOLIDATED/EXP \n" +
"406-92753161CGOSDF/T47K850MC4.51/CONSOLIDATED \n" +
"ULD/AMP652205X \n" +
"406-77806923CGOONT/T4K36MC0.12/CONSOLIDATED \n" +
"406-77806971CGOICN/T8K92MC1.16/CONSOLIDATED/ELI \n" +
"CONT \n" +
"\n" +
"=\n" +
"NNNN";
private String flightNo;
private Date flightDate;
private String order;
private String originStation;
private String destinationStation="UNKONW";
private String planeNo;
private int currentLine = 0;
private static final String KEY_WORD_3 = "FFM,ULD,OSI,COR,OCI,DEG,DIM";
private static final String KEY_WORD_4 = "CONT,LAST";
private List<String> lineList = new ArrayList<>();
@org.junit.jupiter.api.Test
public void send(){
try{
if (StringUtils.isNotBlank(ffm)){
/**
* 检查报文是否包含尾部标识
*/
if(ffm.contains("CONT")|| ffm.contains("LAST")){
BufferedReader reader = new BufferedReader(new StringReader(ffm));
for ( String lineStr = reader.readLine();
lineStr != null;
lineStr = reader.readLine()){
// log.info("已读取行{}-{}",i,lineStr);
lineList.add(lineStr);
}
}else {
throw new FFMResolveException("报文尾部缺少文件结束标识LAST或者CONT");
}
resolve();
}
}catch (IOException ignored){
log.error(ignored.toString());
ignored.printStackTrace();
}catch (FFMResolveException ex){
log.error(ex.toString());
ex.printStackTrace();
}catch (ParseException e){
log.error("航班日期解析错误");
}
}
/**
* 寻找FFM节点并解析航班信息以及航班第一目的站信息
* 找到FFM节点后,开始根据currentLine的值逐步一行一行解析
*/
public void resolve() throws FFMResolveException,ParseException{
if (!lineList.isEmpty()){
int keyword_i = 0;
for (int i = 0; i < lineList.size(); i++) {
//根据行关键字走相应的解析逻辑
String line = lineList.get(i);
log.info("1.开始处理行[{}]-[{}]",i,line);
String keyword = keyword(line);
if (!"NOT_KEYWORD".equals(keyword)){
log.info("2.行[{}]包含关键字,开始处理",i);
if ("FFM".equals(keyword)){
currentLine = i;
//ffm版本
String ver = line.split("/")[1];
log.info("报文版本{}",ver.trim());
//处理航班信息
currentLine++;
flight(lineList.get(currentLine).trim());
/**
* 取第一目的站
* 这里注意,有的错误报文的航班节点后面没带或者忘了带目的站节点,
* 所以这里需要加个校验
*/
currentLine++;
destination(lineList.get(currentLine).trim());
currentLine++;
}else if ("CONT".equals(keyword) || "LAST".equals(keyword) ){
keyword_i++;
}
}
}
if (keyword_i==0){
throw new FFMResolveException("报文尾部缺少文件结束标识LAST或者CONT");
}
resolve_ULD_Waybill();
}
}
/**
* 判断是否有散舱
* 有BUP散舱先处理散舱,没有则进入ULD及ULD货物解析阶段
* 假设默认航班有散舱,将散舱的ULD号默认为 "BUP"
* 设置当前的ULD 为 散舱ULD "BUP"
* 那么散舱下的运单直到解析到真正的板箱号后,uld变成真正的货物板箱
*/
public void resolve_ULD_Waybill() throws FFMResolveException{
log.info("5. 开始解析舱单列表");
String uld = "BUP";
for (int i = currentLine; i < lineList.size(); i++) {
String line = lineList.get(i);
log.info("5.1 开始解析行[{}]-[{}]",i,line);
//行尾部结束标识检查
String keyword = keyword(line);
if ("CONT".equals(keyword) || "LAST".equals(keyword)){
log.info("[END] 已解析到文件结束标识[{}],解析完毕",keyword);
break;
}
/**
* 多航班目的站解析
*/
//校验正则1,取前三位验证是否是机场代码
String pattern_f = "^[A-Z]{3}$|^[A-Z]{3}/\\d{2}[A-Z]{3}\\d{4}";
// 创建 Pattern 对象
Pattern r_f = Pattern.compile(pattern_f);
// 现在创建 matcher 对象
Matcher mF = r_f.matcher(line);
if (mF.find()){
log.info("5-[FLIGHT] 适配到航班其他目的站");
String flightDes = line.substring(0,3);
if (!KEY_WORD_3.contains(flightDes)){
destinationStation = flightDes;
log.info("5-[FLIGHT] 新航班目的站为[{}]",destinationStation);
}
}
if ("UNKONW".equals(destinationStation)){
throw new FFMResolveException("航班目的站节点校验不通过.");
}
if ("ULD".equals(keyword)){
log.info("5-[ULD] 此行为ULD信息行");
//板箱格式适配
String patternULD = "^ULD/([A-Z]{3}\\S{5}\\S{2})";
Pattern rULD = Pattern.compile(patternULD);
// 现在创建 matcher 对象
Matcher mULD = rULD.matcher(line);
if (mULD.find()){
log.info("5-[ULD] 查到分组{}",mULD.groupCount());
// for (int j1 = 1; j1 < mULD.groupCount()+1; j1++) {
// log.info("5-[ULD] 分组{}内容为:{}",j1,mULD.group(j1));
// }
uld= mULD.group(1);
log.info("5-[ULD] 当前ULD已变更为{}",uld);
}else {
throw new FFMResolveException("ULD节点格式错误,无法适配正则(^ULD/([A-Z]{3})(\\S{5})(\\S{2})");
}
}
//运单格式适配
String pattern = "^(\\d{3}-\\d{8})([A-Z]{3})([A-Z]{3})/(T|P|S|M)(\\d+)(K)([0-9\\.]+)(MC)?([0-9\\.]+)?(T)?([0-9]+)?/(\\S+)";
// 创建 Pattern 对象
Pattern r = Pattern.compile(pattern);
// 现在创建 matcher 对象
Matcher m = r.matcher(line);
if(m.find()){
log.info("5-[AWB] 行[{}]属于运单行,开始解析,当前板箱号为:{}",i,uld);
log.info("5-[AWB] 查到分组{}",m.groupCount());
String waybillNo = m.group(1);
String waybillOrigin = m.group(2);
String waybillDes = m.group(3);
String waybillSplit = m.group(4);
String waybillPiece = m.group(5);
String waybillWeight = m.group(7);
String waybillVolume = m.group(9);
String waybillTotalPiece = m.group(11);
String waybillGoodsDes = m.group(12);
// for (int j = 1; j < m.groupCount()+1; j++) {
// log.info("5-[AWB] 分组{}内容为:{}",j,m.group(j));
// }
if(model7Check(waybillNo)){
log.info("5-[AWB] 运单-({})模七校验通过",waybillNo);
}else {
log.error("{}运单模七校验不通过",waybillNo);
throw new FFMResolveException(waybillNo+"运单模七校验不通过");
}
log.info("5-[AWB-INFO] 运单信息:报文序号:{} 所属航班:{}/{} 航班起始站/目的站:{}/{} 所属板箱:{} 运单号:{} 起始站/目的站:{}/{} 分批标识:{} 分批件重:{}/{} 体积:{} 总件数:{} 货物描述:{}",
order,
flightNo,
flightDate,
originStation,
destinationStation,
uld,
waybillNo,
waybillOrigin,
waybillDes,
waybillSplit,
waybillPiece,
waybillWeight,
waybillVolume,
waybillTotalPiece,
waybillGoodsDes);
}
}
}
/**
* 关键字识别
* @param text 每行的内容
* @return 识别为关键字的返回关键字,未被识别为关键字的返回NOT_KEYWORD;
* 返回的关键字 三字码关键字 机场代码关键字 文件结尾关键字 CONT,LAST
*/
public String keyword(@NotNull String text){
//取每行前三位
if (StringUtils.isNotBlank(text) && text.length()>3){
String s_3 = text.substring(0,3);
String s_4 = text.substring(0,4);
if(KEY_WORD_3.contains(s_3)){
return s_3;
}
if(KEY_WORD_4.contains(s_4)){
return s_4;
}
}else if(text.length()==3){
log.info("{}很大可能是机场目的站",text);
}
return "NOT_KEYWORD";
}
/**
* 解析航班第一目的站
*
*/
public void destination(String text) throws FFMResolveException{
log.info("4.开始校验处理航班目的站节点信息");
if (text.length()>=3){
//校验是否空货机
String pattern_nil = "[A-Z]{3}/NIL";
Pattern r_nil = Pattern.compile(pattern_nil);
// 现在创建 matcher 对象
Matcher m_nil = r_nil.matcher(text);
if (m_nil.find()){
log.info("4.1 航班目的站货物为空");
throw new FFMResolveException("航班第一目的站货物为空,解析结束");
}
//校验是否是目的站节点
//校验正则1,取前三位验证是否是机场代码
String pattern = "^[A-Z]{3}$|^[A-Z]{3}/\\d{2}[A-Z]{3}\\d{4}";
// 创建 Pattern 对象
Pattern r = Pattern.compile(pattern);
// 现在创建 matcher 对象
Matcher m = r.matcher(text);
if (m.find()){
log.info("4.1 航班目的站节点校验通过");
destinationStation = text.substring(0,3);
log.info("4.2 航班目的站一为[{}]",destinationStation);
}else {
log.error("4.1[ERROR] !!航班目的站节点校验不通过!!");
throw new FFMResolveException("航班目的站节点校验不通过.");
}
}else {
throw new FFMResolveException("航班目的站节点长度不对.");
}
}
public void flight(String text) throws FFMResolveException, ParseException {
log.info("3.开始处理航班信息");
String[] flight = text.split("/");
if (flight.length>3){
order = flight[0];
log.info("3.1 报文序号为{}",order);
flightNo = flight[1];
log.info("3.2 航班号为[{}]",flightNo);
//航班日期取出来的格式为10SEP或者10SEP1022,带时间的注意
String flightDateStr = flight[2];
log.info("3.3 航班日期信息为[{}]",flightDate);
String flightDateDay = flightDateStr.substring(0,2);
String flightDateMonth = flightDateStr.substring(2,5);
/**
* FFM跨年问题
* FFM报文中的月份为12月,当前服务器时间月份为1月,视为跨年.
* 跨年问题需要设置FFM报文的航班年份为当前年份-1
*/
Calendar cal = Calendar.getInstance();
//当前年份
int yearNow = cal.get(Calendar.YEAR);
int monthNow = cal.get(Calendar.MONTH);
if (monthNow == 1 && "DEC".equals(flightDateMonth)){
log.info("3.3.1 航班日期跨年");
yearNow = yearNow-1;
}
flightDateStr = flightDateStr+yearNow;
flightDate = DateUtil.dateFormatFlight(flightDateStr);
originStation = flight[3];
log.info("3.4 航班起始站为[{}]",originStation);
if (flight.length>4){
planeNo = flight[4];
log.info("3.4 航班飞机号为[{}]",planeNo);
}
}else {
log.error("航班信息节点不正确");
throw new FFMResolveException("航班信息节点不正确");
}
}
/**
* 模七校验
* @param waybillNo
* @return
*/
public boolean model7Check(String waybillNo){
String pattern = "\\d{3}-\\d{8}";
// 创建 Pattern 对象
Pattern r = Pattern.compile(pattern);
// 现在创建 matcher 对象
Matcher m = r.matcher(waybillNo);
if (m.find()){
String num = waybillNo.split("-")[1];
String num_7 = num.substring(0,7);
String num_end = num.substring(7,8);
if (Integer.parseInt(num_7)% 7 == Integer.parseInt(num_end)){
return true;
}else {
log.error("{}模七不通过",waybillNo);
}
}else {
log.error("{}运单格式不正确",waybillNo);
}
return false;
}
}