CheckTask.java 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. package com.nokia.task;
  2. import com.alibaba.fastjson2.JSON;
  3. import com.nokia.common.exception.MyRuntimeException;
  4. import com.nokia.config.TaskConfig;
  5. import com.xxl.job.core.context.XxlJobHelper;
  6. import com.xxl.job.core.handler.annotation.XxlJob;
  7. import lombok.Data;
  8. import lombok.extern.slf4j.Slf4j;
  9. import org.apache.commons.csv.CSVFormat;
  10. import org.apache.commons.csv.CSVParser;
  11. import org.apache.commons.csv.CSVRecord;
  12. import org.springframework.jdbc.core.JdbcTemplate;
  13. import org.springframework.stereotype.Component;
  14. import org.springframework.util.CollectionUtils;
  15. import java.io.IOException;
  16. import java.io.InputStreamReader;
  17. import java.nio.charset.StandardCharsets;
  18. import java.nio.file.Files;
  19. import java.nio.file.Paths;
  20. import java.util.ArrayList;
  21. import java.util.HashMap;
  22. import java.util.List;
  23. import java.util.Map;
  24. import java.util.concurrent.CompletableFuture;
  25. import java.util.concurrent.TimeUnit;
  26. @Data
  27. @Component
  28. @Slf4j
  29. public class CheckTask {
  30. private final JdbcTemplate jdbcTemplate;
  31. private final TaskConfig taskConfig;
  32. public CheckTask(JdbcTemplate jdbcTemplate, TaskConfig taskConfig) {
  33. this.jdbcTemplate = jdbcTemplate;
  34. this.taskConfig = taskConfig;
  35. }
  36. /**
  37. * 同步top用户信息定时任务
  38. */
  39. @XxlJob("check")
  40. public void check() {
  41. try {
  42. CompletableFuture.runAsync(() -> {
  43. try {
  44. singleTask();
  45. } catch (Exception e) {
  46. throw new MyRuntimeException(e);
  47. }
  48. }).get(5, TimeUnit.MINUTES);
  49. } catch (InterruptedException e) {
  50. log.error("线程中断: {}", e.getMessage(), e);
  51. XxlJobHelper.log("线程中断: {}", e.getMessage(), e);
  52. Thread.currentThread().interrupt();
  53. XxlJobHelper.handleFail(e.getMessage());
  54. } catch (Exception e) {
  55. log.error("发生异常了: {}", e.getMessage(), e);
  56. XxlJobHelper.log("发生异常了: {}", e.getMessage(), e);
  57. XxlJobHelper.handleFail(e.getMessage());
  58. }
  59. }
  60. /**
  61. * 单一任务
  62. */
  63. public void singleTask() throws IOException {
  64. List<Object> modifiedUsers = new ArrayList<>();
  65. try (CSVParser parser = CSVFormat.DEFAULT.builder().build()
  66. .parse(new InputStreamReader(Files.newInputStream(Paths.get(taskConfig.getDistinctFilename())),
  67. StandardCharsets.UTF_8))) {
  68. // Set<String> loginNames = new HashSet<>();
  69. Map<Object, Map<String, Object>> userMap = getUserMap();
  70. for (CSVRecord t : parser) {
  71. String loginName = t.get(0);
  72. String orgName = t.get(2);
  73. String cityId = t.get(8);
  74. // loginNames.add(loginName);
  75. checkUserInfoChanged(modifiedUsers, userMap, orgName, loginName, cityId);
  76. }
  77. // checkDeletedUser(loginNames);
  78. checkRoleCity();
  79. if (!CollectionUtils.isEmpty(modifiedUsers)) {
  80. log.error("信息变化用户: {}", JSON.toJSONString(modifiedUsers));
  81. }
  82. }
  83. }
  84. /**
  85. * 获取用户地图
  86. *
  87. * @return {@link Map}<{@link Object}, {@link Map}<{@link String}, {@link Object}>>
  88. */
  89. private Map<Object, Map<String, Object>> getUserMap() {
  90. // 查询所有用户,过滤已删除和测试用户
  91. String sql = "select user_id, login_name, city_id, org from sqmdb_rpt.acl_user"
  92. + " where deleted = 0 and test_user = 0 order by user_id";
  93. List<Map<String, Object>> allUsers = jdbcTemplate.queryForList(sql);
  94. Map<Object, Map<String, Object>> userMap = new HashMap<>();
  95. allUsers.forEach(t -> userMap.put(t.get("login_name"), t));
  96. return userMap;
  97. }
  98. /**
  99. * 检查用户信息改变了
  100. *
  101. * @param modifiedUsers 修改用户
  102. * @param userMap 用户映射
  103. * @param orgName 组织名字
  104. * @param loginName 登录名
  105. * @param cityId 城市标识
  106. */
  107. private void checkUserInfoChanged(List<Object> modifiedUsers, Map<Object, Map<String, Object>> userMap,
  108. String orgName, String loginName, String cityId) {
  109. // 检查用户信息变化
  110. Map<String, Object> user = userMap.get(loginName);
  111. // boolean infoChanged = user != null
  112. // && (!cityId.equals(String.valueOf(user.get("city_id"))) || !orgName.equals(user.get("org")));
  113. boolean infoChanged = user != null
  114. && !cityId.equals(String.valueOf(user.get("city_id")));
  115. if (infoChanged) {
  116. modifiedUsers.add(user.get("user_id"));
  117. log.warn("用户 {} 信息变化: {} -> {}, {} -> {}", user.get("user_id"), user.get("city_id"), cityId,
  118. user.get("org"), orgName);
  119. XxlJobHelper.log("用户 {} 信息变化: {} -> {}, {} -> {}", user.get("user_id"),
  120. user.get("city_id"), cityId, user.get("org"), orgName);
  121. }
  122. }
  123. /**
  124. * 检查角色城市和归属地市是否一致
  125. */
  126. private void checkRoleCity() {
  127. String sql = "select distinct au.user_id"
  128. + " from sqmdb_rpt.acl_user au"
  129. + " inner join sqmdb_rpt.acl_user_role_city aurc on au.user_id = aurc.user_id"
  130. + " where au.city_id != aurc.city_id"
  131. + " and aurc.role_id != 3"
  132. + " order by au.user_id";
  133. List<Integer> diffUsers = jdbcTemplate.queryForList(sql, Integer.class);
  134. if (!CollectionUtils.isEmpty(diffUsers)) {
  135. log.error("权限地市不一致用户: {}", JSON.toJSONString(diffUsers));
  136. }
  137. }
  138. // /**
  139. // * 检查删除用户
  140. // *
  141. // * @param loginNames 账号列表
  142. // */
  143. // private void checkDeletedUser(Set<String> loginNames) {
  144. // String sql = "select user_id from sqmdb_rpt.acl_user"
  145. // + " where deleted = 0 and test_user = 0 and login_name not in (:loginNames)"
  146. // + " order by user_id";
  147. // Map<String, Object> paramMap = new HashMap<>();
  148. // paramMap.put("loginNames", loginNames);
  149. // NamedParameterJdbcTemplate namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(jdbcTemplate);
  150. // List<Integer> deletedUsers = namedParameterJdbcTemplate.queryForList(sql, paramMap, Integer.class);
  151. // if (!CollectionUtils.isEmpty(deletedUsers)) {
  152. // log.error("已删除用户: {}", JSON.toJSONString(deletedUsers));
  153. // XxlJobHelper.log("已删除用户: {}", JSON.toJSONString(deletedUsers));
  154. // }
  155. // }
  156. }