Commit f8893b57 by guanchen

添加处理 合伙人 和 代理商 关系变更

用户的 代理商上级改为从自己开始找
parent 0086b85a
......@@ -44,12 +44,21 @@ public class UserController {
@RequestMapping(value = "/relation/change", method = RequestMethod.POST)
public Result<String> changeRelation(@RequestBody @Valid User user, @RequestHeader HttpHeaders headers) {
if (logger.isInfoEnabled()) {
logger.info("changeRelation userId:{},header:{},time:{}", user.getUserId(), headers.keySet().toArray(), LocalDateTime.now());
logger.info("purgeRelation userId:{},header:{},time:{}", user.getUserId(), headers.keySet().toArray(), LocalDateTime.now());
}
userManager.changeRelation(user.getUserId());
userManager.purgeRelation(user.getUserId());
logger.info("============>" + Thread.currentThread().getName());
Result<String> result = new Result<String>();
result.setData("异步,正在执行刷新......");
return result;
}
@RequestMapping(value = "/update/relation/{from}/{to}/{type}" ,method = RequestMethod.GET)
public Result<String> updateRelation(@PathVariable("from") String from, @PathVariable("to") String to, @PathVariable("type") int type) {
userManager.updateRelation(from, to, type);
logger.info("============>" + Thread.currentThread().getName());
Result<String> result = new Result<String>();
result.setData("异步,正在执行更新新......");
return result;
}
}
......@@ -39,4 +39,8 @@ public interface PartnerAccountMapper extends BaseMapper<PartnerAccount> {
List<PartnerAccount> getChildListByUserId(Integer userId);
PartnerAccount selectOneForUpdate(Integer userId);
int updatePartnerRelation(String from, String to);
PartnerAccount getPartnerAccountByPhone(String phone);
}
\ No newline at end of file
......@@ -24,24 +24,19 @@ public interface UserAgentMapper extends BaseMapper<UserAgent> {
* @param agentId
* @return
*/
// @Select("SELECT b.* " +
// "FROM user_agent a " +
// "JOIN user_agent b on a.#{parentColumnName}=b.agent_id " +
// "WHERE a.agent_id=#{agentId} and b.agent_level<=4")
// UserAgent getParentAgent(Integer agentId, String parentColumnName);
@SelectProvider(type = UserAgentSqlProvider.class, method = "getParentAgent")
UserAgent getParentAgent(@Param("agentId") Integer agentId, @Param("parentColumnName") String parentColumnName);
/**
* 按uid查找下级 UserAgent
*/
// @Select("SELECT b.* " +
// "FROM user_agent a " +
// "JOIN user_agent b on a.#{parentColumnName}=b.agent_id " +
// "WHERE b.user_id=#{userId}")
// List<UserAgent> getChildListByUserId(Integer userId, String parentColumnName);
@SelectProvider(type = UserAgentSqlProvider.class, method = "getChildListByUserId")
List<UserAgent> getChildListByUserId(@Param("userId") Integer userId, @Param("parentColumnName") String parentColumnName);
/**
* 将手机号为from的代理商的 平推 上级改为手机号为to的代理商
*/
int updatePingtuiRelation(String from, String to);
/**
* 将手机号为from的代理商的 层级 上级改为手机号为to的代理商
*/
int updateCengjiRelation(String from, String to);
}
\ No newline at end of file
......@@ -48,10 +48,10 @@ public class UserManager {
* @param userId
*/
@Async
public Result<String> changeRelation(Integer userId) {
public Result<String> purgeRelation(Integer userId) {
Result<String> result = new Result<String>();
try{
logger.info("start changeRelation for userId: {} ...", userId);
logger.info("start purgeRelation for userId: {} ...", userId);
PartnerAccount partnerAccount = userService.getPartnerAccountByUserId(userId);
if (partnerAccount == null) {
result.setCode(413);
......@@ -78,7 +78,7 @@ public class UserManager {
recursiveSet(agentRoot, PARENT_COLUMN_NAME_CENGJI);
recursiveSet(agentRoot, PARENT_COLUMN_NAME_PINGTUI);
}
logger.info("done changeRelation for userId: {}", userId);
logger.info("done purgeRelation for userId: {}", userId);
result.setData("用户刷新成功");
} catch (Exception e) {
logger.error(e.getMessage(), e);
......@@ -189,10 +189,10 @@ public class UserManager {
logger.info("do recursiveSet for userId: {}", tree.getUserId());
if (logger.isDebugEnabled()) {
logger.info("do recursiveSet for userId: {}", tree.getUserId());
/**
* 先找到直接下级
*/
}
/**
* 先找到直接下级
*/
setChildList(tree, parentColumnName);
ArrayList<AgentTree> children = tree.getChildList();
/**
......@@ -263,4 +263,39 @@ public class UserManager {
}
}
}
/**
* 变更 代理商 或 用户的 上级
* 变更类型: 1改层级关系 2改推荐关系 3改层级和推荐关系 4改C端邀请关系 5全部都改
*/
@Async
public Result<String> updateRelation(String from, String to, int type) {
if (type == 1 || type == 3 || type == 5) {
int cnt = updateCengjiRelation(from, to);
logger.info("更新代理商层级关系, 将{}的上级改为{},更新{}行数据", from, to, cnt);
}
if (type == 2 || type == 3 || type == 5) {
int cnt = updatePingtuiRelation(from, to);
logger.info("更新代理商推荐关系, 将{}的上级改为{},更新{}行数据", from, to, cnt);
}
if (type == 4 || type == 5) {
int cnt = updatePartnerRelation(from, to);
logger.info("更新合伙人推荐关系, 将{}的上级改为{},更新{}行数据", from, to, cnt);
}
PartnerAccount partnerAccount = userService.getPartnerAccountByPhone(from);
return purgeRelation(partnerAccount.getUserId());
}
private int updatePartnerRelation(String from, String to) {
return userService.updatePartnerRelation(from, to);
}
private int updatePingtuiRelation(String from, String to) {
return userService.updatePingtuiRelation(from, to);
}
private int updateCengjiRelation(String from, String to) {
return userService.updateCengjiRelation(from, to);
}
}
......@@ -177,7 +177,11 @@ public class PartnerSettleTask {
partnerIncomeSummary.setYeartime(yeartime);
partnerIncomeSummary.setMonthtime(monthtime);
partnerIncomeSummary.setIncome(ZERO);
partnerIncomeSummary.setState(15);
partnerIncomeSummaryService.save(partnerIncomeSummary);
} else {
partnerIncomeSummary.setState(15);
partnerIncomeSummaryService.updateById(partnerIncomeSummary);
}
}
......
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.ParentAgent;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); List<ParentAgent> getAgentListByUserId(Integer userId, String parentColumnName); /** * 清理用户缓存 */ 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
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.ParentAgent;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); List<ParentAgent> getAgentListByUserId(Integer userId, String parentColumnName); /** * 清理用户缓存 */ 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); int updatePartnerRelation(String from, String to); int updatePingtuiRelation(String from, String to); int updateCengjiRelation(String from, String to); PartnerAccount getPartnerAccountByPhone(String phone);}
\ 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> */ @Override public List<ParentAgent> getAgentListByUserId(Integer userId, String parentColumnName) { if (logger.isInfoEnabled()) { logger.info("getAgentListByUserId for user: {} ......", userId); } ArrayList<ParentAgent> agentList = new ArrayList<ParentAgent>(); /** * 无限找 * 先无限找上级合伙人, 直到找到第一个是代理商的合伙人 */ /** * 要改成从自己开始找 所以添加判断 看自己是不是 */ UserAgent selfAgent = getUserAgentByUserId(userId); int loopUserId = userId; int loopTimes = 0; while (true) { if (logger.isDebugEnabled()) { logger.info("now loop uid is {}", loopUserId); } ParentPartner parentPartner = getParentPartner(loopUserId); /** * 有上级合伙人 或者 自己就是代理商 */ if (null != selfAgent || null != parentPartner) { UserAgent firstAgent; if (null != selfAgent) { firstAgent = selfAgent; } else { 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
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; } @Override public int updatePartnerRelation(String from, String to) { return partnerAccountMapper.updatePartnerRelation(from, to); } @Override public PartnerAccount getPartnerAccountByPhone(String phone) { return partnerAccountMapper.getPartnerAccountByPhone(phone); } /** * ****************************************分割线****代理商*************************************************************** */ /** * 代理商数据 * @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); } @Override public int updatePingtuiRelation(String from, String to) { return userAgentMapper.updatePingtuiRelation(from, to); } @Override public int updateCengjiRelation(String from, String to) { return userAgentMapper.updateCengjiRelation(from, to); } /** * 按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> */ @Override public List<ParentAgent> getAgentListByUserId(Integer userId, String parentColumnName) { if (logger.isInfoEnabled()) { logger.info("getAgentListByUserId for user: {} ......", userId); } ArrayList<ParentAgent> agentList = new ArrayList<ParentAgent>(); /** * 无限找 * 先无限找上级合伙人, 直到找到第一个是代理商的合伙人 */ /** * 要改成从自己开始找 所以添加判断 看自己是不是 */ UserAgent selfAgent = getUserAgentByUserId(userId); int loopUserId = userId; int loopTimes = 0; while (true) { if (logger.isDebugEnabled()) { logger.info("now loop uid is {}", loopUserId); } ParentPartner parentPartner = getParentPartner(loopUserId); /** * 有上级合伙人 或者 自己就是代理商 */ if (null != selfAgent || null != parentPartner) { UserAgent firstAgent; if (null != selfAgent) { firstAgent = selfAgent; } else { 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
......
......@@ -501,4 +501,20 @@
<select id="selectOneForUpdate" resultMap="BaseResultMap">
SELECT * FROM partner_account WHERE user_id = #{userId} FOR UPDATE;
</select>
<update id="updatePartnerRelation">
UPDATE partner_invite_relation a
JOIN user_info b ON a.user_id=b.user_id
SET a.invite_user_id = (
SELECT a.user_id
FROM partner_account a JOIN user_info b ON a.user_id=b.user_id
WHERE b.user_phone='#{to}'
)
WHERE b.user_phone='#{from}'
</update>
<select id="getPartnerAccountByPhone" resultMap="BaseResultMap">
SELECT a.*
FROM partner_account a
JOIN user_info b on a.user_id=b.user_id
WHERE b.user_phone=#{phone}
</select>
</mapper>
\ No newline at end of file
......@@ -688,7 +688,7 @@
</select>
<update id="updateRewardStatusByUidAndTs">
UPDATE partner_reward
SET reward_status=120
SET reward_status=120,settle_state=case when settle_state>0 then 300 else settle_state end
WHERE recharge_time
BETWEEN FROM_UNIXTIME(#{beginTs}) AND FROM_UNIXTIME(#{endTs})
AND user_id = #{userId}
......
......@@ -588,4 +588,14 @@
</if>
</trim>
</insert>
<update id="updatePingtuiRelation">
UPDATE user_agent
SET presenter_id=(SELECT agent_id FROM user_agent WHERE agent_phone=#{to})
WHERE agent_phone=#{from}
</update>
<update id="updateCengjiRelation">
UPDATE user_agent
SET parent_agent_id=(SELECT agent_id FROM user_agent WHERE agent_phone=#{to})
WHERE agent_phone=#{from}
</update>
</mapper>
\ 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