TrafficDataConvertHandler.java 11.5 KB
package com.viontech.handler;

import com.alibaba.fastjson.JSONObject;
import com.viontech.constant.RedisConstants;
import com.viontech.utils.DateUtil;
import com.viontech.utils.Gb1400GenerateIDUtil;
import com.viontech.vo.gb1400.MotorVehicleObj;
import com.viontech.vo.gb1400.SubscribeNotificationsObj;
import com.viontech.vo.traffic.TrafficMongoModel;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.web.client.RestTemplate;

import java.util.*;

@Slf4j
@Component
public class TrafficDataConvertHandler {
    @Value("${fanxing.getPicInfo.url:}")
    private String getPicInfoUrl;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private Gb1400GenerateIDUtil gb1400GenerateIDUtil;
    @Autowired
    private Properties wflxProperties;
    @Autowired
    private Properties csysProperties;
    @Autowired
    private Properties cllxProperties;
    @Autowired
    private Properties clppProperties;
    @Autowired
    private Properties hpzlProperties;
    @Autowired
    private Properties hpysProperties;

    public SubscribeNotificationsObj trafficIllegalToSubscribeNotifications(List<TrafficMongoModel> trafficMongoModels) {
        if (CollectionUtils.isEmpty(trafficMongoModels)) {
            log.warn("trafficMongoModels isEmpty");
            return null;
        }
        SubscribeNotificationsObj result = new SubscribeNotificationsObj();
        SubscribeNotificationsObj.SubscribeNotificationListObject resultListObject = new SubscribeNotificationsObj.SubscribeNotificationListObject();
        List<SubscribeNotificationsObj.SubscribeNotificationObject> subNotifList = new ArrayList<>();

        SubscribeNotificationsObj.SubscribeNotificationObject subNotifObj = new SubscribeNotificationsObj.SubscribeNotificationObject();
        JSONObject subscribeInfo = getIllegalSubscribeInfo();
        if (StringUtils.isBlank(subscribeInfo.getString("SubscribeID"))) {
            log.warn("未查询到订阅信息!");
            return null;
        }
        subNotifObj.setNotificationID(gb1400GenerateIDUtil.generateNotificationID());
        subNotifObj.setSubscribeID(subscribeInfo.getString("SubscribeID"));
        subNotifObj.setTitle(subscribeInfo.getString("Title"));
        subNotifObj.setTriggerTime(DateUtil.format(new Date(), DateUtil.DATE_TIME_FORMAT_NO_DELIMITER));

        MotorVehicleObj vehicleObj = new MotorVehicleObj();
        List<MotorVehicleObj.MotorVehicleObject> vehicleList = new ArrayList<>();
        StringBuilder infoIds = new StringBuilder();
        //设置违法数据
        for (TrafficMongoModel trafficMongoModel : trafficMongoModels) {
            MotorVehicleObj.MotorVehicleObject vehicle = tranferToMotorVehicleObject(trafficMongoModel);

            vehicleList.add(vehicle);
            if ("".equals(infoIds.toString())) {
                infoIds.append(vehicle.getMotorVehicleID());
            } else {
                infoIds.append("," + vehicle.getMotorVehicleID());
            }
        }

        vehicleObj.setMotorVehicleObject(vehicleList);
        subNotifObj.setInfoIDs(infoIds.toString());
        subNotifObj.setMotorVehicleObjectList(vehicleObj);

        subNotifList.add(subNotifObj);
        resultListObject.setSubscribeNotificationObjects(subNotifList);
        result.setSubscribeNotificationListObject(resultListObject);
        return result;
    }

    /**
     * 转成MotorVehicleObject对象
     * @param trafficMongoModel
     * @return
     */
    private MotorVehicleObj.MotorVehicleObject tranferToMotorVehicleObject(TrafficMongoModel trafficMongoModel) {
        String passTime = "";
        String picUrl = "";
        String illCode = "";
        String deviceID = "";
        Map eventData = trafficMongoModel.getEvent_data();
        List pics = trafficMongoModel.getPics();
        if (eventData == null || eventData.get("device") == null || eventData.get("illegal") == null || CollectionUtils.isEmpty(pics)) {
            log.warn("缺少必填数据!");
            return null;
        }

        try {
            String eventDateStr = trafficMongoModel.getEvent_dt();
            Date eventDateUtc = DateUtil.parse(eventDateStr, DateUtil.TIMESTAMP_FORMAT);
            passTime = DateUtil.format(DateUtil.addHours(eventDateUtc, 8), DateUtil.DATE_TIME_FORMAT_NO_DELIMITER);
            //图片
            Map picMap = (Map) pics.get(0);
            String pic_unid = (String) picMap.get("pic_unid");
            picUrl = getPicUrlByPicUnid(pic_unid);
//          Map object_rect = (Map) picMap.get("object_rect");
            //设备信息
            Map deviceMap = (Map) eventData.get("device");
            deviceID = (String) deviceMap.get("code");
            //违法代码
            Map illegalMap = (Map) eventData.get("illegal");
            illCode = (String) illegalMap.get("code");
        } catch (Exception e) {
            log.error("获取必须字段异常Exception", e);
        }

        if (StringUtils.isBlank(picUrl) || StringUtils.isBlank(illCode) || StringUtils.isBlank(deviceID) || StringUtils.isBlank(passTime)) {
            log.warn("获取必填数据失败!");
            return null;
        }

        Integer laneNo = 1;
        String hasPlate = "1";
        String plateNo = "-";
        String plateClass = "99";
        String plateColor = "99";
        String csys = "";
        String cllx = "";
        String clpp = "";
        try {
            //车道
            Map laneMap = (Map) eventData.get("lane");
            if (laneMap != null) {
                laneNo = (Integer) laneMap.get("number");
            }
            //车牌
            Map vehicleMap = (Map) eventData.get("vehicle");
            if (vehicleMap != null) {
                Map plateMap = (Map) vehicleMap.get("plate");
                if (plateMap != null) {
                    String text = (String) plateMap.get("text");
                    if (StringUtils.isBlank(text) || "无牌".equals(text)) {
                        hasPlate = "0";
                    } else {
                        plateNo = text;
                    }
                    plateColor = hpysProperties.getProperty((String) plateMap.getOrDefault("color_code", "9"), "99");
                    plateClass = hpzlProperties.getProperty((String) plateMap.getOrDefault("type_code", "99"), "99");
                }
                //车辆信息
                Map bodyMap = (Map) vehicleMap.get("body");
                if (bodyMap != null) {
                    csys = getCsysByVehicleBody(bodyMap, "");
                    cllx = getCllxByVehicleBody(bodyMap, "");
                    clpp = getClppByVehicleBody(bodyMap, "");
                }
            }
        } catch (Exception e) {
            log.error("获取车辆信息异常", e);
        }

        MotorVehicleObj.MotorVehicleObject vehicle = new MotorVehicleObj.MotorVehicleObject();
        //基本信息
        vehicle.setInfoKind(1);
        vehicle.setDeviceID(deviceID);
        vehicle.setPassTime(passTime);
        vehicle.setSourceID(gb1400GenerateIDUtil.generateSourceID(vehicle.getDeviceID(), "02", vehicle.getPassTime()));
        vehicle.setMotorVehicleID(gb1400GenerateIDUtil.generateMotorVehicleID(vehicle.getSourceID(), "02"));

        vehicle.setBreakRuleMode(wflxProperties.getProperty(illCode, illCode));
        vehicle.setStorageUrl1(picUrl);
        vehicle.setLeftTopX(0);
        vehicle.setLeftTopY(0);
        vehicle.setRightBtmX(0);
        vehicle.setRightBtmY(0);
        vehicle.setLaneNo(laneNo);
        vehicle.setHasPlate(hasPlate);
        vehicle.setPlateClass(plateClass);
        vehicle.setPlateColor(plateColor);
        vehicle.setPlateNo(plateNo);
//        vehicle.setDirection("");
        vehicle.setVehicleClass(cllx);
        vehicle.setVehicleBrand(clpp);
//        vehicle.setVehicleStyles("");
        vehicle.setVehicleColor(csys);
        return vehicle;
    }

    /**
     * 获取图片url
     * @param pic_unid
     * @return
     */
    private String getPicUrlByPicUnid(String pic_unid) {
        String geturl = getPicInfoUrl + pic_unid;
        ResponseEntity<String> responseEntity = restTemplate.getForEntity(geturl, String.class);
        if (responseEntity.getStatusCodeValue() == 200) {
            String picget = responseEntity.getBody();
            log.info("图片服务查询响应videoget={}", picget);
            if (StringUtils.isNotBlank(picget)) {
                JSONObject picJson = JSONObject.parseObject(picget);
                if (picJson != null && StringUtils.isNotBlank(picJson.getString("pic_url"))) {
                    return picJson.getString("pic_url");
                }
            }
        } else {
            log.info("error-------------图片服务查询响应失败");
        }
        return "";
    }

    /**
     * 获取违法数据的订阅信息
     * @return
     */
    private JSONObject getIllegalSubscribeInfo() {
        List<Object> values = redisTemplate.opsForHash().values(RedisConstants.GB1400_SUBSCRIBES_HASH);
        if (!CollectionUtils.isEmpty(values)) {
            for (Object value : values) {
                String data = (String) value;
                JSONObject dataObj = JSONObject.parseObject(data);
                String subscribeDetail = dataObj.getString("SubscribeDetail");
                if (StringUtils.isBlank(subscribeDetail)) {
                    continue;
                }
                String[] subArr = subscribeDetail.split(",");
                List<String> subList = Arrays.asList(subArr);
                if (subList.contains("50")) {
                    return dataObj;
                }
            }
        }
        return new JSONObject();
    }

    private String getCsysByVehicleBody(Map bodyMap, String defaultValue) {
        try {
            Map colorMap = (Map) bodyMap.get("color");
            String csys = (String) colorMap.getOrDefault("code", "Z");
            return csysProperties.getProperty(csys, defaultValue);
        } catch (Exception e) {
            log.error("getCsysByVehicleBody.Exception:{}", e.getMessage());
        }
        return defaultValue;
    }

    private String getCllxByVehicleBody(Map bodyMap, String defaultValue) {
        try {
            Map typeMap = (Map) bodyMap.get("type");
            String cllx = (String) typeMap.getOrDefault("code", defaultValue);
            return cllxProperties.getProperty(cllx, defaultValue);
        } catch (Exception e) {
            log.error("getCllxByVehicleBody.Exception:{}", e.getMessage());
        }
        return defaultValue;
    }

    private String getClppByVehicleBody(Map bodyMap, String defaultValue) {
        try {
            Map logoMap= (Map) bodyMap.get("logo");
            String clpp = (String) logoMap.getOrDefault("code", defaultValue);
            return clppProperties.getProperty(clpp, defaultValue);
        } catch (Exception e) {
            log.error("getClppByVehicleBody.Exception:{}", e.getMessage());
        }
        return defaultValue;
    }

    public static void main(String[] args) {
        Integer laneNo = 1;
        String xxx = "";
        Map eventData = new HashMap();
        Map laneMap = new HashMap();
        laneNo = (Integer) laneMap.get("number");
        xxx = (String) laneMap.getOrDefault("xxx", "99");
        System.out.println(laneNo);
    }
}