From bbd18afbcc5d06ec9291947dac9c86114db9ad06 Mon Sep 17 00:00:00 2001 From: owen Date: Fri, 22 Sep 2023 23:13:39 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=86=E4=BD=A3:=20=E7=AE=80=E5=8C=96?= =?UTF-8?q?=E6=8E=A8=E5=B9=BF=E7=94=A8=E6=88=B7=E5=85=B3=E7=B3=BB=E6=A0=91?= =?UTF-8?q?=E6=9F=A5=E8=AF=A2=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sql/mysql/brokerage.sql | 2 - .../dataobject/brokerage/BrokerageUserDO.java | 9 --- .../mysql/brokerage/BrokerageUserMapper.java | 17 +++-- .../brokerage/BrokerageUserServiceImpl.java | 75 ++++++++----------- 4 files changed, 39 insertions(+), 64 deletions(-) diff --git a/sql/mysql/brokerage.sql b/sql/mysql/brokerage.sql index a38c51f4b..4c1d8d7e8 100644 --- a/sql/mysql/brokerage.sql +++ b/sql/mysql/brokerage.sql @@ -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 '更新者', diff --git a/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/dal/dataobject/brokerage/BrokerageUserDO.java b/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/dal/dataobject/brokerage/BrokerageUserDO.java index 320bd0b07..8d73858ec 100644 --- a/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/dal/dataobject/brokerage/BrokerageUserDO.java +++ b/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/dal/dataobject/brokerage/BrokerageUserDO.java @@ -59,13 +59,4 @@ public class BrokerageUserDO extends BaseDO { * 冻结佣金 */ private Integer frozenPrice; - - /** - * 等级 - */ - private Integer level; - /** - * 路径 - */ - private String path; } diff --git a/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/dal/mysql/brokerage/BrokerageUserMapper.java b/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/dal/mysql/brokerage/BrokerageUserMapper.java index 2212ba7f4..9bd2fe078 100644 --- a/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/dal/mysql/brokerage/BrokerageUserMapper.java +++ b/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/dal/mysql/brokerage/BrokerageUserMapper.java @@ -27,13 +27,12 @@ import java.util.List; @Mapper public interface BrokerageUserMapper extends BaseMapperX { - default PageResult selectPage(BrokerageUserPageReqVO reqVO, List levels) { + default PageResult selectPage(BrokerageUserPageReqVO reqVO, List bindUserIds) { return selectPage(reqVO, new LambdaQueryWrapperX() .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 { default void updateBindUserIdAndBindUserTimeToNull(Long id) { update(null, new LambdaUpdateWrapper() .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 { .set(BrokerageUserDO::getBrokerageEnabled, false).set(BrokerageUserDO::getBrokerageTime, null)); } - default Long selectCountByBindUserIdAndLevelIn(Long bindUserId, List levels) { + default Long selectCountByBindUserIdIn(List bindUserIds) { return selectCount(new LambdaQueryWrapperX() - .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 { IPage selectSummaryPageByUserId(Page page, @Param("param") AppBrokerageUserChildSummaryPageReqVO param, @Param("userId") Long userId); + + default List selectListByBindUserId(Long bindUserId) { + return selectList(BrokerageUserDO::getBindUserId, bindUserId); + } } diff --git a/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/service/brokerage/BrokerageUserServiceImpl.java b/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/service/brokerage/BrokerageUserServiceImpl.java index 3a9e3e242..650edda13 100644 --- a/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/service/brokerage/BrokerageUserServiceImpl.java +++ b/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/service/brokerage/BrokerageUserServiceImpl.java @@ -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 getBrokerageUserPage(BrokerageUserPageReqVO pageReqVO) { - List levels = buildUserQueryLevels(pageReqVO.getBindUserId(), pageReqVO.getLevel()); - return brokerageUserMapper.selectPage(pageReqVO, levels); + List 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 levels = buildUserQueryLevels(bindUserId, level); - if (CollUtil.isEmpty(levels)) { + List 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 buildUserQueryLevels(Long bindUserId, Integer level) { - List levels = new ArrayList<>(2); - - BrokerageUserDO bindUser = getBrokerageUser(bindUserId); - if (bindUser == null) { - return levels; + private List buildBindUserIdsByLevel(Long bindUserId, Integer level) { + List bindUserIds = CollUtil.newArrayList(); + if (level == null || level == 1) { + bindUserIds.add(bindUserId); + } + if (level == null || level == 2) { + List 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; }