TollgateSyncScheduled.java 3.68 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.JsonMessage;
import com.viontech.vo.gb1400.SubscribeNotificationsObj;
import com.viontech.vo.traffic.LaneModel;
import com.viontech.vo.traffic.TollgateModel;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
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 javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author msl
 * @date 2022/5/7
 */
@Slf4j
@Component
public class TollgateSyncScheduled {
    @Resource
    private TrafficEventService trafficEventService;
    @Resource
    private Gb1400Service gb1400Service;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private TrafficDataConvertHandler trafficDataConvertHandler;

    @Scheduled(cron = "0 */2 * * * ?")
    public void handleIllegal() {
        log.info("开始处理卡口数据");
        if (!Gb1400Constants.LinkStatus) {
            return;
        }
        Object o = redisTemplate.opsForValue().get(RedisConstants.FANXING_TO_GB1400_KAKOU_LASTTIME);
        //上次对接到的卡口记录时间
        String lastTimeUtc = "";
        if (o == null) {
            //初次查询一小时前的数据,-12,-8:转UTC时间,总-20
            lastTimeUtc = DateUtil.formatDate(DateUtil.addHours(new Date(), -20), 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;
        }
        //查询
        TollgateModel tollgateQry = new TollgateModel();
        tollgateQry.setModifyTime_gte(lastTimeUtc);
        tollgateQry.setModifyTime_lte(currentTimeUtc);
        tollgateQry.setOffset(0);
        tollgateQry.setLimit(20);
        List<TollgateModel> tollgateModels = trafficEventService.selectTollgateDatas(tollgateQry);
        log.info("查询范围(lastTimeUtc,currentTimeUtc]=({},{}],本批次卡口待同步数据总{}条。", lastTimeUtc, currentTimeUtc, tollgateModels.size());
        if (!CollectionUtils.isEmpty(tollgateModels)) {
            //List<TollgateModel> 转 SubscribeNotificationsObj
            SubscribeNotificationsObj subscribeNotificationsObj = trafficDataConvertHandler.tollgateToSubscribeNotifications(tollgateModels);
            //将卡口数据同步gb1400
            if (subscribeNotificationsObj != null) {
                JsonMessage resultVo = gb1400Service.subscribeNotifications(subscribeNotificationsObj);
                if (SystemConstants.APP_CODE_SUCCESS == resultVo.getCode()) {
                    redisTemplate.opsForValue().set(RedisConstants.FANXING_TO_GB1400_KAKOU_LASTTIME, DateUtil.formatDate(tollgateModels.get(tollgateModels.size() - 1).getModifyTime(), DateUtil.TIMESTAMP_FORMAT));
                }
            } else {
                log.error("trafficDataConvertHandler.tollgateToSubscribeNotifications is null");
            }
        }
        log.info("结束处理卡口数据");
    }
}