123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166 |
- package com.nokia.task;
- import com.alibaba.fastjson2.JSON;
- 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.apache.commons.csv.CSVFormat;
- import org.apache.commons.csv.CSVParser;
- import org.apache.commons.csv.CSVRecord;
- import org.springframework.jdbc.core.JdbcTemplate;
- import org.springframework.stereotype.Component;
- import org.springframework.util.CollectionUtils;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.nio.charset.StandardCharsets;
- import java.nio.file.Files;
- import java.nio.file.Paths;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import java.util.concurrent.CompletableFuture;
- import java.util.concurrent.TimeUnit;
- @Data
- @Component
- @Slf4j
- public class CheckTask {
- private final JdbcTemplate jdbcTemplate;
- private final TaskConfig taskConfig;
- public CheckTask(JdbcTemplate jdbcTemplate, TaskConfig taskConfig) {
- this.jdbcTemplate = jdbcTemplate;
- this.taskConfig = taskConfig;
- }
- /**
- * 同步top用户信息定时任务
- */
- @XxlJob("check")
- public void check() {
- 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() throws IOException {
- List<Object> modifiedUsers = new ArrayList<>();
- try (CSVParser parser = CSVFormat.DEFAULT.builder().build()
- .parse(new InputStreamReader(Files.newInputStream(Paths.get(taskConfig.getDistinctFilename())),
- StandardCharsets.UTF_8))) {
- // Set<String> loginNames = new HashSet<>();
- Map<Object, Map<String, Object>> userMap = getUserMap();
- for (CSVRecord t : parser) {
- String loginName = t.get(0);
- String orgName = t.get(2);
- String cityId = t.get(8);
- // loginNames.add(loginName);
- checkUserInfoChanged(modifiedUsers, userMap, orgName, loginName, cityId);
- }
- // checkDeletedUser(loginNames);
- checkRoleCity();
- if (!CollectionUtils.isEmpty(modifiedUsers)) {
- log.error("信息变化用户: {}", JSON.toJSONString(modifiedUsers));
- }
- }
- }
- /**
- * 获取用户地图
- *
- * @return {@link Map}<{@link Object}, {@link Map}<{@link String}, {@link Object}>>
- */
- private Map<Object, Map<String, Object>> getUserMap() {
- // 查询所有用户,过滤已删除和测试用户
- String sql = "select user_id, login_name, city_id, org from sqmdb_rpt.acl_user"
- + " where deleted = 0 and test_user = 0 order by user_id";
- List<Map<String, Object>> allUsers = jdbcTemplate.queryForList(sql);
- Map<Object, Map<String, Object>> userMap = new HashMap<>();
- allUsers.forEach(t -> userMap.put(t.get("login_name"), t));
- return userMap;
- }
- /**
- * 检查用户信息改变了
- *
- * @param modifiedUsers 修改用户
- * @param userMap 用户映射
- * @param orgName 组织名字
- * @param loginName 登录名
- * @param cityId 城市标识
- */
- private void checkUserInfoChanged(List<Object> modifiedUsers, Map<Object, Map<String, Object>> userMap,
- String orgName, String loginName, String cityId) {
- // 检查用户信息变化
- Map<String, Object> user = userMap.get(loginName);
- // boolean infoChanged = user != null
- // && (!cityId.equals(String.valueOf(user.get("city_id"))) || !orgName.equals(user.get("org")));
- boolean infoChanged = user != null
- && !cityId.equals(String.valueOf(user.get("city_id")));
- if (infoChanged) {
- modifiedUsers.add(user.get("user_id"));
- log.warn("用户 {} 信息变化: {} -> {}, {} -> {}", user.get("user_id"), user.get("city_id"), cityId,
- user.get("org"), orgName);
- XxlJobHelper.log("用户 {} 信息变化: {} -> {}, {} -> {}", user.get("user_id"),
- user.get("city_id"), cityId, user.get("org"), orgName);
- }
- }
- /**
- * 检查角色城市和归属地市是否一致
- */
- private void checkRoleCity() {
- String sql = "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> diffUsers = jdbcTemplate.queryForList(sql, Integer.class);
- if (!CollectionUtils.isEmpty(diffUsers)) {
- log.error("权限地市不一致用户: {}", JSON.toJSONString(diffUsers));
- }
- }
- // /**
- // * 检查删除用户
- // *
- // * @param loginNames 账号列表
- // */
- // private void checkDeletedUser(Set<String> loginNames) {
- // String sql = "select user_id from sqmdb_rpt.acl_user"
- // + " where deleted = 0 and test_user = 0 and login_name not in (:loginNames)"
- // + " order by user_id";
- // Map<String, Object> paramMap = new HashMap<>();
- // paramMap.put("loginNames", loginNames);
- // NamedParameterJdbcTemplate namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(jdbcTemplate);
- // List<Integer> deletedUsers = namedParameterJdbcTemplate.queryForList(sql, paramMap, Integer.class);
- // if (!CollectionUtils.isEmpty(deletedUsers)) {
- // log.error("已删除用户: {}", JSON.toJSONString(deletedUsers));
- // XxlJobHelper.log("已删除用户: {}", JSON.toJSONString(deletedUsers));
- // }
- // }
- }
|