CustomXmlHandleThread.java
9.3 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
package com.tianbo.analysis.handle;
import com.tianbo.analysis.model.CustomReception;
import com.tianbo.analysis.service.*;
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.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.springframework.beans.factory.annotation.Autowired;
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.UnsupportedEncodingException;
/**
* @author mrz
* @e
* 海关新舱单回执解析xml文件
*/
@Data
@Component
@Slf4j
public class CustomXmlHandleThread implements Runnable{
//备份目录
@Value("${custom.receptBakDir}")
private String bakupDir;
//解析出错转移目录
@Value("${custom.errBakDir}")
private String errBakDir;
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 = "//Manifest/Response/Consignment/ResponseType/Code";
public final static String ResponseText = "//Manifest/Response/Consignment/ResponseType/Text";
@Autowired
private CoustomAnalysisService coustomAnalysisService;
@Autowired
private ARRIVEDMASTERService arrivedmasterService;
@Autowired
private TALLYMASTERService tallymasterService;
@Autowired
private DEPARTURESLOADINGService departuresloadingService;
@Autowired
private PREPAREMASTERService preparemasterService;
@Autowired
private ALLOCATEARRIVALService allocatearrivalService;
@Autowired
private INPORTALLOCATEService inportallocateService;
//通过@PostConstruct实现初始化bean之前进行的操作,解决service调用空指针问题
@PostConstruct
public void init() {
customXmlHandle = this;
customXmlHandle.coustomAnalysisService = this.coustomAnalysisService;
customXmlHandle.arrivedmasterService = this.arrivedmasterService;
customXmlHandle.tallymasterService = this.tallymasterService;
customXmlHandle.departuresloadingService = this.departuresloadingService;
customXmlHandle.preparemasterService = this.preparemasterService;
customXmlHandle.allocatearrivalService = this.allocatearrivalService;
customXmlHandle.inportallocateService = this.inportallocateService;
customXmlHandle.bakupDir = this.bakupDir;
customXmlHandle.errBakDir = this.errBakDir;
// 初使化时将已静态化的testService实例化
}
@Override
public void run() {
try{
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("错误的解析文件剪切失败,目标目录已存在同名文件",e);
e.printStackTrace();
}catch (Exception e){
e.printStackTrace();
}
}
/**
*
*
* @return
* @throws DocumentException
* @throws UnsupportedEncodingException
* @throws SAXParseException
* @throws FileNotFoundException
*/
public int handelXmlDocument() throws DocumentException,UnsupportedEncodingException,SAXParseException,FileNotFoundException {
int i = 0;
SAXReader saxReader = new SAXReader();
Document document = saxReader.read(xmlfile);
Element contentRoot = document.getRootElement();
String flightNo = "";
String flightDate = "";
//开始解析
// fzeFoh.setGrossweightmeasureuc(XMLXPath.getSingleValueByPath(document,GrossWeightMeasureUc));
String msgType = XMLXPath.getSingleValueByPath(document,MessageType);
String journeyid = XMLXPath.getSingleValueByPath(document,JourneyID);
// XMLXPath.getSingleValueByPath(document, )
if(journeyid != null&& !journeyid.isEmpty()){
String[] flightList = journeyid.split("/");
if(flightList.length > 0){
flightNo = flightList[0];
flightDate = flightList[1];
}
}
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);
switch (msgType){
case "MT9999":
i = customXmlHandle.coustomAnalysisService.insertRecept(customReception);
break;
case "MT3201":
i= customXmlHandle.arrivedmasterService.insertRecept(customReception);
break;
case "MT5202":
i = customXmlHandle.tallymasterService.insertRecept(customReception);
break;
case "MT5201":
i = customXmlHandle.tallymasterService.insertRecept(customReception);
break;
case "MT4201":
i = customXmlHandle.departuresloadingService.insertRecept(customReception);
break;
case "MT1201":
i = customXmlHandle.coustomAnalysisService.insertRecept(customReception);
break;
case "MT2201":
i = customXmlHandle.preparemasterService.insertRecept(customReception);
break;
case "MT6202":
i = customXmlHandle.inportallocateService.insertRecept(customReception);
break;
case "MT3202":
i = customXmlHandle.allocatearrivalService.insertRecept(customReception);
break;
case "MT8205":
break;
}
return i;
}
public 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.error("解析或其他错误备份文件:"+File.pathSeparator);
}catch (FileExistsException e){
log.error("错误的解析文件剪切失败,目标目录已存在同名文件",e);
e.printStackTrace();
}catch (Exception e){
e.printStackTrace();
}
}
}