diff --git a/yudao-framework/yudao-common/src/main/java/cn/iocoder/yudao/framework/common/util/collection/CollectionUtils.java b/yudao-framework/yudao-common/src/main/java/cn/iocoder/yudao/framework/common/util/collection/CollectionUtils.java index 15fa4e03b..12d18dc95 100644 --- a/yudao-framework/yudao-common/src/main/java/cn/iocoder/yudao/framework/common/util/collection/CollectionUtils.java +++ b/yudao-framework/yudao-common/src/main/java/cn/iocoder/yudao/framework/common/util/collection/CollectionUtils.java @@ -78,6 +78,13 @@ public class CollectionUtils { return from.stream().filter(filter).map(func).filter(Objects::nonNull).collect(Collectors.toSet()); } + public static Map convertMapByFilter(Collection from, Predicate filter, Function keyFunc) { + if (CollUtil.isEmpty(from)) { + return new HashMap<>(); + } + return from.stream().filter(filter).collect(Collectors.toMap(keyFunc, v -> v)); + } + public static Map convertMap(Collection from, Function keyFunc) { if (CollUtil.isEmpty(from)) { return new HashMap<>(); diff --git a/yudao-module-mall/yudao-module-trade-api/src/main/java/cn/iocoder/yudao/module/trade/enums/ErrorCodeConstants.java b/yudao-module-mall/yudao-module-trade-api/src/main/java/cn/iocoder/yudao/module/trade/enums/ErrorCodeConstants.java index e6ed1b897..ef5ff7273 100644 --- a/yudao-module-mall/yudao-module-trade-api/src/main/java/cn/iocoder/yudao/module/trade/enums/ErrorCodeConstants.java +++ b/yudao-module-mall/yudao-module-trade-api/src/main/java/cn/iocoder/yudao/module/trade/enums/ErrorCodeConstants.java @@ -81,6 +81,7 @@ public interface ErrorCodeConstants { ErrorCode BROKERAGE_BIND_MODE_REGISTER = new ErrorCode(1_011_007_005, "只有在注册时可以绑定"); ErrorCode BROKERAGE_BIND_OVERRIDE = new ErrorCode(1_011_007_006, "已绑定了推广人"); ErrorCode BROKERAGE_BIND_LOOP = new ErrorCode(1_011_007_007, "下级不能绑定自己的上级"); + ErrorCode BROKERAGE_USER_LEVEL_NOT_SUPPORT = new ErrorCode(1_011_007_008, "目前只支持 level 小于等于 2"); // ========== 分销提现 模块 1-011-008-000 ========== 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 c17975677..0e41c6124 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 @@ -1,5 +1,6 @@ package cn.iocoder.yudao.module.trade.dal.mysql.brokerage; +import cn.hutool.core.convert.Convert; import cn.hutool.core.lang.Assert; import cn.iocoder.yudao.framework.common.pojo.PageResult; import cn.iocoder.yudao.framework.common.pojo.SortingField; @@ -17,6 +18,8 @@ import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Select; import java.time.LocalDateTime; +import java.util.Collection; +import java.util.Collections; import java.util.List; /** @@ -27,12 +30,12 @@ import java.util.List; @Mapper public interface BrokerageUserMapper extends BaseMapperX { - default PageResult selectPage(BrokerageUserPageReqVO reqVO, List bindUserIds) { + default PageResult selectPage(BrokerageUserPageReqVO reqVO, List ids) { return selectPage(reqVO, new LambdaQueryWrapperX() + .inIfPresent(BrokerageUserDO::getId, ids) .eqIfPresent(BrokerageUserDO::getBrokerageEnabled, reqVO.getBrokerageEnabled()) .betweenIfPresent(BrokerageUserDO::getCreateTime, reqVO.getCreateTime()) .betweenIfPresent(BrokerageUserDO::getBindUserTime, reqVO.getBindUserTime()) - .inIfPresent(BrokerageUserDO::getBindUserId, bindUserIds) .orderByDesc(BrokerageUserDO::getId)); } @@ -124,11 +127,6 @@ public interface BrokerageUserMapper extends BaseMapperX { .set(BrokerageUserDO::getBrokerageEnabled, false).set(BrokerageUserDO::getBrokerageTime, null)); } - default Long selectCountByBindUserIdIn(List bindUserIds) { - return selectCount(new LambdaQueryWrapperX() - .inIfPresent(BrokerageUserDO::getBindUserId, bindUserIds)); - } - @Select("SELECT bind_user_id AS id, COUNT(1) AS brokerageUserCount FROM trade_brokerage_user " + "WHERE bind_user_id IS NOT NULL AND deleted = FALSE " + "AND bind_user_time BETWEEN #{beginTime} AND #{endTime} " + @@ -143,32 +141,21 @@ public interface BrokerageUserMapper extends BaseMapperX { * * @param bizType 业务类型 * @param status 状态 - * @param bindUserIds 绑定用户编号列表 + * @param ids 用户编号列表 * @param sortingField 排序字段 * @return 下级分销统计分页列表 */ IPage selectSummaryPageByUserId(Page page, @Param("bizType") Integer bizType, @Param("status") Integer status, - @Param("bindUserIds") List bindUserIds, + @Param("ids") Collection ids, @Param("sortingField") SortingField sortingField); - /** - * 下级分销统计(不分页) - * - * @param bizType 业务类型 - * @param status 状态 - * @param bindUserIds 绑定用户编号列表 - * @param sortingField 排序字段 - * @return 下级分销统计列表 - */ - List selectSummaryListByUserId(@Param("bizType") Integer bizType, - @Param("status") Integer status, - @Param("bindUserIds") List bindUserIds, - @Param("sortingField") SortingField sortingField); - - default List selectListByBindUserId(Long bindUserId) { - return selectList(BrokerageUserDO::getBindUserId, bindUserId); + default List selectIdListByBindUserIdIn(Collection bindUserIds) { + return Convert.toList(Long.class, + selectObjs(new LambdaQueryWrapperX() + .select(Collections.singletonList(BrokerageUserDO::getId)) + .in(BrokerageUserDO::getBindUserId, bindUserIds))); } } 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 71922615c..aa8a3a7cd 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 @@ -31,10 +31,9 @@ import org.springframework.validation.annotation.Validated; import javax.annotation.Resource; import java.time.LocalDateTime; import java.util.*; -import java.util.stream.Collectors; 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.framework.common.util.collection.CollectionUtils.convertMapByFilter; import static cn.iocoder.yudao.module.trade.enums.ErrorCodeConstants.*; /** @@ -67,8 +66,12 @@ public class BrokerageUserServiceImpl implements BrokerageUserService { @Override public PageResult getBrokerageUserPage(BrokerageUserPageReqVO pageReqVO) { - List bindUserIds = buildBindUserIdsByLevel(pageReqVO.getBindUserId(), pageReqVO.getLevel()); - return brokerageUserMapper.selectPage(pageReqVO, bindUserIds); + List childIds = getChildUserIdsByLevel(pageReqVO.getBindUserId(), pageReqVO.getLevel()); + // 有”绑定用户编号“查询条件时,没有查到下级会员,直接返回空 + if (pageReqVO.getBindUserId() != null && CollUtil.isEmpty(childIds)) { + return PageResult.empty(); + } + return brokerageUserMapper.selectPage(pageReqVO, childIds); } @Override @@ -154,11 +157,8 @@ public class BrokerageUserServiceImpl implements BrokerageUserService { @Override public Long getBrokerageUserCountByBindUserId(Long bindUserId, Integer level) { - List bindUserIds = buildBindUserIdsByLevel(bindUserId, level); - if (CollUtil.isEmpty(bindUserIds)) { - return 0L; - } - return brokerageUserMapper.selectCountByBindUserIdIn(bindUserIds); + List childIds = getChildUserIdsByLevel(bindUserId, level); + return (long) CollUtil.size(childIds); } @Override @@ -226,70 +226,29 @@ public class BrokerageUserServiceImpl implements BrokerageUserService { @Override public PageResult getBrokerageUserChildSummaryPage(AppBrokerageUserChildSummaryPageReqVO pageReqVO, Long userId) { - // 生成推广员编号列表 - List bindUserIds = buildBindUserIdsByLevel(userId, pageReqVO.getLevel()); - - // TODO @疯狂:情况一和情况二,可以合并哈; - // 如果有 nickname 的时候,相当于提前查询 users,然后 nickname 过滤掉 bindUserIds; - // 之后,继续使用 selectSummaryPageByUserId 里面 in bindUserIds 查询; - - // 情况一:没有昵称过滤条件时,直接使用数据库的分页查询 - if (StrUtil.isBlank(pageReqVO.getNickname())) { - // 1.1 分页查询 - IPage pageResult = brokerageUserMapper.selectSummaryPageByUserId( - MyBatisUtils.buildPage(pageReqVO), BrokerageRecordBizTypeEnum.ORDER.getType(), - BrokerageRecordStatusEnum.SETTLEMENT.getStatus(), bindUserIds, pageReqVO.getSortingField() - ); - - // 1.2 拼接数据并返回 - List userIds = convertList(pageResult.getRecords(), AppBrokerageUserChildSummaryRespVO::getId); - Map userMap = memberUserApi.getUserMap(userIds); - BrokerageUserConvert.INSTANCE.copyTo(pageResult.getRecords(), userMap); - return new PageResult<>(pageResult.getRecords(), pageResult.getTotal()); - } - - // 情况二:有昵称过滤条件时,需要跨模块(Member)过滤 - // 2.1 查询所有匹配的分销用户 - List list = brokerageUserMapper.selectSummaryListByUserId( - BrokerageRecordBizTypeEnum.ORDER.getType(), BrokerageRecordStatusEnum.SETTLEMENT.getStatus(), - bindUserIds, pageReqVO.getSortingField() - ); - if (CollUtil.isEmpty(list)) { + // 1.1 查询下级用户编号列表 + List childIds = getChildUserIdsByLevel(userId, pageReqVO.getLevel()); + if (CollUtil.isEmpty(childIds)) { return PageResult.empty(); } - // 2.2 查出对应的用户信息 - List users = memberUserApi.getUserList(convertList(list, AppBrokerageUserChildSummaryRespVO::getId)); - if (CollUtil.isEmpty(users)) { - return PageResult.empty(); - } - - // 2.3 根据昵称过滤出用户编号 - Map userMap = users.stream() - .filter(user -> StrUtil.contains(user.getNickname(), pageReqVO.getNickname())) - .collect(Collectors.toMap(MemberUserRespDTO::getId, dto -> dto)); + // 1.2 根据昵称过滤下级用户 + Map userMap = convertMapByFilter(memberUserApi.getUserList(childIds), + user -> StrUtil.contains(user.getNickname(), pageReqVO.getNickname()), + MemberUserRespDTO::getId); if (CollUtil.isEmpty(userMap)) { return PageResult.empty(); } - // 2.4 根据用户编号过滤结果 - list.removeIf(vo -> !userMap.containsKey(vo.getId())); - if (CollUtil.isEmpty(list)) { - return PageResult.empty(); - } + // 2 分页查询 + IPage pageResult = brokerageUserMapper.selectSummaryPageByUserId( + MyBatisUtils.buildPage(pageReqVO), BrokerageRecordBizTypeEnum.ORDER.getType(), + BrokerageRecordStatusEnum.SETTLEMENT.getStatus(), userMap.keySet(), pageReqVO.getSortingField() + ); - // 2.5 处理分页 - List result = list.stream() - .skip((long) (pageReqVO.getPageNo() - 1) * pageReqVO.getPageSize()) - .limit(pageReqVO.getPageSize()) - .collect(Collectors.toList()); - if (CollUtil.isEmpty(result)) { - return PageResult.empty(); - } - - // 2.6 拼接数据并返回 - BrokerageUserConvert.INSTANCE.copyTo(result, userMap); - return new PageResult<>(result, (long) list.size()); + // 3 拼接数据并返回 + BrokerageUserConvert.INSTANCE.copyTo(pageResult.getRecords(), userMap); + return new PageResult<>(pageResult.getRecords(), pageResult.getTotal()); } private boolean isUserCanBind(BrokerageUserDO user) { @@ -360,24 +319,35 @@ public class BrokerageUserServiceImpl implements BrokerageUserService { } /** - * 根据绑定用户编号,获得绑定用户编号列表 + * 根据绑定用户编号,获得下级用户编号列表 * * @param bindUserId 绑定用户编号 - * @param level 绑定用户的层级。 + * @param level 下级用户的层级。 * 如果 level 为空,则查询 1+2 两个层级 - * @return 绑定用户编号列表 + * @return 下级用户编号列表 */ - private List buildBindUserIdsByLevel(Long bindUserId, Integer level) { + private List getChildUserIdsByLevel(Long bindUserId, Integer level) { if (bindUserId == null) { return Collections.emptyList(); } - Assert.isTrue(level == null || level <= 2, "目前只支持 level 小于等于 2"); - List bindUserIds = CollUtil.newArrayList(); - if (level == null || level == 1) { - bindUserIds.add(bindUserId); + + // 先查第 1 级 + List bindUserIds = brokerageUserMapper.selectIdListByBindUserIdIn(Collections.singleton(bindUserId)); + if (CollUtil.isEmpty(bindUserIds)) { + return Collections.emptyList(); } - if (level == null || level == 2) { - bindUserIds.addAll(convertList(brokerageUserMapper.selectListByBindUserId(bindUserId), BrokerageUserDO::getId)); + + if (level == null) { + // level 为空,再查第 2 级,并合并结果 + bindUserIds.addAll(brokerageUserMapper.selectIdListByBindUserIdIn(bindUserIds)); + } else if (level == 2) { + // 只查第 2 级 + bindUserIds = brokerageUserMapper.selectIdListByBindUserIdIn(bindUserIds); + } else if (level == 1) { + // 只查第 1 级 + return bindUserIds; + } else { + throw exception(BROKERAGE_USER_LEVEL_NOT_SUPPORT); } return bindUserIds; } diff --git a/yudao-module-mall/yudao-module-trade-biz/src/main/resources/mapper/brokerage/BrokerageUserMapper.xml b/yudao-module-mall/yudao-module-trade-biz/src/main/resources/mapper/brokerage/BrokerageUserMapper.xml index 362cf7621..066f75d4e 100644 --- a/yudao-module-mall/yudao-module-trade-biz/src/main/resources/mapper/brokerage/BrokerageUserMapper.xml +++ b/yudao-module-mall/yudao-module-trade-biz/src/main/resources/mapper/brokerage/BrokerageUserMapper.xml @@ -2,7 +2,9 @@ - + - - -