|
| 1 | +package com.phantoms.phantomsbackend.service.scheduler; |
| 2 | + |
| 3 | +import com.alibaba.fastjson.JSONArray; |
| 4 | +import com.alibaba.fastjson.JSONObject; |
| 5 | +import com.phantoms.phantomsbackend.common.utils.NapCatQQUtil; |
| 6 | +import com.phantoms.phantomsbackend.common.utils.RisingStonesUtils; |
| 7 | +import org.slf4j.Logger; |
| 8 | +import org.slf4j.LoggerFactory; |
| 9 | +import org.springframework.beans.factory.annotation.Autowired; |
| 10 | +import org.springframework.beans.factory.annotation.Value; |
| 11 | +import org.springframework.scheduling.annotation.Scheduled; |
| 12 | +import org.springframework.stereotype.Component; |
| 13 | + |
| 14 | +import java.io.IOException; |
| 15 | +import java.time.LocalDate; |
| 16 | +import java.time.LocalDateTime; |
| 17 | +import java.time.format.DateTimeFormatter; |
| 18 | + |
| 19 | +@Component |
| 20 | +public class DailySignInScheduler { |
| 21 | + |
| 22 | + private static final Logger logger = LoggerFactory.getLogger(DailySignInScheduler.class); |
| 23 | + private static final DateTimeFormatter MONTH_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM"); |
| 24 | + private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); |
| 25 | + |
| 26 | + @Autowired |
| 27 | + private RisingStonesUtils risingStonesUtils; |
| 28 | + |
| 29 | + @Autowired |
| 30 | + private NapCatQQUtil napCatQQUtil; |
| 31 | + |
| 32 | + @Value("${napcat.admin-qq:944989026}") |
| 33 | + private String adminQQ; |
| 34 | + |
| 35 | + @Value("${scheduler.signin.enabled:true}") |
| 36 | + private boolean signInEnabled; |
| 37 | + |
| 38 | + /** |
| 39 | + * 每日签到任务 - UTC+8每天00:05执行 |
| 40 | + * 先执行签到,然后尝试领取可用奖励 |
| 41 | + */ |
| 42 | + @Scheduled(cron = "0 5 0 * * ?") // 每天00:05执行 |
| 43 | +// @Scheduled(fixedRate = 60000) |
| 44 | + public void dailySignInTask() { |
| 45 | + if (!signInEnabled) { |
| 46 | + logger.info("每日签到任务已禁用"); |
| 47 | + return; |
| 48 | + } |
| 49 | + |
| 50 | + logger.info("开始执行每日签到任务 - {}", LocalDateTime.now().format(DATE_FORMATTER)); |
| 51 | + |
| 52 | + try { |
| 53 | + // 1. 执行每日签到 |
| 54 | + JSONObject signInResult = risingStonesUtils.doSignIn(); |
| 55 | + |
| 56 | + if (signInResult != null && signInResult.getInteger("code") == 10001) { |
| 57 | + logger.info("签到成功 - {}", signInResult.getString("message")); |
| 58 | + |
| 59 | + // 2. 尝试领取可用奖励 |
| 60 | + claimAvailableRewards(); |
| 61 | + |
| 62 | + // 发送成功通知 |
| 63 | + sendNotification("✅ 每日签到任务执行成功", |
| 64 | + "签到结果: " + signInResult.getString("message")); |
| 65 | + } else { |
| 66 | + String errorMsg = signInResult != null ? signInResult.getString("message") : "未知错误"; |
| 67 | + logger.error("签到失败: {}", errorMsg); |
| 68 | + sendNotification("❌ 每日签到任务执行失败", |
| 69 | + "签到失败: " + errorMsg); |
| 70 | + } |
| 71 | + } catch (IOException e) { |
| 72 | + logger.error("每日签到任务执行异常", e); |
| 73 | + sendNotification("❌ 每日签到任务执行异常", |
| 74 | + "异常信息: " + e.getMessage()); |
| 75 | + } catch (Exception e) { |
| 76 | + logger.error("每日签到任务执行发生未知异常", e); |
| 77 | + sendNotification("❌ 每日签到任务执行发生未知异常", |
| 78 | + "异常信息: " + e.getMessage()); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * 领取可用的签到奖励 |
| 84 | + */ |
| 85 | + private void claimAvailableRewards() { |
| 86 | + logger.info("开始尝试领取签到奖励"); |
| 87 | + |
| 88 | + try { |
| 89 | + // 获取当前月份 |
| 90 | + String currentMonth = LocalDate.now().format(MONTH_FORMATTER); |
| 91 | + |
| 92 | + // 获取签到奖励列表 |
| 93 | + JSONObject rewardListResult = risingStonesUtils.getSignInRewardList(currentMonth); |
| 94 | + |
| 95 | + if (rewardListResult != null && rewardListResult.getInteger("code") == 10001) { |
| 96 | + JSONArray rewardList = rewardListResult.getJSONObject("data").getJSONArray("list"); |
| 97 | + |
| 98 | + if (rewardList != null && !rewardList.isEmpty()) { |
| 99 | + logger.info("获取到 {} 个签到奖励", rewardList.size()); |
| 100 | + |
| 101 | + // 遍历奖励列表,尝试领取可用奖励 |
| 102 | + for (int i = 0; i < rewardList.size(); i++) { |
| 103 | + JSONObject reward = rewardList.getJSONObject(i); |
| 104 | + |
| 105 | + // 检查奖励是否可领取 |
| 106 | + // 状态说明: 0-未达成, 1-可领取, 2-已领取 |
| 107 | + Integer status = reward.getInteger("status"); |
| 108 | + if (status != null && status == 1) { |
| 109 | + // 领取奖励 |
| 110 | + Integer rewardId = reward.getInteger("id"); |
| 111 | + String rewardName = reward.getString("name"); |
| 112 | + |
| 113 | + try { |
| 114 | + JSONObject claimResult = risingStonesUtils.getSignInReward(rewardId, currentMonth); |
| 115 | + |
| 116 | + if (claimResult != null && claimResult.getInteger("code") == 10001) { |
| 117 | + logger.info("成功领取奖励: {} (ID: {})", rewardName, rewardId); |
| 118 | + sendNotification("✅ 领取签到奖励成功", |
| 119 | + "奖励名称: " + rewardName + "\n奖励ID: " + rewardId); |
| 120 | + } else { |
| 121 | + String errorMsg = claimResult != null ? claimResult.getString("message") : "未知错误"; |
| 122 | + logger.error("领取奖励失败: {} (ID: {}), 错误信息: {}", rewardName, rewardId, errorMsg); |
| 123 | + } |
| 124 | + } catch (IOException e) { |
| 125 | + logger.error("领取奖励时发生异常: {} (ID: {})", rewardName, rewardId, e); |
| 126 | + } |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | + } else { |
| 131 | + String errorMsg = rewardListResult != null ? rewardListResult.getString("message") : "未知错误"; |
| 132 | + logger.error("获取签到奖励列表失败: {}", errorMsg); |
| 133 | + } |
| 134 | + } catch (IOException e) { |
| 135 | + logger.error("获取签到奖励列表时发生异常", e); |
| 136 | + } catch (Exception e) { |
| 137 | + logger.error("领取奖励过程中发生未知异常", e); |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * 发送通知消息 |
| 143 | + */ |
| 144 | + private void sendNotification(String title, String content) { |
| 145 | + if (adminQQ == null || adminQQ.isEmpty()) { |
| 146 | + logger.warn("未配置管理员QQ,跳过通知发送"); |
| 147 | + return; |
| 148 | + } |
| 149 | + |
| 150 | + try { |
| 151 | + String message = String.format("%s\n\n%s\n\n执行时间: %s", |
| 152 | + title, content, LocalDateTime.now().format(DATE_FORMATTER)); |
| 153 | + |
| 154 | + napCatQQUtil.sendPrivateMessage(adminQQ, message); |
| 155 | + logger.info("已发送通知消息: {}", title); |
| 156 | + } catch (IOException e) { |
| 157 | + logger.error("发送通知消息失败", e); |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + /** |
| 162 | + * 手动触发签到任务(用于测试) |
| 163 | + */ |
| 164 | + public void manualSignIn() { |
| 165 | + logger.info("手动触发每日签到任务"); |
| 166 | + dailySignInTask(); |
| 167 | + } |
| 168 | + |
| 169 | + /** |
| 170 | + * 手动触发奖励领取任务(用于测试) |
| 171 | + */ |
| 172 | + public void manualClaimRewards() { |
| 173 | + logger.info("手动触发奖励领取任务"); |
| 174 | + claimAvailableRewards(); |
| 175 | + } |
| 176 | +} |
0 commit comments