G2X81Notice.groovy 7.4 KB
package com.sy.groovy

import com.alibaba.fastjson.JSON
import com.alibaba.fastjson.JSONArray
import com.alibaba.fastjson.JSONObject
import com.sy.model.G2X81Template
import com.sy.model.GatherInfo
import com.sy.model.LandBusinessTypeList
import com.sy.model.LandRoadVe
import com.sy.response.ResultJson
import com.sy.service.CommandLogService
import com.sy.service.EnginCheckService
import com.sy.service.RedisService
import com.sy.service.feigin.G2X81FeignService
import com.sy.service.feigin.StationManageFeignService
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

import java.nio.charset.Charset
import java.text.SimpleDateFormat

/**
 * 金二特殊区域验放
 * 必须返回false 为异步验放 由X82判定抬杆 ,验放排序放最后
 * todo:需要改造成验放型.返回true false,某些业务类型需要强制走金二验放,海关智能卡口配置端也是根据通道进行配置的
 */
class G2X81Notice extends Script implements ChannelCheckScript{
    private final Logger log = LoggerFactory.getLogger(getClass());
    @Override
    Object run() {
        return null
    }


    /**
     * 此接口不做抬杆判定,只做报文通知,返回只有false
     */
    Boolean check(ExecuteParams executeParams) {
        try{
            /**
             * X21通道信息与流转信息比对
             * 1. 从缓存获取车辆进出场申请信息,有流转信息再进行通道对碰.
             *                             无流转信息则说明缓存失效或者二维码不对.
             */
            GatherInfo info = (GatherInfo) executeParams.get("GatherInfo");
            LandRoadVe ve = (LandRoadVe) executeParams.get("LandRoadVe");

            ApplicationContext context = getContext();
            G2X81FeignService g2X81FeignService = context.getBean(G2X81FeignService.class);

            EnginCheckService enginCheckService = context.getBean(EnginCheckService.class);

            /**
             * 写入本地验放通过信息
             */
            enginCheckService.commandlog(info,true,"双验放:本地验放通过:等待金二验放指令",executeParams);



            if (info!=null){
                //缓存X21 的 seqn 需要作为异步X82回执验放时用到的信息
                cacheWithSeqno(info);
                log.info("[G2-X81-CACHE]-车辆[{}]特殊区域流转已缓存[SEQNO]:{}",info.getVename(),info.getSeqno());
                //将X21报文转换成X81格式
                String xmlStr = x22TransToX81(info,ve);
                //调用本地X81申报接口
                ResultJson g2ResultJson =  g2X81FeignService.send(xmlStr);
                log.info("[G2-X81-API-RSP]-金二通知接口返回,code:{},message:{},err:{}",g2ResultJson.getCode(),g2ResultJson.getMsg(),g2ResultJson.getError());
                if ("200".equals(g2ResultJson.getCode())){
                    record(info,true,"已转金二验放-[SEQN]:"+info.getSeqno(),null);
                    log.info("[G2-ROUTER-SUCCESS]-已转金二验放路由成功-规则验放成功-需要忽略下方报错,SEQNO:{}",info.getSeqno());
                }else{
                    log.error("[G2-X81-API-ERR]-金二路由接口访问出错")
                }
            }else {
                record(info,true,"金二验放失败,未有相关通道流转申请信息",null);
                CommandClient.Client(info,"金二验放失败,未有相关通道流转申请信息");
            }

        }catch (Exception e){
            e.printStackTrace();
            log.error("[G2-ROUTER-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 x22TransToX81(GatherInfo gatherInfo,LandRoadVe ve){
        log.info("[SEQN]-处理X21报文:{}",gatherInfo.getSeqno());
        //当前时间作为X81申报时间
        final SimpleDateFormat sdf = new SimpleDateFormat(
                "yyyy-MM-dd HH:mm:ss");
        final  String startTime = sdf.format(new Date());

        //金二场站ID与通道ID替换
        ApplicationContext context = getContext();
        StationManageFeignService stationManageFeignService = context.getBean(StationManageFeignService.class);
        ResultJson resultJson = stationManageFeignService.getChanels(gatherInfo.getChnlno(),1,1);

        if ("200".equals(resultJson.getCode())){
            JSONObject jsonObject = resultJson.getData();
            int total = jsonObject.getInteger("total");
            if (total>0){
                JSONArray jsonArray = jsonObject.getJSONArray("list");
                JSONObject chanel = jsonArray.getJSONObject(0);
                String channelG2 = chanel.getString("channelG2");
                log.info("[G2-CHANNEL]-{}",channelG2);
                JSONObject yard = chanel.getJSONObject("yard");
                String stationG2 = yard.getString("stationIdG2");
                log.info("[G2-YARD]-{}",stationG2);
                log.info("[VE-RFID-NO]:车辆电子车牌号:{}",ve.getIcCode());

                String x81XML= G2X81Template.template.replace("#{ie_flag}",gatherInfo.getIetype())
                        .replace("#{area_id}",stationG2)
                        .replace("#{chnl_no}",channelG2)
                        .replace("#{session_id}",gatherInfo.getSeqno())
                        .replace("#{ve_license_no}",gatherInfo.getVename())
                        .replace("#{gross_wt}",gatherInfo.getGrosswt().toString())
                        .replace("#{rfid_id}",ve.getIcCode())
                        .replace("#{ve_wt}",ve.getSelfWt())
                        .replace("#{operate_time}",startTime);
                log.info("[X21-TO-X81]-{}",x81XML);
                String mqXMLStr = mqXMLMake(x81XML,gatherInfo.getSeqno(),startTime);
                log.info("[X21-TO-X81]-加密后的报文为:{}",mqXMLStr)
                return mqXMLStr;

            }else {
                throw new Exception("未获取到通道金二配置信息")
            }

        }else {
            log.error("场站管理服务接口访问失败")
        }

        return "";
    }

    String mqXMLMake(String xmlStr,String copMsgId,String creatTime){
        String base64EncodeToString = Base64.getEncoder().encodeToString(xmlStr.getBytes(Charset.forName("utf-8")));
        log.info("[SEND-BASE64-ENCODE]-{}",base64EncodeToString)

       return G2X81Template.mq_template.replace("#{CopMsgId}",copMsgId)
        .replace("#{CreatTime}",creatTime)
        .replace("#{MsgType}","JCKK")
        .replace("#{Data}",base64EncodeToString);
    }

    void cacheWithSeqno(GatherInfo info){
        ApplicationContext context = getContext();
        RedisService redisService = context.getBean(RedisService.class);
        if (info!=null && StringUtils.isNotEmpty(info.getSeqno())) {
            redisService.set(info.getSeqno(), JSON.toJSONString(info),60*60*24*3);
        }
    }
}