完成了第一版

This commit is contained in:
chen-xin-zhi 2025-05-14 10:40:17 +08:00
parent 1a9822350a
commit 26f9fe2a20
11 changed files with 129 additions and 73 deletions

View File

@ -2,6 +2,7 @@ package com.greenorange.promotion.controller.project;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.greenorange.promotion.annotation.RequiresPermission; import com.greenorange.promotion.annotation.RequiresPermission;
import com.greenorange.promotion.annotation.SysLog; import com.greenorange.promotion.annotation.SysLog;
import com.greenorange.promotion.common.BaseResponse; import com.greenorange.promotion.common.BaseResponse;
@ -84,10 +85,7 @@ public class ProjectDetailController {
projectService.updateById(project); projectService.updateById(project);
// 获取所有的小程序用户 // 获取所有的小程序用户
LambdaQueryWrapper<UserInfo> lambdaQueryWrapper = new LambdaQueryWrapper<>(); List<UserInfo> userInfoList = commonService.findByFieldEqTargetField(UserInfo::getUserRole, UserConstant.DEFAULT_ROLE, userInfoService);
lambdaQueryWrapper.eq(UserInfo::getUserRole, UserConstant.DEFAULT_ROLE);
List<UserInfo> userInfoList = userInfoService.list(lambdaQueryWrapper);
// 获取参数信息 // 获取参数信息
List<ProjectCommissionAddRequest> projectCommissionAddRequestList = new ArrayList<>(); List<ProjectCommissionAddRequest> projectCommissionAddRequestList = new ArrayList<>();
Long projectDetailId = projectDetail.getId(); Long projectDetailId = projectDetail.getId();
@ -108,9 +106,9 @@ public class ProjectDetailController {
projectCommissionService.saveBatch(projectCommissions); projectCommissionService.saveBatch(projectCommissions);
// 给所有用户添加一条下级项目明细抽佣表 // 给所有用户添加一条下级项目明细抽佣表
userInfoList = userInfoList.stream().filter(userInfo -> userInfo.getParentUserId() != null).collect(Collectors.toList());
List<SubUserProjectCommissionAddRequest> subUserProjectCommissionAddRequestList = new ArrayList<>(); List<SubUserProjectCommissionAddRequest> subUserProjectCommissionAddRequestList = new ArrayList<>();
for (UserInfo userInfo : userInfoList) { for (UserInfo userInfo : userInfoList) {
if (userInfo.getParentUserId() == 0) continue;
SubUserProjectCommissionAddRequest subUserProjectCommissionAddRequest = SubUserProjectCommissionAddRequest.builder() SubUserProjectCommissionAddRequest subUserProjectCommissionAddRequest = SubUserProjectCommissionAddRequest.builder()
.projectDetailId(projectDetailId) .projectDetailId(projectDetailId)
.myUnitPrice(projectSettlementPrice) .myUnitPrice(projectSettlementPrice)
@ -141,13 +139,13 @@ public class ProjectDetailController {
Long projectDetailId = projectDetailUpdateRequest.getId(); Long projectDetailId = projectDetailUpdateRequest.getId();
ProjectDetail sourceProjectDetail = projectDetailService.getById(projectDetailId); ProjectDetail sourceProjectDetail = projectDetailService.getById(projectDetailId);
ProjectDetail projectDetail = commonService.copyProperties(projectDetailUpdateRequest, ProjectDetail.class); ProjectDetail projectDetail = commonService.copyProperties(projectDetailUpdateRequest, ProjectDetail.class);
// 1.更新项目明细的结算价格
projectDetailService.updateById(projectDetail);
// 更新项目的价格 // 更新项目的价格
Long projectId = projectDetail.getProjectId(); Long projectId = projectDetail.getProjectId();
Project project = projectService.getById(projectId); Project project = projectService.getById(projectId);
project.setProjectPrice(project.getProjectPrice().subtract(sourceProjectDetail.getProjectSettlementPrice()).add(projectDetail.getProjectSettlementPrice())); project.setProjectPrice(project.getProjectPrice().subtract(sourceProjectDetail.getProjectSettlementPrice()).add(projectDetail.getProjectSettlementPrice()));
projectService.updateById(project); projectService.updateById(project);
// 1.更新项目明细的结算价格
projectDetailService.updateById(projectDetail);
// 2.更新抽佣比例如果抽佣比例比原来小 // 2.更新抽佣比例如果抽佣比例比原来小
LambdaQueryWrapper<SubUserProjectCommission> lambdaQueryWrapper = new LambdaQueryWrapper<>(); LambdaQueryWrapper<SubUserProjectCommission> lambdaQueryWrapper = new LambdaQueryWrapper<>();
lambdaQueryWrapper.eq(SubUserProjectCommission::getProjectDetailId, projectDetail.getId()); lambdaQueryWrapper.eq(SubUserProjectCommission::getProjectDetailId, projectDetail.getId());
@ -167,32 +165,31 @@ public class ProjectDetailController {
commissionRateMap.put(key, currentCommissionRate); commissionRateMap.put(key, currentCommissionRate);
} }
// 获取所有的小程序用户 // 获取所有的小程序用户
LambdaQueryWrapper<UserInfo> userInfoLambdaQueryWrapper = new LambdaQueryWrapper<>(); List<UserInfo> userInfoList = commonService.findByFieldEqTargetField(UserInfo::getUserRole, UserConstant.DEFAULT_ROLE, userInfoService);
userInfoLambdaQueryWrapper.eq(UserInfo::getUserRole, UserConstant.DEFAULT_ROLE);
List<UserInfo> userInfoList = userInfoService.list(userInfoLambdaQueryWrapper);
// 用来存储每个用户的上级列表 // 用来存储每个用户的上级列表
Map<Long, List<Long>> userParentMap = new HashMap<>(); // Map<Long, List<Long>> userParentMap = new HashMap<>();
for (UserInfo userInfo : userInfoList) { // for (UserInfo userInfo : userInfoList) {
String superUserList = userInfo.getSuperUserList(); // String superUserList = userInfo.getSuperUserList();
if (superUserList != null) userParentMap.put(userInfo.getId(), Arrays.stream(superUserList.split(",")).map(Long::parseLong).collect(Collectors.toList())); // if (superUserList != null) userParentMap.put(userInfo.getId(), Arrays.stream(superUserList.split(",")).map(Long::parseLong).collect(Collectors.toList()));
} // }
// userInfoService.findPathToRoot()
// 4.更新所有用户的结算价格 // 4.更新所有用户的结算价格
for (SubUserProjectCommission subUserProjectCommission : subUserProjectCommissionList) { // for (SubUserProjectCommission subUserProjectCommission : subUserProjectCommissionList) {
Long userId = subUserProjectCommission.getUserId(); // Long userId = subUserProjectCommission.getUserId();
List<Long> parentIds = userParentMap.get(userId); // List<Long> parentIds = userParentMap.get(userId);
parentIds.add(userId); // parentIds.add(userId);
BigDecimal totalRate = BigDecimal.ONE; // BigDecimal totalRate = BigDecimal.ONE;
for (int i = 0; i < parentIds.size() - 1; i ++ ) { // for (int i = 0; i < parentIds.size() - 1; i ++ ) {
String key = parentIds.get(i) + "-" + parentIds.get(i + 1); // String key = parentIds.get(i) + "-" + parentIds.get(i + 1);
BigDecimal commissionRate = commissionRateMap.get(key); // BigDecimal commissionRate = commissionRateMap.get(key);
totalRate = totalRate.multiply(BigDecimal.ONE.subtract(commissionRate)); // totalRate = totalRate.multiply(BigDecimal.ONE.subtract(commissionRate));
} // }
BigDecimal projectSettlementPrice = projectDetail.getProjectSettlementPrice(); // BigDecimal projectSettlementPrice = projectDetail.getProjectSettlementPrice();
BigDecimal projectMinSettlementPrice = projectDetail.getProjectMinSettlementPrice(); // BigDecimal projectMinSettlementPrice = projectDetail.getProjectMinSettlementPrice();
BigDecimal finallySettlementPrice = projectSettlementPrice.multiply(totalRate); // BigDecimal finallySettlementPrice = projectSettlementPrice.multiply(totalRate);
if (finallySettlementPrice.compareTo(projectMinSettlementPrice) < 0) finallySettlementPrice = projectMinSettlementPrice; // if (finallySettlementPrice.compareTo(projectMinSettlementPrice) < 0) finallySettlementPrice = projectMinSettlementPrice;
subUserProjectCommission.setMyUnitPrice(finallySettlementPrice); // subUserProjectCommission.setMyUnitPrice(finallySettlementPrice);
} // }
subUserProjectCommissionService.updateBatchById(subUserProjectCommissionList); subUserProjectCommissionService.updateBatchById(subUserProjectCommissionList);
return ResultUtils.success(true); return ResultUtils.success(true);
@ -243,7 +240,6 @@ public class ProjectDetailController {
public BaseResponse<ProjectDetailVO> queryProjectDetailById(@Valid @RequestBody CommonRequest commonRequest) { public BaseResponse<ProjectDetailVO> queryProjectDetailById(@Valid @RequestBody CommonRequest commonRequest) {
Long id = commonRequest.getId(); Long id = commonRequest.getId();
ProjectDetail projectDetail = projectDetailService.getById(id); ProjectDetail projectDetail = projectDetailService.getById(id);
ThrowUtils.throwIf(projectDetail == null, ErrorCode.OPERATION_ERROR, "当前项目明细不存在");
ProjectDetailVO projectDetailVO = commonService.copyProperties(projectDetail, ProjectDetailVO.class); ProjectDetailVO projectDetailVO = commonService.copyProperties(projectDetail, ProjectDetailVO.class);
return ResultUtils.success(projectDetailVO); return ResultUtils.success(projectDetailVO);
} }
@ -260,9 +256,7 @@ public class ProjectDetailController {
@SysLog(title = "项目明细管理", content = "web端管理员根据项目id查询项目明细") @SysLog(title = "项目明细管理", content = "web端管理员根据项目id查询项目明细")
public BaseResponse<List<ProjectDetailVO>> queryProjectDetailByPid(@Valid @RequestBody CommonRequest commonRequest) { public BaseResponse<List<ProjectDetailVO>> queryProjectDetailByPid(@Valid @RequestBody CommonRequest commonRequest) {
Long id = commonRequest.getId(); Long id = commonRequest.getId();
LambdaQueryWrapper<ProjectDetail> lambdaQueryWrapper = new LambdaQueryWrapper<>(); List<ProjectDetail> projectDetailList = commonService.findByFieldEqTargetField(ProjectDetail::getProjectId, id, projectDetailService);
lambdaQueryWrapper.eq(ProjectDetail::getProjectId, id);
List<ProjectDetail> projectDetailList = projectDetailService.list(lambdaQueryWrapper);
List<ProjectDetailVO> projectDetailVOS = commonService.convertList(projectDetailList, ProjectDetailVO.class); List<ProjectDetailVO> projectDetailVOS = commonService.convertList(projectDetailList, ProjectDetailVO.class);
return ResultUtils.success(projectDetailVOS); return ResultUtils.success(projectDetailVOS);
} }

View File

@ -390,6 +390,21 @@ public class UserInfoController {
} }
/**
* (小程序端)查询当前用户到根节点的userId路径
* @param commonRequest 用户id
* @return 用户表列表
*/
@PostMapping("query/path")
@Operation(summary = "查询当前用户到根节点的userId路径", description = "参数用户id权限管理员boss, admin),方法名:findPathToRootUserIdList")
@RequiresPermission(mustRole = UserConstant.DEFAULT_ROLE)
@SysLog(title = "用户管理", content = "查询当前用户到根节点的userId路径")
public BaseResponse<List<Long>> findPathToRootUserIdList(@Valid @RequestBody CommonRequest commonRequest) {
Long userId = commonRequest.getId();
List<Long> pathToRoot = userInfoService.findPathToRoot(userId);
return ResultUtils.success(pathToRoot);
}

View File

@ -3,6 +3,8 @@ package com.greenorange.promotion.mapper;
import com.greenorange.promotion.model.entity.UserInfo; import com.greenorange.promotion.model.entity.UserInfo;
import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import java.util.List;
/** /**
* @author 35880 * @author 35880
* @description 针对表user_info(用户基本信息表)的数据库操作Mapper * @description 针对表user_info(用户基本信息表)的数据库操作Mapper
@ -11,6 +13,13 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper;
*/ */
public interface UserInfoMapper extends BaseMapper<UserInfo> { public interface UserInfoMapper extends BaseMapper<UserInfo> {
/**
* 查询从 userId 一路到根节点的所有 id depth 倒序根先出
*/
List<Long> findPathToRoot(Long userId);
} }

View File

@ -55,12 +55,6 @@ public class UserInfoAddRequest implements Serializable {
@Schema(description = "密码(建议加密存储)", example = "qingcheng") @Schema(description = "密码(建议加密存储)", example = "qingcheng")
private String userPassword; private String userPassword;
/**
* 邀请码
*/
@Schema(description = "邀请码", example = "666999")
private String invitationCode;
/** /**
* 用户角色 * 用户角色
*/ */
@ -68,18 +62,6 @@ public class UserInfoAddRequest implements Serializable {
@Schema(description = "用户角色", example = "user") @Schema(description = "用户角色", example = "user")
private String userRole; private String userRole;
/**
* 上级用户id
*/
@Schema(description = "上级用户id", example = "1")
private Long parentUserId;
/**
* 上级用户列表1,2,3
*/
@Schema(description = "上级用户列表1,2,3", example = "1,2,3")
private String superUserList;
@Serial @Serial
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;

View File

@ -84,12 +84,6 @@ public class UserInfoUpdateRequest implements Serializable {
@Schema(description = "上级用户id", example = "1") @Schema(description = "上级用户id", example = "1")
private Long parentUserId; private Long parentUserId;
/**
* 上级用户列表1,2,3
*/
@Schema(description = "上级用户列表1,2,3", example = "1,2,3")
private String superUserList;
@Serial @Serial
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;

View File

@ -65,11 +65,6 @@ public class UserInfo implements Serializable {
*/ */
private Long parentUserId; private Long parentUserId;
/**
* 上级用户列表1,2,3
*/
private String superUserList;
/** /**
* 是否删除 * 是否删除
*/ */

View File

@ -69,12 +69,6 @@ public class UserInfoVO implements Serializable {
@Schema(description = "上级用户id", example = "1") @Schema(description = "上级用户id", example = "1")
private Long parentUserId; private Long parentUserId;
/**
* 上级用户列表1,2,3
*/
@Schema(description = "上级用户列表1,2,3", example = "1,2,3")
private String superUserList;
@Serial @Serial
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;

View File

@ -5,6 +5,8 @@ import com.greenorange.promotion.model.dto.userInfo.*;
import com.greenorange.promotion.model.entity.UserInfo; import com.greenorange.promotion.model.entity.UserInfo;
import com.baomidou.mybatisplus.extension.service.IService; import com.baomidou.mybatisplus.extension.service.IService;
import java.util.List;
/** /**
* @author 35880 * @author 35880
* @description 针对表user_info(用户基本信息表)的数据库操作Service * @description 针对表user_info(用户基本信息表)的数据库操作Service
@ -53,4 +55,10 @@ public interface UserInfoService extends IService<UserInfo> {
* 小程序用户获取验证码 * 小程序用户获取验证码
*/ */
String getVerificationCode(String phoneNumber); String getVerificationCode(String phoneNumber);
/**
* 查询从 userId 一路到根节点的所有 id depth 倒序根先出
*/
List<Long> findPathToRoot(Long userId);
} }

View File

@ -12,10 +12,14 @@ import com.greenorange.promotion.constant.UserConstant;
import com.greenorange.promotion.exception.ThrowUtils; import com.greenorange.promotion.exception.ThrowUtils;
import com.greenorange.promotion.mapper.UserInfoMapper; import com.greenorange.promotion.mapper.UserInfoMapper;
import com.greenorange.promotion.model.dto.userInfo.*; import com.greenorange.promotion.model.dto.userInfo.*;
import com.greenorange.promotion.model.entity.ProjectCommission;
import com.greenorange.promotion.model.entity.SubUserProjectCommission;
import com.greenorange.promotion.model.entity.UserInfo; import com.greenorange.promotion.model.entity.UserInfo;
import com.greenorange.promotion.model.entity.UserMainInfo; import com.greenorange.promotion.model.entity.UserMainInfo;
import com.greenorange.promotion.model.enums.UserRoleEnum; import com.greenorange.promotion.model.enums.UserRoleEnum;
import com.greenorange.promotion.service.common.CommonService; import com.greenorange.promotion.service.common.CommonService;
import com.greenorange.promotion.service.project.ProjectCommissionService;
import com.greenorange.promotion.service.project.SubUserProjectCommissionService;
import com.greenorange.promotion.service.userInfo.UserInfoService; import com.greenorange.promotion.service.userInfo.UserInfoService;
import com.greenorange.promotion.service.userInfo.UserMainInfoService; import com.greenorange.promotion.service.userInfo.UserMainInfoService;
import com.greenorange.promotion.service.wechat.WechatGetQrcodeService; import com.greenorange.promotion.service.wechat.WechatGetQrcodeService;
@ -29,7 +33,10 @@ import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.io.IOException; import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@ -61,6 +68,15 @@ public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo>
@Resource @Resource
private WechatGetQrcodeService wechatGetQrcodeService; private WechatGetQrcodeService wechatGetQrcodeService;
@Resource
private ProjectCommissionService projectCommissionService;
@Resource
private SubUserProjectCommissionService subUserProjectCommissionService;
@Resource
private UserInfoMapper userInfoMapper;
/** /**
* 获取查询条件 * 获取查询条件
@ -127,13 +143,16 @@ public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo>
UserInfo parentUserInfo = this.getOne(invitedLambdaQueryWrapper); UserInfo parentUserInfo = this.getOne(invitedLambdaQueryWrapper);
ThrowUtils.throwIf(parentUserInfo == null, ErrorCode.OPERATION_ERROR, "邀请码错误"); ThrowUtils.throwIf(parentUserInfo == null, ErrorCode.OPERATION_ERROR, "邀请码错误");
UserInfo myUserInfo = commonService.copyProperties(userInfoRegisterRequest, UserInfo.class); UserInfo myUserInfo = commonService.copyProperties(userInfoRegisterRequest, UserInfo.class);
// 判断当前用户是否是根节点
List<UserInfo> userInfoList = commonService.findByFieldEqTargetField(UserInfo::getUserRole, UserConstant.DEFAULT_ROLE, this);
if (userInfoList.isEmpty()) myUserInfo.setParentUserId(0L);
myUserInfo.setParentUserId(parentUserInfo.getId()); myUserInfo.setParentUserId(parentUserInfo.getId());
myUserInfo.setInvitationCode(RandomUtil.randomNumbers(6)); myUserInfo.setInvitationCode(RandomUtil.randomNumbers(6));
myUserInfo.setUserAccount(phoneNumber); myUserInfo.setUserAccount(phoneNumber);
myUserInfo.setUserRole(UserConstant.DEFAULT_ROLE); myUserInfo.setUserRole(UserConstant.DEFAULT_ROLE);
myUserInfo.setUserAvatar(UserConstant.USER_DEFAULT_AVATAR); myUserInfo.setUserAvatar(UserConstant.USER_DEFAULT_AVATAR);
myUserInfo.setSuperUserList(parentUserInfo.getSuperUserList() + "," + parentUserInfo.getId());
this.save(myUserInfo); this.save(myUserInfo);
UserMainInfo userMainInfo = new UserMainInfo(); UserMainInfo userMainInfo = new UserMainInfo();
userMainInfo.setUserId(myUserInfo.getId()); userMainInfo.setUserId(myUserInfo.getId());
@ -146,6 +165,24 @@ public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo>
e.printStackTrace(); e.printStackTrace();
} }
userMainInfoService.save(userMainInfo); userMainInfoService.save(userMainInfo);
List<SubUserProjectCommission> subUserProjectCommissionList = commonService.findByFieldEqTargetField(SubUserProjectCommission::getSubUserId, parentUserInfo.getId(), subUserProjectCommissionService);
List<ProjectCommission> projectCommissionList = new ArrayList<>();
for (SubUserProjectCommission subUserProjectCommission : subUserProjectCommissionList) {
// 插入下级用户项目明细抽佣记录
subUserProjectCommission.setId(null);
subUserProjectCommission.setUserId(parentUserInfo.getId());
subUserProjectCommission.setSubUserId(myUserInfo.getId());
subUserProjectCommission.setMyUnitPrice(subUserProjectCommission.getMyUnitPrice().multiply(BigDecimal.ONE.subtract(subUserProjectCommission.getCurrentCommissionRate())));
// 插入用户项目明细抽佣记录
ProjectCommission projectCommission = commonService.copyProperties(subUserProjectCommission, ProjectCommission.class);
projectCommission.setId(null);
projectCommission.setCurrentCommissionRate(BigDecimal.ZERO);
projectCommission.setUserId(myUserInfo.getId());
}
subUserProjectCommissionService.saveBatch(subUserProjectCommissionList);
projectCommissionService.saveBatch(projectCommissionList);
} }
@ -246,6 +283,14 @@ public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo>
} }
/**
* 查询从 userId 一路到根节点的所有 id depth 倒序根先出
*/
@Override
public List<Long> findPathToRoot(Long userId) {
return userInfoMapper.findPathToRoot(userId);
}
} }

View File

@ -12,8 +12,6 @@
<result property="promoCodeLink" column="promoCodeLink" jdbcType="VARCHAR"/> <result property="promoCodeLink" column="promoCodeLink" jdbcType="VARCHAR"/>
<result property="projectName" column="projectName" jdbcType="VARCHAR"/> <result property="projectName" column="projectName" jdbcType="VARCHAR"/>
<result property="projectImage" column="projectImage" jdbcType="VARCHAR"/> <result property="projectImage" column="projectImage" jdbcType="VARCHAR"/>
<result property="maxProjectPrice" column="maxProjectPrice" jdbcType="DECIMAL"/>
<result property="minProjectPrice" column="minProjectPrice" jdbcType="DECIMAL"/>
<result property="userId" column="userId" jdbcType="BIGINT"/> <result property="userId" column="userId" jdbcType="BIGINT"/>
<result property="isDelete" column="isDelete" jdbcType="TINYINT"/> <result property="isDelete" column="isDelete" jdbcType="TINYINT"/>
<result property="createTime" column="createTime" jdbcType="TIMESTAMP"/> <result property="createTime" column="createTime" jdbcType="TIMESTAMP"/>

View File

@ -25,4 +25,26 @@
userRole,parentUserId,superUserList, userRole,parentUserId,superUserList,
isDelete,createTime,updateTime isDelete,createTime,updateTime
</sql> </sql>
<!-- 查询从 userId 一路到根节点的所有 id按 depth 倒序(根先出)-->
<select id="findPathToRoot" resultType="java.lang.Long">
WITH RECURSIVE user_path AS (
SELECT id, parentUserId, 1 AS depth
FROM user_info
WHERE id = #{userId}
UNION ALL
SELECT u.id, u.parentUserId, up.depth + 1
FROM user_info u
JOIN user_path up ON u.id = up.parentUserId
WHERE up.depth &lt; 50000
)
SELECT id
FROM user_path
ORDER BY depth DESC
</select>
</mapper> </mapper>