MessageRouterX22.java 4.4 KB
package com.sy.service.router;

import com.alibaba.fastjson.JSON;
import com.sy.bwAnalysis.CommandInfoAnalysis;
import com.sy.bwAssist.Message;
import com.sy.model.GatherInfo;
import com.sy.model.LandBusinessTypeList;
import com.sy.model.CommandInfoX22;
import com.sy.service.CommandLogService;
import com.sy.service.RedisService;
import com.sy.service.impl.GatherInfoHandle;
import com.sy.socket.CommandClient;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;


@Service("X22")
@Slf4j
public class MessageRouterX22 implements MessageRouter {

    @Value("${g2.switch}")
    private boolean g2Switch;

    @Autowired
    private RedisService redisService;

    @Autowired
    private CommandLogService commandLogService;


    /**
     * 放行的code标识
     */
    private static String CODE_RELEASE_I = "00000000200000000000";
    private static String CODE_RELEASE_E = "00000000100000000000";



    /**
     * 金二抬杆指令处理
     * 1. 金二通道正常可以接收抬杆指令时,与本地联合验放
     * 2. ERROR 或者 金二 TIME_OUT,金二通道异常 接收不到放行指令,使用金二开关,进行本地验放.
     * @param message
     */
    @Override
    public void route(Message message) {
        try {
            log.info("处理X22:CommandInfo");
            CommandInfoAnalysis commandInfoAnalysis = new CommandInfoAnalysis();
            CommandInfoX22 commandInfoX22  = commandInfoAnalysis.toJavaBean(message);

            if (commandInfoX22!=null){
                /**
                 *  根据SEQNO 读取缓存 确定是否金二指令
                 */
                GatherInfo gatherInfo = readCacheWithSeqno(commandInfoX22);
                //是金二指令
                if (gatherInfo!=null && gatherInfo.getChnlno().equals(commandInfoX22.getChnlNo())){
                    /**
                     * 1. 判断指令是否放行
                     */
                    if (commandInfoX22.getCheckResult().equals(CODE_RELEASE_I) || commandInfoX22.getCheckResult().equals(CODE_RELEASE_E)) {
                        /**
                         * todo:第一种.收到G2指令直接放行.并记录过卡与流转申请的核销
                         */


                        /**
                         * 第二种,结合本地指令进行放行.
                         */
                        record(gatherInfo,true,commandInfoX22.getOpHint(),null);
                        GatherInfoHandle gatherInfoHandle = new GatherInfoHandle();
                        gatherInfoHandle.handel(gatherInfo);
                        return;
                    }else {
                        /**
                         * 是金二指令,但是是未放行指令
                         * 记录未放行原因.
                         * 通知卡口
                         */
                        CommandClient.Client(gatherInfo,commandInfoX22.getOpHint());
                        record(gatherInfo,false,gatherInfo.getSeqno()+"-金二验放失败:"+commandInfoX22.getOpHint(),null);
                    }
                }
                else{
                    log.info("[X22-ERROR]:未找到对应的SEQNO:[{}]及通道的过卡缓存数据,不属于金二验放指令,不进行处理",commandInfoX22.getSeqNo());
                }
            }
        }catch (Exception e){
            log.error("[X22-ERROR]:",e);
        }
    }


    /**
     * 读取X21信息,如果走金二,KEY 为SEQN_NO
     * @param info X22实体
     * @return X21实体.
     */
    private GatherInfo readCacheWithSeqno(CommandInfoX22 info){
        if (info!=null && StringUtils.isNotEmpty(info.getSeqNo())) {
            String X21_GatherInfo = redisService.get(info.getSeqNo());
            if (StringUtils.isNotEmpty(X21_GatherInfo)){
                GatherInfo gatherInfo = JSON.parseObject(X21_GatherInfo, GatherInfo.class);
                return gatherInfo;
            }
        }
        return null;
    }

    /**
     * 车辆过卡指令日志记录
     */
    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);
    }

}