diff --git a/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/convert/brokerage/BrokerageUserConvert.java b/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/convert/brokerage/BrokerageUserConvert.java index ef9daccca..aa4ba348d 100644 --- a/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/convert/brokerage/BrokerageUserConvert.java +++ b/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/convert/brokerage/BrokerageUserConvert.java @@ -11,7 +11,6 @@ import cn.iocoder.yudao.module.trade.controller.app.brokerage.vo.user.AppBrokera import cn.iocoder.yudao.module.trade.dal.dataobject.brokerage.BrokerageUserDO; import cn.iocoder.yudao.module.trade.service.brokerage.bo.BrokerageWithdrawSummaryRespBO; import cn.iocoder.yudao.module.trade.service.brokerage.bo.UserBrokerageSummaryRespBO; -import com.baomidou.mybatisplus.core.metadata.IPage; import org.mapstruct.Mapper; import org.mapstruct.MappingTarget; import org.mapstruct.factory.Mappers; @@ -88,8 +87,8 @@ public interface BrokerageUserConvert { return respVO; } - default void copyTo(IPage pageResult, Map userMap) { - for (AppBrokerageUserChildSummaryRespVO vo : pageResult.getRecords()) { + default void copyTo(List list, Map userMap) { + for (AppBrokerageUserChildSummaryRespVO vo : list) { Optional.ofNullable(userMap.get(vo.getId())).ifPresent(user -> vo.setNickname(user.getNickname()).setAvatar(user.getAvatar())); } 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 3d07ea7c7..c17975677 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 @@ -138,13 +138,35 @@ public interface BrokerageUserMapper extends BaseMapperX { @Param("beginTime") LocalDateTime beginTime, @Param("endTime") LocalDateTime endTime); + /** + * 下级分销统计(分页) + * + * @param bizType 业务类型 + * @param status 状态 + * @param bindUserIds 绑定用户编号列表 + * @param sortingField 排序字段 + * @return 下级分销统计分页列表 + */ IPage selectSummaryPageByUserId(Page page, - @Param("ids") List ids, // BrokerageUser 的 ids 数组 @Param("bizType") Integer bizType, @Param("status") Integer status, @Param("bindUserIds") List bindUserIds, @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); } 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 2cef31c5e..b24a5579b 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,6 +31,7 @@ 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; @@ -225,25 +226,66 @@ public class BrokerageUserServiceImpl implements BrokerageUserService { @Override public PageResult getBrokerageUserChildSummaryPage(AppBrokerageUserChildSummaryPageReqVO pageReqVO, Long userId) { - // 1.1 根据昵称过滤用户 - List ids = StrUtil.isBlank(pageReqVO.getNickname()) - ? Collections.emptyList() - : convertList(memberUserApi.getUserListByNickname(pageReqVO.getNickname()), MemberUserRespDTO::getId); - // 1.2 生成推广员编号列表 - // TODO @疯狂:是不是可以先 1.2 查询出来,然后查询对应的昵称,进行过滤?避免昵称过滤返回的 id 过多; + // 生成推广员编号列表 List bindUserIds = buildBindUserIdsByLevel(userId, pageReqVO.getLevel()); - // 2. 分页查询 - IPage pageResult = brokerageUserMapper.selectSummaryPageByUserId( - MyBatisUtils.buildPage(pageReqVO), ids, BrokerageRecordBizTypeEnum.ORDER.getType(), - BrokerageRecordStatusEnum.SETTLEMENT.getStatus(), bindUserIds, pageReqVO.getSortingField() - ); + // 情况一:没有昵称过滤条件时,直接使用数据库的分页查询 + if (StrUtil.isBlank(pageReqVO.getNickname())) { + // 1.1 分页查询 + IPage pageResult = brokerageUserMapper.selectSummaryPageByUserId( + MyBatisUtils.buildPage(pageReqVO), BrokerageRecordBizTypeEnum.ORDER.getType(), + BrokerageRecordStatusEnum.SETTLEMENT.getStatus(), bindUserIds, pageReqVO.getSortingField() + ); - // 3. 拼接数据并返回 - List userIds = convertList(pageResult.getRecords(), AppBrokerageUserChildSummaryRespVO::getId); - Map userMap = memberUserApi.getUserMap(userIds); - BrokerageUserConvert.INSTANCE.copyTo(pageResult, userMap); - return new PageResult<>(pageResult.getRecords(), pageResult.getTotal()); + // 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)) { + 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)); + if (CollUtil.isEmpty(userMap)) { + return PageResult.empty(); + } + + // 2.4 根据用户编号过滤结果 + list.removeIf(vo -> !userMap.containsKey(vo.getId())); + if (CollUtil.isEmpty(list)) { + return PageResult.empty(); + } + + // 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()); } private boolean isUserCanBind(BrokerageUserDO user) { 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 aa22e4f14..362cf7621 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,8 +2,7 @@ - + + +