Commit ae493e66dface723e8b08f1b93f72a1ff90feebb
1 parent
ea7f9230
MALL-TRADE:新增 BrokerageUserService#getOrCreateBrokerageUser 获得或创建分销用户方法,解决父级推广人不存在的问题
Showing
4 changed files
with
38 additions
and
22 deletions
yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/controller/app/brokerage/AppBrokerageUserController.java
| 1 | 1 | package cn.iocoder.yudao.module.trade.controller.app.brokerage; |
| 2 | 2 | |
| 3 | 3 | import cn.hutool.core.date.LocalDateTimeUtil; |
| 4 | -import cn.hutool.core.util.ObjUtil; | |
| 5 | 4 | import cn.iocoder.yudao.framework.common.pojo.CommonResult; |
| 6 | 5 | import cn.iocoder.yudao.framework.common.pojo.PageResult; |
| 7 | 6 | import cn.iocoder.yudao.framework.security.core.annotations.PreAuthenticated; |
| ... | ... | @@ -11,8 +10,6 @@ import cn.iocoder.yudao.module.trade.controller.app.brokerage.vo.user.*; |
| 11 | 10 | import cn.iocoder.yudao.module.trade.convert.brokerage.BrokerageRecordConvert; |
| 12 | 11 | import cn.iocoder.yudao.module.trade.convert.brokerage.BrokerageUserConvert; |
| 13 | 12 | import cn.iocoder.yudao.module.trade.dal.dataobject.brokerage.BrokerageUserDO; |
| 14 | -import cn.iocoder.yudao.module.trade.dal.dataobject.config.TradeConfigDO; | |
| 15 | -import cn.iocoder.yudao.module.trade.enums.brokerage.BrokerageEnabledConditionEnum; | |
| 16 | 13 | import cn.iocoder.yudao.module.trade.enums.brokerage.BrokerageRecordBizTypeEnum; |
| 17 | 14 | import cn.iocoder.yudao.module.trade.enums.brokerage.BrokerageRecordStatusEnum; |
| 18 | 15 | import cn.iocoder.yudao.module.trade.enums.brokerage.BrokerageWithdrawStatusEnum; |
| ... | ... | @@ -20,7 +17,6 @@ import cn.iocoder.yudao.module.trade.service.brokerage.BrokerageRecordService; |
| 20 | 17 | import cn.iocoder.yudao.module.trade.service.brokerage.BrokerageUserService; |
| 21 | 18 | import cn.iocoder.yudao.module.trade.service.brokerage.BrokerageWithdrawService; |
| 22 | 19 | import cn.iocoder.yudao.module.trade.service.brokerage.bo.BrokerageWithdrawSummaryRespBO; |
| 23 | -import cn.iocoder.yudao.module.trade.service.config.TradeConfigService; | |
| 24 | 20 | import io.swagger.v3.oas.annotations.Operation; |
| 25 | 21 | import io.swagger.v3.oas.annotations.Parameter; |
| 26 | 22 | import io.swagger.v3.oas.annotations.tags.Tag; |
| ... | ... | @@ -55,22 +51,16 @@ public class AppBrokerageUserController { |
| 55 | 51 | @Resource |
| 56 | 52 | private BrokerageWithdrawService brokerageWithdrawService; |
| 57 | 53 | @Resource |
| 58 | - private TradeConfigService tradeConfigService; | |
| 59 | - @Resource | |
| 60 | 54 | private MemberUserApi memberUserApi; |
| 61 | 55 | |
| 62 | 56 | @GetMapping("/get") |
| 63 | 57 | @Operation(summary = "获得个人分销信息") |
| 64 | 58 | @PreAuthenticated |
| 65 | 59 | public CommonResult<AppBrokerageUserRespVO> getBrokerageUser() { |
| 66 | - Optional<BrokerageUserDO> user = Optional.ofNullable(brokerageUserService.getBrokerageUser(getLoginUserId())); | |
| 67 | - // 获得交易中心配置 | |
| 68 | - TradeConfigDO tradeConfig = tradeConfigService.getTradeConfig(); | |
| 69 | - // 如果是人人分佣 BrokerageUserDO 为 null 时,也有分销资格 | |
| 70 | - boolean brokerageEnabled = ObjUtil.equal(BrokerageEnabledConditionEnum.ALL.getCondition(), tradeConfig.getBrokerageEnabledCondition()); | |
| 60 | + Optional<BrokerageUserDO> user = Optional.ofNullable(brokerageUserService.getOrCreateBrokerageUser(getLoginUserId())); | |
| 71 | 61 | // 返回数据 |
| 72 | 62 | AppBrokerageUserRespVO respVO = new AppBrokerageUserRespVO() |
| 73 | - .setBrokerageEnabled(user.map(BrokerageUserDO::getBrokerageEnabled).orElse(brokerageEnabled)) | |
| 63 | + .setBrokerageEnabled(user.map(BrokerageUserDO::getBrokerageEnabled).orElse(false)) | |
| 74 | 64 | .setBrokeragePrice(user.map(BrokerageUserDO::getBrokeragePrice).orElse(0)) |
| 75 | 65 | .setFrozenPrice(user.map(BrokerageUserDO::getFrozenPrice).orElse(0)); |
| 76 | 66 | return success(respVO); | ... | ... |
yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/controller/app/brokerage/vo/user/AppBrokerageUserBindReqVO.java
| ... | ... | @@ -8,7 +8,7 @@ import jakarta.validation.constraints.NotNull; |
| 8 | 8 | |
| 9 | 9 | @Schema(description = "应用 App - 绑定推广员 Request VO") |
| 10 | 10 | @Data |
| 11 | -public class AppBrokerageUserBindReqVO extends PageParam { | |
| 11 | +public class AppBrokerageUserBindReqVO { | |
| 12 | 12 | |
| 13 | 13 | @Schema(description = "推广员编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "1024") |
| 14 | 14 | @NotNull(message = "推广员编号不能为空") | ... | ... |
yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/service/brokerage/BrokerageUserService.java
| ... | ... | @@ -7,8 +7,8 @@ import cn.iocoder.yudao.module.trade.controller.app.brokerage.vo.user.AppBrokera |
| 7 | 7 | import cn.iocoder.yudao.module.trade.controller.app.brokerage.vo.user.AppBrokerageUserRankByUserCountRespVO; |
| 8 | 8 | import cn.iocoder.yudao.module.trade.controller.app.brokerage.vo.user.AppBrokerageUserRankPageReqVO; |
| 9 | 9 | import cn.iocoder.yudao.module.trade.dal.dataobject.brokerage.BrokerageUserDO; |
| 10 | - | |
| 11 | 10 | import jakarta.validation.constraints.NotNull; |
| 11 | + | |
| 12 | 12 | import java.util.Collection; |
| 13 | 13 | import java.util.List; |
| 14 | 14 | |
| ... | ... | @@ -68,6 +68,14 @@ public interface BrokerageUserService { |
| 68 | 68 | BrokerageUserDO getBindBrokerageUser(Long id); |
| 69 | 69 | |
| 70 | 70 | /** |
| 71 | + * 获得或创建分销用户 | |
| 72 | + * | |
| 73 | + * @param id 用户编号 | |
| 74 | + * @return 分销用户 | |
| 75 | + */ | |
| 76 | + BrokerageUserDO getOrCreateBrokerageUser(Long id); | |
| 77 | + | |
| 78 | + /** | |
| 71 | 79 | * 更新用户佣金 |
| 72 | 80 | * |
| 73 | 81 | * @param id 用户编号 |
| ... | ... | @@ -104,8 +112,8 @@ public interface BrokerageUserService { |
| 104 | 112 | /** |
| 105 | 113 | * 【会员】绑定推广员 |
| 106 | 114 | * |
| 107 | - * @param userId 用户编号 | |
| 108 | - * @param bindUserId 推广员编号 | |
| 115 | + * @param userId 用户编号 | |
| 116 | + * @param bindUserId 推广员编号 | |
| 109 | 117 | * @return 是否绑定 |
| 110 | 118 | */ |
| 111 | 119 | boolean bindBrokerageUser(@NotNull Long userId, @NotNull Long bindUserId); |
| ... | ... | @@ -134,4 +142,5 @@ public interface BrokerageUserService { |
| 134 | 142 | * @return 下级分销统计分页 |
| 135 | 143 | */ |
| 136 | 144 | PageResult<AppBrokerageUserChildSummaryRespVO> getBrokerageUserChildSummaryPage(AppBrokerageUserChildSummaryPageReqVO pageReqVO, Long userId); |
| 145 | + | |
| 137 | 146 | } | ... | ... |
yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/service/brokerage/BrokerageUserServiceImpl.java
| ... | ... | @@ -4,6 +4,7 @@ import cn.hutool.core.collection.CollUtil; |
| 4 | 4 | import cn.hutool.core.lang.Assert; |
| 5 | 5 | import cn.hutool.core.util.ArrayUtil; |
| 6 | 6 | import cn.hutool.core.util.BooleanUtil; |
| 7 | +import cn.hutool.core.util.ObjUtil; | |
| 7 | 8 | import cn.hutool.core.util.StrUtil; |
| 8 | 9 | import cn.iocoder.yudao.framework.common.pojo.PageResult; |
| 9 | 10 | import cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils; |
| ... | ... | @@ -25,10 +26,10 @@ import cn.iocoder.yudao.module.trade.enums.brokerage.BrokerageRecordBizTypeEnum; |
| 25 | 26 | import cn.iocoder.yudao.module.trade.enums.brokerage.BrokerageRecordStatusEnum; |
| 26 | 27 | import cn.iocoder.yudao.module.trade.service.config.TradeConfigService; |
| 27 | 28 | import com.baomidou.mybatisplus.core.metadata.IPage; |
| 29 | +import jakarta.annotation.Resource; | |
| 28 | 30 | import org.springframework.stereotype.Service; |
| 29 | 31 | import org.springframework.validation.annotation.Validated; |
| 30 | 32 | |
| 31 | -import jakarta.annotation.Resource; | |
| 32 | 33 | import java.time.LocalDateTime; |
| 33 | 34 | import java.util.*; |
| 34 | 35 | |
| ... | ... | @@ -128,6 +129,18 @@ public class BrokerageUserServiceImpl implements BrokerageUserService { |
| 128 | 129 | } |
| 129 | 130 | |
| 130 | 131 | @Override |
| 132 | + public BrokerageUserDO getOrCreateBrokerageUser(Long id) { | |
| 133 | + BrokerageUserDO brokerageUser = brokerageUserMapper.selectById(id); | |
| 134 | + if (brokerageUser == null && ObjUtil.equal(BrokerageEnabledConditionEnum.ALL.getCondition(), | |
| 135 | + tradeConfigService.getTradeConfig().getBrokerageEnabledCondition())) { // 人人分销的情况下,如果分销人为空则创建分销人 | |
| 136 | + brokerageUser = new BrokerageUserDO().setId(id).setBrokerageEnabled(true).setBrokeragePrice(0) | |
| 137 | + .setBrokerageTime(LocalDateTime.now()).setFrozenPrice(0); | |
| 138 | + brokerageUserMapper.insert(brokerageUser); | |
| 139 | + } | |
| 140 | + return brokerageUser; | |
| 141 | + } | |
| 142 | + | |
| 143 | + @Override | |
| 131 | 144 | public boolean updateUserPrice(Long id, Integer price) { |
| 132 | 145 | if (price > 0) { |
| 133 | 146 | brokerageUserMapper.updatePriceIncr(id, price); |
| ... | ... | @@ -184,7 +197,6 @@ public class BrokerageUserServiceImpl implements BrokerageUserService { |
| 184 | 197 | if (BrokerageEnabledConditionEnum.ALL.getCondition().equals(enabledCondition)) { // 人人分销:用户默认就有分销资格 |
| 185 | 198 | brokerageUser.setBrokerageEnabled(true).setBrokerageTime(LocalDateTime.now()); |
| 186 | 199 | } |
| 187 | - brokerageUser.setBindUserId(bindUserId).setBindUserTime(LocalDateTime.now()); | |
| 188 | 200 | brokerageUserMapper.insert(fillBindUserData(bindUserId, brokerageUser)); |
| 189 | 201 | } else { |
| 190 | 202 | brokerageUserMapper.updateById(fillBindUserData(bindUserId, new BrokerageUserDO().setId(userId))); |
| ... | ... | @@ -294,18 +306,23 @@ public class BrokerageUserServiceImpl implements BrokerageUserService { |
| 294 | 306 | } |
| 295 | 307 | |
| 296 | 308 | private void validateCanBindUser(BrokerageUserDO user, Long bindUserId) { |
| 297 | - // 校验要绑定的用户有无推广资格 | |
| 298 | - BrokerageUserDO bindUser = brokerageUserMapper.selectById(bindUserId); | |
| 309 | + // 1.1 校验推广人是否存在 | |
| 310 | + MemberUserRespDTO bindUserInfo = memberUserApi.getUser(bindUserId); | |
| 311 | + if (bindUserInfo == null) { | |
| 312 | + throw exception(BROKERAGE_USER_NOT_EXISTS); | |
| 313 | + } | |
| 314 | + // 1.2 校验要绑定的用户有无推广资格 | |
| 315 | + BrokerageUserDO bindUser = getOrCreateBrokerageUser(bindUserId); | |
| 299 | 316 | if (bindUser == null || BooleanUtil.isFalse(bindUser.getBrokerageEnabled())) { |
| 300 | 317 | throw exception(BROKERAGE_BIND_USER_NOT_ENABLED); |
| 301 | 318 | } |
| 302 | 319 | |
| 303 | - // 校验绑定自己 | |
| 320 | + // 2. 校验绑定自己 | |
| 304 | 321 | if (Objects.equals(user.getId(), bindUserId)) { |
| 305 | 322 | throw exception(BROKERAGE_BIND_SELF); |
| 306 | 323 | } |
| 307 | 324 | |
| 308 | - // 下级不能绑定自己的上级 | |
| 325 | + // 3. 下级不能绑定自己的上级 | |
| 309 | 326 | for (int i = 0; i <= Short.MAX_VALUE; i++) { |
| 310 | 327 | if (Objects.equals(bindUser.getBindUserId(), user.getId())) { |
| 311 | 328 | throw exception(BROKERAGE_BIND_LOOP); | ... | ... |