审查视图

src/main/java/com/sy/service/impl/GatherInfoHandle.java 35.2 KB
朱兆平 authored
1 2 3 4 5
package com.sy.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
6
import com.sy.crossDomain.BuildBarCode;
7
import com.sy.mapper.LandListDao;
朱兆平 authored
8 9 10 11
import com.sy.model.*;
import com.sy.service.*;
import com.sy.socket.CommandClient;
import com.sy.utils.FileTool;
12
import lombok.extern.slf4j.Slf4j;
朱兆平 authored
13 14
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
15 16
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
朱兆平 authored
17
18
import javax.annotation.PostConstruct;
19
import javax.annotation.Resource;
朱兆平 authored
20 21 22 23 24 25 26
import javax.validation.constraints.NotNull;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

import static com.sy.service.impl.ResMessageServiceImpl.toStrArry;
27
@Slf4j
28 29
@Component("GatherInfoHandle")
public class GatherInfoHandle implements GatherInfoService {
朱兆平 authored
30
31
    private static GatherInfoHandle gatherInfoHandle;
朱兆平 authored
32 33 34 35 36 37 38 39 40 41 42 43 44 45

    //逻辑判断后的返回信息定义
    private static String PERMITTHOUGH =        "直接放行";
    private static String GROWSSEXCETION =      "禁止通行,重量不在可控范围";
    private static String NORECORD =            "车辆未备案或者识别错误,车牌号:";
    private static String NO_CHEPAI =           "未识别到车牌号:";
    private static String INPUTSTATION =        "此车辆未做进站申请";
    private static String ENTERSTATION =        "此车辆未做出站申请";
    private static String ISVALID =             "二维码数据异常,请使用正确的二维码数据";
    private static String ERRORWT =             "出起始场站的重量和进目的场站的重量不一致";
    private static String IEPORSE =             "无相对应进出场申请";
    private static String FANGXING=             "有运单未放行";
    private static String FENBO =               "装载运单的分拨申请舱单未通过校验";
    private static String NOGrossWt =           "过磅重量异常";
46
    private static String BLACKLIST=            "车辆已被拉进黑名单,不允许出区";
47
    private static String CHANEL_ERR=           "无对应通道流转申请,走错通道";
48 49
    private static String TUIKU=           "车辆装载有退库货物,人工放行";
    private static String CHECKINNOEMPTY=           "提货业务禁止非空车驶入";
朱兆平 authored
50 51 52


    @Autowired
53
    private NmmsService nmmsService;
朱兆平 authored
54 55 56 57 58

    /**
     *  车辆信息备案表
     */
    @Autowired
59
    private LandRoadVeService veService;
朱兆平 authored
60 61

    @Autowired
62
    private RedisService redisService;
朱兆平 authored
63 64

    @Autowired
65 66 67 68
    private BusnesslistinfoService busnesslistinfoService;

    @Resource
    private LandListDao landListDao;
朱兆平 authored
69
70 71 72
    @Value("${devdebug}")
    private Boolean debug;
73
朱兆平 authored
74 75 76 77
    /**
     * 进出场申请表
     */
    @Autowired
78
    private LandBusListService listService;
朱兆平 authored
79 80 81 82 83

    /**
     * 重量校验算法
     */
    @Autowired
84
    private WeightCheckHandleService weightCheckHandleService;
朱兆平 authored
85 86 87 88 89

    /**
     * 查询运单放行表
     */
    @Autowired
90
    private ResMessageService resMessageService;
朱兆平 authored
91
92 93 94
    /**
     * 指令日志表
     */
朱兆平 authored
95
    @Autowired
96
    private CommandLogService commandLogService;
朱兆平 authored
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

    /**
     * 车辆过卡信息
     */
    private GatherInfo info;

    /**
     * 车辆流转申请表头信息
     */
    private LandBusinessTypeList landBusinessTypeList;

    private List<LandBusinessTypeList> landBusinessTypeListList;

    /**
     * 车辆申请表体信息
     */
    private List<LAND_BUSINEESTYPE_LIST_INFO> listinfos;



    /**
     *  过磅重量
     */
    private double growssWt=0.0;
    /**
     *  车牌号
     */
    private String vaName;

    /**
     *  车辆备案重量
     */
    private double selfWt=0.0;

    /**
     * 初始化装载货物重量
     */
    private double goodsWt=0.0;
    /**
     * 车辆入场时的重量
     */
    private double inWt=0.0;

    /**
     * 计算后的车辆进出场差值
     */
    private double diffVal=0.0;

146 147 148 149
    @PostConstruct
    public void init() {
        gatherInfoHandle = this;
    }
朱兆平 authored
150 151 152 153 154 155 156 157

    @Override
    public int save(GatherInfo info) {
        return 0;
    }

    @Override
    public void handel(GatherInfo gatherInfo) {
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
        try {
            this.info = gatherInfo;
            //获取过磅重量
            growssWt = info.getGrosswt().doubleValue();

            //取车牌号,判定卡口是否取到车牌号
            vaName = info.getVename();


                                                //车牌校验
            if (veNameCheck(vaName)
                    && veRecordCheck()          //车辆备案
                    && LandBusinessFormCheck()  //流转申请校验
                    && weightCheck(info)        //过磅重量校验
            ){

                //  场站白名单放行验证
                if (areaWhiteListCheck()){
                    pass();
177
                    releaseFormCheck();     //流转申请单核销
178 179 180 181
                    return;
                }
                //入区核放
                if(chanelInCheck()){
182 183 184 185 186 187 188 189 190

                    switch (landBusinessTypeList.getBusinesstype()){
                        case "进口转关":
                        case "进口提货":
                            if (gatherInfoHandle.weightCheckHandleService.checkImportAtCheckIN(growssWt, selfWt, goodsWt,inWt)){
                                pass();
                                log.info("[X21-SUCCESS]:[IN]-进口提货放行");
                            }else{
                                sendBw(info,false,CHECKINNOEMPTY,landBusinessTypeList,listinfos);
191
                                log.info("[X21-ERR]:[IN]-进口提货-非空车驶入");
192 193 194 195 196 197
                            }
                            break;
                        default:
                            pass();
                            log.info("[X21-SUCCESS]:[IN]-车辆入区直接放行");
                    }
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
//                    if ("区内分拨".equals(landBusinessTypeList.getBusinesstype())) {
//                        //入区后 核销
//                        //todo:测试注释掉,二维码释放
//                        if (!gatherInfoHandle.debug){
//                            buildBarCode.cancleBarCode(vaName);
//                            log.info("[流转申请]-二维码已释放:"+vaName);
//                        }
//
//                        //分拨申请验放
////                        boolean allocatCheck = checkNmmsAllocate(landBusinessTypeList.getMasterList());
////                        if(!allocatCheck){
////                            log.error("[分拨业务]-分拨申请舱单未通过校验:"+FENBO);
////                            sendBw(info,false,FENBO,landBusinessTypeList,listinfos);
////                            return;
////                        }
//
//                        int rc = gatherInfoHandle.landListDao.releaseBarcode(info.getBarcode());
//                        if (rc>0) {
//                            log.info("[进出场核销]-{}[区内分拨]-流转已核销:{}",vaName,info.getBarcode());
//                        }else{
//                            log.error("[进出场核销-ERROR]-{}[区内分拨]-流转未核销成功:{}",vaName,info.getBarcode());
//                        }
//                        //车辆流转申请缓存删除
//                        releaseCache();
//                    }
228
                    return;
229 230 231 232
                }else{
                    inStationInfo();            //入场信息获取
                    setListinfos();             //流转申请表体获取
233
                    if (goodsWhiteListCheck() && !"调拨业务".equals(landBusinessTypeList.getBusinesstype()) && !"分拨业务".equals(landBusinessTypeList.getBusinesstype())){ //货物类型白名单
234
                        pass();
235 236
                        log.info("[SPECIAL]-特殊业务出区验放通过:{}",landBusinessTypeList.getCocode());
                        releaseFormCheck();     //流转申请单核销
237 238 239 240 241
                        return;
                    }

                    if(checkRelease()){         //出区核放
                        sendBw(info, true,PERMITTHOUGH,landBusinessTypeList,listinfos);
朱兆平 authored
242
                        log.info("[SUCCESS]:{}车辆验放成功出区",info.getVename());
243 244
                        releaseFormCheck();     //流转申请单核销
                    }
朱兆平 authored
245
246
                }
247 248
            }else{
                log.info("[X21-ERROR]:{}车辆验放失败",info.getVename());
249
            }
250
251 252

        }catch (Exception e){
253
            log.error("放行判定异常",e);
朱兆平 authored
254
            CommandClient.Client(gatherInfo, "放行判定程序异常");
255 256 257
        }

朱兆平 authored
258 259 260 261 262 263

    }


    /**
     * 一.
264
     * 场站白名单,顺丰
朱兆平 authored
265 266 267 268 269 270
     * @return 白名单 true. 其他走校验
     */
    private boolean areaWhiteListCheck(){
        if(FileTool.readProperties("stationsf").equals(info.getAreaid())) {
            return true;
        }
271 272 273
        if (zbqAreaWhiteListCheck()) {
            return true;
        }
朱兆平 authored
274 275 276
        return false;
    }
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
    /**
     * 一.
     * 场站白名单,综保区白名单特殊验放
     * 综保区不做重量验放,但是还要进行单证放行验放
     * @return 白名单 true. 其他走校验
     */
    private boolean zbqAreaWhiteListCheck(){
        if(FileTool.readProperties("station").contains(info.getAreaid())) {
            log.info("[ZBQ]-综保区核验放行");
            if ("E".equals(info.getIetype())){

                inStationInfo();            //入场信息获取
                setListinfos();             //流转申请表体获取

                //业务类型及放行判定
                switch (landBusinessTypeList.getBusinesstype()){
                    case "进口提货":
                        if ("退库货物".equals(landBusinessTypeList.getCocode()) || "换单货物".equals(landBusinessTypeList.getCocode())){
                            return true;
                        }else {
                            //检查运单放行
                            if (gatherInfoHandle.resMessageService.checkManifestRelease(info,listinfos)){
                                return true;
                            }else {
                                //有运单未放行
                                log.error("[进口提货]-出场未通过校验:"+FANGXING+landBusinessTypeList.getMasterList());
                                sendBw(info,false,FANGXING,landBusinessTypeList,listinfos);
                                return false;
                            }
                        }

                    case "调拨业务":
309
                        if (goodsWhiteListCheck()){
310 311
                            return true;
                        }else {
312 313 314 315 316 317 318 319 320
                            //检查运单放行,拉板可出
                            if (gatherInfoHandle.resMessageService.checkManifestRelease(info,listinfos)){
                                return true;
                            }else {
                                //有运单未放行
                                log.error("[调拨业务]-出场未通过校验:"+FANGXING+landBusinessTypeList.getMasterList());
                                sendBw(info,false,FANGXING,landBusinessTypeList,listinfos);
                                return false;
                            }
321
                        }
322
323 324 325 326 327 328 329 330 331 332 333
                    default:
                        return true;
                }
            }
            return true;
        }
        return false;
    }


朱兆平 authored
334 335 336 337 338 339 340

    /**
     * 1. 车牌信息校验
     */
    private boolean veNameCheck(String veName){
        //1. 若无车牌号,返回未识别错误
        if (StringUtils.isEmpty(vaName)) {
341
            log.error(NO_CHEPAI+vaName);
342
            sendBw(info,false,NO_CHEPAI + vaName,null,listinfos);
朱兆平 authored
343 344 345 346 347 348 349 350 351 352 353
            //CommandClient.Client(info, NO_CHEPAI + vaName);
            return false;
        }
        return true;
    }

    /**
     * 2. 车辆备案信息校验
     */
    private boolean veRecordCheck(){
        //2. 校验车辆是否备案
354
        LandRoadVe ve = gatherInfoHandle.veService.selectByFrameNo(vaName);
朱兆平 authored
355
        //若无备案信息或者黑名单,返回车辆未备案或者识别错误,
356
        if (ve == null) {
357
            log.error(NORECORD+vaName);
358
            sendBw(info,false,NORECORD + vaName,null,listinfos);
朱兆平 authored
359 360 361 362
            //CommandClient.Client(info, NORECORD + vaName);
            return false;
        }
363 364 365 366
        if (!veBlackListCheck(ve)){
            return false;
        }
朱兆平 authored
367 368 369 370 371 372 373 374 375 376
        //车辆备案重量
        selfWt=Double.parseDouble(ve.getSelfWt());
        return true;

    }

    /**
     * 3. 车辆黑名单校验
     */
    private boolean veBlackListCheck(LandRoadVe ve){
377
        if("Y".equals(ve.getVeState())){
378 379
            log.info(vaName+BLACKLIST);
            sendBw(info,false,vaName+BLACKLIST,null,listinfos);
朱兆平 authored
380 381 382 383 384 385 386 387 388 389 390 391
            return false;
        }
        return true;
    }

    /**
     * 4. 车辆进出场流转申请单信息校验
     * 二维码不用校验了,二维码失效会删除redis缓存
     * @return
     */
    private boolean LandBusinessFormCheck(){
        try {
392
            log.info("车辆-{}核碰缓存",info.getVename());
393
            landBusinessTypeListList = new ArrayList<>();
朱兆平 authored
394
            //通过车牌号,二维码,场站,通道号,进出类型查询进出场站申请列表
395
            if (info==null){
396
                log.error("info 实体为NULL");
397 398 399 400
                return false;
            }

            String landBusinessJson = gatherInfoHandle.redisService.get(info.getVename());
朱兆平 authored
401 402 403
            List<LandBusinessTypeList> list = new ArrayList<>();
            if (StringUtils.isNotEmpty(landBusinessJson)){
404
                 list =  JSONArray.parseArray(landBusinessJson,LandBusinessTypeList.class);
朱兆平 authored
405 406 407 408

                landBusinessTypeListList  = list;
                //4. 若查询结果为null,返回无相对应进出场申请
                if (list == null || list.isEmpty()) {
409
                    log.error(vaName+IEPORSE);
朱兆平 authored
410 411 412 413
                    sendBw(info,false,vaName+IEPORSE,null,listinfos);
                    return false;
                }
414
                for (LandBusinessTypeList item : list) {
朱兆平 authored
415
416 417 418 419
                    if (item.getAisle().equals(info.getChnlno()) && item.getBarcode().equals(info.getBarcode())){
                        landBusinessTypeList = item;
                        //离场装载货物重量
                        goodsWt = Double.parseDouble(landBusinessTypeList.getRemark());
420
                        log.info("车辆-{}的申请缓存信息核碰成功,通道:{}",info.getVename(),info.getChnlno());
421 422 423
                        return true;
                    }
                }
朱兆平 authored
424
425
                //核碰流转申请失败
426
                log.info("[CACHE]:车辆-{}的流转申请校验失败,无对应流转申请信息",info.getVename());
427
                sendBw(info,false,info.getVename()+CHANEL_ERR,null,listinfos);
朱兆平 authored
428
            }else {
429 430 431
                log.error("[CACHE-ERROR]:未找到车辆-{}的申请缓存信息",info.getVename());
                log.info("[CACHE-ERROR]:未找到车辆-{}的申请缓存信息,或者流转已超期失效",info.getVename());
                sendBw(info,false,info.getVename()+IEPORSE+",或者流转已超期失效",null,listinfos);
432
                return false;
朱兆平 authored
433 434
            }
        }catch (Exception e){
435
            e.printStackTrace();
436
            log.error("[CACHE-PARSE-ERROR]:",e);
朱兆平 authored
437 438 439
            return false;
        }
440 441 442 443 444 445 446 447 448 449 450 451
        return false;
    }




    /**
     * 5. 车辆过磅重量校验
     * @return true 通过 false 不通过
     */
    private boolean weightCheck(GatherInfo info){
        if(info.getGrosswt().compareTo(BigDecimal.ZERO) > 0){
452
            log.info("过磅重量校验通过,过磅重量大于0");
453 454 455 456
        }else{
            sendBw(info,false,NOGrossWt + growssWt,new LandBusinessTypeList(),listinfos);
            return false;
        }
朱兆平 authored
457 458 459 460 461
        return true;
    }

    /**
     * 流转申请-货物类型白名单,白名单的货物类型目前直接放行
462
     * @return true 白名单货物直接放行   false 非白名单 需要验放
朱兆平 authored
463 464 465 466
     */
    private boolean goodsWhiteListCheck(){
        if(!"普通货物".equals(landBusinessTypeList.getCocode())
                && !"转关货物".equals(landBusinessTypeList.getCocode())
467 468 469
                && !"退库货物".equals(landBusinessTypeList.getCocode())
                && !"换单货物".equals(landBusinessTypeList.getCocode())
        ) {
朱兆平 authored
470 471 472 473 474 475
            //todo:比如货物类型为快件货物,只走重量校验,不走放行信息等校验.
            //todo:退库货物可以关联货运系统,得到退库信息进行比对
            //todo:查验货物走查验信息比对
            //todo:快件暂不校验直接放行,不过得录单
            //todo:换单货物校验逻辑待定
            //todo:未来取消货物类型判定.
476
            log.info("车辆装载货物为:" + landBusinessTypeList.getCocode());
477
            return true;
朱兆平 authored
478 479 480 481 482
        }
        return false;
    }

    /**
483 484 485
     * todo:通道及金二路由判定,是金二通道的话,等金二X22
     * 目前宽进严出策略,本地判定
     *
朱兆平 authored
486
     */
487 488 489 490
    private boolean chanelInCheck(){
        if ("I".equals(info.getIetype())){
            return true;
        }else {
朱兆平 authored
491 492 493 494 495 496 497 498
            return false;
        }
    }

    /**
     * 离场时需要获取的车辆进场信息,入场重量,进出场差值
     */
    private void inStationInfo(){
499
        List<LandBusinessTypeList> stationInChanleInfo= gatherInfoHandle.listService.selectwt(info.getVename(),info.getBarcode(),info.getAreaid(),"I");
朱兆平 authored
500
        if (stationInChanleInfo.isEmpty()){
501
            log.info("未查询到车辆:{}的入场信息",info.getVename());
朱兆平 authored
502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523
        }else {
            for(LandBusinessTypeList typeList:stationInChanleInfo){
                if(typeList.getAislewt()!=null){
                    //对应场站进场过磅重量
                    inWt=typeList.getAislewt();
                }
                diffVal = inWt- growssWt;
            }

            //TODO:进场校验 增加 车辆备案重量要 <= 进场过磅重量 ,要有误差判定
        }
    }



    /**
     * 此通道对应的流转申请货物重量.
     */
    private void setGoodsWt(LandBusinessTypeList list){

    }
524
朱兆平 authored
525 526 527 528 529 530

    /**
     * 设置表体全局变量
     */
    private void setListinfos(){
        //查询运单列表
531
        listinfos=gatherInfoHandle.busnesslistinfoService.selectmanilist(info.getBarcode());
朱兆平 authored
532 533 534
    }

    /**
535
     * todo:与金二指令一起核碰
朱兆平 authored
536
     * 出区抬杆放行判定
537
     * @return true 抬杆放行
朱兆平 authored
538 539
     */
    private boolean checkRelease(){
540
        log.info("[进出场申请]-业务类型为:"+landBusinessTypeList.getCocode()+landBusinessTypeList.getBusinesstype());
朱兆平 authored
541 542
        switch (landBusinessTypeList.getBusinesstype()){
            case "空车业务":
543
                if(gatherInfoHandle.weightCheckHandleService.checkEmpty(growssWt,selfWt)){
朱兆平 authored
544 545
                    return true;
                }else{
546
                    log.error("[空车业务]-出场重量未通过校验:"+GROWSSEXCETION);
朱兆平 authored
547 548 549 550 551
                    sendBw(info,false,GROWSSEXCETION,landBusinessTypeList,listinfos);
                    return false;
                }
            case "出口转关":
            case "出口送货":
552
                if (gatherInfoHandle.weightCheckHandleService.checkExportDownLoading(growssWt, selfWt, goodsWt,inWt)){
朱兆平 authored
553 554
                    return true;
                }else{
555
                    log.error("[出口送货]-出场重量未通过校验:"+GROWSSEXCETION);
朱兆平 authored
556 557 558 559 560
                    sendBw(info,false,GROWSSEXCETION,landBusinessTypeList,listinfos);
                    return false;
                }
            case "进口转关":
            case "进口提货":
561
                if (gatherInfoHandle.weightCheckHandleService.checkImportDlv(growssWt, selfWt, goodsWt,inWt)){
562
                    if ("退库货物".equals(landBusinessTypeList.getCocode()) || "换单货物".equals(landBusinessTypeList.getCocode())){
朱兆平 authored
563 564
                        /**
                         * 退库货物不校验运单放行.
565
                         * @chenageNew 退库货物不自动抬杆放行,改为人工验放模式
朱兆平 authored
566
                         */
567 568 569
                        log.info("[退库业务]-人工验放模式.");
                        sendBw(info,false,TUIKU,landBusinessTypeList,listinfos);
                        return false;
朱兆平 authored
570 571
                    }else{
                        //检查运单放行
572
                        if (gatherInfoHandle.resMessageService.checkManifestRelease(info,listinfos)){
朱兆平 authored
573 574 575
                            return true;
                        }else {
                            //有运单未放行
576
                            log.error("[进口提货]-出场未通过校验:"+FANGXING+landBusinessTypeList.getMasterList());
朱兆平 authored
577
                            sendBw(info,false,FANGXING,landBusinessTypeList,listinfos);
朱兆平 authored
578 579 580 581 582
                            return false;
                        }
                    }
                    //todo:装载运单历史数据累加超重判定
                }else{
583
                    log.error("[进口提货]-出场重量未通过校验:"+GROWSSEXCETION);
朱兆平 authored
584 585 586 587
                    sendBw(info,false,GROWSSEXCETION,landBusinessTypeList,listinfos);
                    return false;
                }
            case "分拨业务":
588
                if (gatherInfoHandle.weightCheckHandleService.checkAllocateOrDispatch(growssWt, selfWt, goodsWt,inWt)){
589 590 591 592 593 594 595
                    /**
                     * 分拨业务如果空车出,可出.(就是申请不带运单的)
                     */
                    if (listinfos!=null && listinfos.isEmpty()){
                        log.info("[分拨-空车]-{}空车离场",info.getVename());
                        return true;
                    }
朱兆平 authored
596 597 598
                    //todo:检查分拨申请
                    boolean allocatCheck = checkNmmsAllocate(landBusinessTypeList.getMasterList());
                    if(!allocatCheck){
599
                        log.error("[分拨业务]-分拨申请舱单未通过校验:"+FENBO);
朱兆平 authored
600 601 602 603 604 605
                        sendBw(info,false,FENBO,landBusinessTypeList,listinfos);
                        return false;
                    }
                    return true;
                    //todo:分拨运抵通知,重车入场视为运抵
                }else{
606
                    log.error("[分拨业务]-出场重量未通过校验:"+GROWSSEXCETION);
朱兆平 authored
607 608 609
                    sendBw(info,false,GROWSSEXCETION,landBusinessTypeList,listinfos);
                    return false;
                }
610
            case "区内调拨":
朱兆平 authored
611
            case "调拨业务":
612
                if (gatherInfoHandle.weightCheckHandleService.checkAllocateOrDispatch(growssWt, selfWt, goodsWt,inWt)){
613
                    /**
614
                     * 调拨业务如果空车出,可出.(就是申请不带运单的)
615 616 617 618 619
                     */
                    if (listinfos!=null && listinfos.isEmpty()){
                        log.info("[调拨-空车]-{}空车离场",info.getVename());
                        return true;
                    }
620 621 622

                    //白名单货物不检查放行
                    if (goodsWhiteListCheck()) {
朱兆平 authored
623
                        return true;
624 625 626 627 628 629 630 631 632 633 634
                    }else{
                        //检查运单放行,拉板可出
                        if (gatherInfoHandle.resMessageService.checkManifestRelease(info,listinfos)){
                            return true;
                        }else {
                            //有运单未放行
                            log.error("[调拨业务]-出场未通过校验:"+FANGXING+landBusinessTypeList.getMasterList());
                            sendBw(info,false,FANGXING,landBusinessTypeList,listinfos);
                            return false;
                        }
                        //todo:检查ULD放行.
朱兆平 authored
635
                    }
636
朱兆平 authored
637
                }else{
638
                    log.error("[调拨业务]-出场未通过校验:"+GROWSSEXCETION);
朱兆平 authored
639 640 641 642
                    sendBw(info,false,GROWSSEXCETION,landBusinessTypeList,listinfos);
                    return false;
                }
            default:
643
                log.error("[未知业务类型]-出场未通过校验");
朱兆平 authored
644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675
                break;
        }

        return false;
    }

    /**
     * 记录进出区信息
     */
    private void record(){
        if ("I".equals(info.getIetype())){
            landBusinessTypeList.setAislewt(info.getGrosswt().doubleValue());
            landBusinessTypeList.setUpdateDate(new Date());
            landBusinessTypeList.setRemark(String.format("%.1f", goodsWt));
            //车辆备案重量
            landBusinessTypeList.setRemark2(String.valueOf(selfWt));
            landBusinessTypeList.setContrastflag("已进站");
        }else {
            landBusinessTypeList.setAislewt(info.getGrosswt().doubleValue());
            landBusinessTypeList.setUpdateDate(new Date());
            //装载货物总重量
            landBusinessTypeList.setRemark(String.format("%.1f", goodsWt));
            //进出差值
            landBusinessTypeList.setRemark1(String.format("%.1f", diffVal));
            landBusinessTypeList.setRemark2(String.valueOf(selfWt));
            landBusinessTypeList.setContrastflag("已出站");
        }

        //todo:判定放行后,插入数据库,出入区记录
        landBusinessTypeList.setId(UUID.randomUUID().toString());
        landBusinessTypeList.setIsvalid("1");
        //todo:这里SEQN也要入库
676
        gatherInfoHandle.listService.saveList(landBusinessTypeList);
朱兆平 authored
677 678 679
    }

    /**
680 681 682 683 684 685 686
     * 核销记录
     */
    private void releaseRecord(){
        commandlog(info,true,"流转核销",landBusinessTypeList,null);
    }

    /**
687
     * 流转申请是否进行核销判定
朱兆平 authored
688 689
     */
    private void releaseFormCheck(){
690
        List<LandBusinessTypeList> temp =  landBusinessTypeListList;
朱兆平 authored
691
        //二维码已出区的记录
692 693 694
        List<LandBusinessTypeList> havenCrossList = gatherInfoHandle.listService.selectByBarcodeWithE(info.getBarcode());

        if (landBusinessTypeListList.isEmpty() && havenCrossList.isEmpty()){
朱兆平 authored
695
            log.error("[releaseFormCheck-CACHE-CHECK]-车辆进出场对应申请缓存数据为空:{}",info.getBarcode());
696 697 698 699
            return;
        }

        //申请核销判定,已出区的记录与流转申请数据核碰.如果流转申请列表核碰完,还剩下的需要出区的场站则不核销.
朱兆平 authored
700 701
        for (LandBusinessTypeList businessTypeList : havenCrossList) {
            List<LandBusinessTypeList> r =  landBusinessTypeListList.stream().filter(item ->{
702
                //判断还有没有没有出区的场站,还有没有出区的场站就不核销
朱兆平 authored
703
                log.debug("[releaseFormCheck-Stream-loop]-缓存与已出核碰,缓存元素场站:[{}],已出记录场站:[{}]",item.getEndstation(),businessTypeList.getEndstation());
704 705 706 707
                //解决缓存那里存储有不一样的把人错的的有问题的点,二维码不一致也把缓存删除
                if (!item.getBarcode().equals(businessTypeList.getBarcode())){
                    return true;
                }
朱兆平 authored
708
                 if(item.getEndstation().equals(businessTypeList.getEndstation())) {
朱兆平 authored
709
                     log.info("[releaseFormCheck-Stream-loop]-缓存元素场站:[{}],已出记录场站:[{}]-核销判定对碰成功",item.getEndstation(),businessTypeList.getEndstation());
朱兆平 authored
710 711 712 713 714 715
                     return true;
                 }else {
                     return false;
                 }
            })
            .collect(Collectors.toList());
716
            temp.removeAll(r);
朱兆平 authored
717
        }
朱兆平 authored
718
        log.info("[releaseFormCheck]-核销判定结果:{}",temp.size());
719
        if (temp.isEmpty()){
朱兆平 authored
720
            //todo:流转申请状态核销
721
            log.info("[进出场申请]-流转申请开始核销:"+vaName);
朱兆平 authored
722 723
            //二维码核销
            releaseBarCode();
724
            log.info("[进出场申请]-流转申请已核销:"+vaName);
朱兆平 authored
725 726 727 728 729 730 731
        }

    }

    /**
     * 二维码失效核销->判定及失效通知.
     */
732
    public void releaseBarCode(){
733
        //是否有未驶离的流转记录二维码判定
734
        int count=gatherInfoHandle.listService.selectlaststation(info.getVename(),info.getBarcode());
朱兆平 authored
735 736 737
        //二维码失效通知,已进入场站的与缓存比较.
        if(count==0){
            //todo:测试注释掉,二维码释放
738
            if (!gatherInfoHandle.debug){
739
                BuildBarCode.cancleBarCode(vaName);
740
                log.info("[流转申请]-二维码已释放:"+vaName);
741
            }
742
743
            int rc = gatherInfoHandle.landListDao.releaseBarcode(info.getBarcode());
744 745 746 747 748
            if (rc>0) {
                log.info("[进出场核销]-{}流转已核销:{}",vaName,info.getBarcode());
            }else{
                log.error("[进出场核销-ERROR]-{}流转未核销成功:{}",vaName,info.getBarcode());
            }
749
            //车辆流转申请缓存删除
750 751 752
            releaseCache();
        }else{
            log.error("[进出场核销-ERROR]-{}二维码应该核销,但是不符合核销条件:{}",vaName,info.getBarcode());
朱兆平 authored
753 754 755
        }
    }
756 757 758 759 760 761 762
    private void releaseCache(){
        //车辆流转申请缓存删除
        gatherInfoHandle.redisService.del(info.getVename());
        //流转申请时生成的临时核碰场站代码列表
        gatherInfoHandle.redisService.del(info.getVename()+"_endstationList");
        //车辆过卡信息缓存删除-X22金二判定时候生成的这个缓存
        gatherInfoHandle.redisService.del(info.getSeqno());
763
        log.info("[流转缓存]-{}缓存已核销",vaName);
764 765
        //核销记录
        releaseRecord();
766 767
    }
朱兆平 authored
768 769 770 771 772 773
    /**
     * todo:车单关系绑定报文
     */

    /**
     * 发送X22指令
774 775 776 777 778 779 780
     * @param info          过卡信息
     * @param check         true 抬杆,false 不抬杆
     * @param reason        原因
     * @param land          流转申请表头信息
     * @param list_infos    流转申请表体信息
     * @return              调试模式 直接返回true,生产模式按业务走
     */
781
    public boolean sendBw(GatherInfo info, boolean check, String reason, LandBusinessTypeList land, List<LAND_BUSINEESTYPE_LIST_INFO> list_infos) {
782 783 784 785
        //调试模式 直接返回true
        if (gatherInfoHandle.debug){
            if (check) {
                record();
786 787
                //总进出车次计数
                gatherInfoHandle.redisService.incr("kako-total",1);
788 789 790 791 792
            }
            commandlog(info,check,reason,land,list_infos);
            return check;
        }
793
        log.info(String.format("开始发送指令:车牌%s,场站%s,通道%s,重量%s",info.getVename(),info.getAreaid(),info.getChnlno(),info.getGrosswt()));
朱兆平 authored
794 795 796
        boolean flag = false;
        if (check) {
            CommandClient.Client(info, PERMITTHOUGH);
797
            log.info("=============>>>>>>>>放行报文发送成功<<<<<<<<<==============");
朱兆平 authored
798
            flag = true;
799
            record();
800 801 802

            //总进出车次计数
            gatherInfoHandle.redisService.incr("kako-total",1);
朱兆平 authored
803 804
        } else {
            CommandClient.Client(info, reason);
805
            log.info("=============>>>>>>>>重量异常报文发送成功<<<<<<<<<==============");
朱兆平 authored
806 807 808 809 810 811
        }
        commandlog(info,check,reason,land,list_infos);
        return flag;
    }

    /**
812 813 814 815 816 817 818
     * 直接放行
     */
    private void pass(){
        sendBw(info,true,PERMITTHOUGH,landBusinessTypeList,listinfos);
    }

    /**
朱兆平 authored
819 820 821 822 823 824 825
     * 放行日志记录
     * @param info
     * @param check
     * @param reason
     * @param land
     * @param list_infos
     */
826
    private void commandlog(GatherInfo info,boolean check,String reason,LandBusinessTypeList land,List<LAND_BUSINEESTYPE_LIST_INFO> list_infos){
朱兆平 authored
827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856
        String flag="",type="";
        commandLog command=new commandLog();
        command.setId(UUID.randomUUID().toString());
        command.setBarcode(info.getBarcode());

        if(land!=null){
            command.setBarcode(land.getBarcode());
            command.setBusnessType(land.getBusinesstype());
        }
        command.setAreaId(info.getAreaid());
        command.setChnlNo(info.getChnlno());
        if (check){
            flag = "00";
        } else{
            flag = "11";
        }
        if("I".equals(info.getIetype())){
            type="000000200000000000";
        }else{
            type="000000100000000000";
        }
        command.setReasonCode(flag+type);
        command.setReasonText(reason);
        command.setVeName(info.getVename());
        command.setVeWeight(selfWt);
        command.setIeType(info.getIetype());
        command.setExitGrossWeight(info.getGrosswt().doubleValue());
        command.setInGrossWeight(inWt);
        command.setGoodsWeight(goodsWt);
        command.setActualGoodsWeight(diffVal);
857
        if(list_infos !=null && list_infos.size()>0){
朱兆平 authored
858 859
            command.setMasterList(Arrays.toString(toStrArry(list_infos)));
        }
860
        gatherInfoHandle.commandLogService.insert(command);
朱兆平 authored
861 862 863 864 865 866 867 868
    }

    /**
     * 新舱单分拨申请数据查询
     * @param waybill 查询的运单号
     * @return
     */
    private Map nmmsAllocate(@NotNull String waybill){
869
        log.info("新舱单查询分拨申请数据开始");
朱兆平 authored
870
        Feign_Allocate_Search feignAllocateSearch = new Feign_Allocate_Search(waybill,0,10);
871
        Map map = gatherInfoHandle.nmmsService.getAllocate(feignAllocateSearch);
872
        log.info(""+map);
朱兆平 authored
873 874 875 876 877 878 879 880 881 882 883 884 885
        return map;
    }


    /**
     * 分拨申请查询
     * @param waybill
     * @return
     */
    private boolean checkNmmsAllocate(String waybill){
        if (waybill.length() < 1) {
            return false;
        }
886 887 888
        if (gatherInfoHandle.debug){
            return true;
        }
朱兆平 authored
889 890 891 892
        waybill = waybill.replace("-", "");
        //中文逗号替换
        waybill = waybill.replace(",", ",");
        String[] maifest = waybill.split(",");
893
        log.info("运单列表:" + waybill);
朱兆平 authored
894 895 896 897 898 899 900 901 902 903 904 905
        boolean flag = false;

        for (String awb : maifest) {
            Map map= nmmsAllocate(awb);
            if (map.containsKey("reslut")){
                String result = map.get("reslut").toString();
                JSONObject jsonObject = JSON.parseObject(result);
                if (jsonObject.containsKey("ds")) {
                    JSONArray ds = jsonObject.getJSONArray("ds");
                    JSONObject awbinfo = ds.getJSONObject(0);
                    if (awbinfo.containsKey("RECEIPTINFORMATION")){
                        if (awbinfo.getString("RECEIPTINFORMATION").contains("39301") || awbinfo.getString("RECEIPTINFORMATION").contains("39103")){
906
                            log.info("运单:{},分拨回执查询结果:{}",awb,awbinfo.getString("RECEIPTINFORMATION"));
朱兆平 authored
907
                        }else {
908
                            log.info("运单{}分拨申请回执不正常:{}",awb,awbinfo.getString("RECEIPTINFORMATION"));
朱兆平 authored
909 910 911 912
                            return false;
                        }
                    }
                }else {
913
                    log.info("运单:{}分拨申请回执未查询到,或未进行分拨申请",awb);
朱兆平 authored
914 915 916
                    return false;
                }
            }else{
917
                log.info("运单:{}分拨申请回执未查询到,或未进行分拨申请",awb);
朱兆平 authored
918 919 920 921 922 923 924
                return false;
            }

        }
        return true;
    }
}