|
|
@@ -5,6 +5,7 @@ import cn.hutool.json.JSONUtil;
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
import com.alibaba.fastjson.JSONArray;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
|
|
|
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
import com.fs.common.core.redis.RedisCache;
|
|
|
@@ -1136,4 +1137,45 @@ public class FsCourseWatchLogServiceImpl extends ServiceImpl<FsCourseWatchLogMap
|
|
|
return list;
|
|
|
}
|
|
|
|
|
|
+ @Transactional
|
|
|
+ @Override
|
|
|
+ public int deleteOldDataBatch() {
|
|
|
+ int totalDeleted = 0;
|
|
|
+ LocalDateTime cutoffTime = LocalDateTime.now().minusMonths(1);
|
|
|
+ while (true) {
|
|
|
+ // 1. 查询需要删除的数据ID(每次1000条)
|
|
|
+ List<Long> idsToDelete = baseMapper.selectDelOldDataIds(cutoffTime);
|
|
|
+
|
|
|
+ // 2. 如果没有数据可删除,则退出循环
|
|
|
+ if (CollectionUtils.isEmpty(idsToDelete)) {
|
|
|
+ break;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 3. 执行批量删除
|
|
|
+ int deletedCount = baseMapper.batchDelLog(idsToDelete);
|
|
|
+ totalDeleted += deletedCount;
|
|
|
+
|
|
|
+ // 4. 如果删除的数量小于1000,说明已删除完毕
|
|
|
+ if (deletedCount < 1000) {
|
|
|
+ break;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 5. 短暂休眠,避免对数据库造成过大压力
|
|
|
+ try {
|
|
|
+ Thread.sleep(5000);
|
|
|
+ } catch (InterruptedException e) {
|
|
|
+ Thread.currentThread().interrupt();
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return totalDeleted;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public long getOldDataCount() {
|
|
|
+ LocalDateTime cutoffTime = LocalDateTime.now().minusMonths(1);
|
|
|
+ return baseMapper.countOldData(cutoffTime);
|
|
|
+ }
|
|
|
+
|
|
|
}
|