| 
					
				 | 
			
			
				@@ -0,0 +1,136 @@ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+package com.tzld.longarticle.recommend.server.service; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+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.lang.math.RandomUtils; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import org.apache.commons.lang3.StringUtils; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import org.dom4j.Document; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import org.dom4j.Element; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import org.dom4j.io.SAXReader; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import org.springframework.beans.factory.annotation.Autowired; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import org.springframework.stereotype.Service; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import java.util.Collections; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import java.util.HashMap; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import java.util.List; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import java.util.Map; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+/** 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ * @author dyp 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ */ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+@Slf4j 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+@Service 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+public class UserManagementService { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    @Autowired 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    private WxUserManagementRemoteService wxUserManagementRemoteService; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    @Autowired 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    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) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            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 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     * 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     * <xml> 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     * <ToUserName><![CDATA[toUser]]></ToUserName> 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     * <FromUserName><![CDATA[FromUser]]></FromUserName> 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     * <CreateTime>123456789</CreateTime> 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     * <MsgType><![CDATA[event]]></MsgType> 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     * <Event><![CDATA[subscribe]]></Event> 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     * </xml> 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     */ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    public void listenWx(String xmlData) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        try { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            SAXReader saxReader = new SAXReader(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            Document document = saxReader.read(xmlData); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            Element root = document.getRootElement(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            Map<String, String> param = new HashMap<>(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            for (Element e : root.elements()) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                param.put(e.getName(), e.getTextTrim()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            String gzhId = param.get("ToUserName"); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            String openId = param.get("FromUserName"); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            if (StringUtils.equals("event", param.get("MsgType"))) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                String event = param.get("Event"); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                switch (event) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    case "subscribe": 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                        handleSubscribe(gzhId, openId); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                        break; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    case "unsubscribe": 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                        handleUnsubscribe(gzhId, openId); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                        break; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    default: 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                        break; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        } catch (Exception e) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            log.error("listenWx handle error {} ", xmlData, e); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    private void handleSubscribe(String gzhId, String openId) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        // 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        ArticleUserGroup group = articleUserGroupRepository.findFirstByGzhIdAndOpenId(gzhId, openId); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        if (group == null) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            // insert 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            // 查库,知道所有分组 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            List<Integer> ugIds = articleUserGroupRepository.findAllUserGroupId(gzhId); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            // 随机选一个组 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            int ugId = ugIds.get(RandomUtils.nextInt(ugIds.size())); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            group = new ArticleUserGroup(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            group.setGzhId(gzhId); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            group.setOpenId(openId); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            group.setUserGroupId(ugId); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            articleUserGroupRepository.save(group); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        } else { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            // update 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            group.setIsDelete(0); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            articleUserGroupRepository.save(group); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    private void handleUnsubscribe(String gzhId, String openId) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        ArticleUserGroup group = articleUserGroupRepository.findFirstByGzhIdAndOpenId(gzhId, openId); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        if (group != null) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            group.setIsDelete(1); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            articleUserGroupRepository.save(group); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    public List<String> listByUserGroupId(String gzhId, int userGroupId) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        return articleUserGroupRepository.findAllOpenId(gzhId, userGroupId); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+} 
			 |