浏览代码

修改获取用户信息任务

xueyiming 7 月之前
父节点
当前提交
96d50c151c
共有 1 个文件被更改,包括 93 次插入77 次删除
  1. 93 77
      we-com-server/src/main/java/com/tzld/piaoquan/wecom/job/WeComUserDataJob.java

+ 93 - 77
we-com-server/src/main/java/com/tzld/piaoquan/wecom/job/WeComUserDataJob.java

@@ -32,51 +32,57 @@ public class WeComUserDataJob {
     private UserMapper userMapper;
 
     public void insertAllUser() {
-        Long endTime = System.currentTimeMillis() / 1000;
-        int sum = 0;
-        for (int i = 0; i < 1000; i++) {
-            String res = null;
-            try {
+        try {
+            Long endTime = System.currentTimeMillis() / 1000;
+            Long startTime = 1720540800L;
+            Integer total = getUserTotal(startTime, endTime);
+            if (total == null || total == 0) {
+                return;
+            }
+            int page = total / size + 1;
+            int sum = 0;
+            for (int i = 0; i < page; i++) {
                 //TODO accessToken 暂时写死 后续增加接口刷新和存储
-                res = getUser(ACCESS_TOKEN, size, i * size, 1720540800L, endTime);
+                String res = getUser(ACCESS_TOKEN, size, i * size, 1720540800L, endTime);
                 //TODO 记录查询数据 info日志
-            } catch (IOException e) {
-                //TODO 记录异常日志 记录起止时间和页数 startTime endTime i
-                e.printStackTrace();
-            }
-            if (ObjectUtils.isEmpty(res)) {
-                continue;
-            }
-            JSONObject jsonObject = JSONObject.parseObject(res);
-            Integer total = (Integer) jsonObject.get("total");
-            JSONArray jsonArray = jsonObject.getJSONArray("external_user_list");
-            for (int j = 0; j < jsonArray.size(); j++) {
-                String id = (String) jsonArray.getJSONObject(j).get("id");
-                jsonArray.getJSONObject(j).put("id", null);
-                User user = jsonArray.getJSONObject(j).toJavaObject(User.class);
-                user.setExternalUserId3rdParty(id);
-                //TODO 记录对象数据  info日志
-                try {
+                if (ObjectUtils.isEmpty(res)) {
+                    continue;
+                }
+                JSONObject jsonObject = JSONObject.parseObject(res);
+                JSONArray jsonArray = jsonObject.getJSONArray("external_user_list");
+                for (int j = 0; j < jsonArray.size(); j++) {
+                    String id = (String) jsonArray.getJSONObject(j).get("id");
+                    jsonArray.getJSONObject(j).put("id", null);
+                    User user = jsonArray.getJSONObject(j).toJavaObject(User.class);
+                    user.setExternalUserId3rdParty(id);
+                    //TODO 记录对象数据  info日志
                     int insert = userMapper.insert(user);
                     if (insert <= 0) {
                         //TODO  异常日志
                         continue;
                     }
-                } catch (Exception e) {
-                    //TODO 打印异常日志
+                    sum++;
                 }
-                sum++;
-            }
-            if (jsonArray.size() < size) {
-                if (total != null && total > sum) {
-                    //TODO 输出异常  插入数量不足
-                    System.out.println("插入数量不足");
+                if (jsonArray.size() < size) {
+                    if (total > sum) {
+                        //TODO 输出异常  插入数量不足
+                        System.out.println("插入数量不足");
+                    }
+                    break;
                 }
-                break;
             }
+        } catch (Exception e) {
+            //TODO 记录异常日志
+            e.printStackTrace();
         }
     }
 
+    public Integer getUserTotal(Long startTime, Long endTime) throws IOException {
+        String res = getUser(ACCESS_TOKEN, 1, 0, startTime, endTime);
+        JSONObject jsonObject = JSONObject.parseObject(res);
+        return jsonObject.getInteger("total");
+    }
+
     public String getUser(String accessToken, Integer limit, Integer offset, Long startTime, Long endTime) throws IOException {
         String url = GET_USER_URL
                 + "?access_token=" + accessToken
@@ -86,60 +92,70 @@ public class WeComUserDataJob {
 
 
     public void updateUser() {
-        Long endTime = System.currentTimeMillis() / 1000;
-        int sum = 0;
-        for (int i = 0; i < 1000; i++) {
-            String res = null;
-            try {
+        try {
+            Long endTime = System.currentTimeMillis() / 1000;
+            Long startTime = endTime - 60 * 60;
+            Integer total = getUpdateUserTotal(startTime, endTime);
+            if (total == null || total == 0) {
+                return;
+            }
+            int page = total / size + 1;
+            int sum = 0;
+            for (int i = 0; i < page; i++) {
                 //TODO accessToken 暂时写死 后续增加接口刷新和存储
-                res = getUpdateUser(ACCESS_TOKEN, size, i * size, 1726821389L, endTime);
+                String res = getUpdateUser(ACCESS_TOKEN, size, i * size, 1726821389L, endTime);
                 //TODO 记录查询数据 info日志
-            } catch (IOException e) {
-                //TODO 记录异常日志 记录起止时间和页数 startTime endTime i
-                e.printStackTrace();
-            }
-            if (ObjectUtils.isEmpty(res)) {
-                continue;
-            }
-            JSONObject jsonObject = JSONObject.parseObject(res);
-            Integer total = (Integer) jsonObject.get("total");
-            JSONArray jsonArray = jsonObject.getJSONArray("external_user_list");
-            for (int j = 0; j < jsonArray.size(); j++) {
-                String id = (String) jsonArray.getJSONObject(j).get("id");
-                jsonArray.getJSONObject(j).put("id", null);
-                User user = jsonArray.getJSONObject(j).toJavaObject(User.class);
-                user.setExternalUserId3rdParty(id);
-                UserExample example = new UserExample();
-                example.createCriteria().andExternalUserId3rdPartyEqualTo(user.getExternalUserId3rdParty());
-                List<User> list = userMapper.selectByExample(example);
-                if (CollectionUtils.isEmpty(list)) {
-                    //没有用户,走插入逻辑
-                    int insert = userMapper.insert(user);
-                    if (insert <= 0) {
-                        //TODO  异常日志
-                        continue;
-                    }
-                } else {
-                    User oldUser = list.get(0);
-                    user.setId(oldUser.getId());
-                    int update = userMapper.updateByPrimaryKeySelective(user);
-                    if (update < 0) {
-                        //TODO  异常日志
-                        continue;
-                    }
+                if (ObjectUtils.isEmpty(res)) {
+                    continue;
                 }
-                sum++;
-                if (jsonArray.size() < size) {
-                    if (total != null && total > sum) {
-                        //TODO 输出异常  插入数量不足
-                        System.out.println("插入数量不足");
+                JSONObject jsonObject = JSONObject.parseObject(res);
+                JSONArray jsonArray = jsonObject.getJSONArray("external_user_list");
+                for (int j = 0; j < jsonArray.size(); j++) {
+                    String id = (String) jsonArray.getJSONObject(j).get("id");
+                    jsonArray.getJSONObject(j).put("id", null);
+                    User user = jsonArray.getJSONObject(j).toJavaObject(User.class);
+                    user.setExternalUserId3rdParty(id);
+                    UserExample example = new UserExample();
+                    example.createCriteria().andExternalUserId3rdPartyEqualTo(user.getExternalUserId3rdParty());
+                    List<User> list = userMapper.selectByExample(example);
+                    if (CollectionUtils.isEmpty(list)) {
+                        //没有用户,走插入逻辑
+                        int insert = userMapper.insert(user);
+                        if (insert <= 0) {
+                            //TODO  异常日志
+                            continue;
+                        }
+                    } else {
+                        User oldUser = list.get(0);
+                        user.setId(oldUser.getId());
+                        int update = userMapper.updateByPrimaryKeySelective(user);
+                        if (update < 0) {
+                            //TODO  异常日志
+                            continue;
+                        }
+                    }
+                    sum++;
+                    if (jsonArray.size() < size) {
+                        if (total > sum) {
+                            //TODO 输出异常  插入数量不足
+                            System.out.println("插入数量不足");
+                        }
+                        break;
                     }
-                    break;
                 }
             }
+        } catch (IOException e) {
+            //TODO 记录异常日志
+            e.printStackTrace();
         }
     }
 
+    public Integer getUpdateUserTotal(Long startTime, Long endTime) throws IOException {
+        String res = getUpdateUser(ACCESS_TOKEN, 1, 0, startTime, endTime);
+        JSONObject jsonObject = JSONObject.parseObject(res);
+        return jsonObject.getInteger("total");
+    }
+
     public String getUpdateUser(String accessToken, Integer limit, Integer offset, Long startTime, Long endTime) throws IOException {
         String url = UPDATE_USER_URL
                 + "?access_token=" + accessToken