|
@@ -0,0 +1,107 @@
|
|
|
+package com.flow.job;
|
|
|
+
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
+import com.flow.entity.User;
|
|
|
+import com.flow.mapstruct.UserMapper;
|
|
|
+import com.flow.service.UserService;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import lombok.RequiredArgsConstructor;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.boot.ApplicationArguments;
|
|
|
+import org.springframework.boot.ApplicationRunner;
|
|
|
+import org.springframework.scheduling.annotation.Scheduled;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+
|
|
|
+import java.sql.*;
|
|
|
+import java.time.LocalDateTime;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Set;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+/**
|
|
|
+ *
|
|
|
+ * @author fyc
|
|
|
+ * @email yuchuan.fu@chinausky.com
|
|
|
+ * @date 2025/9/3
|
|
|
+ */
|
|
|
+@Component
|
|
|
+@Slf4j
|
|
|
+@RequiredArgsConstructor
|
|
|
+public class SynchronizeUserInformationJob implements ApplicationRunner {
|
|
|
+
|
|
|
+ private static final String REMOTE_URL = "jdbc:mysql://47.111.81.118:13307/usky-cloud?useSSL=false&serverTimezone=Asia/Shanghai";
|
|
|
+ private static final String REMOTE_USER = "usky";
|
|
|
+ private static final String REMOTE_PASSWORD = "Yt#75Usky";
|
|
|
+ /* ===================================== */
|
|
|
+
|
|
|
+ private static final String SQL =
|
|
|
+ "SELECT user_name, nick_name, email, password, avatar, " +
|
|
|
+ "phonenumber, sex, status, dept_id, create_by, create_time, update_by, update_time " +
|
|
|
+ "FROM sys_user " +
|
|
|
+ "WHERE tenant_id = '1003'";
|
|
|
+
|
|
|
+ private final UserService userService;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void run(ApplicationArguments args) {
|
|
|
+ log.info("立即执行一次用户数据同步");
|
|
|
+ sync();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Scheduled(cron = "0 0 0 * * ?")
|
|
|
+ public void sync() {
|
|
|
+ // 1. 取本地已有 user_name
|
|
|
+ Set<String> exists = userService.list(
|
|
|
+ new QueryWrapper<User>().select("name"))
|
|
|
+ .stream()
|
|
|
+ .map(User::getName)
|
|
|
+ .collect(Collectors.toSet());
|
|
|
+
|
|
|
+ List<User> missing = new ArrayList<>();
|
|
|
+
|
|
|
+ try (Connection conn = DriverManager.getConnection(REMOTE_URL, REMOTE_USER, REMOTE_PASSWORD);
|
|
|
+ PreparedStatement ps = conn.prepareStatement(SQL);
|
|
|
+ ResultSet rs = ps.executeQuery()) {
|
|
|
+
|
|
|
+ while (rs.next()) {
|
|
|
+ String username = rs.getString("nick_name");
|
|
|
+ if (exists.contains(username)) continue;
|
|
|
+
|
|
|
+ missing.add(build(rs));
|
|
|
+ }
|
|
|
+ } catch (SQLException e) {
|
|
|
+ log.error("拉取远程用户表失败", e);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (missing.isEmpty()) {
|
|
|
+ log.info("本地已是最新,无需同步");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 2. 批量插入(ServiceImpl 自带 saveBatch)
|
|
|
+ userService.saveBatch(missing, 1000);
|
|
|
+ log.info("增量同步完成,新增 {} 条", missing.size());
|
|
|
+ }
|
|
|
+
|
|
|
+ private User build(ResultSet rs) throws SQLException {
|
|
|
+ User f = new User();
|
|
|
+ f.setUsername(rs.getString("user_name"));
|
|
|
+ f.setName(rs.getString("nick_name"));
|
|
|
+ f.setPassword(rs.getString("password"));
|
|
|
+ f.setAvatar(rs.getString("avatar"));
|
|
|
+ f.setPhone(rs.getString("phonenumber"));
|
|
|
+ f.setEmail(rs.getString("email"));
|
|
|
+ f.setDeptId(rs.getLong("dept_id"));
|
|
|
+ f.setCreateTime(rs.getObject("create_time", LocalDateTime.class));
|
|
|
+ f.setUpdateTime(rs.getObject("update_time", LocalDateTime.class));
|
|
|
+ f.setCreatedBy(rs.getString("create_by"));
|
|
|
+ f.setUpdatedBy(rs.getString("update_by"));
|
|
|
+
|
|
|
+ String sex = rs.getString("sex");
|
|
|
+ f.setSex("0".equals(sex) ? 1 : "1".equals(sex) ? 2 : 0);
|
|
|
+ f.setEnabled("0".equals(rs.getString("status")));
|
|
|
+ return f;
|
|
|
+ }
|
|
|
+}
|