MessageRouterX21.java 6.1 KB
package com.sy.service.router;

import com.alibaba.fastjson.JSON;
import com.sy.bwAnalysis.GatherInfoAnalysis;
import com.sy.bwAssist.Message;
import com.sy.mapper.LandRouterConfigDao;
import com.sy.model.GatherInfo;
import com.sy.model.LandBusinessTypeList;
import com.sy.model.LandRouterConfig;
import com.sy.service.CommandLogService;
import com.sy.service.LandBusListService;
import com.sy.service.RedisService;
import com.sy.service.impl.GatherInfoHandle;
import com.sy.socket.CommandClient;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service("X21")
public class MessageRouterX21 implements MessageRouter {
    private static final Logger logger = LoggerFactory.getLogger(MessageRouterX21.class);
    /**
     * 金二与本地认证兼容性开关
     * Y 是金二业务必须走金二
     * N 车辆扫二维码或本地和金二都可进出区验放
     */
    @Value("${g2.switch}")
    private boolean g2Switch;

    @Resource
    private LandRouterConfigDao landRouterConfigDao;

    @Autowired
    private LandBusListService landBusListService;

    @Autowired
    private RedisService redisService;

    @Autowired
    private CommandLogService commandLogService;

    @Override
    public void route(Message message) {
        logger.info("处理X21:gatherInfo");
        GatherInfoAnalysis gatherInfoAnalysis = new GatherInfoAnalysis();
        GatherInfo info = gatherInfoAnalysis.toJavaBean(message);

        if (g2Switch){
            //金二判断,业务类型,通道,路由目的地
                routerCheckG2(info);
        }else {
            X21Local(info);
        }

    }

    /**
     * 判断X21报文是否给金二
     * @param info 过卡信息
     *
     * @return true 给金二 false 不给金二
     */
    private void routerCheckG2(GatherInfo info){
        /**
         * 通过业务类型进行核对
         * 1. 获取进出场申请信息,主要获取业务类型
         */
        LandBusinessTypeList landBusinessTypeList = landBusListService.getLandBusinessTypeListByGather(info);
        if (landBusinessTypeList!=null){
            rightChnelCheck(landBusinessTypeList.getBusinesstype(),info);
        }else {

        }


        /**
         * 通过X21的通道进行核对.暂时不用这个
         */
//        LandRouterConfig config = new LandRouterConfig();
//        config.setChanelId(info.getChnlno());
//        config.setBusinessType(landBusinessTypeList.getBusinesstype());
//        config.setChanelType(info.getIetype());
//        config.setGatherReceiver("G2");
//        config.setStatus("Y");
//
//        List<LandRouterConfig> routerConfigs =  landRouterConfigDao.selectByGatherInfo(config);
//        return  !routerConfigs.isEmpty();
    }

    /**
     * 有金二通道配置 ,但是车辆没走金二通道进行验放,用这个方法验证
     * G2_SWITCH 开关为Y的时候.进行此校验.
     * G2_SWITCH 开关为N时此校验永远返回true
     * 金二业务的正确性校验
     * 比如进口提货业务,走金二的应该是出一通道验放金二.但是车辆从出二扫二维码出区了,也是不允许的.
     * 要返回走错通道G2.
     * 逻辑:查询路由表 流转业务类型 目的核放处理方是G2 反向查找得到 通道列表,车辆没走此列表中的通道 视为走错通道.
     * @param businessType  业务类型
     * @param info 过卡信息
     * @return true 走对通道 false 走错通道
     */
    private void rightChnelCheck(String businessType,GatherInfo info){

        List<LandRouterConfig> routerConfigs  = landRouterConfigDao.selectByBussType(businessType);
        //有金二与业务类型绑定
        if (!routerConfigs.isEmpty()){

            //是否走对通道的判定
            for (LandRouterConfig routerConfig : routerConfigs) {

                if (routerConfig.getChanelId().equals(info.getChnlno())){
                    /**
                     * 查找到金二与通道的验放配置
                     */

                    //缓存X21,用来接收海关的x82指令核对.收到X22指令后进行核销此缓存.走金二验放的才缓存
                    cacheWithSeqno(info);
                    //将X21报文发给三宝,让三宝发给金二
                    sendToSample(info);
                    record(info,true,"已转金二验放-[SEQN]:"+info.getSeqno(),null);
                    logger.info("[G2]>>为[金二业务],已将[{}]过卡信息发送与金二",info.getVename());
                    return;
                }
            }

            /**
             * 没适配上 为走错通道,走错通道直接给指令,不走本地验放.
             * 为提高抬杆效率
             */
            //todo:走错通道的回执指令处理
            record(info,false,"走错通道-[SEQN]:"+info.getSeqno(),null);
            logger.info("走错通道");

        }else {
            //没有金二配置,走本地
            X21Local(info);
        }


    }

    /**
     * 如果是金二将报文发送给三宝接口
     */
    private void sendToSample(GatherInfo info){
        CommandClient.gatherInfoBuildAndSend(info);
    }

    /**
     * 缓存X21信息,如果走金二,KEY 为SEQN_NO
     */
    private void cacheWithSeqno(GatherInfo info){
        if (info!=null && StringUtils.isNotEmpty(info.getSeqno())) {
            redisService.set(info.getSeqno(), JSON.toJSONString(info),60*60*24);
        }
    }

    /**
     * 本地处理
     */
    private void X21Local(GatherInfo info){
        GatherInfoHandle gatherInfoHandle = new GatherInfoHandle();
        gatherInfoHandle.handel(info);
    }

    /**
     * 车辆过卡指令日志记录
     */
    private void record(GatherInfo info,boolean result,String reason,LandBusinessTypeList landBusinessTypeList){
        commandLogService.commandlog(info,result,reason,landBusinessTypeList,null,0.0,0.0,0.0,0.0);
    }
}