package com.tanpu.community.manager;

import com.tanpu.community.service.*;
import com.tanpu.community.service.quartz.TopicReportService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

@Slf4j
@Service
@Configuration
public class ConJobManager {

    @Autowired
    private VisitLogService visitLogService;

    @Autowired
    private RedisService redisService;

    @Autowired
    private RankService rankService;

    @Autowired
    private RankLogService rankLogService;

    @Autowired
    private RecommendService recommendService;

    @Autowired
    private TopicReportService topicReportService;

    /**
     * 定时统计 话题 访问数据,并刷到redis
     */
    // @Scheduled(cron = "*/10 * * * * ?")
    public void topicVisitorStats() {
        String topicId = "123";
        Integer detailVisitTimes = visitLogService.queryTopicDetailVisit(topicId);
        redisService.set("topicVisitorStats", detailVisitTimes);
    }

    /**
     * 定时统计主题、话题排行
     */
    @Scheduled(cron = "10 0/2 * * * ?")
    public void themeRank() {
        rankService.rankThemes();
        rankService.rankTopics();
    }

    /**
     * 定时刷新最新帖子
     */
    @Scheduled(cron = "*/10 * * * * ?")
    public void getThemeNewest() {
        recommendService.refreshNewestThemes();
    }

    /**
     * 定时把rank_log的日志拿出来,清理数据库
     */
    @Scheduled(cron = "0 0 0 ? * 1")
    public void clearRankLog() {
        rankLogService.clearRankLog();
    }


    /**
     * 每个工作日早上09:30汇报 topic数据
     */
    @Scheduled(cron = "0 30 9 ? * *")
    public void reportTopicWeekday() {
        log.info("reportTopicWeekday start");
        topicReportService.reportTopicWeekday();
    }

    /**
     * 每周六早上09:00汇报 topic数据
     */
    @Scheduled(cron = "0 0 9 ? * SAT")
    public void reportTopicSaturday() {
        log.info("reportTopicSaturday start");
        topicReportService.reportTopicSaturday();
    }
}