ソースを参照

feat: 同步top用户定时任务添加检查用户信息变化逻辑

weijianghai 2 年 前
コミット
4035133f1f

+ 32 - 2
src/main/java/com/nokia/dao/UserDao.java

@@ -10,6 +10,7 @@ import com.nokia.vo.flow.FlowUserVo;
 import org.apache.ibatis.annotations.*;
 
 import java.util.List;
+import java.util.Set;
 
 @Mapper
 public interface UserDao extends BaseMapper<User> {
@@ -282,10 +283,39 @@ public interface UserDao extends BaseMapper<User> {
     List<Integer> getCityIdsByUserIds(List<Integer> list);
 
     /**
-     * 查询所有用户
+     * 查询所有用户,过滤已删除和测试用户
      *
      * @return {@link List}<{@link UserVo}>
      */
-    @Select("select * from sqmdb_rpt.acl_user where deleted = 0 order by user_id")
+    @Select("select * from sqmdb_rpt.acl_user where deleted = 0 and test_user = 0 order by user_id")
     List<UserVo> baseList();
+
+    /**
+     * 查找不在login_name列表里且非测试用户未删除的user_id
+     *
+     * @param list login_name列表
+     * @return {@link List}<{@link Integer}>
+     */
+    @Select("<script>"
+            + " select user_id from sqmdb_rpt.acl_user"
+            + " where deleted = 0 and test_user = 0 and login_name not in"
+            + " <foreach open=\"(\" close=\")\" collection=\"list\" item=\"item\" separator=\",\">"
+            + "   #{item}"
+            + " </foreach>"
+            + " order by user_id"
+            + " </script>")
+    List<Integer> loginNameNotIn(Set<String> list);
+
+    /**
+     * 查找权限地市不一致的user_id
+     *
+     * @return {@link List}<{@link Integer}>
+     */
+    @Select("select distinct au.user_id"
+            + " from sqmdb_rpt.acl_user au"
+            + " inner join sqmdb_rpt.acl_user_role_city aurc on au.user_id = aurc.user_id"
+            + " where au.city_id != aurc.city_id"
+            + " and aurc.role_id != 3"
+            + " order by au.user_id")
+    List<Integer> roleCityDiff();
 }

+ 39 - 10
src/main/java/com/nokia/task/SyncTask.java

@@ -1,5 +1,6 @@
 package com.nokia.task;
 
+import com.alibaba.fastjson2.JSON;
 import com.jcraft.jsch.JSchException;
 import com.jcraft.jsch.SftpException;
 import com.nokia.common.exception.MyRuntimeException;
@@ -7,7 +8,9 @@ import com.nokia.common.gpload.GploadUtil;
 import com.nokia.common.gpload.entity.GploadResult;
 import com.nokia.common.ssh.SSHUtil;
 import com.nokia.dao.AreaDao;
+import com.nokia.dao.UserDao;
 import com.nokia.pojo.Area;
+import com.nokia.vo.UserVo;
 import com.xxl.job.core.context.XxlJobHelper;
 import com.xxl.job.core.handler.annotation.XxlJob;
 import lombok.Data;
@@ -50,9 +53,11 @@ public class SyncTask {
     private final DateFormat dateFormat = new SimpleDateFormat("yyyyMMddHH");
     private SSHUtil sshUtil;
     private final AreaDao areaDao;
+    private final UserDao userDao;
 
-    public SyncTask(AreaDao areaDao) {
+    public SyncTask(AreaDao areaDao, UserDao userDao) {
         this.areaDao = areaDao;
+        this.userDao = userDao;
     }
 
     /**
@@ -127,6 +132,7 @@ public class SyncTask {
         XxlJobHelper.log("文件 {} 去重", filename);
         String inputFilePath = downloadDir + filename;
         Path inputPath = Paths.get(inputFilePath);
+        List<Integer> modifiedUsers = new ArrayList<>();
         try (CSVParser parser = CSVFormat.DEFAULT.builder().build()
                 .parse(new InputStreamReader(Files.newInputStream(inputPath), "gbk"));
              OutputStreamWriter osw = new OutputStreamWriter(Files.newOutputStream(Paths.get(distinctFilename)),
@@ -145,6 +151,10 @@ public class SyncTask {
             if (CollectionUtils.isEmpty(map)) {
                 throw new MyRuntimeException("数据为空");
             }
+            // 查询所有用户
+            List<UserVo> allUsers = userDao.baseList();
+            Map<String, UserVo> userMap = new HashMap<>();
+            allUsers.forEach(t -> userMap.put(t.getLoginName(), t));
             // 查询地区
             List<Area> areas = areaDao.getAll();
             Map<String, Area> cityMap = new HashMap<>();
@@ -169,7 +179,7 @@ public class SyncTask {
                 String phone = t.get(5);
                 String employeeCode = t.get(6);
                 Integer provinceId = -1;
-                Integer cityId = null;
+                Integer cityId = -1;
                 Integer areaId = null;
                 for (Map.Entry<String, Area> tt : cityMap.entrySet()) {
                     // 匹配地市
@@ -187,16 +197,35 @@ public class SyncTask {
                         break;
                     }
                 }
-                printer.printRecord(loginName, orgId, orgName, userId, userName, phone, employeeCode, provinceId, cityId, areaId);
+                printer.printRecord(loginName, orgId, orgName, userId, userName, phone, employeeCode, provinceId,
+                        cityId, areaId);
+                // 检查用户信息变化
+                UserVo user = userMap.get(loginName);
+                if (user != null && !cityId.equals(user.getCityId())) {
+                    modifiedUsers.add(user.getUserId());
+                    log.debug("用户 {} 地市变化: {} -> {}", user.getUserId(), user.getCityId(), cityId);
+                }
+            }
+            log.info("文件 {} 去重完成", filename);
+            XxlJobHelper.log("文件 {} 去重完成", filename);
+            // 删除本地源文件
+            Files.deleteIfExists(inputPath);
+            log.info("删除本地源文件 {}", filename);
+            XxlJobHelper.log("删除本地源文件 {}", filename);
+            // 检查删除用户
+            List<Integer> deletedUsers = userDao.loginNameNotIn(map.keySet());
+            if (!CollectionUtils.isEmpty(deletedUsers)) {
+                log.error("已删除用户: {}", JSON.toJSONString(deletedUsers));
+            }
+            // 检查权限地市
+            List<Integer> diffUsers = userDao.roleCityDiff();
+            if (!CollectionUtils.isEmpty(diffUsers)) {
+                log.error("权限地市不一致用户: {}", JSON.toJSONString(diffUsers));
+            }
+            if (!CollectionUtils.isEmpty(modifiedUsers)) {
+                log.error("地市变化用户: {}", JSON.toJSONString(modifiedUsers));
             }
         }
-
-        log.info("文件 {} 去重完成", filename);
-        XxlJobHelper.log("文件 {} 去重完成", filename);
-        // 删除本地源文件
-        Files.deleteIfExists(inputPath);
-        log.info("删除本地源文件 {}", filename);
-        XxlJobHelper.log("删除本地源文件 {}", filename);
     }
 
     public void gpload() throws IOException {