|  | @@ -1,11 +1,14 @@
 | 
	
		
			
				|  |  |  package com.tzld.longarticle.recommend.server.service;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -import com.tzld.longarticle.recommend.server.model.vo.WxUserGetResponse;
 | 
	
		
			
				|  |  | -import com.tzld.longarticle.recommend.server.remote.WxUserManagementRemoteService;
 | 
	
		
			
				|  |  |  import com.tzld.longarticle.recommend.server.mapper.crawler.ArticleGzhDeveloperMapper;
 | 
	
		
			
				|  |  |  import com.tzld.longarticle.recommend.server.mapper.crawler.ArticleUserGroupMapper;
 | 
	
		
			
				|  |  | +import com.tzld.longarticle.recommend.server.mapper.crawler.ext.ArticleUserGroupMapperExt;
 | 
	
		
			
				|  |  | +import com.tzld.longarticle.recommend.server.model.dto.UserGroupCountDTO;
 | 
	
		
			
				|  |  | +import com.tzld.longarticle.recommend.server.model.vo.WxUserGetResponse;
 | 
	
		
			
				|  |  | +import com.tzld.longarticle.recommend.server.remote.WxUserManagementRemoteService;
 | 
	
		
			
				|  |  |  import com.tzld.longarticle.recommend.server.repository.model.ArticleGzhDeveloper;
 | 
	
		
			
				|  |  |  import com.tzld.longarticle.recommend.server.repository.model.ArticleUserGroup;
 | 
	
		
			
				|  |  | +import com.tzld.longarticle.recommend.server.repository.model.ArticleUserGroupExample;
 | 
	
		
			
				|  |  |  import com.tzld.longarticle.recommend.server.util.CommonCollectionUtils;
 | 
	
		
			
				|  |  |  import lombok.extern.slf4j.Slf4j;
 | 
	
		
			
				|  |  |  import org.apache.commons.collections4.CollectionUtils;
 | 
	
	
		
			
				|  | @@ -15,10 +18,14 @@ import org.dom4j.Document;
 | 
	
		
			
				|  |  |  import org.dom4j.Element;
 | 
	
		
			
				|  |  |  import org.dom4j.io.SAXReader;
 | 
	
		
			
				|  |  |  import org.springframework.beans.factory.annotation.Autowired;
 | 
	
		
			
				|  |  | +import org.springframework.beans.factory.annotation.Value;
 | 
	
		
			
				|  |  |  import org.springframework.stereotype.Service;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  import java.io.StringReader;
 | 
	
		
			
				|  |  | -import java.util.*;
 | 
	
		
			
				|  |  | +import java.util.Collections;
 | 
	
		
			
				|  |  | +import java.util.HashMap;
 | 
	
		
			
				|  |  | +import java.util.List;
 | 
	
		
			
				|  |  | +import java.util.Map;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  /**
 | 
	
		
			
				|  |  |   * @author dyp
 | 
	
	
		
			
				|  | @@ -32,9 +39,18 @@ public class UserManagementService {
 | 
	
		
			
				|  |  |      @Autowired
 | 
	
		
			
				|  |  |      private ArticleUserGroupMapper articleUserGroupMapper;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | +    @Autowired
 | 
	
		
			
				|  |  | +    private ArticleUserGroupMapperExt articleUserGroupMapperExt;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |      @Autowired
 | 
	
		
			
				|  |  |      private ArticleGzhDeveloperMapper articleGzhDeveloperMapper;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | +    @Value("${gzh.user.group.size:8}")
 | 
	
		
			
				|  |  | +    private Integer gzhUserGroupSize;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    @Value("${gzh.user.group.per.count:10000}")
 | 
	
		
			
				|  |  | +    private Integer gzhUserGroupPerCount;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |      public void addGZH(String gzhId, String appId, int groupNum) {
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |          // syncAllUserOnce(gzhId, groupNum);
 | 
	
	
		
			
				|  | @@ -65,7 +81,7 @@ public class UserManagementService {
 | 
	
		
			
				|  |  |              });
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |              //articleUserGroupRepository.saveAll(userGroups);
 | 
	
		
			
				|  |  | -            articleUserGroupMapper.insertBatch(userGroups);
 | 
	
		
			
				|  |  | +            articleUserGroupMapperExt.insertBatch(userGroups);
 | 
	
		
			
				|  |  |          }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |          while (StringUtils.isNotBlank(nextOpenId)) {
 | 
	
	
		
			
				|  | @@ -86,10 +102,9 @@ public class UserManagementService {
 | 
	
		
			
				|  |  |                      ug.setOpenId(s);
 | 
	
		
			
				|  |  |                      ug.setUserGroupId(RandomUtils.nextInt(groupNum));
 | 
	
		
			
				|  |  |                      ug.setIsDelete(0);
 | 
	
		
			
				|  |  | -                    ug.setUserGroupId(RandomUtils.nextInt(groupNum));
 | 
	
		
			
				|  |  |                      return ug;
 | 
	
		
			
				|  |  |                  });
 | 
	
		
			
				|  |  | -                articleUserGroupMapper.insertBatch(userGroups);
 | 
	
		
			
				|  |  | +                articleUserGroupMapperExt.insertBatch(userGroups);
 | 
	
		
			
				|  |  |                  // articleUserGroupRepository.saveAll(userGroups);
 | 
	
		
			
				|  |  |              }
 | 
	
		
			
				|  |  |          }
 | 
	
	
		
			
				|  | @@ -101,7 +116,29 @@ public class UserManagementService {
 | 
	
		
			
				|  |  |          obj.setGzhId(gzhId);
 | 
	
		
			
				|  |  |          articleGzhDeveloperMapper.insert(obj);
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | +        resetUserGroup(gzhId);
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    private void resetUserGroup(String gzhId) {
 | 
	
		
			
				|  |  | +        Long count = getGzhUserCount(gzhId);
 | 
	
		
			
				|  |  | +        long pageSize = gzhUserGroupPerCount;
 | 
	
		
			
				|  |  | +        int pageNum = 1;
 | 
	
		
			
				|  |  | +        if (count < ((long) gzhUserGroupSize * gzhUserGroupPerCount)) {
 | 
	
		
			
				|  |  | +            pageSize = (count / gzhUserGroupSize) + 1;
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        int groupId = 0;
 | 
	
		
			
				|  |  | +        while (true) {
 | 
	
		
			
				|  |  | +            int offset = (pageNum - 1) * (int) pageSize;
 | 
	
		
			
				|  |  | +            List<ArticleUserGroup> userGroups = articleUserGroupMapperExt.selectByGzhIdAndPage(gzhId, offset, pageSize);
 | 
	
		
			
				|  |  | +            if (CollectionUtils.isEmpty(userGroups)) {
 | 
	
		
			
				|  |  | +                break;
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | +            pageNum++;
 | 
	
		
			
				|  |  | +            List<Long> ids = CommonCollectionUtils.toList(userGroups, ArticleUserGroup::getId);
 | 
	
		
			
				|  |  | +            articleUserGroupMapperExt.updateUserGroup(ids, groupId);
 | 
	
		
			
				|  |  | +            groupId++;
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  //    private void syncAllUserOnce(String gzhId, int groupNum) {
 | 
	
	
		
			
				|  | @@ -182,13 +219,14 @@ public class UserManagementService {
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |      private void handleSubscribe(String gzhId, String openId) {
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -        ArticleUserGroup group = articleUserGroupMapper.selectByGzhIdAndOpenId(gzhId, openId);
 | 
	
		
			
				|  |  | +        ArticleUserGroup group = articleUserGroupMapperExt.selectByGzhIdAndOpenId(gzhId, openId);
 | 
	
		
			
				|  |  | +        Long count = getGzhUserCount(gzhId);
 | 
	
		
			
				|  |  |          if (group == null) {
 | 
	
		
			
				|  |  |              // insert
 | 
	
		
			
				|  |  |              // 查库,知道所有分组
 | 
	
		
			
				|  |  | -            List<Integer> ugIds = articleUserGroupMapper.selectUserGroupIdByGzhId(gzhId);
 | 
	
		
			
				|  |  | -            // 随机选一个组
 | 
	
		
			
				|  |  | -            int ugId = ugIds.get(RandomUtils.nextInt(ugIds.size()));
 | 
	
		
			
				|  |  | +            List<UserGroupCountDTO> userGroupCountDTOList = articleUserGroupMapperExt.selectUserGroupIdByGzhId(gzhId);
 | 
	
		
			
				|  |  | +            // 选择分组
 | 
	
		
			
				|  |  | +            int ugId = getUserGroupId(userGroupCountDTOList, count);
 | 
	
		
			
				|  |  |              group = new ArticleUserGroup();
 | 
	
		
			
				|  |  |              group.setGzhId(gzhId);
 | 
	
		
			
				|  |  |              group.setOpenId(openId);
 | 
	
	
		
			
				|  | @@ -203,8 +241,32 @@ public class UserManagementService {
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | +    private Long getGzhUserCount(String gzhId) {
 | 
	
		
			
				|  |  | +        ArticleUserGroupExample countExample = new ArticleUserGroupExample();
 | 
	
		
			
				|  |  | +        countExample.createCriteria().andGzhIdEqualTo(gzhId);
 | 
	
		
			
				|  |  | +        return articleUserGroupMapper.countByExample(countExample);
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    private Integer getUserGroupId(List<UserGroupCountDTO> userGroupCountDTOList, long count) {
 | 
	
		
			
				|  |  | +        if (count >= ((long) gzhUserGroupSize * gzhUserGroupPerCount)
 | 
	
		
			
				|  |  | +                || userGroupCountDTOList.size() > gzhUserGroupSize) {
 | 
	
		
			
				|  |  | +            UserGroupCountDTO max = userGroupCountDTOList.get(userGroupCountDTOList.size() - 1);
 | 
	
		
			
				|  |  | +            if (max.getCount() < gzhUserGroupPerCount) {
 | 
	
		
			
				|  |  | +                return max.getUserGroupId();
 | 
	
		
			
				|  |  | +            } else {
 | 
	
		
			
				|  |  | +                return max.getUserGroupId() + 1;
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +        } else {
 | 
	
		
			
				|  |  | +            int ugId = RandomUtils.nextInt(userGroupCountDTOList.size());
 | 
	
		
			
				|  |  | +            while (userGroupCountDTOList.get(ugId).getCount() >= gzhUserGroupPerCount) {
 | 
	
		
			
				|  |  | +                ugId = RandomUtils.nextInt(userGroupCountDTOList.size());
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +            return ugId;
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |      private void handleUnsubscribe(String gzhId, String openId) {
 | 
	
		
			
				|  |  | -        ArticleUserGroup group = articleUserGroupMapper.selectByGzhIdAndOpenId(gzhId, openId);
 | 
	
		
			
				|  |  | +        ArticleUserGroup group = articleUserGroupMapperExt.selectByGzhIdAndOpenId(gzhId, openId);
 | 
	
		
			
				|  |  |          if (group != null) {
 | 
	
		
			
				|  |  |              group.setIsDelete(1);
 | 
	
		
			
				|  |  |              articleUserGroupMapper.updateByPrimaryKey(group);
 | 
	
	
		
			
				|  | @@ -212,6 +274,6 @@ public class UserManagementService {
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |      public List<String> listByUserGroupId(String gzhId, int userGroupId) {
 | 
	
		
			
				|  |  | -        return articleUserGroupMapper.selectOpenIdByGzhIdAndUserGroupId(gzhId, userGroupId);
 | 
	
		
			
				|  |  | +        return articleUserGroupMapperExt.selectOpenIdByGzhIdAndUserGroupId(gzhId, userGroupId);
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  |  }
 |