CustomXmlHandleThread.java
17.2 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
package com.tianbo.analysis.handle;
import com.tianbo.analysis.model.*;
import com.tianbo.analysis.service.CustomMessageService;
import com.tianbo.analysis.tools.AWBTools;
import com.tianbo.util.Date.DateUtil;
import com.tianbo.util.XML.XMLXPath;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileExistsException;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.xml.sax.SAXParseException;
import javax.annotation.PostConstruct;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.concurrent.CountDownLatch;
/**
* @author mrz
* @e
* 海关新舱单回执解析xml文件
*/
@Data
@Component
@Slf4j
public class CustomXmlHandleThread implements Runnable{
private CountDownLatch latch;
//备份目录
@Value("${custom.receptBakDir}")
private String bakupDir;
//解析出错转移目录
@Value("${custom.errBakDir}")
private String errBakDir;
//回执转发目录
@Value("${custom.transmitDir}")
private String transmitDir;
//匹配技术回执正则
@Value("${custom.delTechnologyReceptMatch}")
private String delTechnologyReceptMatch;
private File xmlfile;
private static CustomXmlHandleThread customXmlHandle;
//海关新舱单回执报头
public final static String MessageID = "//Manifest/Head/MessageID";
public final static String FunctionCode = "//Manifest/Head/FunctionCode";
public final static String MessageType = "//Manifest/Head/MessageType";
public final static String SenderID = "//Manifest/Head/SenderID";
public final static String ReceiverID = "//Manifest/Head/ReceiverID";
public final static String SendTime = "//Manifest/Head/SendTime";
public final static String Version = "//Manifest/Head/Version";
/**
* 海关普通业务新舱单回执报体
*/
public final static String JourneyID = "//Manifest/Response/BorderTransportMeans/JourneyID";
public final static String WaybillMaster = "//Manifest/Response/Consignment/TransportContractDocument/ID";
public final static String WaybillSecond = "//Manifest/Response/Consignment/AssociatedTransportDocument/ID";
public final static String ResponseCode = "//ResponseType/Code";
public final static String ResponseText = "//ResponseType/Text";
/**
* 海关国际转运业务回执报体
*/
public final static String ImportJourneyID = "//Manifest/Response/ImportInformation/BorderTransportMeans/JourneyID";
public final static String ImportWaybillMaster = "//Manifest/Response/ImportInformation/Consignment/TransportContractDocument/ID";
public final static String ExportJourneyID = "//Manifest/Response/ExportInformation/BorderTransportMeans/JourneyID";
public final static String ExportWaybillMaster = "//Manifest/Response/ExportInformation/Consignment/TransportContractDocument/ID";
// public final static String TransResponseCode = "//Manifest/Response/ResponseType/Code";
// public final static String TransResponseText = "//Manifest/Response/ResponseType/Text";
public final static String TransResponseCode = "//ResponseType/Code";
public final static String TransResponseText = "//ResponseType/Text";
//通过@PostConstruct实现初始化bean之前进行的操作,解决service调用空指针问题
@PostConstruct
public void init() {
customXmlHandle = this;
customXmlHandle.bakupDir = this.bakupDir;
customXmlHandle.errBakDir = this.errBakDir;
customXmlHandle.delTechnologyReceptMatch = this.delTechnologyReceptMatch;
customXmlHandle.transmitDir = this.transmitDir;
// 初使化时将已静态化的testService实例化
}
@Override
public void run() {
String filename = xmlfile.getName();
log.info("线程:{}开始",filename);
if(filename.matches(customXmlHandle.delTechnologyReceptMatch)){
log.warn("{}报文为技术回执,剪切到错误备份目录,不解析",filename);
errBak(xmlfile);
log.info("{}报文为技术回执,剪切备份成功..开始解析下一文件,当前线程即将结束",filename);
}
else
{
try{
//解析前先转发
FileUtils.copyFileToDirectory(xmlfile, new File(customXmlHandle.transmitDir));
int i =handelXmlDocument();
String today = DateUtil.getTodayBy_yyyyMMdd();
String backdireByDay = customXmlHandle.bakupDir + "/" + today;
//操作成功,则转移剪切解析文件到备份目录,否则转移到error目录备份
if(i>0){
File bakupDirectory = new File(backdireByDay);
//解析成功备份一份到备份目录
FileUtils.moveFileToDirectory(xmlfile,bakupDirectory,true);
}else {
errBak(xmlfile);
}
}catch (FileExistsException e){
log.error("错误的解析文件剪切失败,目标目录已存在同名文件");
}catch (Exception e){
log.error("错误的解析文件剪切失败,目标目录已存在同名文件",e);
errBak(xmlfile);
e.printStackTrace();
}
}
log.info("线程:{}结束",xmlfile.getName());
latch.countDown();
log.info("剩余线程数量{}",latch.getCount());
}
/**
*
*
* @return
* @throws DocumentException
* @throws UnsupportedEncodingException
* @throws SAXParseException
* @throws FileNotFoundException
*/
private int handelXmlDocument() throws DocumentException,UnsupportedEncodingException,SAXParseException,FileNotFoundException {
int i = 0;
String secondSplit = "_";
SAXReader saxReader = new SAXReader();
Document document = saxReader.read(xmlfile);
Element contentRoot = document.getRootElement();
String flightNo = "UNKONW";
String flightDate = "20101010";
//开始解析
String msgType = XMLXPath.getSingleValueByPath(document,MessageType);
String journeyid = XMLXPath.getSingleValueByPath(document,JourneyID);
// XMLXPath.getSingleValueByPath(document, )
String awbA = XMLXPath.getSingleValueByPath(document,WaybillMaster);
//全格式的分单 如 17212345678_ADBD
String awbH = XMLXPath.getSingleValueByPath(document,WaybillSecond);
String resCode = XMLXPath.getSingleValueByPath(document,ResponseCode);
String resText = XMLXPath.getSingleValueByPath(document,ResponseText);
String messageId = XMLXPath.getSingleValueByPath(document, MessageID);
String sendTime = XMLXPath.getSingleValueByPath(document,SendTime);
String sendId = XMLXPath.getSingleValueByPath(document,SenderID);
String reciveId = XMLXPath.getSingleValueByPath(document,ReceiverID);
String version = XMLXPath.getSingleValueByPath(document,Version);
String functionCode = XMLXPath.getSingleValueByPath(document,FunctionCode);
CustomReception customReception = new CustomReception( msgType,
flightNo,
flightDate,
awbA,
awbH,
resCode,
resText,
messageId,
sendTime,
sendId,
reciveId,
version,
functionCode);
/**
* 如果回执中没有携带航班信息节点,说明是出错报文
* 到发送日志表根据messageid 找到相应的发送日志报文的航班及运单信息,再进行解析
*/
if(!StringUtils.isEmpty(journeyid)){
String[] flightList = journeyid.split("/");
if(flightList.length > 0){
flightNo = flightList[0];
flightDate = flightList[1];
customReception.setFlightNo(flightNo);
customReception.setFlightDate(flightDate);
}
}else {
CUSTOMSMESSAGE customsmessage = new CUSTOMSMESSAGE();
customReception = customsmessage.getWaybillInfoByCutomResponse(customReception);
}
switch (msgType){
case "MT9999":
if (customReception.getWayBillSecond()!=null && customReception.getWayBillSecond().contains(secondSplit)){
PREPARESECONDARY preparesecondary = new PREPARESECONDARY(customReception);
int pre_i = preparesecondary.secondAnalysisReception();
if (pre_i>0){
i=1;
}else {
ARRIVEDSECONDARY arrivedsecondary = new ARRIVEDSECONDARY(customReception);
int arr_i=arrivedsecondary.secondAnalysisReception();
if (arr_i>0){
i= 1;
}else {
Originmanifestsecondary originmanifestsecondary = new Originmanifestsecondary(customReception);
int org_i=originmanifestsecondary.secondAnalysisReception();
if (org_i>0){
i=1;
}
}
}
}else{
ORIGINMANIFESTMASTER originmanifestmaster = new ORIGINMANIFESTMASTER(customReception);
PREPAREMASTER preparemaster= new PREPAREMASTER(customReception);
ARRIVEDMASTER arrivedmaster9999 = new ARRIVEDMASTER(customReception);
if(originmanifestmaster.masterAnalysisReception()>0){
i=1;
}else if(preparemaster.masterAnalysisReception()>0){
i=1;
}else {
i=arrivedmaster9999.masterAnalysisReception();
}
}
AgentXmlHandle agentXmlHandle=new AgentXmlHandle();
agentXmlHandle.Http_resolver(customReception);
break;
case "MT3201":
if (customReception.getWayBillSecond()!=null && customReception.getWayBillSecond().contains(secondSplit)){
ARRIVEDSECONDARY arrivedsecondary = new ARRIVEDSECONDARY(customReception);
i=arrivedsecondary.secondAnalysisReception();
}else {
ARRIVEDMASTER arrivedmaster = new ARRIVEDMASTER(customReception);
i=arrivedmaster.masterAnalysisReception();
}
break;
case "MT5202":
if (customReception.getWayBillSecond()!=null && customReception.getWayBillSecond().contains(secondSplit)){
TALLYSECONDARY tallysecondary = new TALLYSECONDARY(customReception);
i=tallysecondary.secondAnalysisReception();
}else {
TALLYMASTER tallymaster= new TALLYMASTER(customReception);
i = tallymaster.masterAnalysisReception();
}
break;
case "MT5201":
if (customReception.getWayBillSecond()!=null && customReception.getWayBillSecond().contains(secondSplit)){
TALLYSECONDARY tallysecondary = new TALLYSECONDARY(customReception);
i=tallysecondary.secondAnalysisReception();
}else {
TALLYMASTER tallymaster= new TALLYMASTER(customReception);
i = tallymaster.masterAnalysisReception();
}
break;
case "MT4201":
DEPARTURESLOADING departuresloading = new DEPARTURESLOADING(customReception);
i=departuresloading.masterAnalysisReception();
break;
case "MT1201":
if (customReception.getWayBillSecond()!=null && customReception.getWayBillSecond().contains(secondSplit)){
Originmanifestsecondary originmanifestsecondary = new Originmanifestsecondary(customReception);
i=originmanifestsecondary.secondAnalysisReception();
}else {
ORIGINMANIFESTMASTER originmanifestmaster1201 = new ORIGINMANIFESTMASTER(customReception);
i = originmanifestmaster1201.masterAnalysisReception();
}
break;
case "MT2201":
if (customReception.getWayBillSecond()!=null && customReception.getWayBillSecond().contains(secondSplit)){
PREPARESECONDARY preparesecondary = new PREPARESECONDARY(customReception);
i = preparesecondary.secondAnalysisReception();
}else {
PREPAREMASTER preparemaster1201= new PREPAREMASTER(customReception);
i = preparemaster1201.masterAnalysisReception();
}
break;
case "MT6202":
INPORTALLOCATE inportallocate = new INPORTALLOCATE(customReception);
i = inportallocate.masterAnalysisReception();
break;
case "MT3202":
ALLOCATEARRIVAL allocatearrival = new ALLOCATEARRIVAL(customReception);
i= allocatearrival.masterAnalysisReception();
break;
case "MT8205":
i = transXmlHandel(document,customReception);
break;
case "MT8202":
MANIFESTLOAD manifestload = new MANIFESTLOAD(customReception);
i= manifestload.secondAnalysisReception();
break;
case "MT8203":
MANIFESTLOSTCHANGE manifestlostchange = new MANIFESTLOSTCHANGE(customReception);
i= manifestlostchange.secondAnalysisReception();
break;
default:
break;
}
updateCustomMessage(customReception);
return i;
}
/**
* 国际转运回执处理,特殊业务
* @param document
* @param customReception
* @return
*/
private int transXmlHandel(Document document,CustomReception customReception){
String importJourneyID = XMLXPath.getSingleValueByPath(document,ImportJourneyID);
String importFlightNo = "UNKONW";
String importFlightDate = "20101010";
if(!StringUtils.isEmpty(importJourneyID)){
importFlightNo = AWBTools.splitFlightAndDate(importJourneyID)[0];
importFlightDate = AWBTools.splitFlightAndDate(importJourneyID)[1];
}
String importWaybillMaster= XMLXPath.getSingleValueByPath(document,ImportWaybillMaster);
String transResponseCode=XMLXPath.getSingleValueByPath(document,TransResponseCode);
String transResponseText=XMLXPath.getSingleValueByPath(document,TransResponseText);
CustomReception transCustomReception = new CustomReception( customReception.getMessageType(),
importFlightNo,
importFlightDate,
importWaybillMaster,
transResponseCode,
transResponseText,
customReception.getMessageID(),
customReception.getSendTime(),
customReception.getSenderID(),
customReception.getReceiverID(),
customReception.getVersion(),
customReception.getFunctionCode());
INTERNATIONALTRANSIT internationaltransit = new INTERNATIONALTRANSIT(transCustomReception);
int i=internationaltransit.masterAnalysisReception();
return i;
}
private int updateCustomMessage(CustomReception customReception){
CUSTOMSMESSAGE customsmessage = new CUSTOMSMESSAGE(customReception);
return customsmessage.updateMessageByMessageid();
}
private void errBak(File file){
try {
String today = DateUtil.getTodayBy_yyyyMMdd();
String errDirByDay = customXmlHandle.errBakDir + "/" + today;
File berrDirectory = new File(errDirByDay);
FileUtils.moveFileToDirectory(file,berrDirectory,true);
log.info("数据库未找到与回执适配的信息备份文件:{}:",file.getName());
}catch (FileExistsException e){
log.error("备份解析错误文件失败,目标文件夹存在同名文件{}",file.getName());
}catch (Exception e){
e.printStackTrace();
}
}
}