分佣: 简化推广用户关系树查询方式

This commit is contained in:
owen 2023-09-22 23:13:39 +08:00
parent 6e6486ccae
commit bbd18afbcc
4 changed files with 39 additions and 64 deletions

View File

@ -40,8 +40,6 @@ create table trade_brokerage_user
brokerage_time datetime null comment '成为分销员时间',
price int default 0 not null comment '可用佣金',
frozen_price int default 0 not null comment '冻结佣金',
level int default 1 not null comment '等级',
path varchar(2000) null comment '路径',
creator varchar(64) default '' null comment '创建者',
create_time datetime default CURRENT_TIMESTAMP not null comment '创建时间',
updater varchar(64) default '' null comment '更新者',

View File

@ -59,13 +59,4 @@ public class BrokerageUserDO extends BaseDO {
* 冻结佣金
*/
private Integer frozenPrice;
/**
* 等级
*/
private Integer level;
/**
* 路径
*/
private String path;
}

View File

@ -27,13 +27,12 @@ import java.util.List;
@Mapper
public interface BrokerageUserMapper extends BaseMapperX<BrokerageUserDO> {
default PageResult<BrokerageUserDO> selectPage(BrokerageUserPageReqVO reqVO, List<Integer> levels) {
default PageResult<BrokerageUserDO> selectPage(BrokerageUserPageReqVO reqVO, List<Long> bindUserIds) {
return selectPage(reqVO, new LambdaQueryWrapperX<BrokerageUserDO>()
.eqIfPresent(BrokerageUserDO::getBrokerageEnabled, reqVO.getBrokerageEnabled())
.betweenIfPresent(BrokerageUserDO::getCreateTime, reqVO.getCreateTime())
.betweenIfPresent(BrokerageUserDO::getBindUserTime, reqVO.getBindUserTime())
.findInSetIfPresent(BrokerageUserDO::getPath, reqVO.getBindUserId())
.inIfPresent(BrokerageUserDO::getLevel, levels)
.inIfPresent(BrokerageUserDO::getBindUserId, bindUserIds)
.orderByDesc(BrokerageUserDO::getId));
}
@ -116,8 +115,7 @@ public interface BrokerageUserMapper extends BaseMapperX<BrokerageUserDO> {
default void updateBindUserIdAndBindUserTimeToNull(Long id) {
update(null, new LambdaUpdateWrapper<BrokerageUserDO>()
.eq(BrokerageUserDO::getId, id)
.set(BrokerageUserDO::getBindUserId, null).set(BrokerageUserDO::getBindUserTime, null)
.set(BrokerageUserDO::getLevel, 1).set(BrokerageUserDO::getPath, ""));
.set(BrokerageUserDO::getBindUserId, null).set(BrokerageUserDO::getBindUserTime, null));
}
default void updateEnabledFalseAndBrokerageTimeToNull(Long id) {
@ -126,10 +124,9 @@ public interface BrokerageUserMapper extends BaseMapperX<BrokerageUserDO> {
.set(BrokerageUserDO::getBrokerageEnabled, false).set(BrokerageUserDO::getBrokerageTime, null));
}
default Long selectCountByBindUserIdAndLevelIn(Long bindUserId, List<Integer> levels) {
default Long selectCountByBindUserIdIn(List<Long> bindUserIds) {
return selectCount(new LambdaQueryWrapperX<BrokerageUserDO>()
.findInSetIfPresent(BrokerageUserDO::getPath, bindUserId)
.inIfPresent(BrokerageUserDO::getLevel, levels));
.inIfPresent(BrokerageUserDO::getBindUserId, bindUserIds));
}
@Select("SELECT bind_user_id AS id, COUNT(1) AS brokerageUserCount FROM trade_brokerage_user " +
@ -144,4 +141,8 @@ public interface BrokerageUserMapper extends BaseMapperX<BrokerageUserDO> {
IPage<AppBrokerageUserChildSummaryRespVO> selectSummaryPageByUserId(Page<?> page,
@Param("param") AppBrokerageUserChildSummaryPageReqVO param,
@Param("userId") Long userId);
default List<BrokerageUserDO> selectListByBindUserId(Long bindUserId) {
return selectList(BrokerageUserDO::getBindUserId, bindUserId);
}
}

View File

@ -4,8 +4,6 @@ import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.BooleanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.util.MyBatisUtils;
import cn.iocoder.yudao.module.trade.controller.admin.brokerage.vo.user.BrokerageUserPageReqVO;
@ -25,9 +23,13 @@ import org.springframework.validation.annotation.Validated;
import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.*;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.module.trade.enums.ErrorCodeConstants.*;
/**
@ -57,8 +59,8 @@ public class BrokerageUserServiceImpl implements BrokerageUserService {
@Override
public PageResult<BrokerageUserDO> getBrokerageUserPage(BrokerageUserPageReqVO pageReqVO) {
List<Integer> levels = buildUserQueryLevels(pageReqVO.getBindUserId(), pageReqVO.getLevel());
return brokerageUserMapper.selectPage(pageReqVO, levels);
List<Long> bindUserIds = buildBindUserIdsByLevel(pageReqVO.getBindUserId(), pageReqVO.getLevel());
return brokerageUserMapper.selectPage(pageReqVO, bindUserIds);
}
@Override
@ -66,6 +68,11 @@ public class BrokerageUserServiceImpl implements BrokerageUserService {
// 校验存在
BrokerageUserDO brokerageUser = validateBrokerageUserExists(id);
// 绑定关系未发生变化
if (Objects.equals(brokerageUser.getBindUserId(), bindUserId)) {
return;
}
// 情况一清除推广员
if (bindUserId == null) {
// 清除推广员
@ -73,12 +80,6 @@ public class BrokerageUserServiceImpl implements BrokerageUserService {
return;
}
// 绑定关系未发生变化
// TODO @疯狂这个放到情况一之前貌似也没关系
if (Objects.equals(brokerageUser.getBindUserId(), bindUserId)) {
return;
}
// 情况二修改推广员
validateCanBindUser(brokerageUser, bindUserId);
brokerageUserMapper.updateById(fillBindUserData(bindUserId, new BrokerageUserDO().setId(id)));
@ -146,11 +147,11 @@ public class BrokerageUserServiceImpl implements BrokerageUserService {
@Override
public Long getBrokerageUserCountByBindUserId(Long bindUserId, Integer level) {
List<Integer> levels = buildUserQueryLevels(bindUserId, level);
if (CollUtil.isEmpty(levels)) {
List<Long> bindUserIds = buildBindUserIdsByLevel(bindUserId, level);
if (CollUtil.isEmpty(bindUserIds)) {
return 0L;
}
return brokerageUserMapper.selectCountByBindUserIdAndLevelIn(bindUserId, levels);
return brokerageUserMapper.selectCountByBindUserIdIn(bindUserIds);
}
@Override
@ -185,20 +186,7 @@ public class BrokerageUserServiceImpl implements BrokerageUserService {
}
private BrokerageUserDO fillBindUserData(Long bindUserId, BrokerageUserDO brokerageUser) {
BrokerageUserDO bindUser = getBrokerageUser(bindUserId);
Integer bindUserLevel = 0;
String bindUserPath = "";
if (bindUser != null) {
bindUserLevel = ObjectUtil.defaultIfNull(bindUser.getLevel(), 0);
bindUserPath = bindUser.getPath();
}
String path = StrUtil.isEmpty(bindUserPath)
? String.valueOf(bindUserId)
: String.format("%s,%s", bindUserPath, bindUserId);
return brokerageUser.setBindUserId(bindUserId).setBindUserTime(LocalDateTime.now())
.setLevel(bindUserLevel + 1).setPath(path);
return brokerageUser.setBindUserId(bindUserId).setBindUserTime(LocalDateTime.now());
}
@Override
@ -267,28 +255,25 @@ public class BrokerageUserServiceImpl implements BrokerageUserService {
}
// 下级不能绑定自己的上级
if (StrUtil.split(bindUser.getPath(), ",").contains(String.valueOf(user.getId()))) {
throw exception(BROKERAGE_BIND_LOOP);
for (int i = 0; i <= Short.MAX_VALUE; i++) {
if (Objects.equals(bindUser.getBindUserId(), user.getId())) {
throw exception(BROKERAGE_BIND_LOOP);
}
bindUser = getBrokerageUser(bindUser.getBindUserId());
}
}
// TODO @芋艿这个层级要微信讨论下
private List<Integer> buildUserQueryLevels(Long bindUserId, Integer level) {
List<Integer> levels = new ArrayList<>(2);
BrokerageUserDO bindUser = getBrokerageUser(bindUserId);
if (bindUser == null) {
return levels;
private List<Long> buildBindUserIdsByLevel(Long bindUserId, Integer level) {
List<Long> bindUserIds = CollUtil.newArrayList();
if (level == null || level == 1) {
bindUserIds.add(bindUserId);
}
if (level == null || level == 2) {
List<Long> firstUserIds = convertList(brokerageUserMapper.selectListByBindUserId(bindUserId), BrokerageUserDO::getId);
bindUserIds.addAll(firstUserIds);
}
if (level == null) {
// 默认查两层
levels.add(bindUser.getLevel() + 1);
levels.add(bindUser.getLevel() + 2);
} else {
levels.add(bindUser.getLevel() + level);
}
return levels;
return bindUserIds;
}