|
|
1
|
+package com.tianbo.analysis.handle;
|
|
|
2
|
+
|
|
|
3
|
+import com.tianbo.analysis.model.*;
|
|
|
4
|
+import com.tianbo.analysis.tools.AWBTools;
|
|
|
5
|
+import com.tianbo.util.Date.DateUtil;
|
|
|
6
|
+import com.tianbo.util.XML.XMLXPath;
|
|
|
7
|
+import lombok.Data;
|
|
|
8
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
9
|
+import org.apache.commons.io.FileExistsException;
|
|
|
10
|
+import org.apache.commons.io.FileUtils;
|
|
|
11
|
+import org.apache.commons.lang.StringUtils;
|
|
|
12
|
+import org.dom4j.Document;
|
|
|
13
|
+import org.dom4j.DocumentException;
|
|
|
14
|
+import org.dom4j.Element;
|
|
|
15
|
+import org.dom4j.io.SAXReader;
|
|
|
16
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
17
|
+import org.springframework.stereotype.Component;
|
|
|
18
|
+import org.xml.sax.SAXParseException;
|
|
|
19
|
+
|
|
|
20
|
+import javax.annotation.PostConstruct;
|
|
|
21
|
+import java.io.ByteArrayInputStream;
|
|
|
22
|
+import java.io.File;
|
|
|
23
|
+import java.io.FileNotFoundException;
|
|
|
24
|
+import java.io.UnsupportedEncodingException;
|
|
|
25
|
+import java.util.UUID;
|
|
|
26
|
+
|
|
|
27
|
+/**
|
|
|
28
|
+ * @author mrz
|
|
|
29
|
+ * @e
|
|
|
30
|
+ * 海关新舱单回执解析xml文件
|
|
|
31
|
+ */
|
|
|
32
|
+@Data
|
|
|
33
|
+@Component
|
|
|
34
|
+@Slf4j
|
|
|
35
|
+public class CustomResponseHandleThread implements Runnable{
|
|
|
36
|
+
|
|
|
37
|
+ //解析出错转移目录
|
|
|
38
|
+ @Value("${custom.errBakDir}")
|
|
|
39
|
+ private String errBakDir;
|
|
|
40
|
+
|
|
|
41
|
+ @Value("${custom.receptBakDir}")
|
|
|
42
|
+ private String receptBakDir;
|
|
|
43
|
+
|
|
|
44
|
+ private String responseMessage;
|
|
|
45
|
+
|
|
|
46
|
+ private static CustomResponseHandleThread customXmlHandle;
|
|
|
47
|
+
|
|
|
48
|
+ /**
|
|
|
49
|
+ * 单一窗口回执的技术回执
|
|
|
50
|
+ */
|
|
|
51
|
+ public final static String DONT_NEED = "<Code>0</Code><Text>接收成功</Text>";
|
|
|
52
|
+
|
|
|
53
|
+ //海关新舱单回执报头
|
|
|
54
|
+ public final static String MessageID = "//Manifest/Head/MessageID";
|
|
|
55
|
+ public final static String FunctionCode = "//Manifest/Head/FunctionCode";
|
|
|
56
|
+ public final static String MessageType = "//Manifest/Head/MessageType";
|
|
|
57
|
+ public final static String SenderID = "//Manifest/Head/SenderID";
|
|
|
58
|
+ public final static String ReceiverID = "//Manifest/Head/ReceiverID";
|
|
|
59
|
+ public final static String SendTime = "//Manifest/Head/SendTime";
|
|
|
60
|
+ public final static String Version = "//Manifest/Head/Version";
|
|
|
61
|
+
|
|
|
62
|
+
|
|
|
63
|
+ /**
|
|
|
64
|
+ * 海关普通业务新舱单回执报体
|
|
|
65
|
+ */
|
|
|
66
|
+ public final static String JourneyID = "//Manifest/Response/BorderTransportMeans/JourneyID";
|
|
|
67
|
+ public final static String WaybillMaster = "//Manifest/Response/Consignment/TransportContractDocument/ID";
|
|
|
68
|
+ public final static String WaybillSecond = "//Manifest/Response/Consignment/AssociatedTransportDocument/ID";
|
|
|
69
|
+ public final static String ResponseCode = "//ResponseType/Code";
|
|
|
70
|
+ public final static String ResponseText = "//ResponseType/Text";
|
|
|
71
|
+
|
|
|
72
|
+ /**
|
|
|
73
|
+ * 海关国际转运业务回执报体
|
|
|
74
|
+ */
|
|
|
75
|
+ public final static String ImportJourneyID = "//Manifest/Response/ImportInformation/BorderTransportMeans/JourneyID";
|
|
|
76
|
+ public final static String ImportWaybillMaster = "//Manifest/Response/ImportInformation/Consignment/TransportContractDocument/ID";
|
|
|
77
|
+ public final static String ExportJourneyID = "//Manifest/Response/ExportInformation/BorderTransportMeans/JourneyID";
|
|
|
78
|
+ public final static String ExportWaybillMaster = "//Manifest/Response/ExportInformation/Consignment/TransportContractDocument/ID";
|
|
|
79
|
+// public final static String TransResponseCode = "//Manifest/Response/ResponseType/Code";
|
|
|
80
|
+// public final static String TransResponseText = "//Manifest/Response/ResponseType/Text";
|
|
|
81
|
+ public final static String TransResponseCode = "//ResponseType/Code";
|
|
|
82
|
+ public final static String TransResponseText = "//ResponseType/Text";
|
|
|
83
|
+
|
|
|
84
|
+
|
|
|
85
|
+ //通过@PostConstruct实现初始化bean之前进行的操作,解决service调用空指针问题
|
|
|
86
|
+ @PostConstruct
|
|
|
87
|
+ public void init() {
|
|
|
88
|
+ customXmlHandle = this;
|
|
|
89
|
+
|
|
|
90
|
+ customXmlHandle.errBakDir = this.errBakDir;
|
|
|
91
|
+ customXmlHandle.receptBakDir = this.receptBakDir;
|
|
|
92
|
+
|
|
|
93
|
+ // 初使化时将已静态化的testService实例化
|
|
|
94
|
+ }
|
|
|
95
|
+
|
|
|
96
|
+ @Override
|
|
|
97
|
+ public void run() {
|
|
|
98
|
+ log.info("线程开始");
|
|
|
99
|
+ String today = DateUtil.getTodayBy_yyyyMMdd();
|
|
|
100
|
+ String errDirByDay = customXmlHandle.errBakDir + "/" + today;
|
|
|
101
|
+ String successDir = customXmlHandle.receptBakDir + "/" + today;
|
|
|
102
|
+ if(responseMessage.contains(DONT_NEED)){
|
|
|
103
|
+ log.warn("{}报文为技术回执,剪切到错误备份目录,不解析",responseMessage);
|
|
|
104
|
+
|
|
|
105
|
+ errBak(responseMessage,errDirByDay);
|
|
|
106
|
+ log.info("报文为技术回执备份成功..开始解析下一文件,当前线程即将结束");
|
|
|
107
|
+ }
|
|
|
108
|
+ else
|
|
|
109
|
+ {
|
|
|
110
|
+ try{
|
|
|
111
|
+
|
|
|
112
|
+ int i =handelXmlDocument();
|
|
|
113
|
+ //操作成功,则转移剪切解析文件到备份目录,否则转移到error目录备份
|
|
|
114
|
+ if(i>0){
|
|
|
115
|
+ errBak(responseMessage,successDir);
|
|
|
116
|
+ log.info("报文解析入库成功");
|
|
|
117
|
+ }else {
|
|
|
118
|
+ errBak(responseMessage,errDirByDay);
|
|
|
119
|
+ }
|
|
|
120
|
+ }catch (Exception e){
|
|
|
121
|
+ log.error("报文备份异常",e);
|
|
|
122
|
+ errBak(responseMessage,errDirByDay);
|
|
|
123
|
+ e.printStackTrace();
|
|
|
124
|
+ }
|
|
|
125
|
+
|
|
|
126
|
+ }
|
|
|
127
|
+ log.info("线程结束");
|
|
|
128
|
+ }
|
|
|
129
|
+ /**
|
|
|
130
|
+ *
|
|
|
131
|
+ *
|
|
|
132
|
+ * @return
|
|
|
133
|
+ * @throws DocumentException
|
|
|
134
|
+ * @throws UnsupportedEncodingException
|
|
|
135
|
+ * @throws SAXParseException
|
|
|
136
|
+ * @throws FileNotFoundException
|
|
|
137
|
+ */
|
|
|
138
|
+ private int handelXmlDocument() throws DocumentException,UnsupportedEncodingException,SAXParseException,FileNotFoundException {
|
|
|
139
|
+
|
|
|
140
|
+ int i = 0;
|
|
|
141
|
+ String secondSplit = "_";
|
|
|
142
|
+ SAXReader saxReader = new SAXReader();
|
|
|
143
|
+ Document document = saxReader.read(new ByteArrayInputStream(responseMessage.getBytes("UTF-8")));
|
|
|
144
|
+ Element contentRoot = document.getRootElement();
|
|
|
145
|
+
|
|
|
146
|
+ String flightNo = "UNKONW";
|
|
|
147
|
+ String flightDate = "20101010";
|
|
|
148
|
+
|
|
|
149
|
+ //开始解析
|
|
|
150
|
+ String msgType = XMLXPath.getSingleValueByPath(document,MessageType);
|
|
|
151
|
+ String journeyid = XMLXPath.getSingleValueByPath(document,JourneyID);
|
|
|
152
|
+// XMLXPath.getSingleValueByPath(document, )
|
|
|
153
|
+
|
|
|
154
|
+
|
|
|
155
|
+ String awbA = XMLXPath.getSingleValueByPath(document,WaybillMaster);
|
|
|
156
|
+ //全格式的分单 如 17212345678_ADBD
|
|
|
157
|
+ String awbH = XMLXPath.getSingleValueByPath(document,WaybillSecond);
|
|
|
158
|
+ String resCode = XMLXPath.getSingleValueByPath(document,ResponseCode);
|
|
|
159
|
+ String resText = XMLXPath.getSingleValueByPath(document,ResponseText);
|
|
|
160
|
+ String messageId = XMLXPath.getSingleValueByPath(document, MessageID);
|
|
|
161
|
+ String sendTime = XMLXPath.getSingleValueByPath(document,SendTime);
|
|
|
162
|
+ String sendId = XMLXPath.getSingleValueByPath(document,SenderID);
|
|
|
163
|
+ String reciveId = XMLXPath.getSingleValueByPath(document,ReceiverID);
|
|
|
164
|
+ String version = XMLXPath.getSingleValueByPath(document,Version);
|
|
|
165
|
+ String functionCode = XMLXPath.getSingleValueByPath(document,FunctionCode);
|
|
|
166
|
+
|
|
|
167
|
+
|
|
|
168
|
+
|
|
|
169
|
+ CustomReception customReception = new CustomReception( msgType,
|
|
|
170
|
+ flightNo,
|
|
|
171
|
+ flightDate,
|
|
|
172
|
+ awbA,
|
|
|
173
|
+ awbH,
|
|
|
174
|
+ resCode,
|
|
|
175
|
+ resText,
|
|
|
176
|
+ messageId,
|
|
|
177
|
+ sendTime,
|
|
|
178
|
+ sendId,
|
|
|
179
|
+ reciveId,
|
|
|
180
|
+ version,
|
|
|
181
|
+ functionCode);
|
|
|
182
|
+
|
|
|
183
|
+ /**
|
|
|
184
|
+ * 如果回执中没有携带航班信息节点,说明是出错报文
|
|
|
185
|
+ * 到发送日志表根据messageid 找到相应的发送日志报文的航班及运单信息,再进行解析
|
|
|
186
|
+ */
|
|
|
187
|
+ if(!StringUtils.isEmpty(journeyid)){
|
|
|
188
|
+
|
|
|
189
|
+ String[] flightList = journeyid.split("/");
|
|
|
190
|
+ if(flightList.length > 0){
|
|
|
191
|
+ flightNo = flightList[0];
|
|
|
192
|
+ flightDate = flightList[1];
|
|
|
193
|
+ customReception.setFlightNo(flightNo);
|
|
|
194
|
+ customReception.setFlightDate(flightDate);
|
|
|
195
|
+ }
|
|
|
196
|
+ }else {
|
|
|
197
|
+ CUSTOMSMESSAGE customsmessage = new CUSTOMSMESSAGE();
|
|
|
198
|
+ customReception = customsmessage.getWaybillInfoByCutomResponse(customReception);
|
|
|
199
|
+ }
|
|
|
200
|
+
|
|
|
201
|
+ switch (msgType){
|
|
|
202
|
+ case "MT9999":
|
|
|
203
|
+ if (customReception.getWayBillSecond()!=null && customReception.getWayBillSecond().contains(secondSplit)){
|
|
|
204
|
+ PREPARESECONDARY preparesecondary = new PREPARESECONDARY(customReception);
|
|
|
205
|
+ int pre_i = preparesecondary.secondAnalysisReception();
|
|
|
206
|
+
|
|
|
207
|
+ if (pre_i>0){
|
|
|
208
|
+ i=1;
|
|
|
209
|
+ }else {
|
|
|
210
|
+ ARRIVEDSECONDARY arrivedsecondary = new ARRIVEDSECONDARY(customReception);
|
|
|
211
|
+ int arr_i=arrivedsecondary.secondAnalysisReception();
|
|
|
212
|
+
|
|
|
213
|
+ if (arr_i>0){
|
|
|
214
|
+ i= 1;
|
|
|
215
|
+ }else {
|
|
|
216
|
+ Originmanifestsecondary originmanifestsecondary = new Originmanifestsecondary(customReception);
|
|
|
217
|
+ int org_i=originmanifestsecondary.secondAnalysisReception();
|
|
|
218
|
+ if (org_i>0){
|
|
|
219
|
+ i=1;
|
|
|
220
|
+ }
|
|
|
221
|
+ }
|
|
|
222
|
+ }
|
|
|
223
|
+ }else{
|
|
|
224
|
+ ORIGINMANIFESTMASTER originmanifestmaster = new ORIGINMANIFESTMASTER(customReception);
|
|
|
225
|
+ PREPAREMASTER preparemaster= new PREPAREMASTER(customReception);
|
|
|
226
|
+ ARRIVEDMASTER arrivedmaster9999 = new ARRIVEDMASTER(customReception);
|
|
|
227
|
+ if(originmanifestmaster.masterAnalysisReception()>0){
|
|
|
228
|
+ i=1;
|
|
|
229
|
+ }else if(preparemaster.masterAnalysisReception()>0){
|
|
|
230
|
+ i=1;
|
|
|
231
|
+ }else {
|
|
|
232
|
+ i=arrivedmaster9999.masterAnalysisReception();
|
|
|
233
|
+ }
|
|
|
234
|
+ }
|
|
|
235
|
+ AgentXmlHandle agentXmlHandle=new AgentXmlHandle();
|
|
|
236
|
+ agentXmlHandle.Http_resolver(customReception);
|
|
|
237
|
+ break;
|
|
|
238
|
+ case "MT3201":
|
|
|
239
|
+ if (customReception.getWayBillSecond()!=null && customReception.getWayBillSecond().contains(secondSplit)){
|
|
|
240
|
+ ARRIVEDSECONDARY arrivedsecondary = new ARRIVEDSECONDARY(customReception);
|
|
|
241
|
+ i=arrivedsecondary.secondAnalysisReception();
|
|
|
242
|
+ }else {
|
|
|
243
|
+ ARRIVEDMASTER arrivedmaster = new ARRIVEDMASTER(customReception);
|
|
|
244
|
+ i=arrivedmaster.masterAnalysisReception();
|
|
|
245
|
+ }
|
|
|
246
|
+ break;
|
|
|
247
|
+ case "MT5202":
|
|
|
248
|
+ if (customReception.getWayBillSecond()!=null && customReception.getWayBillSecond().contains(secondSplit)){
|
|
|
249
|
+ TALLYSECONDARY tallysecondary = new TALLYSECONDARY(customReception);
|
|
|
250
|
+ i=tallysecondary.secondAnalysisReception();
|
|
|
251
|
+ }else {
|
|
|
252
|
+ TALLYMASTER tallymaster= new TALLYMASTER(customReception);
|
|
|
253
|
+ i = tallymaster.masterAnalysisReception();
|
|
|
254
|
+ }
|
|
|
255
|
+ break;
|
|
|
256
|
+ case "MT5201":
|
|
|
257
|
+ if (customReception.getWayBillSecond()!=null && customReception.getWayBillSecond().contains(secondSplit)){
|
|
|
258
|
+ TALLYSECONDARY tallysecondary = new TALLYSECONDARY(customReception);
|
|
|
259
|
+ i=tallysecondary.secondAnalysisReception();
|
|
|
260
|
+ }else {
|
|
|
261
|
+ TALLYMASTER tallymaster= new TALLYMASTER(customReception);
|
|
|
262
|
+ i = tallymaster.masterAnalysisReception();
|
|
|
263
|
+ }
|
|
|
264
|
+ break;
|
|
|
265
|
+ case "MT4201":
|
|
|
266
|
+ DEPARTURESLOADING departuresloading = new DEPARTURESLOADING(customReception);
|
|
|
267
|
+ i=departuresloading.masterAnalysisReception();
|
|
|
268
|
+ break;
|
|
|
269
|
+ case "MT1201":
|
|
|
270
|
+ if (customReception.getWayBillSecond()!=null && customReception.getWayBillSecond().contains(secondSplit)){
|
|
|
271
|
+ Originmanifestsecondary originmanifestsecondary = new Originmanifestsecondary(customReception);
|
|
|
272
|
+ i=originmanifestsecondary.secondAnalysisReception();
|
|
|
273
|
+ }else {
|
|
|
274
|
+ ORIGINMANIFESTMASTER originmanifestmaster1201 = new ORIGINMANIFESTMASTER(customReception);
|
|
|
275
|
+ i = originmanifestmaster1201.masterAnalysisReception();
|
|
|
276
|
+ }
|
|
|
277
|
+ break;
|
|
|
278
|
+ case "MT2201":
|
|
|
279
|
+ if (customReception.getWayBillSecond()!=null && customReception.getWayBillSecond().contains(secondSplit)){
|
|
|
280
|
+ PREPARESECONDARY preparesecondary = new PREPARESECONDARY(customReception);
|
|
|
281
|
+ i = preparesecondary.secondAnalysisReception();
|
|
|
282
|
+ }else {
|
|
|
283
|
+ PREPAREMASTER preparemaster1201= new PREPAREMASTER(customReception);
|
|
|
284
|
+ i = preparemaster1201.masterAnalysisReception();
|
|
|
285
|
+ }
|
|
|
286
|
+ break;
|
|
|
287
|
+ case "MT6202":
|
|
|
288
|
+ INPORTALLOCATE inportallocate = new INPORTALLOCATE(customReception);
|
|
|
289
|
+ i = inportallocate.masterAnalysisReception();
|
|
|
290
|
+ break;
|
|
|
291
|
+ case "MT3202":
|
|
|
292
|
+ ALLOCATEARRIVAL allocatearrival = new ALLOCATEARRIVAL(customReception);
|
|
|
293
|
+ i= allocatearrival.masterAnalysisReception();
|
|
|
294
|
+ break;
|
|
|
295
|
+ case "MT8205":
|
|
|
296
|
+ i = transXmlHandel(document,customReception);
|
|
|
297
|
+ break;
|
|
|
298
|
+ case "MT8202":
|
|
|
299
|
+ MANIFESTLOAD manifestload = new MANIFESTLOAD(customReception);
|
|
|
300
|
+ i= manifestload.secondAnalysisReception();
|
|
|
301
|
+ break;
|
|
|
302
|
+ case "MT8203":
|
|
|
303
|
+ MANIFESTLOSTCHANGE manifestlostchange = new MANIFESTLOSTCHANGE(customReception);
|
|
|
304
|
+ i= manifestlostchange.secondAnalysisReception();
|
|
|
305
|
+ break;
|
|
|
306
|
+ default:
|
|
|
307
|
+ break;
|
|
|
308
|
+
|
|
|
309
|
+ }
|
|
|
310
|
+
|
|
|
311
|
+ updateCustomMessage(customReception);
|
|
|
312
|
+ return i;
|
|
|
313
|
+
|
|
|
314
|
+ }
|
|
|
315
|
+
|
|
|
316
|
+ /**
|
|
|
317
|
+ * 国际转运回执处理,特殊业务
|
|
|
318
|
+ * @param document
|
|
|
319
|
+ * @param customReception
|
|
|
320
|
+ * @return
|
|
|
321
|
+ */
|
|
|
322
|
+ private int transXmlHandel(Document document,CustomReception customReception){
|
|
|
323
|
+
|
|
|
324
|
+ String importJourneyID = XMLXPath.getSingleValueByPath(document,ImportJourneyID);
|
|
|
325
|
+ String importFlightNo = "UNKONW";
|
|
|
326
|
+ String importFlightDate = "20101010";
|
|
|
327
|
+ if(!StringUtils.isEmpty(importJourneyID)){
|
|
|
328
|
+ importFlightNo = AWBTools.splitFlightAndDate(importJourneyID)[0];
|
|
|
329
|
+ importFlightDate = AWBTools.splitFlightAndDate(importJourneyID)[1];
|
|
|
330
|
+ }
|
|
|
331
|
+
|
|
|
332
|
+
|
|
|
333
|
+ String importWaybillMaster= XMLXPath.getSingleValueByPath(document,ImportWaybillMaster);
|
|
|
334
|
+ String transResponseCode=XMLXPath.getSingleValueByPath(document,TransResponseCode);
|
|
|
335
|
+ String transResponseText=XMLXPath.getSingleValueByPath(document,TransResponseText);
|
|
|
336
|
+ CustomReception transCustomReception = new CustomReception( customReception.getMessageType(),
|
|
|
337
|
+ importFlightNo,
|
|
|
338
|
+ importFlightDate,
|
|
|
339
|
+ importWaybillMaster,
|
|
|
340
|
+ transResponseCode,
|
|
|
341
|
+ transResponseText,
|
|
|
342
|
+ customReception.getMessageID(),
|
|
|
343
|
+ customReception.getSendTime(),
|
|
|
344
|
+ customReception.getSenderID(),
|
|
|
345
|
+ customReception.getReceiverID(),
|
|
|
346
|
+ customReception.getVersion(),
|
|
|
347
|
+ customReception.getFunctionCode());
|
|
|
348
|
+ INTERNATIONALTRANSIT internationaltransit = new INTERNATIONALTRANSIT(transCustomReception);
|
|
|
349
|
+
|
|
|
350
|
+ int i=internationaltransit.masterAnalysisReception();
|
|
|
351
|
+
|
|
|
352
|
+ return i;
|
|
|
353
|
+
|
|
|
354
|
+ }
|
|
|
355
|
+
|
|
|
356
|
+ private int updateCustomMessage(CustomReception customReception){
|
|
|
357
|
+ CUSTOMSMESSAGE customsmessage = new CUSTOMSMESSAGE(customReception);
|
|
|
358
|
+ return customsmessage.updateMessageByMessageid();
|
|
|
359
|
+ }
|
|
|
360
|
+
|
|
|
361
|
+ private void errBak(String responseText,String dir){
|
|
|
362
|
+ try {
|
|
|
363
|
+
|
|
|
364
|
+
|
|
|
365
|
+ File errfile = new File(dir + "/" + UUID.randomUUID().toString()+".xml");
|
|
|
366
|
+ FileUtils.writeStringToFile(errfile,responseText,"UTF-8");
|
|
|
367
|
+ log.info("数据库未找到与回执适配的信息备份报文");
|
|
|
368
|
+ }catch (FileExistsException e){
|
|
|
369
|
+ e.printStackTrace();
|
|
|
370
|
+ log.error("备份文件失败");
|
|
|
371
|
+ }catch (Exception e){
|
|
|
372
|
+ e.printStackTrace();
|
|
|
373
|
+ }
|
|
|
374
|
+ }
|
|
|
375
|
+
|
|
|
376
|
+} |