| 
					
				 | 
			
			
				@@ -1,11 +1,12 @@ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 package com.tzld.longarticle.recommend.server.service; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import com.tzld.longarticle.recommend.server.model.WxUserGetResponse; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 import com.tzld.longarticle.recommend.server.remote.WxUserManagementRemoteService; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 import com.tzld.longarticle.recommend.server.repository.crawler.ArticleUserGroupRepository; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 import com.tzld.longarticle.recommend.server.repository.entity.crawler.ArticleUserGroup; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 import com.tzld.longarticle.recommend.server.util.CommonCollectionUtils; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 import lombok.extern.slf4j.Slf4j; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-import org.apache.commons.collections4.ListUtils; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import org.apache.commons.collections4.CollectionUtils; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 import org.apache.commons.lang.math.RandomUtils; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 import org.apache.commons.lang3.StringUtils; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 import org.dom4j.Document; 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -31,33 +32,89 @@ public class UserManagementService { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     private ArticleUserGroupRepository articleUserGroupRepository; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     public void addGZH(String gzhId, int groupNum) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        List<String> openIds = wxUserManagementRemoteService.getAllUser(gzhId); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        // 相对均匀 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        Collections.shuffle(openIds); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        int size = openIds.size() / groupNum; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        List<List<String>> partition = ListUtils.partition(openIds, size); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        for (int i = 0; i < (openIds.size() % groupNum); i++) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            partition.get(i).add(openIds.get(size - i - 1)); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        // 写入DB 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        int ugId = 0; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        for (List<String> partOpenIds : partition) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        // syncAllUserOnce(gzhId, groupNum); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        // 同步用户 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        log.info("start sync user gzhId={}", gzhId); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            int finalUgId = ugId; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            List<ArticleUserGroup> userGroups = CommonCollectionUtils.toList(partOpenIds, s -> { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        WxUserGetResponse response = wxUserManagementRemoteService.getAllUserLimit10000(gzhId); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        if (response == null) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            return; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        int total = response.getTotal(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        int count = response.getCount(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        log.info("sync user gzhId={} total={} count={}", gzhId, total, count); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        List<String> openIds = response.getData() == null 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                ? Collections.emptyList() 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                : response.getData().getOpenid(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        String nextOpenId = response.getNext_openid(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        if (CollectionUtils.isNotEmpty(openIds)) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            List<ArticleUserGroup> userGroups = CommonCollectionUtils.toList(openIds, s -> { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                 ArticleUserGroup ug = new ArticleUserGroup(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                 ug.setGzhId(gzhId); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                 ug.setOpenId(s); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                ug.setUserGroupId(finalUgId); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                ug.setUserGroupId(RandomUtils.nextInt(groupNum)); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                 return ug; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             }); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             articleUserGroupRepository.saveAll(userGroups); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            ugId++; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        while (StringUtils.isNotBlank(nextOpenId)) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            response = wxUserManagementRemoteService.getAllUserLimit10000(gzhId, nextOpenId); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            if (response == null) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                return; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            openIds = response.getData() == null 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    ? Collections.emptyList() 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    : response.getData().getOpenid(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            nextOpenId = response.getNext_openid(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            count += response.getCount(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            log.info("sync user gzhId={} total={} count={}", gzhId, total, count); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            if (CollectionUtils.isNotEmpty(openIds)) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                List<ArticleUserGroup> userGroups = CommonCollectionUtils.toList(openIds, s -> { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    ArticleUserGroup ug = new ArticleUserGroup(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    ug.setGzhId(gzhId); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    ug.setOpenId(s); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    ug.setUserGroupId(RandomUtils.nextInt(groupNum)); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    return ug; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                }); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                articleUserGroupRepository.saveAll(userGroups); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        log.info("end sync user gzhId={}", gzhId); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+//    private void syncAllUserOnce(String gzhId, int groupNum) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+//        List<String> openIds = wxUserManagementRemoteService.getAllUser(gzhId); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+//        // 相对均匀 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+//        Collections.shuffle(openIds); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+//        int size = openIds.size() / groupNum; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+//        List<List<String>> partition = ListUtils.partition(openIds, size); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+//        for (int i = 0; i < (openIds.size() % groupNum); i++) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+//            partition.get(i).add(openIds.get(size - i - 1)); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+//        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+// 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+//        // 写入DB 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+//        int ugId = 0; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+//        for (List<String> partOpenIds : partition) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+// 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+//            int finalUgId = ugId; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+//            List<ArticleUserGroup> userGroups = CommonCollectionUtils.toList(partOpenIds, s -> { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+//                ArticleUserGroup ug = new ArticleUserGroup(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+//                ug.setGzhId(gzhId); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+//                ug.setOpenId(s); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+//                ug.setUserGroupId(finalUgId); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+//                return ug; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+//            }); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+//            articleUserGroupRepository.saveAll(userGroups); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+//            ugId++; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+//        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+//    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     /** 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				      * https://developers.weixin.qq.com/doc/offiaccount/Message_Management/Receiving_event_pushes.html 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				      * 
			 |