package com.sonic.frog.job;


import com.sonic.common.client.JobmanClient;
import com.sonic.common.utils.LogUtils;
import com.sonic.frog.service.RankService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * 更新榜单相关的定时任务
 */
@Slf4j
@Component
public class UpdateRankJob {

    @Autowired
    private JobmanClient jobmanClient;
    @Autowired
    private RankService rankService;

    /** 每5分钟执行一次 */
    @Scheduled(cron = "0 0/5 * * * ?")
    public void chatRankSchedule() {
        // 锁 1 分钟过期
        jobmanClient.run("chatRankJob", TimeUnit.MINUTES.toSeconds(2), this::chatRankJob);
    }

    protected JobmanClient.JobResult chatRankJob(JobmanClient.JobContext jobContext) {
        try {
            LogUtils.setTraceId();
            log.info("===> chatRankJob start !!!");
            int size = rankService.chatRankJob();
            log.info("===> chatRankJob count : {} end !!!", size);
            return JobmanClient.JobResult.success(size);
        } finally {
            LogUtils.removeTraceId();
        }
    }

    /** 每5分钟执行一次 */
    @Scheduled(cron = "0 0/5 * * * ?")
    public void aiHeartbeatRankSchedule() {
        // 锁 1 分钟过期
        jobmanClient.run("aiHeartbeatRankJob", TimeUnit.MINUTES.toSeconds(2), this::aiHeartbeatRankJob);
    }

    protected JobmanClient.JobResult aiHeartbeatRankJob(JobmanClient.JobContext jobContext) {
        try {
            LogUtils.setTraceId();
            log.info("===> aiHeartbeatRankJob start !!!");
            int size = rankService.aiHeartbeatRankJob();
            log.info("===> aiHeartbeatRankJob count : {} end !!!", size);
            return JobmanClient.JobResult.success(size);
        } finally {
            LogUtils.removeTraceId();
        }
    }


    /** 每5分钟执行一次 */
    @Scheduled(cron = "0 0/5 * * * ?")
    public void aiGiftRankSchedule() {
        // 锁 1 分钟过期
        jobmanClient.run("aiGiftRankJob", TimeUnit.MINUTES.toSeconds(2), this::aiGiftRankJob);
    }

    protected JobmanClient.JobResult aiGiftRankJob(JobmanClient.JobContext jobContext) {
        try {
            LogUtils.setTraceId();
            log.info("===> aiGiftRankJob start !!!");
            int size = rankService.giftRankJob();
            log.info("===> aiGiftRankJob count : {} end !!!", size);
            return JobmanClient.JobResult.success(size);
        } finally {
            LogUtils.removeTraceId();
        }
    }

}