CustomXmlHandle.java 6.3 KB
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.XML.XMLXPath;
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.stereotype.Component;
import org.xml.sax.SAXParseException;

import javax.annotation.PostConstruct;
import java.io.File;
import java.io.UnsupportedEncodingException;
import java.util.List;

/**
 * @author mrz
 * @e
 * 海关新舱单回执解析xml文件
 */
@Component
public class CustomXmlHandle{

    private  static  CustomXmlHandle 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;
        // 初使化时将已静态化的testService实例化
    }

    /**
     *
     * @param file 回执目录的文件
     * @return
     * @throws DocumentException
     * @throws UnsupportedEncodingException
     * @throws SAXParseException
     */
    public int handelXmlDocument(File file) throws DocumentException,UnsupportedEncodingException,SAXParseException {

        int i = 0;

        SAXReader saxReader = new SAXReader();
        Document document = saxReader.read(file);
        Element contentRoot = document.getRootElement();

        //要解析的回执类型
        String type= "MT9999";

        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){

            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);
        CustomReception customReception = new CustomReception(msgType,flightNo,flightDate,awbA,awbH,resText, messageId);
        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, "MT5202");
                break;
            case "MT5201":
                i = customXmlHandle.tallymasterService.insertRecept(customReception, "MT5201");
                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;
    }
}