Эх сурвалжийг харах

feat: 添加删除不活跃用户定时任务

weijianghai 3 сар өмнө
parent
commit
1a57f1c40e

+ 96 - 0
src/main/java/com/nokia/task/DeleteInactiveUsersTask.java

@@ -0,0 +1,96 @@
+package com.nokia.task;
+
+import com.nokia.common.exception.MyRuntimeException;
+import com.nokia.config.TaskConfig;
+import com.xxl.job.core.context.XxlJobHelper;
+import com.xxl.job.core.handler.annotation.XxlJob;
+import lombok.Data;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.jdbc.core.JdbcTemplate;
+import org.springframework.stereotype.Component;
+
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.TimeUnit;
+
+@Data
+@Component
+@Slf4j
+public class DeleteInactiveUsersTask {
+    private final JdbcTemplate jdbcTemplate;
+    private final TaskConfig taskConfig;
+
+    public DeleteInactiveUsersTask(JdbcTemplate jdbcTemplate, TaskConfig taskConfig) {
+        this.jdbcTemplate = jdbcTemplate;
+        this.taskConfig = taskConfig;
+    }
+
+    /**
+     * 删除不活跃用户定时任务
+     */
+    @XxlJob("deleteInactiveUsers")
+    public void deleteInactiveUsers() {
+        try {
+            CompletableFuture.runAsync(() -> {
+                try {
+                    singleTask();
+                } catch (Exception e) {
+                    throw new MyRuntimeException(e);
+                }
+            }).get(5, TimeUnit.MINUTES);
+        } catch (InterruptedException e) {
+            log.error("线程中断: {}", e.getMessage(), e);
+            XxlJobHelper.log("线程中断: {}", e.getMessage(), e);
+            Thread.currentThread().interrupt();
+            XxlJobHelper.handleFail(e.getMessage());
+        } catch (Exception e) {
+            log.error("发生异常了: {}", e.getMessage(), e);
+            XxlJobHelper.log("发生异常了: {}", e.getMessage(), e);
+            XxlJobHelper.handleFail(e.getMessage());
+        }
+    }
+
+    /**
+     * 单一任务
+     */
+    public void singleTask() {
+        String sql = "with a as (\n"
+                + "select\n"
+                + "    user_id,\n"
+                + "    login_name,\n"
+                + "    user_name,\n"
+                + "    city_id,\n"
+                + "    max(login_time) as last_login\n"
+                + "from\n"
+                + "    sqmdb_rpt.acl_verification_log avl\n"
+                + "where\n"
+                + "    login_time > (date_trunc('month', current_date) - interval '3 months')\n"
+                + "group by\n"
+                + "    user_id,\n"
+                + "    login_name,\n"
+                + "    user_name,\n"
+                + "    city_id ),\n"
+                + "b as (\n"
+                + "select\n"
+                + "    au.user_id\n"
+                + "from\n"
+                + "    sqmdb_rpt.acl_user au\n"
+                + "left join a\n"
+                + "on\n"
+                + "    a.user_id = au.user_id\n"
+                + "where\n"
+                + "    au.deleted = 0\n"
+                + "    and a.last_login is null)\n"
+                + "update\n"
+                + "    sqmdb_rpt.acl_user aur\n"
+                + "set\n"
+                + "    deleted = 1,\n"
+                + "    update_time = current_timestamp\n"
+                + "where\n"
+                + "    aur.user_id in (\n"
+                + "    select\n"
+                + "        user_id\n"
+                + "    from\n"
+                + "        b)";
+        jdbcTemplate.execute(sql);
+    }
+}