package com.gci.boot.modules.task.gps;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.gci.boot.common.util.DateUtils;
import com.gci.boot.common.util.RedisKey;
import com.gci.boot.modules.basedata.entity.TbVehicle;
import com.gci.boot.modules.basedata.service.ITbVehicleService;
import com.gci.boot.modules.gis.service.GisRoadMileLogService;
import com.gci.boot.modules.gis.service.GpsConsumerService;
import com.gci.boot.modules.gis.service.IGisGpsAnalysisService;
import com.gci.boot.modules.gis.vo.FenceSpeedBeanVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.Date;
import java.util.List;

/**
 * @author lyz
 * @date 2023/9/14 15:37
 */
@Configuration
@EnableScheduling
@Slf4j
public class GisGpsAnalysisTask {

    @Autowired
    private GisRoadMileLogService gisRoadMileLogService;

    @Autowired
    private ITbVehicleService tbVehicleService;

    @Autowired
    private IGisGpsAnalysisService gisGpsAnalysisService;

    @Autowired
    private StringRedisTemplate redisTemplate;

    /**
     * 每天凌晨三点半执行
     */
    @Scheduled(cron = "0 30 03 * * ?")
    public void gpsAnalysisData() {
        LocalDate date = LocalDate.now().minusDays(1);
        log.info("定时任务每日gps数据分析开始{}", date);
        Long startMl = System.currentTimeMillis();
        LocalDateTime start = LocalDateTime.of(date, LocalTime.of(0, 0, 0));
        LocalDateTime end = LocalDateTime.of(date, LocalTime.of(23, 59, 59));
        Date startTime = DateUtils.convertLocalDateTimeToDate(start);
        Date endTime = DateUtils.convertLocalDateTimeToDate(end);
        List<TbVehicle> list = tbVehicleService.list();
        list.parallelStream().forEach(vehicle -> {
            try{
                String s = redisTemplate.opsForValue().get(RedisKey.PROJECT_GIS_SOURCE + vehicle.getProjectId());
                if (Convert.toBool(s,false)) {
                    gisGpsAnalysisService.gpsDataAnalysisByVehicle(vehicle, startTime, endTime,true);
                } else {
                    gisGpsAnalysisService.gpsDataAnalysisByVehicleBy808(vehicle, startTime, endTime);
                }
            }catch (Exception e){
                e.printStackTrace();
                log.error("执行gps分析失败，车牌号{}",vehicle.getPlateNumber());
            }
        });
        Long endMl = System.currentTimeMillis();
        log.info("定时任务每日gps数据分析完成{},耗时-{}ms", date,endMl-startMl);
    }



    /**
     * 每天凌晨二点执行
     */
    @Scheduled(cron = "0 08 02 * * ?")
    public void consumeRoadMileData() {
        LocalDate date = LocalDate.now().minusDays(1);
        log.info("定时任务每日计算能耗开始{}", date);
        Long startMl = System.currentTimeMillis();
        LocalDateTime start = LocalDateTime.of(date, LocalTime.of(0, 0, 0));
        LocalDateTime end = LocalDateTime.of(date, LocalTime.of(23, 59, 59));
        Date startTime = DateUtils.convertLocalDateTimeToDate(start);
        Date endTime = DateUtils.convertLocalDateTimeToDate(end);
        List<TbVehicle> list = tbVehicleService.list();
        list.parallelStream().forEach(vehicle -> {
            try{
                gisRoadMileLogService.coverGpsToRoadMile(vehicle, startTime, endTime);
            }catch (Exception e){
                e.printStackTrace();
                log.error("执行计算能耗失败，车牌号{}",vehicle.getPlateNumber());
            }
        });
        Long endMl = System.currentTimeMillis();
        log.info("定时任务每日计算能耗完成{},耗时-{}ms", date,endMl-startMl);
    }
}
