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

登录日志和操作日志表定时删除60天前的日志

gao.qiang 1 месяц назад
Родитель
Сommit
464596de94
1 измененных файлов с 90 добавлено и 0 удалено
  1. 90 0
      hazard-admin/src/main/java/com/ozs/web/core/util/DeleteDataUtil.java

+ 90 - 0
hazard-admin/src/main/java/com/ozs/web/core/util/DeleteDataUtil.java

@@ -0,0 +1,90 @@
+package com.ozs.web.core.util;
+
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
+import com.ozs.system.domain.SysLoginInfo;
+import com.ozs.system.domain.SysOperLog;
+import com.ozs.system.mapper.SysLoginInfoMapper;
+import com.ozs.system.mapper.SysOperLogMapper;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.scheduling.annotation.Async;
+import org.springframework.scheduling.annotation.EnableAsync;
+import org.springframework.scheduling.annotation.EnableScheduling;
+import org.springframework.scheduling.annotation.Scheduled;
+
+import java.time.LocalDateTime;
+import java.time.temporal.ChronoUnit;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.ThreadPoolExecutor;
+
+@Configuration
+@EnableScheduling
+@EnableAsync
+public class DeleteDataUtil {
+
+    private static final Logger log = LoggerFactory.getLogger(DeleteDataUtil.class);
+    @Autowired
+    ThreadPoolExecutor executor;
+    @Autowired
+    private SysLoginInfoMapper sysLoginInfoMapper;
+    @Autowired
+    private SysOperLogMapper sysOperLogMapper;
+
+    @Async
+    @Scheduled(cron = "0 0 0 1 * ?")
+    public void tableCreationAndDelete() {
+        log.info("开始执行 tableCreationAndDelete 任务");
+        // 计算当前时间往前推 90 天的日期
+        LocalDateTime ninetyDaysAgo = LocalDateTime.now().minus(60, ChronoUnit.DAYS);
+
+        // 执行1
+        CompletableFuture<Integer> future1 = CompletableFuture.supplyAsync(() -> {
+            try {
+                deleteSysLoginInfoData(ninetyDaysAgo);
+                return 1;
+            } catch (Exception e) {
+                log.error("执行 createTable 方法时出现异常", e);
+                throw new RuntimeException(e);
+            }
+        }, executor);
+
+        // 执行2,确保在 future1 完成后执行
+        CompletableFuture<String> future2 = future1.thenApplyAsync(f1 -> {
+            try {
+                return deleteSysOperLogData(ninetyDaysAgo);
+            } catch (Exception e) {
+                log.error("执行 batchInsertData 方法时出现异常", e);
+                throw new RuntimeException(e);
+            }
+        }, executor);
+
+        // 记录任务完成情况
+        future2.whenComplete((result, ex) -> {
+            if (ex != null) {
+                log.error("tableCreationAndDelete 任务执行失败", ex);
+            } else {
+                log.info("tableCreationAndDelete 任务执行成功,结果: {}", result);
+            }
+        });
+    }
+
+    public void deleteSysLoginInfoData(LocalDateTime ninetyDaysAgo) {
+        log.info("开始执行---deleteSysLoginInfoData");
+        int deletedCount = sysLoginInfoMapper.delete(new LambdaQueryWrapper<SysLoginInfo>().le(SysLoginInfo::getLoginTime, ninetyDaysAgo));
+
+        // 可以根据需要添加日志记录
+        System.out.println("deleteSysLoginInfoData方法成功删除 " + deletedCount + " 条登录记录。");
+    }
+
+    public String deleteSysOperLogData(LocalDateTime ninetyDaysAgo) {
+        log.info("开始执行---deleteSysOperLogData");
+        int deletedCount = sysOperLogMapper.delete(new LambdaQueryWrapper<SysOperLog>().le(SysOperLog::getOperTime, ninetyDaysAgo));
+
+        // 可以根据需要添加日志记录
+        System.out.println("deleteSysOperLogData方法成功删除 " + deletedCount + " 条登录记录。");
+        return "数据删除完成";
+    }
+
+}