|
|
package com.tianbo.analysis.handle;
|
|
|
|
|
|
import com.fasterxml.jackson.databind.annotation.JsonAppend;
|
|
|
import com.sun.xml.internal.ws.util.xml.XmlUtil;
|
|
|
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.DocumentHelper;
|
|
|
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;
|
|
|
import java.util.List;
|
|
|
|
|
|
/**
|
|
|
* @author mrz
|
|
|
* @e
|
|
|
* 海关新舱单回执解析xml文件
|
|
|
*/
|
|
|
@Data
|
|
|
@Component
|
|
|
public class CustomXmlHandle{
|
|
|
@Slf4j
|
|
|
public class CustomXmlHandleThread implements Runnable{
|
|
|
|
|
|
private static CustomXmlHandle customXmlHandle;
|
|
|
//备份目录
|
|
|
@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";
|
...
|
...
|
@@ -40,10 +53,10 @@ public class CustomXmlHandle{ |
|
|
|
|
|
//海关新舱单回执报体
|
|
|
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";
|
|
|
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;
|
...
|
...
|
@@ -66,6 +79,7 @@ public class CustomXmlHandle{ |
|
|
@Autowired
|
|
|
private INPORTALLOCATEService inportallocateService;
|
|
|
|
|
|
|
|
|
//通过@PostConstruct实现初始化bean之前进行的操作,解决service调用空指针问题
|
|
|
@PostConstruct
|
|
|
public void init() {
|
...
|
...
|
@@ -83,28 +97,52 @@ public class CustomXmlHandle{ |
|
|
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();
|
|
|
}
|
|
|
|
|
|
}
|
|
|
/**
|
|
|
*
|
|
|
* @param file 回执目录的文件
|
|
|
*
|
|
|
* @return
|
|
|
* @throws DocumentException
|
|
|
* @throws UnsupportedEncodingException
|
|
|
* @throws SAXParseException
|
|
|
* @throws FileNotFoundException
|
|
|
*/
|
|
|
public int handelXmlDocument(File file) throws DocumentException,UnsupportedEncodingException,SAXParseException {
|
|
|
public int handelXmlDocument() throws DocumentException,UnsupportedEncodingException,SAXParseException,FileNotFoundException {
|
|
|
|
|
|
int i = 0;
|
|
|
|
|
|
SAXReader saxReader = new SAXReader();
|
|
|
Document document = saxReader.read(file);
|
|
|
Document document = saxReader.read(xmlfile);
|
|
|
Element contentRoot = document.getRootElement();
|
|
|
|
|
|
//要解析的回执类型
|
|
|
String type= "MT9999";
|
|
|
|
|
|
String flightNo = "";
|
|
|
String flightDate = "";
|
|
|
|
...
|
...
|
@@ -114,7 +152,7 @@ public class CustomXmlHandle{ |
|
|
String journeyid = XMLXPath.getSingleValueByPath(document,JourneyID);
|
|
|
// XMLXPath.getSingleValueByPath(document, )
|
|
|
|
|
|
if(journeyid != null){
|
|
|
if(journeyid != null&& !journeyid.isEmpty()){
|
|
|
|
|
|
String[] flightList = journeyid.split("/");
|
|
|
if(flightList.length > 0){
|
...
|
...
|
@@ -125,13 +163,31 @@ public class CustomXmlHandle{ |
|
|
|
|
|
}
|
|
|
|
|
|
String awbA = XMLXPath.getSingleValueByPath(document,waybillMaster);
|
|
|
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 awbH = XMLXPath.getSingleValueByPath(document,WaybillSecond);
|
|
|
String resCode = XMLXPath.getSingleValueByPath(document,ResponseCode);
|
|
|
String resText = XMLXPath.getSingleValueByPath(document,ResponseText);
|
|
|
String messageId = XMLXPath.getSingleValueByPath(document, MessageID);
|
|
|
CustomReception customReception = new CustomReception(msgType,flightNo,flightDate,awbA,awbH,resText, 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);
|
...
|
...
|
@@ -168,4 +224,19 @@ public class CustomXmlHandle{ |
|
|
|
|
|
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();
|
|
|
}
|
|
|
}
|
|
|
} |
...
|
...
|
|