LockNoticeCheck.groovy 11.9 KB
package com.sy.groovy

import com.sy.mapper.LandListDao
import com.sy.model.GatherInfo
import com.sy.model.LandBusinessTypeList
import com.sy.model.LandList
import com.sy.model.LockFeignResponse
import com.sy.model.NoticeLock
import com.sy.service.CommandLogService
import com.sy.service.CustomsLockService
import com.sy.service.LandBusListService
import com.sy.service.RedisService
import com.sy.service.feigin.LockFeignService
import com.sy.socket.CommandClient
import org.apache.commons.lang.StringUtils
import org.basis.enhance.groovy.entity.ExecuteParams
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.context.ApplicationContext

/**
 * 关锁验放
 * 关锁通知验放规则
 */
class LockNoticeCheck extends Script implements ChannelCheckScript {
    private final Logger log = LoggerFactory.getLogger(getClass());
    /**
     * 需要通知
     */
    private final String NEED_NOTICE = "Y";
    /**
     * 不需要通知
     */
    private final String DOT_NOTICE = "N";
    /**
     * 验证失败
     */
    private final String FAIL_NOTICE = "F";
    @Override
    Object run() {
        return null
    }

    @Override
    Boolean check(ExecuteParams executeParams) {
        try{
            /**
             * 入场标识
             */
            String IN_TYPE="I";
            /**
             * 离场标识
             */
            String OUT_TYPE="E";
            GatherInfo info = (GatherInfo) executeParams.get("GatherInfo");
            // 获取容器中的bean
            // 调用方法
            ApplicationContext context = getContext();
            CustomsLockService customsLockService = context.getBean(CustomsLockService.class);
            LandListDao landListDao = context.getBean(LandListDao.class);
            LockFeignService lockFeignService = context.getBean(LockFeignService.class);

            //判定是否需要关锁业务通知
            String checkResult = lockNoticeCheck(info,executeParams);
            if (NEED_NOTICE.equals(checkResult)){
                /**
                 * 关锁号申请检查
                 * 根据二维码查申请是否有关锁信息,没有查到实体返回null
                 */
                LandList landList = landListDao.selectLockInfoByBarcode(info.getBarcode());
                if (landList!=null && StringUtils.isNotEmpty(landList.getLockNum())) {
                    log.info("[LOCK-CHECK]-流转申请携带关锁,二维码:{}",info.getBarcode());

                    NoticeLock noticeLock = new NoticeLock();
                    noticeLock.barcode = info.getBarcode();
                    noticeLock.areaId = info.getAreaid();
                    noticeLock.chnlNo = info.getChnlno();
                    noticeLock.vehicleNo = info.getVename();

                    //1. 判定是上锁通知还是解锁通知
                    if (OUT_TYPE.equals(info.getIetype()) && customsLockService.lockCheck(info)) {
                        //写入缓存
                        customsLockService.cacheWrite(info);
                        //接口通知
                        noticeLock.lockNo =landList.getLockNum();
                        noticeLock.type = "1";
                        LockFeignResponse lockFeignResponse = lockFeignService.noticeLock(noticeLock);
                        log.info("[LOCK-API-RSP]-关锁通知接口返回,code:{},message:{},success:{}",lockFeignResponse.code,lockFeignResponse.message,lockFeignResponse.success);
                        record(info,false,"关锁施封通知中,等待下一步指令",null);

                        /**
                         * 因为关锁业务为异步,实际上这里的返回是进行关锁通知成功
                         * 但是要返回false,抬杆指令判定才能中断
                         * 后续等待关锁服务从接口进行通知并抬杆验放
                         */
                        return false;
                    }else {
                        if (IN_TYPE.equals(info.getIetype()) && customsLockService.unLockCheck(info)) {
                            customsLockService.cacheWrite(info);

                            //接口通知
                            noticeLock.lockNo =landList.getLockNum();
                            noticeLock.type = "2";
                            LockFeignResponse lockFeignResponse = lockFeignService.noticeLock(noticeLock);
                            log.info("[LOCK-API-RSP]-关锁通知接口返回,code:{},message:{},success:{}",lockFeignResponse.code,lockFeignResponse.message,lockFeignResponse.success);
                            record(info,false,"关锁解封通知中,等待下一步指令",null);
                            return false;
                        }
                    }

                    /**
                     * 无需上锁,无需解锁,则返回true
                     * 继续抬杆指令判定.
                     */
                    return true;

                }else {
                    log.error("[LockNoticeCheck-ERR]:流转业务-未申请关锁号");
                    CommandClient.Client(info,"流转业务-未申请关锁号");
                    record(info,false,"业务异常:流转业务-未申请关锁号",null);
                    //这里需要返回true,是关锁业务,但是中断,不抬杆不放行,给予关锁业务异常通知
                    return false;
                }
            }
            if (DOT_NOTICE.equals(checkResult)){
                /**
                 * 不进行关锁通知,
                 * 所以可以继续走验放流程,进行自动抬杆
                 */
                return true;
            }
            //关锁通知验放失败
            record(info,false,"业务异常:流转业务-关锁验放失败",null);
            return false;
        }catch (Exception e){
            e.printStackTrace();
            log.error("[LockNoticeCheck-ERR]:",e);
            return false;
        }
    }

    // 获取spring容器
    ApplicationContext getContext() {
        // 获取spring IOC容器
        ApplicationContext context = applicationContext;
        return context;
    }

    void record(GatherInfo info, boolean result, String reason, LandBusinessTypeList landBusinessTypeList){
        ApplicationContext context = getContext();
        CommandLogService commandLogService = context.getBean(CommandLogService.class);
        commandLogService.commandlog(info,result,reason,landBusinessTypeList,null,0.0,0.0,0.0,0.0);
    }

    String lockNoticeCheck(GatherInfo info,ExecuteParams executeParams) {
        if (info!=null && StringUtils.isNotEmpty(info.getBarcode())) {

            //todo:这里后期判定可以复用场站白名单验证
//            FileTool.readProperties("station").contains(info.getAreaid());
            /**
             * 申请业务类型检查
             * 调拨业务及分拨业务装载货物为单证的,必须有关锁号
             * 就算申请信息里面没关锁号也要通知
             */
            LandBusinessTypeList landBusinessTypeList = (LandBusinessTypeList) executeParams.get("ChanelFormInfo");
            if (landBusinessTypeList!=null){
                if ("调拨业务".equals(landBusinessTypeList.getBusinesstype())
                        || "分拨业务".equals(landBusinessTypeList.getBusinesstype())
                        || "区港分拨".equals(landBusinessTypeList.getBusinesstype())
                        || "区内分拨".equals(landBusinessTypeList.getBusinesstype())
                ) {

                    String sfAreaid = "4600541001";
                    String zbqAreaid = "4600329012";
                    String zbqN3Areaid = "4612199001";
                    String xhAreaid = "4604000000";

                    //北货三号卡口出通道
                    String bh_3_out = "4604600030";
                    //北货三号卡口进通道1
                    String bh_3_in1= "4604601030";
                    //北货三号卡口进通道2
                    String bh_3_in2= "4604601031";
                    // 西货站出通道1
                    String xh_1_out = "4604333312";
                    // 西货站出通道2
                    String xh_2_out = "4604333322";

                    ApplicationContext context = getContext();
                    // 获取容器中的bean
                    RedisService redisService = context.getBean(RedisService.class);

                    String carEndstationList = redisService.get(info.getVename() + "_endstationList");
                    if (StringUtils.isNotEmpty(info.getVename()) && StringUtils.isNotEmpty(carEndstationList)){

                        //涉及综保区场站的申请不核验关锁业务.
                        if (carEndstationList.contains(zbqAreaid)) {
                            if ("分拨业务".equals(landBusinessTypeList.getBusinesstype())
                                    || "区港分拨".equals(landBusinessTypeList.getBusinesstype())
                                    || "区内分拨".equals(landBusinessTypeList.getBusinesstype())
                            ){
                                if (info.getChnlno().equals(bh_3_out)) {
                                    log.info("[LOCK-CHECK-TRUE]-分拨业务-综保区北货流转业务,北货区域关锁通知,当前场站北货站");
                                    return NEED_NOTICE;
                                }
                                if (info.getAreaid().equals(xhAreaid)) {
                                    log.info("[LOCK-CHECK-TRUE]-分拨业务-西货综保区流转业务,西货区域关锁通知,当前场站西货站");
                                    return NEED_NOTICE;
                                }
                            }
                            if ("调拨业务".equals(landBusinessTypeList.getBusinesstype())){
                                //申请设计综保区的流转场站,除了综保区不通知,其他场站正常通知
                                /**
                                 *  从综保区往北货或者西货调拨时,都不进行关锁通知,因为从综保区出来没有关锁施封记录无法到北货解封.没关锁秘钥,采用人工解封
                                 *  从西货或者北货往综保区调拨的话,进行施封通知,到综保区后不进行解封通知,采用人工解封
                                 */


                                if (info.getChnlno().equals(bh_3_out) ||  info.getChnlno().equals(xh_1_out) ||  info.getChnlno().equals(xh_2_out)){
                                    log.info("[LOCK-CHECK]-含综保区调拨业务-关锁施封通知,当前通道:{}",info.getChnlno());
                                    return NEED_NOTICE;
                                }
                            }
                            log.info("[LOCK-CHECK-NOTICE]-申报场站列表包含综保区,不处理关锁业务,当前场站:{}",landBusinessTypeList.getEndstation());
                            return DOT_NOTICE;
                        }

                        //综保区场站及内三不通知,顺丰场站不通知
                        if (zbqAreaid.equals(landBusinessTypeList.getEndstation())
                                || zbqN3Areaid.equals(landBusinessTypeList.getEndstation())
                                || sfAreaid.equals(landBusinessTypeList.getEndstation())
                        ) {
                            log.info("[LOCK-CHECK]-综保区及内三场站,顺丰场站,不处理关锁业务,当前场站:{}",landBusinessTypeList.getEndstation());
                            return DOT_NOTICE;
                        }else {
                            return NEED_NOTICE;
                        }
                    }else {
                        return FAIL_NOTICE;
                    }
                }
            }else {
                log.info("[LOCK-CHECK]-无对应进出场申请");
                return FAIL_NOTICE;
            }
        }
        return FAIL_NOTICE;
    }
}