Просмотр исходного кода

perf(activity): P2-1 过期活动结束定时化——每日02:00执行,阈值改 endTime<now()

- autoEndExpiredActivities 改为 public + @Scheduled(cron=0 0 2 * * ?)(@EnableScheduling 已在 CfcApplication 启用)
- 结束阈值由旧的 7 天延迟改为 endTime < now(),去掉查询延迟
- list/detail 保留轻量实时调用兜底,try-catch 保证不影响主查询链路
E2E Test Bot 3 недель назад
Родитель
Сommit
024c3472da
1 измененных файлов с 16 добавлено и 4 удалено
  1. 16 4
      cfc-backend/src/main/java/com/etotem/cfc/service/ActivityService.java

+ 16 - 4
cfc-backend/src/main/java/com/etotem/cfc/service/ActivityService.java

@@ -18,6 +18,7 @@ import com.etotem.cfc.mapper.PointsLogMapper;
 import com.etotem.cfc.mapper.TaskMapper;
 import com.etotem.cfc.mapper.TaskMapper;
 import com.etotem.cfc.mapper.UserMapper;
 import com.etotem.cfc.mapper.UserMapper;
 import lombok.extern.slf4j.Slf4j;
 import lombok.extern.slf4j.Slf4j;
+import org.springframework.scheduling.annotation.Scheduled;
 import org.springframework.stereotype.Service;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 import org.springframework.transaction.annotation.Transactional;
 import org.springframework.util.StringUtils;
 import org.springframework.util.StringUtils;
@@ -456,14 +457,25 @@ public class ActivityService extends ServiceImpl<ActivityMapper, Activity> {
         }
         }
     }
     }
 
 
-    private void autoEndExpiredActivities() {
+    /**
+     * 自动结束过期活动
+     * 逻辑:发布中且结束时间已过(endTime < now),将状态置为 ended。
+     * 备注:正式上线建议改为 Spring @Scheduled(cron = "0 0 2 * * ?") 每日 02:00 执行(见 TODOs),此处保留方法供 list/detail 触发兜底。
+     */
+    /**
+     * 自动结束过期活动(P2-1 定时化)
+     * 每日 02:00 定时执行,结束阈值 = endTime < now()(去掉旧的 7 天延迟)。
+     * 同时保留在 list/detail 查询时轻量调用兜底(实时状态判断)。
+     */
+    @Scheduled(cron = "0 0 2 * * ?")
+    public void autoEndExpiredActivities() {
         try {
         try {
-            Date threshold = new Date(System.currentTimeMillis() - 7L * 24 * 3600 * 1000);
+            Date now = new Date();
             this.update(new LambdaUpdateWrapper<Activity>()
             this.update(new LambdaUpdateWrapper<Activity>()
                     .eq(Activity::getStatus, "published")
                     .eq(Activity::getStatus, "published")
-                    .lt(Activity::getEndTime, threshold)
+                    .lt(Activity::getEndTime, now)
                     .set(Activity::getStatus, "ended")
                     .set(Activity::getStatus, "ended")
-                    .set(Activity::getUpdatedAt, new Date()));
+                    .set(Activity::getUpdatedAt, now));
         } catch (Exception e) {
         } catch (Exception e) {
             log.warn("自动结束过期活动失败: {}", e.getMessage());
             log.warn("自动结束过期活动失败: {}", e.getMessage());
         }
         }