GpsTask.java 1.75 KB
package com.trash.quartz.task;

import java.util.Collection;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONObject;
import com.trash.common.core.redis.RedisCache;
import com.trash.common.utils.DateUtils;
import com.trash.gps.domain.GpsOrientation;
import com.trash.gps.mapper.GpsOrientationMapper;

@Component("GpsTask")
public class GpsTask {

    @Autowired
    private RedisCache redisCache;

    @Autowired
    private GpsOrientationMapper gpsOrientationMapper;

    //30秒执行一次

    public void insertGps() {
        Collection<String> keys =  redisCache.keys("GPS:*");
        for (String key : keys) {
            for(int i=0;i<10;i++){
                JSONObject jsonObject = redisCache.getCacheListRight(key);
                if(jsonObject==null){
                    break;
                }
                String terminalNumber = key.replaceAll("GPS:","");
                GpsOrientation gps = new GpsOrientation();
                gps.setTerminalNumber(terminalNumber);
                gps.setLongitude(jsonObject.getString("lng"));
                gps.setLatitude(jsonObject.getString("lat"));
                gps.setCreateTime(DateUtils.parseDate(jsonObject.getString("time").replaceAll("T"," ")));
                gps.setYearMonth(jsonObject.getString("time").substring(0,7).replace("-",""));
                gps.setCarCode(jsonObject.getString("carCode"));
                gpsOrientationMapper.insertGpsOrientation(gps);
            }

        }
    }

    public void createGps(){
        //获取下个月的年月yyyyMM
        String nextMonth = DateUtils.getNextMonth(new Date());
        gpsOrientationMapper.createGpsTable(nextMonth);
    }
}