分销:重构下级分销统计

This commit is contained in:
owen 2023-09-30 23:30:06 +08:00
parent cafa938ff4
commit 7d68bdc7c8
4 changed files with 93 additions and 28 deletions

View File

@ -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<AppBrokerageUserChildSummaryRespVO> pageResult, Map<Long, MemberUserRespDTO> userMap) {
for (AppBrokerageUserChildSummaryRespVO vo : pageResult.getRecords()) {
default void copyTo(List<AppBrokerageUserChildSummaryRespVO> list, Map<Long, MemberUserRespDTO> userMap) {
for (AppBrokerageUserChildSummaryRespVO vo : list) {
Optional.ofNullable(userMap.get(vo.getId())).ifPresent(user ->
vo.setNickname(user.getNickname()).setAvatar(user.getAvatar()));
}

View File

@ -138,13 +138,35 @@ public interface BrokerageUserMapper extends BaseMapperX<BrokerageUserDO> {
@Param("beginTime") LocalDateTime beginTime,
@Param("endTime") LocalDateTime endTime);
/**
* 下级分销统计分页
*
* @param bizType 业务类型
* @param status 状态
* @param bindUserIds 绑定用户编号列表
* @param sortingField 排序字段
* @return 下级分销统计分页列表
*/
IPage<AppBrokerageUserChildSummaryRespVO> selectSummaryPageByUserId(Page<?> page,
@Param("ids") List<Long> ids, // BrokerageUser ids 数组
@Param("bizType") Integer bizType,
@Param("status") Integer status,
@Param("bindUserIds") List<Long> bindUserIds,
@Param("sortingField") SortingField sortingField);
/**
* 下级分销统计不分页
*
* @param bizType 业务类型
* @param status 状态
* @param bindUserIds 绑定用户编号列表
* @param sortingField 排序字段
* @return 下级分销统计列表
*/
List<AppBrokerageUserChildSummaryRespVO> selectSummaryListByUserId(@Param("bizType") Integer bizType,
@Param("status") Integer status,
@Param("bindUserIds") List<Long> bindUserIds,
@Param("sortingField") SortingField sortingField);
default List<BrokerageUserDO> selectListByBindUserId(Long bindUserId) {
return selectList(BrokerageUserDO::getBindUserId, bindUserId);
}

View File

@ -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<AppBrokerageUserChildSummaryRespVO> getBrokerageUserChildSummaryPage(AppBrokerageUserChildSummaryPageReqVO pageReqVO, Long userId) {
// 1.1 根据昵称过滤用户
List<Long> ids = StrUtil.isBlank(pageReqVO.getNickname())
? Collections.emptyList()
: convertList(memberUserApi.getUserListByNickname(pageReqVO.getNickname()), MemberUserRespDTO::getId);
// 1.2 生成推广员编号列表
// TODO @疯狂是不是可以先 1.2 查询出来然后查询对应的昵称进行过滤避免昵称过滤返回的 id 过多
// 生成推广员编号列表
List<Long> bindUserIds = buildBindUserIdsByLevel(userId, pageReqVO.getLevel());
// 2. 分页查询
IPage<AppBrokerageUserChildSummaryRespVO> pageResult = brokerageUserMapper.selectSummaryPageByUserId(
MyBatisUtils.buildPage(pageReqVO), ids, BrokerageRecordBizTypeEnum.ORDER.getType(),
BrokerageRecordStatusEnum.SETTLEMENT.getStatus(), bindUserIds, pageReqVO.getSortingField()
);
// 情况一没有昵称过滤条件时直接使用数据库的分页查询
if (StrUtil.isBlank(pageReqVO.getNickname())) {
// 1.1 分页查询
IPage<AppBrokerageUserChildSummaryRespVO> pageResult = brokerageUserMapper.selectSummaryPageByUserId(
MyBatisUtils.buildPage(pageReqVO), BrokerageRecordBizTypeEnum.ORDER.getType(),
BrokerageRecordStatusEnum.SETTLEMENT.getStatus(), bindUserIds, pageReqVO.getSortingField()
);
// 3. 拼接数据并返回
List<Long> userIds = convertList(pageResult.getRecords(), AppBrokerageUserChildSummaryRespVO::getId);
Map<Long, MemberUserRespDTO> userMap = memberUserApi.getUserMap(userIds);
BrokerageUserConvert.INSTANCE.copyTo(pageResult, userMap);
return new PageResult<>(pageResult.getRecords(), pageResult.getTotal());
// 1.2 拼接数据并返回
List<Long> userIds = convertList(pageResult.getRecords(), AppBrokerageUserChildSummaryRespVO::getId);
Map<Long, MemberUserRespDTO> userMap = memberUserApi.getUserMap(userIds);
BrokerageUserConvert.INSTANCE.copyTo(pageResult.getRecords(), userMap);
return new PageResult<>(pageResult.getRecords(), pageResult.getTotal());
}
// 情况二有昵称过滤条件时需要跨模块Member过滤
// 2.1 查询所有匹配的分销用户
List<AppBrokerageUserChildSummaryRespVO> list = brokerageUserMapper.selectSummaryListByUserId(
BrokerageRecordBizTypeEnum.ORDER.getType(), BrokerageRecordStatusEnum.SETTLEMENT.getStatus(),
bindUserIds, pageReqVO.getSortingField()
);
if (CollUtil.isEmpty(list)) {
return PageResult.empty();
}
// 2.2 查出对应的用户信息
List<MemberUserRespDTO> users = memberUserApi.getUserList(convertList(list, AppBrokerageUserChildSummaryRespVO::getId));
if (CollUtil.isEmpty(users)) {
return PageResult.empty();
}
// 2.3 根据昵称过滤出用户编号
Map<Long, MemberUserRespDTO> 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<AppBrokerageUserChildSummaryRespVO> 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) {

View File

@ -2,8 +2,7 @@
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.iocoder.yudao.module.trade.dal.mysql.brokerage.BrokerageUserMapper">
<select id="selectSummaryPageByUserId"
resultType="cn.iocoder.yudao.module.trade.controller.app.brokerage.vo.user.AppBrokerageUserChildSummaryRespVO">
<sql id="selectSummaryListByUserId">
SELECT bu.id, bu.bind_user_time AS brokerageTime,
(SELECT SUM(price) FROM trade_brokerage_record r
WHERE r.user_id = bu.id AND biz_type = #{bizType} AND r.status = #{status} AND r.deleted = FALSE) AS brokeragePrice,
@ -14,12 +13,6 @@
FROM trade_brokerage_user AS bu
<where>
bu.deleted = false
<if test="ids != null and ids.size() > 0">
and bu.id in
<foreach collection="ids" open="(" item="id" separator="," close=")">
#{id}
</foreach>
</if>
<if test="bindUserIds != null and bindUserIds.size() > 0">
and bu.bind_user_id in
<foreach collection="bindUserIds" open="(" item="bindUserId" separator="," close=")">
@ -41,6 +34,15 @@
ORDER BY bu.bind_user_time DESC
</otherwise>
</choose>
</sql>
<select id="selectSummaryPageByUserId"
resultType="cn.iocoder.yudao.module.trade.controller.app.brokerage.vo.user.AppBrokerageUserChildSummaryRespVO">
<include refid="selectSummaryListByUserId" />
</select>
<select id="selectSummaryListByUserId"
resultType="cn.iocoder.yudao.module.trade.controller.app.brokerage.vo.user.AppBrokerageUserChildSummaryRespVO">
<include refid="selectSummaryListByUserId" />
</select>
</mapper>