TrafficVehicleScheduled.java 4.23 KB
package com.viontech.scheduled;

import com.viontech.constant.Gb1400Constants;
import com.viontech.constant.RedisConstants;
import com.viontech.constant.SystemConstants;
import com.viontech.handler.TrafficDataConvertHandler;
import com.viontech.service.Gb1400Service;
import com.viontech.service.TrafficEventService;
import com.viontech.utils.DateUtil;
import com.viontech.utils.JavaBean2Json;
import com.viontech.utils.JsonMessage;
import com.viontech.vo.gb1400.SubscribeNotificationsObj;
import com.viontech.vo.traffic.TrafficModel;
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.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

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

@Slf4j
@Component
public class TrafficVehicleScheduled {
    @Resource
    private TrafficEventService trafficEventService;
    @Resource
    private Gb1400Service gb1400Service;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private TrafficDataConvertHandler trafficDataConvertHandler;

//    @Scheduled(cron = "0/5 * * * * ?")
    public void handleVehicle() {
        log.info("开始处理过车数据");
        if (!Gb1400Constants.LinkStatus) {
            return;
        }
        Object o = redisTemplate.opsForValue().get(RedisConstants.FANXING_TO_GB1400_VEHICLE_LASTTIME);
        //上次对接到的过车记录时间
        String lastTimeUtc = "";
        if (o == null) {
            //查询一小时前的数据,-1,-8:转UTC时间,总-9
            lastTimeUtc = DateUtil.formatDate(DateUtil.addHours(new Date(), -9), DateUtil.TIMESTAMP_FORMAT);
        } else {
            lastTimeUtc = (String) o;
        }
        String currentTimeUtc = DateUtil.formatDate(DateUtil.addHours(new Date(), -8), DateUtil.TIMESTAMP_FORMAT);
        if (StringUtils.isBlank(lastTimeUtc) || StringUtils.isBlank(currentTimeUtc)) {
            log.warn("lastTimeUtc or currentTimeUtc isBlank");
            return;
        }

        int offset = 0;
        int limit = 20;
        //utc时间,当前时间减8小时
        String event_dt__gte = lastTimeUtc;
        String event_dt__lt = currentTimeUtc;
        TrafficModel trafficQry = new TrafficModel();
        trafficQry.setEvent_cate("traffic");
        trafficQry.setEvent_type("vehicle");
        trafficQry.setIllegal_state("false");
        trafficQry.setEvent_dt__gte(event_dt__gte);
        trafficQry.setEvent_dt__lt(event_dt__lt);
        trafficQry.setLimit(limit);
        trafficQry.setOffset(offset);
        List<Object> trafficModels = trafficEventService.selectJsonDatas(trafficQry);
        log.info("查询范围(lastTimeUtc,currentTimeUtc]=({},{}],本批次待同步过车数据总{}条。", lastTimeUtc, currentTimeUtc, trafficModels.size());
        if (!CollectionUtils.isEmpty(trafficModels)) {
            List<TrafficMongoModel> trafficMongoModels = new ArrayList<>();
            for (Object model : trafficModels) {
                trafficMongoModels.add(JavaBean2Json.Json2JavaBean(JavaBean2Json.javaBean2Json(model), TrafficMongoModel.class));
            }
            //List<TrafficMongoModel> 转 SubscribeNotificationsObj
            SubscribeNotificationsObj subscribeNotificationsObj = trafficDataConvertHandler.trafficVehicleToSubscribeNotifications(trafficMongoModels);
            //将违法数据同步gb1400
            if (subscribeNotificationsObj != null) {
                JsonMessage resultVo = gb1400Service.subscribeNotifications(subscribeNotificationsObj);
                if (SystemConstants.APP_CODE_SUCCESS == resultVo.getCode()) {
                    redisTemplate.opsForValue().set(RedisConstants.FANXING_TO_GB1400_VEHICLE_LASTTIME, trafficMongoModels.get(trafficMongoModels.size() - 1).getEvent_dt());
                }
            } else {
                log.error("trafficDataConvertHandler.trafficVehicleToSubscribeNotifications is null");
            }
        }
        log.info("结束处理过车数据");
    }
}