package com.ruoyi.job.service;

import com.ruoyi.demo.mapper.WdInfoDao;
import com.ruoyi.demo.utils.WdRedisStoreage;
import com.ruoyi.demo.utils.statistics.AttentionPoolStat;
import com.ruoyi.demo.utils.statistics.Together;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.ZoneId;

@Slf4j
@Service
public class BenyunService {

    @Autowired
    WdRedisStoreage wdRedisStoreage;

    @Autowired
    WdInfoDao wdInfoDao;
    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    AttentionPoolStat stat;

    @Autowired
    Together together;


    /**
     * 将网点数据加载到redis中
     * */
    @XxlJob("WriteRedisJob")
    public void WriteRedisJob(){
        Boolean aBoolean = redisTemplate.hasKey("4501");
        if (aBoolean){
            //将当天新的网点信息加载redis
            XxlJobHelper.log("WriteRedisJob-day start");
            LocalDateTime now = LocalDateTime.now(ZoneId.of("Asia/Shanghai"));
            LocalDateTime localDateTime = now.plusDays(-1);
            try {
                wdRedisStoreage.writeWdGeoRedis1(localDateTime.toString());
            } catch (Exception e) {
                e.printStackTrace();
                XxlJobHelper.log("WriteRedisJob-day ERROR："+e.getMessage());
            }
        }else {
            //将所有信息放入redis中心
            XxlJobHelper.log("WriteRedisJob-full start");
            LocalDateTime localDateTime = LocalDateTime.of(2023, 6, 1, 0,0);
            try {
                wdRedisStoreage.writeWdGeoRedis1(localDateTime.toString());
            } catch (Exception e) {
                e.printStackTrace();
                XxlJobHelper.log("WriteRedisJob-full ERROR："+e.getMessage());
                redisTemplate.boundValueOps("4501").set(1);
            }
            AnalyseJob();
        }
    }

    /**
     * 网点标签分析定时任务
     */
    @XxlJob("AnalyseJob")
    public void AnalyseJob(){
        System.out.println("大统计.....");
        wdRedisStoreage.writeWdGeoRedis2(new String[]{"1"});
        wdRedisStoreage.writeWdGeoRedis2(new String[]{"2","3","5"});
        System.out.println("大统计.....结束");
    }


    /**
     * 关注池定时任务
     */
    @XxlJob("AttentionPoolJob")
    public void AttentionPoolJob(){
        stat.insert();
    }

    /**
     * 品牌统计定时任务
     * */
    @XxlJob("BrandStatsiticsJob")
    public void BrandStatsiticsJob(){
        together.start();
    }

}
