审查视图

src/main/java/com/tianbo/analysis/handle/CustomXmlHandleThread.java 17.8 KB
朱兆平 authored
1 2
package com.tianbo.analysis.handle;
3
import com.tianbo.analysis.model.*;
4
import com.tianbo.analysis.service.CustomMessageService;
5
import com.tianbo.analysis.tools.AWBTools;
朱兆平 authored
6
import com.tianbo.util.Date.DateUtil;
朱兆平 authored
7
import com.tianbo.util.XML.XMLXPath;
朱兆平 authored
8 9 10
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileExistsException;
朱兆平 authored
11
import org.apache.commons.io.FileUtils;
12
import org.apache.commons.lang.StringUtils;
朱兆平 authored
13 14 15 16
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
朱兆平 authored
17
import org.springframework.beans.factory.annotation.Value;
朱兆平 authored
18 19 20 21 22
import org.springframework.stereotype.Component;
import org.xml.sax.SAXParseException;

import javax.annotation.PostConstruct;
import java.io.File;
朱兆平 authored
23
import java.io.FileNotFoundException;
朱兆平 authored
24
import java.io.IOException;
朱兆平 authored
25
import java.io.UnsupportedEncodingException;
26
import java.util.concurrent.CountDownLatch;
朱兆平 authored
27 28 29 30 31 32

/**
 * @author mrz
 * @e
 * 海关新舱单回执解析xml文件
 */
朱兆平 authored
33
@Data
朱兆平 authored
34
@Component
朱兆平 authored
35 36
@Slf4j
public class CustomXmlHandleThread implements Runnable{
朱兆平 authored
37
38
    private CountDownLatch latch;
朱兆平 authored
39 40 41 42 43 44 45 46
    //备份目录
    @Value("${custom.receptBakDir}")
    private String bakupDir;

    //解析出错转移目录
    @Value("${custom.errBakDir}")
    private String errBakDir;
47 48 49 50
    //回执转发目录
    @Value("${custom.transmitDir}")
    private String transmitDir;
朱兆平 authored
51 52 53 54
    //匹配技术回执正则
    @Value("${custom.delTechnologyReceptMatch}")
    private String delTechnologyReceptMatch;
朱兆平 authored
55
    private File xmlfile;
56
朱兆平 authored
57
    private  static CustomXmlHandleThread customXmlHandle;
朱兆平 authored
58 59 60 61 62 63 64 65 66
    //海关新舱单回执报头
    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";
67 68 69 70

    /**
     * 海关普通业务新舱单回执报体
     */
朱兆平 authored
71
    public final static String JourneyID =      "//Manifest/Response/BorderTransportMeans/JourneyID";
朱兆平 authored
72 73
    public final static String WaybillMaster =  "//Manifest/Response/Consignment/TransportContractDocument/ID";
    public final static String WaybillSecond =  "//Manifest/Response/Consignment/AssociatedTransportDocument/ID";
74 75
    public final static String ResponseCode =   "//ResponseType/Code";
    public final static String ResponseText =   "//ResponseType/Text";
朱兆平 authored
76
77 78 79 80 81
    /**
     * 海关国际转运业务回执报体
     */
    public final static String ImportJourneyID = "//Manifest/Response/ImportInformation/BorderTransportMeans/JourneyID";
    public final static String ImportWaybillMaster = "//Manifest/Response/ImportInformation/Consignment/TransportContractDocument/ID";
82
    public final static String ImportWaybillSecond = "//Manifest/Response/ImportInformation/Consignment/AssociatedTransportDocument/ID";
83 84
    public final static String ExportJourneyID = "//Manifest/Response/ExportInformation/BorderTransportMeans/JourneyID";
    public final static String ExportWaybillMaster = "//Manifest/Response/ExportInformation/Consignment/TransportContractDocument/ID";
85
    public final static String ExportWaybillSecond = "//Manifest/Response/ExportInformation/Consignment/AssociatedTransportDocument/ID";
86 87 88 89
//    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";
90
朱兆平 authored
91
朱兆平 authored
92 93 94 95
    //通过@PostConstruct实现初始化bean之前进行的操作,解决service调用空指针问题
    @PostConstruct
    public void init() {
        customXmlHandle = this;
朱兆平 authored
96 97 98 99

        customXmlHandle.bakupDir = this.bakupDir;

        customXmlHandle.errBakDir = this.errBakDir;
朱兆平 authored
100
朱兆平 authored
101 102
        customXmlHandle.delTechnologyReceptMatch = this.delTechnologyReceptMatch;
103 104
        customXmlHandle.transmitDir = this.transmitDir;
朱兆平 authored
105 106 107
        // 初使化时将已静态化的testService实例化
    }
朱兆平 authored
108 109
    @Override
    public void run() {
朱兆平 authored
110 111 112 113 114 115 116 117 118 119
        String filename = xmlfile.getName();
        log.info("线程:{}开始",filename);
        if(filename.matches(customXmlHandle.delTechnologyReceptMatch)){
            log.warn("{}报文为技术回执,剪切到错误备份目录,不解析",filename);
            errBak(xmlfile);
            log.info("{}报文为技术回执,剪切备份成功..开始解析下一文件,当前线程即将结束",filename);
        }
        else
        {
            try{
120 121
                //解析前先转发
                FileUtils.copyFileToDirectory(xmlfile, new File(customXmlHandle.transmitDir));
朱兆平 authored
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
                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);
朱兆平 authored
137
                errBak(xmlfile);
朱兆平 authored
138
                e.printStackTrace();
朱兆平 authored
139
            }
朱兆平 authored
140
朱兆平 authored
141 142
        }
143 144 145
        log.info("线程:{}结束",xmlfile.getName());
        latch.countDown();
        log.info("剩余线程数量{}",latch.getCount());
朱兆平 authored
146
    }
朱兆平 authored
147 148
    /**
     *
朱兆平 authored
149
     *
朱兆平 authored
150 151 152 153
     * @return
     * @throws DocumentException
     * @throws UnsupportedEncodingException
     * @throws SAXParseException
朱兆平 authored
154
     * @throws FileNotFoundException
朱兆平 authored
155
     */
156
    private int handelXmlDocument() throws DocumentException,UnsupportedEncodingException,SAXParseException,FileNotFoundException {
朱兆平 authored
157
申海龙 authored
158
        int i = 0;
159
        String secondSplit = "_";
朱兆平 authored
160
        SAXReader saxReader = new SAXReader();
朱兆平 authored
161
        Document document = saxReader.read(xmlfile);
朱兆平 authored
162 163
        Element contentRoot = document.getRootElement();
164 165
        String flightNo = "UNKONW";
        String flightDate = "20101010";
166
朱兆平 authored
167 168
        //开始解析
        String msgType = XMLXPath.getSingleValueByPath(document,MessageType);
申海龙 authored
169 170
        String journeyid = XMLXPath.getSingleValueByPath(document,JourneyID);
//            XMLXPath.getSingleValueByPath(document, )
171 172

朱兆平 authored
173
        String awbA = XMLXPath.getSingleValueByPath(document,WaybillMaster);
申海龙 authored
174
        //全格式的分单 如 17212345678_ADBD
朱兆平 authored
175 176 177
        String awbH = XMLXPath.getSingleValueByPath(document,WaybillSecond);
        String resCode = XMLXPath.getSingleValueByPath(document,ResponseCode);
        String resText = XMLXPath.getSingleValueByPath(document,ResponseText);
申海龙 authored
178
        String messageId = XMLXPath.getSingleValueByPath(document, MessageID);
朱兆平 authored
179 180 181 182 183 184
        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);
185 186

朱兆平 authored
187 188 189 190 191 192 193 194 195 196 197 198 199
        CustomReception customReception = new CustomReception(  msgType,
                                                                flightNo,
                                                                flightDate,
                                                                awbA,
                                                                awbH,
                                                                resCode,
                                                                resText,
                                                                messageId,
                                                                sendTime,
                                                                sendId,
                                                                reciveId,
                                                                version,
                                                                functionCode);
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218

        /**
         * 如果回执中没有携带航班信息节点,说明是出错报文
         * 到发送日志表根据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);
        }
申海龙 authored
219 220
        switch (msgType){
            case "MT9999":
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
                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();
                    }
252
                }
253 254
                AgentXmlHandle agentXmlHandle=new AgentXmlHandle();
                agentXmlHandle.Http_resolver(customReception);
255
                break;
申海龙 authored
256
            case "MT3201":
257
                if (customReception.getWayBillSecond()!=null && customReception.getWayBillSecond().contains(secondSplit)){
258 259 260 261 262 263
                    ARRIVEDSECONDARY arrivedsecondary = new ARRIVEDSECONDARY(customReception);
                    i=arrivedsecondary.secondAnalysisReception();
                }else {
                    ARRIVEDMASTER arrivedmaster = new ARRIVEDMASTER(customReception);
                    i=arrivedmaster.masterAnalysisReception();
                }
264
                break;
申海龙 authored
265
            case "MT5202":
266
                if (customReception.getWayBillSecond()!=null && customReception.getWayBillSecond().contains(secondSplit)){
267 268 269 270 271 272
                    TALLYSECONDARY tallysecondary = new TALLYSECONDARY(customReception);
                    i=tallysecondary.secondAnalysisReception();
                }else {
                    TALLYMASTER tallymaster= new TALLYMASTER(customReception);
                    i = tallymaster.masterAnalysisReception();
                }
273
                break;
申海龙 authored
274
            case "MT5201":
275
                if (customReception.getWayBillSecond()!=null && customReception.getWayBillSecond().contains(secondSplit)){
276 277 278 279 280 281
                    TALLYSECONDARY tallysecondary = new TALLYSECONDARY(customReception);
                    i=tallysecondary.secondAnalysisReception();
                }else {
                    TALLYMASTER tallymaster= new TALLYMASTER(customReception);
                    i = tallymaster.masterAnalysisReception();
                }
282
                break;
申海龙 authored
283
            case "MT4201":
284 285
                DEPARTURESLOADING departuresloading = new DEPARTURESLOADING(customReception);
                i=departuresloading.masterAnalysisReception();
286
                break;
申海龙 authored
287
            case "MT1201":
288
                if (customReception.getWayBillSecond()!=null && customReception.getWayBillSecond().contains(secondSplit)){
289
                    Originmanifestsecondary originmanifestsecondary = new Originmanifestsecondary(customReception);
290
                    i=originmanifestsecondary.secondAnalysisReception();
291 292 293 294
                }else {
                    ORIGINMANIFESTMASTER originmanifestmaster1201 = new ORIGINMANIFESTMASTER(customReception);
                    i = originmanifestmaster1201.masterAnalysisReception();
                }
295
                break;
申海龙 authored
296
            case "MT2201":
297
                if (customReception.getWayBillSecond()!=null && customReception.getWayBillSecond().contains(secondSplit)){
298 299 300 301 302 303
                    PREPARESECONDARY preparesecondary = new PREPARESECONDARY(customReception);
                    i = preparesecondary.secondAnalysisReception();
                }else {
                    PREPAREMASTER preparemaster1201= new PREPAREMASTER(customReception);
                    i = preparemaster1201.masterAnalysisReception();
                }
304
                break;
305
            case "MT6202":
306 307
                INPORTALLOCATE inportallocate = new INPORTALLOCATE(customReception);
                i = inportallocate.masterAnalysisReception();
308 309
                break;
            case "MT3202":
310
                ALLOCATEARRIVAL allocatearrival = new ALLOCATEARRIVAL(customReception);
311
                i= allocatearrival.masterAnalysisReception();
312 313
                break;
            case "MT8205":
314
                i = transXmlHandel(document,customReception);
315
                break;
316 317 318 319 320 321 322 323
            case "MT8202":
                MANIFESTLOAD manifestload = new MANIFESTLOAD(customReception);
                i= manifestload.secondAnalysisReception();
                break;
            case "MT8203":
                MANIFESTLOSTCHANGE manifestlostchange = new MANIFESTLOSTCHANGE(customReception);
                i= manifestlostchange.secondAnalysisReception();
                break;
朱兆平 authored
324 325 326 327
            case "MT8024":
                PREPAREMASTER preparemaster1201= new PREPAREMASTER(customReception);
                i = preparemaster1201.masterAnalysisReception();
                break;
328
            default:
329
                break;
朱兆平 authored
330 331 332

        }
333
        updateCustomMessage(customReception);
申海龙 authored
334
        return i;
335 336 337 338

    }

    /**
339
     * 国际转运回执处理,特殊业务
340 341 342 343 344 345 346 347
     * @param document
     * @param customReception
     * @return
     */
    private int transXmlHandel(Document document,CustomReception customReception){

        String importJourneyID = XMLXPath.getSingleValueByPath(document,ImportJourneyID);
        String importFlightNo = "UNKONW";
348
        String importFlightDate = "20101010";
349 350 351 352 353 354 355
        if(!StringUtils.isEmpty(importJourneyID)){
            importFlightNo = AWBTools.splitFlightAndDate(importJourneyID)[0];
            importFlightDate = AWBTools.splitFlightAndDate(importJourneyID)[1];
        }


        String importWaybillMaster= XMLXPath.getSingleValueByPath(document,ImportWaybillMaster);
356
        String importWaybillSecond= XMLXPath.getSingleValueByPath(document,ImportWaybillSecond);
357 358 359 360 361 362
        String transResponseCode=XMLXPath.getSingleValueByPath(document,TransResponseCode);
        String transResponseText=XMLXPath.getSingleValueByPath(document,TransResponseText);
        CustomReception transCustomReception = new CustomReception(  customReception.getMessageType(),
                importFlightNo,
                importFlightDate,
                importWaybillMaster,
363
                importWaybillSecond,
364 365 366 367 368 369 370 371
                transResponseCode,
                transResponseText,
                customReception.getMessageID(),
                customReception.getSendTime(),
                customReception.getSenderID(),
                customReception.getReceiverID(),
                customReception.getVersion(),
                customReception.getFunctionCode());
372 373 374
        INTERNATIONALTRANSIT internationaltransit = new INTERNATIONALTRANSIT(transCustomReception);

        int i=internationaltransit.masterAnalysisReception();
375 376 377

        return i;
朱兆平 authored
378
    }
朱兆平 authored
379
380 381 382 383 384
    private int updateCustomMessage(CustomReception customReception){
        CUSTOMSMESSAGE customsmessage = new CUSTOMSMESSAGE(customReception);
        return customsmessage.updateMessageByMessageid();
    }
385
    private void errBak(File file){
朱兆平 authored
386 387 388 389 390
        try {
            String today = DateUtil.getTodayBy_yyyyMMdd();
            String errDirByDay = customXmlHandle.errBakDir + "/" + today;
            File berrDirectory = new File(errDirByDay);
            FileUtils.moveFileToDirectory(file,berrDirectory,true);
391
            log.info("数据库未找到与回执适配的信息备份文件:{}:",file.getName());
朱兆平 authored
392
        }catch (FileExistsException e){
393
            log.error("备份解析错误文件失败,目标文件夹存在同名文件{}",file.getName());
朱兆平 authored
394 395 396 397
        }catch (Exception e){
            e.printStackTrace();
        }
    }
朱兆平 authored
398
朱兆平 authored
399
}