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

定时删除 表 fs_user_course_count 数据 只保留三个月的

xgb 2 дней назад
Родитель
Сommit
4fe5e85e8e

+ 27 - 0
fs-admin/src/main/java/com/fs/his/task/Task.java

@@ -242,6 +242,9 @@ public class Task {
     @Autowired
     private IFsImMsgSendLogService fsImMsgSendLogService;
 
+    @Autowired
+    private com.fs.store.mapper.FsUserCourseCountMapper fsUserCourseCountMapper;
+
 
     /**
      * 定时任务,处理ai禁止回复之后的消息
@@ -1900,6 +1903,30 @@ public class Task {
         logger.info("删除fs_im_msg_send_log数据:{}条", logCount);
     }
 
+    /**
+     * 定时清理 fs_user_course_count 表,只保留3个月的数据,1000条一批次删除
+     */
+    public void cleanFsUserCourseCount() {
+        logger.info("开始执行清理 fs_user_course_count 表数据任务");
+        Date thresholdTime = org.apache.commons.lang3.time.DateUtils.addMonths(new Date(), -3);
+        int totalDeleted = 0;
+        int batchCount;
+        do {
+            batchCount = fsUserCourseCountMapper.deleteByCreateTimeLimit(thresholdTime);
+            totalDeleted += batchCount;
+            if (batchCount > 0) {
+                logger.info("清理 fs_user_course_count 数据, 本批删除 {} 条, 累计已删除 {} 条", batchCount, totalDeleted);
+                try {
+                    Thread.sleep(100);
+                } catch (InterruptedException e) {
+                    Thread.currentThread().interrupt();
+                    break;
+                }
+            }
+        } while (batchCount > 0);
+        logger.info("清理 fs_user_course_count 表数据完成, 总共删除 {} 条", totalDeleted);
+    }
+
 
 
 }

+ 7 - 0
fs-service/src/main/java/com/fs/store/mapper/FsUserCourseCountMapper.java

@@ -104,4 +104,11 @@ public interface FsUserCourseCountMapper
     List<FsUserLastCount> selectUserLastCount(@Param("userIds") Set<Long> userIds);
 
     List<FsUserCourseCount> selectUserLastCountList(@Param("userIds") Set<Long> userIds);
+
+    /**
+     * 按创建时间批量删除(每次最多1000条)
+     * @param thresholdTime 删除此时间之前的数据
+     * @return 受影响行数
+     */
+    int deleteByCreateTimeLimit(@Param("thresholdTime") java.util.Date thresholdTime);
 }

+ 4 - 0
fs-service/src/main/resources/mapper/store/FsUserCourseCountMapper.xml

@@ -337,6 +337,10 @@
         </foreach>
     </select>
     <!-- 查询看客数量和最后看课时间   -->
+    <delete id="deleteByCreateTimeLimit">
+        delete from fs_user_course_count where create_time &lt; #{thresholdTime} limit 1000
+    </delete>
+
     <select id="selectUserLastCountList" resultType="com.fs.store.domain.FsUserCourseCount">
         select
         fs_user_course_count.user_id,