소스 검색

Merge branch 'dev-xym-updateUser' into dev-xym-refresh-token

# Conflicts:
#	we-com-server/src/main/java/com/tzld/piaoquan/wecom/job/WeComUserDataJob.java
xueyiming 7 달 전
부모
커밋
cad6f5659a
1개의 변경된 파일96개의 추가작업 그리고 78개의 파일을 삭제
  1. 96 78
      we-com-server/src/main/java/com/tzld/piaoquan/wecom/job/WeComUserDataJob.java

+ 96 - 78
we-com-server/src/main/java/com/tzld/piaoquan/wecom/job/WeComUserDataJob.java

@@ -23,6 +23,8 @@ public class WeComUserDataJob {
 
     String UPDATE_USER_URL = "https://open.weibanzhushou.com/open-api/external_user/update/list";
 
+    String ACCESS_TOKEN = "38969e430219e6b18f88792184a37b11";
+
     private static final HttpPoolClient httpPoolClientDefault = HttpClientUtil.create(30000, 30000, 2000, 5000, 5, 30000);
 
     final int size = 100;
@@ -34,51 +36,57 @@ public class WeComUserDataJob {
     private AccessTokenService accessTokenService;
 
     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(size, i * size, 1720540800L, endTime);
+                String res = getUser(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(1, 0, startTime, endTime);
+        JSONObject jsonObject = JSONObject.parseObject(res);
+        return jsonObject.getInteger("total");
+    }
+
     public String getUser(Integer limit, Integer offset, Long startTime, Long endTime) throws IOException {
         String accessToken = accessTokenService.getAccessToken();
         String url = GET_USER_URL
@@ -89,60 +97,70 @@ public class WeComUserDataJob {
 
 
     public void updateUser() {
-        Long endTime = System.currentTimeMillis() / 1000;
-        Long startTime = endTime - 60 * 60;
-        int sum = 0;
-        for (int i = 0; i < 1000; i++) {
-            String res = null;
-            try {
-                res = getUpdateUser(size, i * size, startTime, endTime);
-                //TODO 记录查询数据 info日志
-            } catch (IOException e) {
-                //TODO 记录异常日志 记录起止时间和页数 startTime endTime i
-                e.printStackTrace();
+        try {
+            Long endTime = System.currentTimeMillis() / 1000;
+            Long startTime = endTime - 60 * 60;
+            Integer total = getUpdateUserTotal(startTime, endTime);
+            if (total == null || total == 0) {
+                return;
             }
-            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;
-                    }
+            int page = total / size + 1;
+            int sum = 0;
+            for (int i = 0; i < page; i++) {
+                //TODO accessToken 暂时写死 后续增加接口刷新和存储
+                String res = getUpdateUser(size, i * size, 1726821389L, endTime);
+                //TODO 记录查询数据 info日志
+                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(1, 0, startTime, endTime);
+        JSONObject jsonObject = JSONObject.parseObject(res);
+        return jsonObject.getInteger("total");
+    }
+
     public String getUpdateUser(Integer limit, Integer offset, Long startTime, Long endTime) throws IOException {
         String accessToken = accessTokenService.getAccessToken();
         String url = UPDATE_USER_URL