package com.red.circle.other.app.scheduler;


import com.red.circle.component.redis.annotation.TaskCacheLock;
import com.red.circle.other.app.manager.activity.award.WeeklyRewardsSentManager;
import com.red.circle.other.infra.database.mongo.entity.sys.ActivityConfig;
import com.red.circle.other.infra.database.mongo.service.sys.ActivityConfigService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * rankingActivity 活动奖励发放
 */

@Slf4j
@Component
@RequiredArgsConstructor
public class RankingActivityRewardTask {

    private final WeeklyRewardsSentManager weeklyRewardsSentManager;
    private final ActivityConfigService activityConfigService;

    /**
     * 每周一 0点0分执行.
     */
//    @Scheduled(cron = "0 0 0 ? * MON", zone = "Asia/Riyadh")
    @TaskCacheLock(key = "RANKING_ACTIVITY_REWARD_TASK", expireSecond = 86400)
    public void sendRankingActivityReward() {
      long startTime = System.currentTimeMillis();
      log.warn("========== rankingActivity 活动奖励发放定时任务开始 ==========");

      try {

        // 查询上周结束的活动
        List<ActivityConfig> lastWeekActivities =
            activityConfigService.listRecentlyEndedActivities("LIKEI", 10);
        lastWeekActivities.forEach(activityConfig -> {
          weeklyRewardsSentManager.sendLastRankingActivity(activityConfig.getTemplateId());
        });

        log.warn("========== rankingActivity 活动奖励发放定时任务完成，耗时：{} ms ==========",
            System.currentTimeMillis() - startTime);
      } catch (Exception e) {
        log.error("========== rankingActivity 活动奖励发放定时任务异常 ==========", e);
      }
    }

}