trade: 分销业务review代码修改

This commit is contained in:
owen 2023-09-11 00:54:12 +08:00
parent cddeb37289
commit 77daa0085e
15 changed files with 103 additions and 34 deletions

View File

@ -21,6 +21,13 @@
<groupId>cn.iocoder.boot</groupId>
<artifactId>yudao-common</artifactId>
</dependency>
<!-- 参数校验 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
</project>

View File

@ -1,7 +1,11 @@
package cn.iocoder.yudao.module.trade.api.brokerage;
import cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils;
import cn.iocoder.yudao.module.trade.api.brokerage.dto.BrokerageUserDTO;
import javax.validation.constraints.NotNull;
import java.time.LocalDateTime;
/**
* 分销 API 接口
*
@ -17,6 +21,20 @@ public interface BrokerageApi {
*/
BrokerageUserDTO getBrokerageUser(Long userId);
/**
* 会员绑定推广员
*
* @param userId 用户编号
* @param bindUserId 推广员编号
* @param registerTime 用户注册时间
* @return 是否绑定
*/
default boolean bindUser(@NotNull Long userId, @NotNull Long bindUserId, @NotNull LocalDateTime registerTime) {
// 注册时间在30秒内的都算新用户
boolean isNewUser = LocalDateTimeUtils.afterNow(registerTime.minusSeconds(30));
return bindUser(userId, bindUserId, isNewUser);
}
/**
* 绑定推广员
*
@ -25,5 +43,5 @@ public interface BrokerageApi {
* @param isNewUser 是否为新用户
* @return 是否绑定
*/
boolean bindUser(Long userId, Long bindUserId, Boolean isNewUser);
boolean bindUser(@NotNull Long userId, @NotNull Long bindUserId, @NotNull Boolean isNewUser);
}

View File

@ -9,6 +9,7 @@ import cn.iocoder.yudao.module.trade.convert.brokerage.user.BrokerageUserConvert
import cn.iocoder.yudao.module.trade.dal.dataobject.brokerage.user.BrokerageUserDO;
import cn.iocoder.yudao.module.trade.enums.brokerage.BrokerageRecordBizTypeEnum;
import cn.iocoder.yudao.module.trade.enums.brokerage.BrokerageRecordStatusEnum;
import cn.iocoder.yudao.module.trade.enums.brokerage.BrokerageUserTypeEnum;
import cn.iocoder.yudao.module.trade.service.brokerage.bo.UserBrokerageSummaryBO;
import cn.iocoder.yudao.module.trade.service.brokerage.record.BrokerageRecordService;
import cn.iocoder.yudao.module.trade.service.brokerage.user.BrokerageUserService;
@ -95,7 +96,7 @@ public class BrokerageUserController {
// 合计推广用户数量
Map<Long, Long> brokerageUserCountMap = convertMap(userIds,
userId -> userId,
userId -> brokerageUserService.getBrokerageUserCountByBindUserId(userId));
userId -> brokerageUserService.getBrokerageUserCountByBindUserId(userId, BrokerageUserTypeEnum.ALL));
// todo 合计提现

View File

@ -28,7 +28,7 @@ public class BrokerageUserRespVO extends BrokerageUserBaseVO {
// ========== 推广信息 ==========
@Schema(description = "推广用户数量(一级)", example = "20019")
@Schema(description = "推广用户数量", example = "20019")
private Integer brokerageUserCount;
@Schema(description = "推广订单数量", example = "20019")
private Integer brokerageOrderCount;

View File

@ -6,6 +6,7 @@ import cn.iocoder.yudao.framework.security.core.annotations.PreAuthenticated;
import cn.iocoder.yudao.module.trade.controller.app.brokerage.vo.record.AppBrokerageProductPriceRespVO;
import cn.iocoder.yudao.module.trade.controller.app.brokerage.vo.record.AppBrokerageRecordPageReqVO;
import cn.iocoder.yudao.module.trade.controller.app.brokerage.vo.record.AppBrokerageRecordRespVO;
import cn.iocoder.yudao.module.trade.service.brokerage.user.BrokerageUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
@ -15,10 +16,12 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import javax.validation.Valid;
import java.time.LocalDateTime;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.web.core.util.WebFrameworkUtils.getLoginUserId;
import static java.util.Arrays.asList;
@Tag(name = "用户 APP - 分销用户")
@ -27,6 +30,8 @@ import static java.util.Arrays.asList;
@Validated
@Slf4j
public class AppBrokerageRecordController {
@Resource
private BrokerageUserService brokerageUserService;
// TODO 芋艿临时 mock =>
@GetMapping("/page")
@ -46,7 +51,7 @@ public class AppBrokerageRecordController {
@Operation(summary = "获得商品的分销金额")
public CommonResult<AppBrokerageProductPriceRespVO> getProductBrokeragePrice(@RequestParam("spuId") Long spuId) {
AppBrokerageProductPriceRespVO respVO = new AppBrokerageProductPriceRespVO();
respVO.setEnabled(true); // TODO @疯狂需要开启分销 + 人允许分销
respVO.setEnabled(brokerageUserService.getUserBrokerageEnabled(getLoginUserId()));
respVO.setBrokerageMinPrice(1);
respVO.setBrokerageMaxPrice(2);
return success(respVO);

View File

@ -19,7 +19,7 @@ public class AppBrokerageUserMySummaryRespVO {
@Schema(description = "冻结的佣金,单位:分", requiredMode = Schema.RequiredMode.REQUIRED, example = "234")
private Integer frozenPrice;
@Schema(description = "分销用户数量(一级)", requiredMode = Schema.RequiredMode.REQUIRED, example = "10")
@Schema(description = "分销用户数量", requiredMode = Schema.RequiredMode.REQUIRED, example = "10")
private Integer firstBrokerageUserCount;
@Schema(description = "分销用户数量(二级)", requiredMode = Schema.RequiredMode.REQUIRED, example = "10")

View File

@ -33,7 +33,6 @@ public interface BrokerageRecordConvert {
PageResult<BrokerageRecordRespVO> convertPage(PageResult<BrokerageRecordDO> page);
// TODO @疯狂可能 title 不是很固化会存在类似沐晴成功购买XXX JVM 实战
default BrokerageRecordDO convert(BrokerageUserDO user, BrokerageRecordBizTypeEnum bizType, String bizId,
Integer brokerageFrozenDays, int brokeragePrice, LocalDateTime unfreezeTime,
String title, Long sourceUserId, Integer sourceUserType) {

View File

@ -39,7 +39,7 @@ public interface BrokerageUserConvert {
// 用户信息
copyTo(userMap.get(vo.getId()), vo);
// 推广用户数量一级
// 推广用户数量
vo.setBrokerageUserCount(MapUtil.getInt(brokerageUserCountMap, vo.getId(), 0));
// 推广订单数量推广订单金额
Optional<UserBrokerageSummaryBO> orderSummaryOptional = Optional.ofNullable(userOrderSummaryMap.get(vo.getId()));

View File

@ -7,7 +7,6 @@ import cn.iocoder.yudao.framework.common.util.string.StrUtils;
import cn.iocoder.yudao.framework.dict.core.util.DictFrameworkUtils;
import cn.iocoder.yudao.framework.ip.core.utils.AreaUtils;
import cn.iocoder.yudao.module.member.api.address.dto.AddressRespDTO;
import cn.iocoder.yudao.module.trade.service.brokerage.bo.BrokerageAddReqBO;
import cn.iocoder.yudao.module.member.api.user.dto.MemberUserRespDTO;
import cn.iocoder.yudao.module.pay.api.order.dto.PayOrderCreateReqDTO;
import cn.iocoder.yudao.module.pay.enums.DictTypeConstants;
@ -28,9 +27,11 @@ import cn.iocoder.yudao.module.trade.dal.dataobject.cart.CartDO;
import cn.iocoder.yudao.module.trade.dal.dataobject.delivery.DeliveryExpressDO;
import cn.iocoder.yudao.module.trade.dal.dataobject.order.TradeOrderDO;
import cn.iocoder.yudao.module.trade.dal.dataobject.order.TradeOrderItemDO;
import cn.iocoder.yudao.module.trade.enums.brokerage.BrokerageRecordBizTypeEnum;
import cn.iocoder.yudao.module.trade.enums.order.TradeOrderItemAfterSaleStatusEnum;
import cn.iocoder.yudao.module.trade.framework.delivery.core.client.dto.ExpressTrackRespDTO;
import cn.iocoder.yudao.module.trade.framework.order.config.TradeOrderProperties;
import cn.iocoder.yudao.module.trade.service.brokerage.bo.BrokerageAddReqBO;
import cn.iocoder.yudao.module.trade.service.price.bo.TradePriceCalculateReqBO;
import cn.iocoder.yudao.module.trade.service.price.bo.TradePriceCalculateRespBO;
import org.mapstruct.Mapper;
@ -282,6 +283,7 @@ public interface TradeOrderConvert {
.setSourceUserId(item.getUserId())
.setBasePrice(item.getPayPrice() * item.getCount())
.setFirstFixedPrice(sku.getSubCommissionFirstPrice())
.setSecondFixedPrice(sku.getSubCommissionSecondPrice());
.setSecondFixedPrice(sku.getSubCommissionSecondPrice())
.setTitle(BrokerageRecordBizTypeEnum.ORDER.getTitle());
}
}

View File

@ -48,13 +48,13 @@ public interface BrokerageRecordMapper extends BaseMapperX<BrokerageRecordDO> {
.eq(BrokerageRecordDO::getStatus, status));
}
default BrokerageRecordDO selectByBizTypeAndBizId(Integer bizType, String bizId) {
default BrokerageRecordDO selectByBizTypeAndBizIdAndUserId(Integer bizType, String bizId, Long userId) {
return selectOne(BrokerageRecordDO::getBizType, bizType,
BrokerageRecordDO::getBizId, bizId);
BrokerageRecordDO::getBizId, bizId,
BrokerageRecordDO::getUserId, userId);
}
// TODO @疯狂mysql 关键字大写哈这样看起来清晰点例如说 SELECT COUNT(1)
@Select("select count(1), sum(price) from trade_brokerage_record where user_id = #{userId} and biz_type = #{bizType} and status = #{status}")
@Select("SELECT COUNT(1), SUM(price) FROM trade_brokerage_record WHERE user_id = #{userId} AND biz_type = #{bizType} AND status = #{status}")
UserBrokerageSummaryBO selectCountAndSumPriceByUserIdAndBizTypeAndStatus(@Param("userId") Long userId,
@Param("bizType") Integer bizType,
@Param("status") Integer status);

View File

@ -11,6 +11,7 @@ import cn.iocoder.yudao.module.trade.enums.brokerage.BrokerageUserTypeEnum;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
/**
* 分销用户 Mapper
@ -135,4 +136,10 @@ public interface BrokerageUserMapper extends BaseMapperX<BrokerageUserDO> {
.set(BrokerageUserDO::getBrokerageEnabled, false).set(BrokerageUserDO::getBrokerageTime, null));
}
default Long selectCountByBindUserId(Long bindUserId) {
return selectCount(BrokerageUserDO::getBindUserId, bindUserId);
}
@Select("SELECT COUNT(1) from trade_brokerage_user WHERE bind_user_id IN (SELECT id FROM trade_brokerage_user WHERE bind_user_id = #{bindUserId})")
Long selectCountByBindUserIdInBindUserId(Long bindUserId);
}

View File

@ -40,4 +40,9 @@ public class BrokerageAddReqBO {
* 来源用户编号
*/
private Long sourceUserId;
/**
* 佣金记录标题
*/
private String title;
}

View File

@ -91,9 +91,8 @@ public class BrokerageRecordServiceImpl implements BrokerageRecordService {
@Override
@Transactional(rollbackFor = Exception.class)
public void cancelBrokerage(Long userId, BrokerageRecordBizTypeEnum bizType, String bizId) {
// TODO @疯狂userId 加进去查询会不会更好一点万一穿错参数
BrokerageRecordDO record = brokerageRecordMapper.selectByBizTypeAndBizId(bizType.getType(), bizId);
if (record == null || ObjectUtil.notEqual(record.getUserId(), userId)) {
BrokerageRecordDO record = brokerageRecordMapper.selectByBizTypeAndBizIdAndUserId(bizType.getType(), bizId, userId);
if (record == null) {
log.error("[cancelBrokerage][userId({})][bizId({}) 更新为已失效失败:记录不存在]", userId, bizId);
return;
}
@ -167,7 +166,7 @@ public class BrokerageRecordServiceImpl implements BrokerageRecordService {
continue;
}
records.add(BrokerageRecordConvert.INSTANCE.convert(user, bizType, item.getBizId(),
brokerageFrozenDays, brokeragePerItem, unfreezeTime, bizType.getTitle(),
brokerageFrozenDays, brokeragePerItem, unfreezeTime, item.getTitle(),
item.getSourceUserId(), sourceUserType.getType()));
totalBrokerage += brokeragePerItem;
}

View File

@ -3,6 +3,7 @@ package cn.iocoder.yudao.module.trade.service.brokerage.user;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.trade.controller.admin.brokerage.user.vo.BrokerageUserPageReqVO;
import cn.iocoder.yudao.module.trade.dal.dataobject.brokerage.user.BrokerageUserDO;
import cn.iocoder.yudao.module.trade.enums.brokerage.BrokerageUserTypeEnum;
import java.util.Collection;
import java.util.List;
@ -86,14 +87,14 @@ public interface BrokerageUserService {
*/
void updateFrozenPriceDecrAndPriceIncr(Long id, Integer frozenPrice);
// TODO @疯狂这个后面可能要支持下二级
/**
* 获得推广用户数量一级
* 获得推广用户数量
*
* @param bindUserId 绑定的推广员编号
* @param userType 用户类型
* @return 推广用户数量
*/
Long getBrokerageUserCountByBindUserId(Long bindUserId);
Long getBrokerageUserCountByBindUserId(Long bindUserId, BrokerageUserTypeEnum userType);
/**
* 会员绑定推广员
@ -105,4 +106,11 @@ public interface BrokerageUserService {
*/
boolean bindBrokerageUser(Long userId, Long bindUserId, Boolean isNewUser);
/**
* 获取用户是否有分销资格
*
* @param userId 用户编号
* @return 是否有分销资格
*/
Boolean getUserBrokerageEnabled(Long userId);
}

View File

@ -9,6 +9,7 @@ import cn.iocoder.yudao.module.trade.dal.dataobject.config.TradeConfigDO;
import cn.iocoder.yudao.module.trade.dal.mysql.brokerage.user.BrokerageUserMapper;
import cn.iocoder.yudao.module.trade.enums.brokerage.BrokerageBindModeEnum;
import cn.iocoder.yudao.module.trade.enums.brokerage.BrokerageEnabledConditionEnum;
import cn.iocoder.yudao.module.trade.enums.brokerage.BrokerageUserTypeEnum;
import cn.iocoder.yudao.module.trade.service.config.TradeConfigService;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
@ -131,19 +132,23 @@ public class BrokerageUserServiceImpl implements BrokerageUserService {
}
@Override
public Long getBrokerageUserCountByBindUserId(Long bindUserId) {
// TODO @疯狂mapper 封装下哈不直接在 service 调用这种基础 mapper 的基础方法
return brokerageUserMapper.selectCount(BrokerageUserDO::getBindUserId, bindUserId);
public Long getBrokerageUserCountByBindUserId(Long bindUserId, BrokerageUserTypeEnum userType) {
switch (userType) {
case ALL:
Long firstCount = brokerageUserMapper.selectCountByBindUserId(bindUserId);
Long secondCount = brokerageUserMapper.selectCountByBindUserIdInBindUserId(bindUserId);
return firstCount + secondCount;
case FIRST:
return brokerageUserMapper.selectCountByBindUserId(bindUserId);
case SECOND:
return brokerageUserMapper.selectCountByBindUserIdInBindUserId(bindUserId);
default:
return 0L;
}
}
// TODO @疯狂因为现在 user 会存在使用验证码直接注册所以 isNewUser 不太好传递我们是不是可以约定绑定的时间createTime 30 秒内就认为新用户
@Override
public boolean bindBrokerageUser(Long userId, Long bindUserId, Boolean isNewUser) {
// TODO @疯狂userId 为空搞到参数校验里哇
if (userId == null) {
throw exception(0);
}
// 1. 获得分销用户
boolean isNewBrokerageUser = false;
BrokerageUserDO brokerageUser = brokerageUserMapper.selectById(userId);
@ -153,7 +158,7 @@ public class BrokerageUserServiceImpl implements BrokerageUserService {
}
// 2.1 校验是否能绑定用户
boolean validated = isUserCanBind(brokerageUser, bindUserId, isNewUser);
boolean validated = isUserCanBind(brokerageUser, isNewUser);
if (!validated) {
return false;
}
@ -163,10 +168,9 @@ public class BrokerageUserServiceImpl implements BrokerageUserService {
if (isNewBrokerageUser) {
Integer enabledCondition = tradeConfigService.getTradeConfig().getBrokerageEnabledCondition();
if (BrokerageEnabledConditionEnum.ALL.getCondition().equals(enabledCondition)) { // 人人分销用户默认就有分销资格
// TODO @疯狂应该设置下 brokerageTime而不是 bindUserTime
brokerageUser.setBrokerageEnabled(true).setBindUserTime(LocalDateTime.now());
brokerageUser.setBrokerageEnabled(true).setBrokerageTime(LocalDateTime.now());
}
// TODO @疯狂这里是不是要设置 bindUserIdbindUserTime 字段哈
brokerageUser.setBindUserId(bindUserId).setBindUserTime(LocalDateTime.now());
brokerageUserMapper.insert(brokerageUser);
} else {
brokerageUserMapper.updateById(new BrokerageUserDO().setId(userId)
@ -175,7 +179,21 @@ public class BrokerageUserServiceImpl implements BrokerageUserService {
return true;
}
private boolean isUserCanBind(BrokerageUserDO user, Long bindUserId, Boolean isNewUser) {
@Override
public Boolean getUserBrokerageEnabled(Long userId) {
// 全局分销功能是否开启
TradeConfigDO tradeConfig = tradeConfigService.getTradeConfig();
if (tradeConfig == null || !BooleanUtil.isTrue(tradeConfig.getBrokerageEnabled())) {
return false;
}
// 用户是否有分销资格
return Optional.ofNullable(getBrokerageUser(userId))
.map(BrokerageUserDO::getBrokerageEnabled)
.orElse(false);
}
private boolean isUserCanBind(BrokerageUserDO user, Boolean isNewUser) {
// 校验分销功能是否启用
TradeConfigDO tradeConfig = tradeConfigService.getTradeConfig();
if (tradeConfig == null || !BooleanUtil.isTrue(tradeConfig.getBrokerageEnabled())) {