TrafficIllegalScheduled.java 5.59 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.CheckUtil;
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 TrafficIllegalScheduled {
    @Resource
    private TrafficEventService trafficEventService;
    @Resource
    private Gb1400Service gb1400Service;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private TrafficDataConvertHandler trafficDataConvertHandler;

    @Scheduled(cron = "0/5 * * * * ?")
    public void handleIllegal() {
        log.info("开始处理违法数据");
        if (!Gb1400Constants.LinkStatus) {
            return;
        }
        Object o = redisTemplate.opsForValue().get(RedisConstants.FANXING_TO_GB1400_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 = 10;
        //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("true");
        trafficQry.setEvent_dt__gte(event_dt__gte);
        trafficQry.setEvent_dt__lt(event_dt__lt);
        trafficQry.setLimit(limit);
        trafficQry.setOffset(offset);
        log.info("违法查询范围(lastTimeUtc,currentTimeUtc]=({},{}]。", lastTimeUtc, currentTimeUtc);
        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) {
                TrafficMongoModel trafficModel = JavaBean2Json.Json2JavaBean(JavaBean2Json.javaBean2Json(model), TrafficMongoModel.class);
                if (CheckUtil.checkDeviceId(trafficModel)) {
                    trafficMongoModels.add(trafficModel);
                } else {
                    log.info("ill.update.record:{}-{}", trafficModel.getId(), trafficModel.getEvent_dt());
                    if (CheckUtil.checkTime(trafficModel, lastTimeUtc)) {
                        redisTemplate.opsForValue().set(RedisConstants.FANXING_TO_GB1400_LASTTIME, trafficModel.getEvent_dt());
                    }
                }
            }
            //List<TrafficMongoModel> 转 SubscribeNotificationsObj
            SubscribeNotificationsObj subscribeNotificationsObj = trafficDataConvertHandler.trafficIllegalToSubscribeNotifications(trafficMongoModels);
            //将违法数据同步gb1400
            if (subscribeNotificationsObj != null) {
                JsonMessage resultVo = gb1400Service.subscribeNotifications(subscribeNotificationsObj);
                if (SystemConstants.APP_CODE_SUCCESS == resultVo.getCode()) {
                    TrafficMongoModel last = trafficMongoModels.get(trafficMongoModels.size() - 1);
                    log.info("ill.update.record2:{}-{}", last.getId(), last.getEvent_dt());
                    if (CheckUtil.checkTime(last, lastTimeUtc)) {
                        redisTemplate.opsForValue().set(RedisConstants.FANXING_TO_GB1400_LASTTIME, last.getEvent_dt());
                    } else {
                        log.info("ill.update.record-false:{}-{}", last.getId(), last.getEvent_dt());
                        Date lastRecord = DateUtil.parse(lastTimeUtc, DateUtil.TIMESTAMP_FORMAT);
                        Date lastRecord1 = DateUtil.addSeconds(lastRecord, 1);
                        String lastRecord1Str = DateUtil.format(lastRecord1, DateUtil.TIMESTAMP_FORMAT);
                        redisTemplate.opsForValue().set(RedisConstants.FANXING_TO_GB1400_LASTTIME, lastRecord1Str);
                    }
                }
            } else {
                log.error("trafficDataConvertHandler.trafficIllegalToSubscribeNotifications is null");
            }
        }
        log.info("结束处理违法数据");
    }
}