Commit cbdadbdb by guanchen

修复代理商查找的潜在死循环问题

更新方法名称
parent 75e7fe9d
......@@ -42,17 +42,17 @@ public class AgentController {
return agentManager.pushReward();
}
@RequestMapping(value = "/hierarchy/parent", method = RequestMethod.POST)
public Result<ArrayList<ParentAgent>> getHierarchyParents(@RequestBody @Valid Agent agent, @RequestHeader HttpHeaders headers) {
public Result<ArrayList<ParentAgent>> getHierarchyParentList(@RequestBody @Valid Agent agent, @RequestHeader HttpHeaders headers) {
if (logger.isInfoEnabled()) {
logger.info("AgentController getHierarchyParents, header:{},time:{}", headers.keySet().toArray(), LocalDateTime.now());
logger.info("AgentController getHierarchyParentList, header:{},time:{}", headers.keySet().toArray(), LocalDateTime.now());
}
return agentManager.getHierarchyParents(agent.getAgentId());
return agentManager.getHierarchyParentList(agent.getAgentId());
}
@RequestMapping(value = "/pingtui/parent", method = RequestMethod.POST)
public Result<ArrayList<ParentAgent>> getPingtuiParents(@RequestBody @Valid Agent agent, @RequestHeader HttpHeaders headers) {
public Result<ArrayList<ParentAgent>> getPingtuiParentList(@RequestBody @Valid Agent agent, @RequestHeader HttpHeaders headers) {
if (logger.isInfoEnabled()) {
logger.info("AgentController getPingtuiParents, header:{},time:{}", headers.keySet().toArray(), LocalDateTime.now());
logger.info("AgentController getPingtuiParentList, header:{},time:{}", headers.keySet().toArray(), LocalDateTime.now());
}
return agentManager.getPingtuiParents(agent.getAgentId());
return agentManager.getPingtuiParentList(agent.getAgentId());
}
}
......@@ -36,5 +36,5 @@ public interface PartnerAccountMapper extends BaseMapper<PartnerAccount> {
"FROM partner_invite_relation a " +
"JOIN partner_account b on a.user_id=b.user_id " +
"WHERE a.invite_user_id=#{userId}")
List<PartnerAccount> getChildrenByUserId(Integer userId);
List<PartnerAccount> getChildListByUserId(Integer userId);
}
\ No newline at end of file
......@@ -40,8 +40,8 @@ public interface UserAgentMapper extends BaseMapper<UserAgent> {
// "FROM user_agent a " +
// "JOIN user_agent b on a.#{parentColumnName}=b.agent_id " +
// "WHERE a.user_id=#{userId}")
// List<UserAgent> getChildrenByUserId(Integer userId, String parentColumnName);
// List<UserAgent> getChildListByUserId(Integer userId, String parentColumnName);
@SelectProvider(type = UserAgentSqlProvider.class, method = "getChildrenByUserId")
List<UserAgent> getChildrenByUserId(@Param("userId") Integer userId, @Param("parentColumnName") String parentColumnName);
@SelectProvider(type = UserAgentSqlProvider.class, method = "getChildListByUserId")
List<UserAgent> getChildListByUserId(@Param("userId") Integer userId, @Param("parentColumnName") String parentColumnName);
}
\ No newline at end of file
......@@ -17,7 +17,7 @@ public class UserAgentSqlProvider {
"JOIN user_agent b on a." + parentColumnName + "=b.agent_id " +
"WHERE a.agent_id=#{agentId} and b.agent_level<=4";
}
public String getChildrenByUserId(@Param("userId") Integer userId, @Param("parentColumnName") String parentColumnName) {
public String getChildListByUserId(@Param("userId") Integer userId, @Param("parentColumnName") String parentColumnName) {
return "SELECT b.* " +
"FROM user_agent a " +
"JOIN user_agent b on a." + parentColumnName + "=b.agent_id " +
......
......@@ -79,7 +79,7 @@ public class AgentManager {
* @author chen
* @date 2019-07-04 14:48
*/
public Result<ArrayList<ParentAgent>> getHierarchyParents(Integer agentId) {
public Result<ArrayList<ParentAgent>> getHierarchyParentList(Integer agentId) {
ArrayList<ParentAgent> parentAgents = (ArrayList<ParentAgent>) userAgentService.getParentAgentListByAgentId(agentId, PARENT_COLUMN_NAME_CENGJI);
Result<ArrayList<ParentAgent>> result = new Result<ArrayList<ParentAgent>>();
result.setData(parentAgents);
......@@ -90,7 +90,7 @@ public class AgentManager {
* @author chen
* @date 2019-07-04 14:48
*/
public Result<ArrayList<ParentAgent>> getPingtuiParents(Integer agentId) {
public Result<ArrayList<ParentAgent>> getPingtuiParentList(Integer agentId) {
ArrayList<ParentAgent> parentAgents = (ArrayList<ParentAgent>) userAgentService.getParentAgentListByAgentId(agentId, PARENT_COLUMN_NAME_PINGTUI);
Result<ArrayList<ParentAgent>> result = new Result<ArrayList<ParentAgent>>();
result.setData(parentAgents);
......
......@@ -103,8 +103,8 @@ public class UserManager {
/**
* 先找到直接下级
*/
setChildren(tree);
ArrayList<PartnerTree> children = tree.getChildrenList();
setChildList(tree);
ArrayList<PartnerTree> children = tree.getChildList();
/**
* 如果没有下级了 那么直接输出结果
*/
......@@ -122,7 +122,7 @@ public class UserManager {
* 如果有 那么在各自的recursiveSet中 应该没有走到 line:52 的outputTree 方法
* 所以要在这里执行一次, 把他们自己输出
*/
if (child.getChildrenList().size() > 0) {
if (child.getChildList().size() > 0) {
outputTree(child);
}
}
......@@ -160,8 +160,8 @@ public class UserManager {
* 把下级的数据, 设置到输入的tree中的childrenList
* @param tree
*/
private void setChildren(PartnerTree tree) {
ArrayList<PartnerAccount> partnerAccountArrayList = (ArrayList<PartnerAccount>) userService.getPartnerAccountChildrenByUserId(tree.getUserId());
private void setChildList(PartnerTree tree) {
ArrayList<PartnerAccount> partnerAccountArrayList = (ArrayList<PartnerAccount>) userService.getPartnerAccountChildListByUserId(tree.getUserId());
for (PartnerAccount partnerAccount : partnerAccountArrayList) {
Integer childId = partnerAccount.getUserId();
Integer childLevel = partnerAccount.getIsSuperPartner() == 1 ? 30 : partnerAccount.getPartnerLevel();
......@@ -172,7 +172,7 @@ public class UserManager {
oneChild.setUserId(childId);
oneChild.setLevel(childLevel);
oneChild.setParentList(parentList4Child);
tree.getChildrenList().add(oneChild);
tree.getChildList().add(oneChild);
}
}
}
......@@ -193,8 +193,8 @@ public class UserManager {
/**
* 先找到直接下级
*/
setChildren(tree, parentColumnName);
ArrayList<AgentTree> children = tree.getChildrenList();
setChildList(tree, parentColumnName);
ArrayList<AgentTree> children = tree.getChildList();
/**
* 如果没有下级了 那么直接输出结果
*/
......@@ -212,7 +212,7 @@ public class UserManager {
* 如果有 那么在各自的recursiveSet中 应该没有走到 line:52 的outputTree 方法
* 所以要在这里执行一次, 把他们自己输出
*/
if (child.getChildrenList().size() > 0) {
if (child.getChildList().size() > 0) {
outputTree(child);
}
}
......@@ -245,8 +245,8 @@ public class UserManager {
* 把下级的数据, 设置到输入的tree中的childrenList
* @param tree
*/
private void setChildren(AgentTree tree, String parentColumnName) {
ArrayList<UserAgent> partnerAccountArrayList = (ArrayList<UserAgent>) userService.getUserAgentChildrenByUserId(tree.getUserId(), parentColumnName);
private void setChildList(AgentTree tree, String parentColumnName) {
ArrayList<UserAgent> partnerAccountArrayList = (ArrayList<UserAgent>) userService.getUserAgentChildListByUserId(tree.getUserId(), parentColumnName);
for (UserAgent userAgent : partnerAccountArrayList) {
Integer childId = userAgent.getUserId();
Integer agentId = userAgent.getAgentId();
......@@ -259,7 +259,7 @@ public class UserManager {
oneChild.setAgentId(agentId);
oneChild.setLevel(childLevel);
oneChild.setParentList(parentList4Child);
tree.getChildrenList().add(oneChild);
tree.getChildList().add(oneChild);
}
}
}
......
......@@ -19,7 +19,7 @@ public class AgentTree extends BaseModel {
Integer agentId;
Integer level;
ArrayList<ParentAgent> parentList;
ArrayList<AgentTree> childrenList = new ArrayList<AgentTree>();
ArrayList<AgentTree> childList = new ArrayList<AgentTree>();
public ArrayList<ParentAgent> cloneParent() {
ArrayList<ParentAgent> copyList = new ArrayList<ParentAgent>();
......
......@@ -22,4 +22,23 @@ public class ParentAgent extends BaseModel {
this.agentId = agentId;
this.level = level;
}
@Override
public boolean equals(Object obj) {
if(obj==null) {
return false;
}
if(this==obj) {
return true;
}
if(obj instanceof ParentAgent){
ParentAgent parentAgent = (ParentAgent) obj;
if(parentAgent.agentId == this.agentId) {
return true;
} else {
return false;
}
}
return false;
}
}
......@@ -18,7 +18,7 @@ public class PartnerTree extends BaseModel {
Integer userId;
Integer level;
ArrayList<ParentPartner> parentList;
ArrayList<PartnerTree> childrenList = new ArrayList<PartnerTree>();
ArrayList<PartnerTree> childList = new ArrayList<PartnerTree>();
public ArrayList<ParentPartner> cloneParent() {
ArrayList<ParentPartner> copyList = new ArrayList<ParentPartner>();
......
......@@ -7,7 +7,6 @@ import java.util.List;
/**
* @title: ${NAME}
* @projectName partner
* @description: TODO
* @author chen
* @package ${PACKAGE_NAME}
* @date 2019-07-04 11:44
......
......@@ -24,16 +24,15 @@ public interface UserAgentService extends IService<UserAgent> {
UserAgent getOneByAgentId(Integer agentId);
/**
* @description: 查找一个用户的全部代理商上级, 根据字段presenter_id
* @param agentId
* @return List<ParentAgent>
*/
List<ParentAgent> getParentAgentListByAgentId(Integer agentId, String parentColumnName);
/**
* @description: 查找一个用户的直接代理商上级, 根据字段presenter_id
* @param agentId
* @return ParentAgent
*/
ParentAgent getParentAgentByAgentId(Integer agentId, String parentColumnName);
/**
* @description: 查找一个用户的全部直属代理商上级, 根据字段presenter_id
* @param agentId
* @return List<ParentAgent>
*/
List<ParentAgent> getParentAgentListByAgentId(Integer agentId, String parentColumnName);
}
package com.lanren.huhu.partner.service; import com.lanren.huhu.partner.domain.PartnerAccount;import com.lanren.huhu.partner.domain.UserAgent;import com.lanren.huhu.partner.model.User; import java.util.List; /** * @author houseme * @date 2019-06-28 18:35 * @Project partner * @Package com.lanren.huhu.partner.service * @File: UserService */public interface UserService { /** * @description: 根据用户id, 查找一个用户的全部合伙人上级 和 代理商上级(此处为层级关系 非平推关系) * 缓存用户邀请关系和代理商关系数据 * 缓存不能带有构造方法的Bean 所以只能返回User的Bean * 包装Result在Cacheable注解的方法之外进行操作 * 注意 !!!!!!!! 这个方法是按输入用户的邀请关系链上查找 第一个有agent_level<=4的代理商身份的邀请人, 然后再走他的代理关系 * 注意 !!!!!!!! 如果需要获取一个代理商的上级代理 需要从UserAgentService里的方法获取 * @param userId * @return User * @throws * @author chen * @date 2019-06-26 20:01 */ User getRelationByUserId(Integer userId); /** * 清理用户缓存 */ boolean deleteRalationByUserId(Integer userId); /*** * PartnerAccount * * 根据用户id查询账户信息 * @param userId * @return PartnerAccount */ PartnerAccount getPartnerAccountByUserId(Integer userId); /** * @description: 根据用户id查询全部下级合伙人 * @param userId * @return List<PartnerAccount> * @author chen * @date 2019-06-28 11:49 */ List<PartnerAccount> getPartnerAccountChildrenByUserId(Integer userId); /*** * UserAgent * 根据用户id查询账户信息 * @param userId * @return */ UserAgent getUserAgentByUserId(Integer userId); /** * @description: 根据用户id查询全部下级代理商 * @param userId * @return List<PartnerAccount> * @author chen * @date 2019-06-28 11:49 */ List<UserAgent> getUserAgentChildrenByUserId(Integer userId, String parentColumnName); // /** // * 按userId查找上级合伙人的 userId 和 level // * @param userId // * @return // */ // ParentPartner getParentPartner(Integer userId); // /** // * @description: 查找一个用户的全部合伙人上级, set到User对象的partnerList中 // * @param userId // * @author chen // * @return List<ParentPartner> // */ // List<ParentPartner> getPartnerListByUserId(Integer userId); // /** // * 按agentId 查找上级代理商的 agentId 和 level // * @param agentId // * @return // */ // ParentAgent getParentAgent(Integer agentId); // /** // * @description: 查找一个用户的全部代理商上级 set到User对象的agentList中 // * @param userId // * @return List<ParentAgent> // */ // List<ParentAgent> getAgentListByUserId(Integer userId); }
\ No newline at end of file
package com.lanren.huhu.partner.service; import com.lanren.huhu.partner.domain.PartnerAccount;import com.lanren.huhu.partner.domain.UserAgent;import com.lanren.huhu.partner.model.User; import java.util.List; /** * @author houseme * @date 2019-06-28 18:35 * @Project partner * @Package com.lanren.huhu.partner.service * @File: UserService */public interface UserService { /** * @description: 根据用户id, 查找一个用户的全部合伙人上级 和 代理商上级(此处为层级关系 非平推关系) * 缓存用户邀请关系和代理商关系数据 * 缓存不能带有构造方法的Bean 所以只能返回User的Bean * 包装Result在Cacheable注解的方法之外进行操作 * 注意 !!!!!!!! 这个方法是按输入用户的邀请关系链上查找 第一个有agent_level<=4的代理商身份的邀请人, 然后再走他的代理关系 * 注意 !!!!!!!! 如果需要获取一个代理商的上级代理 需要从UserAgentService里的方法获取 * @param userId * @return User * @throws * @author chen * @date 2019-06-26 20:01 */ User getRelationByUserId(Integer userId); /** * 清理用户缓存 */ boolean deleteRalationByUserId(Integer userId); /*** * PartnerAccount * * 根据用户id查询账户信息 * @param userId * @return PartnerAccount */ PartnerAccount getPartnerAccountByUserId(Integer userId); /** * @description: 根据用户id查询全部直属下级合伙人 * @param userId * @return List<PartnerAccount> * @author chen * @date 2019-06-28 11:49 */ List<PartnerAccount> getPartnerAccountChildListByUserId(Integer userId); /*** * UserAgent * 根据用户id查询账户信息 * @param userId * @return */ UserAgent getUserAgentByUserId(Integer userId); /** * @description: 根据用户id查询全部直属下级代理商 * @param userId * @return List<PartnerAccount> * @author chen * @date 2019-06-28 11:49 */ List<UserAgent> getUserAgentChildListByUserId(Integer userId, String parentColumnName); }
\ No newline at end of file
......
......@@ -11,7 +11,6 @@ import java.util.List;
/**
* @title: ${NAME}
* @projectName partner
* @description: TODO
* @author chen
* @package ${PACKAGE_NAME}
* @date 2019-07-04 11:44
......
......@@ -6,6 +6,8 @@ import com.lanren.huhu.partner.dao.UserAgentMapper;
import com.lanren.huhu.partner.domain.UserAgent;
import com.lanren.huhu.partner.model.ParentAgent;
import com.lanren.huhu.partner.service.UserAgentService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
......@@ -16,6 +18,7 @@ import java.util.List;
*/
@Service
public class UserAgentServiceImpl extends ServiceImpl<UserAgentMapper, UserAgent> implements UserAgentService {
private static Logger logger = LoggerFactory.getLogger(UserAgentServiceImpl.class);
// @Autowired
// UserAgentMapper userAgentMapper;
......@@ -51,18 +54,30 @@ public class UserAgentServiceImpl extends ServiceImpl<UserAgentMapper, UserAgent
@Override
public List<ParentAgent> getParentAgentListByAgentId(Integer agentId, String parentColumnName) {
List<ParentAgent> returnList = new ArrayList<ParentAgent>();
List<ParentAgent> parentList = new ArrayList<ParentAgent>();
int loogAgentId = agentId;
int loopTimes = 0;
while (true) {
ParentAgent parentAgent = getParentAgentByAgentId(loogAgentId, parentColumnName);
if (null == parentAgent) {
break;
} else {
returnList.add(parentAgent);
if (parentList.contains(parentAgent) || parentAgent.getAgentId() == agentId) {
logger.info("发现互为上级的代理关系 代理商({})", parentAgent.getAgentId());
logger.info("退出循环, 不再继续查找");
break;
}
parentList.add(parentAgent);
loogAgentId = parentAgent.getAgentId();
loopTimes++;
if (loopTimes > 500) {
parentList.clear();
logger.error("代理商:{} 邀请关系异常, 已超500层", agentId);
break;
}
}
}
return returnList;
return parentList;
}
@Override
......
package com.lanren.huhu.partner.service.impl; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;import com.lanren.huhu.partner.dao.PartnerAccountMapper;import com.lanren.huhu.partner.dao.UserAgentMapper;import com.lanren.huhu.partner.domain.PartnerAccount;import com.lanren.huhu.partner.domain.UserAgent;import com.lanren.huhu.partner.model.ParentAgent;import com.lanren.huhu.partner.model.ParentPartner;import com.lanren.huhu.partner.model.User;import com.lanren.huhu.partner.service.UserService;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.cache.annotation.CacheConfig;import org.springframework.cache.annotation.CacheEvict;import org.springframework.cache.annotation.Cacheable;import org.springframework.stereotype.Service; import java.util.ArrayList;import java.util.List; import static com.lanren.huhu.partner.constants.Constants.PARENT_COLUMN_NAME_CENGJI;import static com.lanren.huhu.partner.constants.Constants.PARENT_COLUMN_NAME_PINGTUI; /** * @author houseme * @date 2019-06-28 18:36 * @Project partner * @Package com.lanren.huhu.partner.service.impl * @File: UserServiceImpl */@Service@CacheConfig(cacheNames = "user:relation:cache")public class UserServiceImpl implements UserService { private static Logger logger = LoggerFactory.getLogger(UserServiceImpl.class); @Autowired private UserAgentMapper userAgentMapper; @Autowired private PartnerAccountMapper partnerAccountMapper; /** * ****************************************分割线****用户邀请关系*************************************************************** */ /** * 用户上级 合伙人 和 代理 关系 * @param userId * @return */ @Override @Cacheable public User getRelationByUserId(Integer userId) { logger.info("UserServiceImpl get user relation from database, userId: {}", userId); User user = new User(); user.setUserId(userId); user.setPartnerList((ArrayList<ParentPartner>) getPartnerListByUserId(userId)); user.setAgentList((ArrayList<ParentAgent>) getAgentListByUserId(userId, PARENT_COLUMN_NAME_CENGJI)); user.setPingtuiAgentList((ArrayList<ParentAgent>) getAgentListByUserId(userId, PARENT_COLUMN_NAME_PINGTUI)); return user; } /** * 如果第一个ParentAgent是4级, 过滤掉后面的4级 * 相同级别只返回一个, 而且返回的数组元素 agent_level 越来越小 4->3->2->1 * @param agentList * @return * * 已废弃: 平推和上下级走两条不同的线, 不再先找出平推 再过滤出上下级 */ @Deprecated private ArrayList<ParentAgent> filterAgentList(List<ParentAgent> agentList) { int preAgentLevel = 5; ArrayList<ParentAgent> returnList = new ArrayList<ParentAgent>(); for (ParentAgent parentAgent : agentList) { if (parentAgent.getLevel() < preAgentLevel) { returnList.add(new ParentAgent(parentAgent.getUserId(), parentAgent.getAgentId(), parentAgent.getLevel())); preAgentLevel = parentAgent.getLevel(); } } return returnList; } @Override @CacheEvict(key = "#userId") public boolean deleteRalationByUserId(Integer userId) { logger.info("UserServiceImpl delete user relation cache, userId: {}", userId); return true; } /** * ****************************************分割线****合伙人*************************************************************** */ /** * 合伙人数据 * @param userId * @return */ @Override public PartnerAccount getPartnerAccountByUserId(Integer userId) { QueryWrapper<PartnerAccount> queryWrapper = new QueryWrapper<PartnerAccount>(); queryWrapper.eq("user_id", userId); return partnerAccountMapper.selectOne(queryWrapper); } @Override public List<PartnerAccount> getPartnerAccountChildrenByUserId(Integer userId) { return partnerAccountMapper.getChildrenByUserId(userId); } /** * 按userId查找上级合伙人的 userId 和 level * @param userId * @return */ private ParentPartner getParentPartner(Integer userId) { PartnerAccount partnerAccount = partnerAccountMapper.getParentByUserId(userId); ParentPartner parentPartner = null; if (null != partnerAccount) { int partnerLevel = partnerAccount.getIsSuperPartner() == 1 ? 30 : partnerAccount.getPartnerLevel(); parentPartner = new ParentPartner(partnerAccount.getUserId(),partnerLevel); } return parentPartner; } /** * @description: 查找一个用户的全部合伙人上级 * @param userId * @author chen * @return List<ParentPartner> */ private List<ParentPartner> getPartnerListByUserId(Integer userId) { if (logger.isInfoEnabled()) { logger.info("UserServiceImpl getPartnerListByUserId for user: {} ......", userId); } ArrayList<ParentPartner> partnerList = new ArrayList<ParentPartner>(); /** * 无限找 */ int loopUserId = userId; int loopTimes = 0; while (true) { if (logger.isDebugEnabled()) { logger.info("now loop uid is {}", loopUserId); } ParentPartner parentPartner = getParentPartner(loopUserId); if (null == parentPartner) { break; } else { if (logger.isDebugEnabled()) { logger.info("parent is {} ", parentPartner.getUserId()); } if (partnerList.contains(parentPartner)) { logger.info("发现互相邀请的关系 用户({}) 和 用户({})", loopUserId, parentPartner.getUserId()); logger.info("退出循环, 不再继续查找"); break; } partnerList.add(parentPartner); loopUserId = parentPartner.getUserId(); loopTimes++; if (loopTimes > 500) { partnerList.clear(); logger.error("用户:{} 邀请关系异常, 已超500层", userId); break; } } } if (logger.isInfoEnabled()) { logger.info("setPartnerList for user: {} end", userId); } return partnerList; } /** * ****************************************分割线****代理商*************************************************************** */ /** * 代理商数据 * @param userId * @return */ @Override public UserAgent getUserAgentByUserId(Integer userId) { QueryWrapper<UserAgent> queryWrapper = new QueryWrapper<UserAgent>(); queryWrapper.eq("user_id", userId).le("agent_level", 4); return userAgentMapper.selectOne(queryWrapper); } @Override public List<UserAgent> getUserAgentChildrenByUserId(Integer userId, String parentColumnName) { return userAgentMapper.getChildrenByUserId(userId, parentColumnName); } /** * 按agentId 查找上级代理商的 agentId 和 level * @param agentId * @return */ private ParentAgent getParentAgent(Integer agentId, String parentColumnName) { UserAgent userAgent = userAgentMapper.getParentAgent(agentId, parentColumnName); ParentAgent parentAgent = null; if (null != userAgent) { int agentLevel = userAgent.getAgentLevel(); parentAgent = new ParentAgent(userAgent.getUserId(), userAgent.getAgentId(), agentLevel); } return parentAgent; } /** * @description: 查找一个用户的全部代理商上级 * 注意 !!!!!!!! 这个方法是按输入用户的邀请关系链上查找 第一个有agent_level<=4的代理商身份的邀请人, 然后再走他的代理关系 * 注意 !!!!!!!! 如果需要获取一个代理商的上级代理 需要从UserAgentService里的方法获取 * @param userId * @return List<ParentAgent> */ private List<ParentAgent> getAgentListByUserId(Integer userId, String parentColumnName) { if (logger.isInfoEnabled()) { logger.info("getAgentListByUserId for user: {} ......", userId); } ArrayList<ParentAgent> agentList = new ArrayList<ParentAgent>(); /** * 无限找 * 先无限找上级合伙人, 直到找到第一个是代理商的合伙人 */ int loopUserId = userId; int loopTimes = 0; while (true) { if (logger.isDebugEnabled()) { logger.info("now loop uid is {}", loopUserId); } ParentPartner parentPartner = getParentPartner(loopUserId); /** * 有上级合伙人 */ if (null != parentPartner) { UserAgent userAgent = getUserAgentByUserId(parentPartner.getUserId()); /** * 找到了第一个是代理商的合伙人 */ if (null != userAgent) { /** * 然后开始 无限找 这个人的上级代理商 */ if (logger.isDebugEnabled()) { logger.info("find first agent({}) for user: {}", userAgent.getAgentId(), userId); } agentList.add(new ParentAgent(userAgent.getUserId(), userAgent.getAgentId(), userAgent.getAgentLevel())); loopTimes++; if (logger.isDebugEnabled()) { logger.info("loop find parent agent start ......"); } int loogAgentId = userAgent.getAgentId(); while (true) { ParentAgent loopUserAgent = getParentAgent(loogAgentId, parentColumnName); if (null == loopUserAgent) { break; } else { if (logger.isDebugEnabled()) { logger.info("parent agent is ", loopUserAgent.getAgentId()); } agentList.add(new ParentAgent(loopUserAgent.getUserId(), loopUserAgent.getAgentId(), loopUserAgent.getLevel())); loogAgentId = loopUserAgent.getAgentId(); loopTimes++; if (loopTimes > 500) { agentList.clear(); logger.error("用户ID:{} 代理商关系异常, 已超500层", userId); break; } } } /** * 找完 "第一个是代理商的合伙人" 的 全部上级代理后 退出while循环 */ if (logger.isDebugEnabled()) { logger.info("loop find parent agent end"); } break; } else { /** * 这个人不是代理商 继续往上找 */ loopUserId = parentPartner.getUserId(); } } else { /** * 没有上级合伙人 打印结束信息 跳出循环 */ if (logger.isDebugEnabled()) { logger.info("loop end with userId: {}", loopUserId); } break; } } if (logger.isInfoEnabled()) { logger.info("setAgentList for user: {} end", userId); } return agentList; }}
\ No newline at end of file
package com.lanren.huhu.partner.service.impl; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;import com.lanren.huhu.partner.dao.PartnerAccountMapper;import com.lanren.huhu.partner.dao.UserAgentMapper;import com.lanren.huhu.partner.domain.PartnerAccount;import com.lanren.huhu.partner.domain.UserAgent;import com.lanren.huhu.partner.model.ParentAgent;import com.lanren.huhu.partner.model.ParentPartner;import com.lanren.huhu.partner.model.User;import com.lanren.huhu.partner.service.UserService;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.cache.annotation.CacheConfig;import org.springframework.cache.annotation.CacheEvict;import org.springframework.cache.annotation.Cacheable;import org.springframework.stereotype.Service; import java.util.ArrayList;import java.util.List; import static com.lanren.huhu.partner.constants.Constants.PARENT_COLUMN_NAME_CENGJI;import static com.lanren.huhu.partner.constants.Constants.PARENT_COLUMN_NAME_PINGTUI; /** * @author houseme * @date 2019-06-28 18:36 * @Project partner * @Package com.lanren.huhu.partner.service.impl * @File: UserServiceImpl */@Service@CacheConfig(cacheNames = "user:relation:cache")public class UserServiceImpl implements UserService { private static Logger logger = LoggerFactory.getLogger(UserServiceImpl.class); @Autowired private UserAgentMapper userAgentMapper; @Autowired private PartnerAccountMapper partnerAccountMapper; /** * ****************************************分割线****用户邀请关系*************************************************************** */ /** * 用户上级 合伙人 和 代理 关系 * @param userId * @return */ @Override @Cacheable public User getRelationByUserId(Integer userId) { logger.info("UserServiceImpl get user relation from database, userId: {}", userId); User user = new User(); user.setUserId(userId); user.setPartnerList((ArrayList<ParentPartner>) getPartnerListByUserId(userId)); user.setAgentList((ArrayList<ParentAgent>) getAgentListByUserId(userId, PARENT_COLUMN_NAME_CENGJI)); user.setPingtuiAgentList((ArrayList<ParentAgent>) getAgentListByUserId(userId, PARENT_COLUMN_NAME_PINGTUI)); return user; } @Override @CacheEvict(key = "#userId") public boolean deleteRalationByUserId(Integer userId) { logger.info("UserServiceImpl delete user relation cache, userId: {}", userId); return true; } /** * ****************************************分割线****合伙人*************************************************************** */ /** * 合伙人数据 * @param userId * @return */ @Override public PartnerAccount getPartnerAccountByUserId(Integer userId) { QueryWrapper<PartnerAccount> queryWrapper = new QueryWrapper<PartnerAccount>(); queryWrapper.eq("user_id", userId); return partnerAccountMapper.selectOne(queryWrapper); } @Override public List<PartnerAccount> getPartnerAccountChildListByUserId(Integer userId) { return partnerAccountMapper.getChildListByUserId(userId); } /** * 按userId查找上级合伙人的 userId 和 level * @param userId * @return */ private ParentPartner getParentPartner(Integer userId) { PartnerAccount partnerAccount = partnerAccountMapper.getParentByUserId(userId); ParentPartner parentPartner = null; if (null != partnerAccount) { int partnerLevel = partnerAccount.getIsSuperPartner() == 1 ? 30 : partnerAccount.getPartnerLevel(); parentPartner = new ParentPartner(partnerAccount.getUserId(),partnerLevel); } return parentPartner; } /** * @description: 查找一个用户的全部合伙人上级 * @param userId * @author chen * @return List<ParentPartner> */ private List<ParentPartner> getPartnerListByUserId(Integer userId) { if (logger.isInfoEnabled()) { logger.info("UserServiceImpl getPartnerListByUserId for user: {} ......", userId); } ArrayList<ParentPartner> partnerList = new ArrayList<ParentPartner>(); /** * 无限找 */ int loopUserId = userId; int loopTimes = 0; while (true) { if (logger.isDebugEnabled()) { logger.info("now loop uid is {}", loopUserId); } ParentPartner parentPartner = getParentPartner(loopUserId); if (null == parentPartner) { break; } else { if (logger.isDebugEnabled()) { logger.info("parent is {} ", parentPartner.getUserId()); } if (partnerList.contains(parentPartner) || parentPartner.getUserId() == userId) { logger.info("发现互相邀请的关系 用户({})", parentPartner.getUserId()); logger.info("退出循环, 不再继续查找"); break; } partnerList.add(parentPartner); loopUserId = parentPartner.getUserId(); loopTimes++; if (loopTimes > 500) { partnerList.clear(); logger.error("用户:{} 邀请关系异常, 已超500层", userId); break; } } } if (logger.isInfoEnabled()) { logger.info("setPartnerList for user: {} end", userId); } return partnerList; } /** * ****************************************分割线****代理商*************************************************************** */ /** * 代理商数据 * @param userId * @return */ @Override public UserAgent getUserAgentByUserId(Integer userId) { QueryWrapper<UserAgent> queryWrapper = new QueryWrapper<UserAgent>(); queryWrapper.eq("user_id", userId).le("agent_level", 4); return userAgentMapper.selectOne(queryWrapper); } @Override public List<UserAgent> getUserAgentChildListByUserId(Integer userId, String parentColumnName) { return userAgentMapper.getChildListByUserId(userId, parentColumnName); } /** * 按agentId 查找上级代理商的 agentId 和 level * @param agentId * @return */ private ParentAgent getParentAgent(Integer agentId, String parentColumnName) { UserAgent userAgent = userAgentMapper.getParentAgent(agentId, parentColumnName); ParentAgent parentAgent = null; if (null != userAgent) { int agentLevel = userAgent.getAgentLevel(); parentAgent = new ParentAgent(userAgent.getUserId(), userAgent.getAgentId(), agentLevel); } return parentAgent; } /** * @description: 查找一个用户的全部代理商上级 * 注意 !!!!!!!! 这个方法是按输入用户的邀请关系链上查找 第一个有agent_level<=4的代理商身份的邀请人, 然后再走他的代理关系 * 注意 !!!!!!!! 如果需要获取一个代理商的上级代理 需要从UserAgentService里的方法获取 * @param userId * @return List<ParentAgent> */ private List<ParentAgent> getAgentListByUserId(Integer userId, String parentColumnName) { if (logger.isInfoEnabled()) { logger.info("getAgentListByUserId for user: {} ......", userId); } ArrayList<ParentAgent> agentList = new ArrayList<ParentAgent>(); /** * 无限找 * 先无限找上级合伙人, 直到找到第一个是代理商的合伙人 */ int loopUserId = userId; int loopTimes = 0; while (true) { if (logger.isDebugEnabled()) { logger.info("now loop uid is {}", loopUserId); } ParentPartner parentPartner = getParentPartner(loopUserId); /** * 有上级合伙人 */ if (null != parentPartner) { UserAgent firstAgent = getUserAgentByUserId(parentPartner.getUserId()); /** * 找到了第一个是代理商的合伙人 */ if (null != firstAgent) { /** * 然后开始 无限找 这个人的上级代理商 */ if (logger.isDebugEnabled()) { logger.info("find first agent({}) for user: {}", firstAgent.getAgentId(), userId); } agentList.add(new ParentAgent(firstAgent.getUserId(), firstAgent.getAgentId(), firstAgent.getAgentLevel())); loopTimes++; if (logger.isDebugEnabled()) { logger.info("loop find parent agent start ......"); } int loogAgentId = firstAgent.getAgentId(); while (true) { ParentAgent loopUserAgent = getParentAgent(loogAgentId, parentColumnName); if (null == loopUserAgent) { break; } else { if (logger.isDebugEnabled()) { logger.info("parent agent is ", loopUserAgent.getAgentId()); } if (agentList.contains(loopUserAgent) || loopUserAgent.getAgentId() == firstAgent.getAgentId()) { logger.info("发现互为上级的代理关系 代理商({})", loopUserAgent.getAgentId()); logger.info("退出循环, 不再继续查找"); break; } agentList.add(new ParentAgent(loopUserAgent.getUserId(), loopUserAgent.getAgentId(), loopUserAgent.getLevel())); loogAgentId = loopUserAgent.getAgentId(); loopTimes++; if (loopTimes > 500) { agentList.clear(); logger.error("用户ID:{} 代理商关系异常, 已超500层", userId); break; } } } /** * 找完 "第一个是代理商的合伙人" 的 全部上级代理后 退出while循环 */ if (logger.isDebugEnabled()) { logger.info("loop find parent agent end"); } break; } else { /** * 这个人不是代理商 继续往上找 */ loopUserId = parentPartner.getUserId(); } } else { /** * 没有上级合伙人 打印结束信息 跳出循环 */ if (logger.isDebugEnabled()) { logger.info("loop end with userId: {}", loopUserId); } break; } } if (logger.isInfoEnabled()) { logger.info("setAgentList for user: {} end", userId); } return agentList; }}
\ No newline at end of file
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment