|
|
@@ -3,6 +3,7 @@ package com.fs.course.service.impl;
|
|
|
import java.math.BigDecimal;
|
|
|
import java.text.SimpleDateFormat;
|
|
|
import java.time.LocalDate;
|
|
|
+import java.time.ZoneId;
|
|
|
import java.util.*;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
@@ -521,4 +522,85 @@ public class FsCourseTrafficLogServiceImpl implements IFsCourseTrafficLogService
|
|
|
}
|
|
|
return res;
|
|
|
}
|
|
|
+
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void batchDelTraffic() {
|
|
|
+ // 设置删除的时间条件(2025-09-01之前)
|
|
|
+ LocalDate targetLocalDate = LocalDate.of(2025, 10, 1);
|
|
|
+ Date targetDate = Date.from(targetLocalDate.atStartOfDay(ZoneId.systemDefault()).toInstant());
|
|
|
+
|
|
|
+ int batchSize = 5000; // 每批次处理数量
|
|
|
+ int sleepMillis = 100; // 批次间休眠时间(毫秒)
|
|
|
+
|
|
|
+ batchDeleteExpiredData(targetDate, batchSize, sleepMillis);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 批量删除过期数据
|
|
|
+ */
|
|
|
+ private void batchDeleteExpiredData(Date targetDate, int batchSize, int sleepMillis) {
|
|
|
+ int currentPage = 0;
|
|
|
+ long totalDeleted = 0;
|
|
|
+
|
|
|
+ try {
|
|
|
+ // 查询总记录数用于进度监控
|
|
|
+ Long totalCount = fsCourseTrafficLogMapper.countExpireLink(targetDate);
|
|
|
+ log.info("开始批量删除过期流量记录,目标时间: {}, 总记录数: {}", targetDate, totalCount);
|
|
|
+
|
|
|
+ if (totalCount == null || totalCount == 0) {
|
|
|
+ log.info("没有需要删除的过期记录");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ long startTime = System.currentTimeMillis();
|
|
|
+
|
|
|
+ while (true) {
|
|
|
+ // 分页查询过期记录的log_id
|
|
|
+ int offset = currentPage * batchSize;
|
|
|
+ List<Long> logIds = fsCourseTrafficLogMapper.selectExpireLinkIds(targetDate, offset, batchSize);
|
|
|
+
|
|
|
+ if (logIds == null || logIds.isEmpty()) {
|
|
|
+ log.info("批量删除完成,共删除 {} 条记录", totalDeleted);
|
|
|
+ break;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 批量删除当前批次的log_id
|
|
|
+ int deletedCount = fsCourseTrafficLogMapper.batchDeleteByIds(logIds);
|
|
|
+ totalDeleted += deletedCount;
|
|
|
+
|
|
|
+ // 每5批次或最后一批输出日志
|
|
|
+ if (currentPage % 5 == 0 || logIds.size() < batchSize) {
|
|
|
+ double progress = (double) totalDeleted / totalCount * 100;
|
|
|
+ long currentTime = System.currentTimeMillis();
|
|
|
+ long elapsedSeconds = (currentTime - startTime) / 1000;
|
|
|
+
|
|
|
+ log.info("批次 {}: 删除 {} 条,进度: {}/{} ({:.2f}%),耗时: {}秒",
|
|
|
+ currentPage + 1, deletedCount, totalDeleted, totalCount,
|
|
|
+ progress, elapsedSeconds);
|
|
|
+ }
|
|
|
+
|
|
|
+ currentPage++;
|
|
|
+
|
|
|
+ // 批次间短暂休眠,避免数据库压力过大
|
|
|
+ if (sleepMillis > 0 && logIds.size() == batchSize) {
|
|
|
+ try {
|
|
|
+ Thread.sleep(sleepMillis);
|
|
|
+ } catch (InterruptedException e) {
|
|
|
+ Thread.currentThread().interrupt();
|
|
|
+ log.warn("删除任务被中断");
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ long endTime = System.currentTimeMillis();
|
|
|
+ long totalSeconds = (endTime - startTime) / 1000;
|
|
|
+ log.info("批量删除任务完成,总计删除: {} 条记录,总耗时: {} 秒", totalDeleted, totalSeconds);
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("批量删除流量记录失败,已删除: {} 条", totalDeleted, e);
|
|
|
+ throw new RuntimeException("批量删除失败", e);
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|