trade: 分销Review代码修改

This commit is contained in:
owen 2023-09-08 12:29:38 +08:00
parent 166b1b31c0
commit 1cc2dfc963
22 changed files with 155 additions and 170 deletions

View File

@ -3,8 +3,8 @@ create table trade_config
(
id bigint auto_increment comment '自增主键' primary key,
brokerage_enabled bit default 1 not null comment '是否启用分佣',
brokerage_enabled_condition tinyint default 0 not null comment '分佣模式0-人人分销 1-指定分销',
brokerage_bind_mode tinyint default 0 not null comment '分销关系绑定模式: 0-没有推广人1-新用户',
brokerage_enabled_condition tinyint default 0 not null comment '分佣模式1-人人分销 2-指定分销',
brokerage_bind_mode tinyint default 0 not null comment '分销关系绑定模式: 1-没有推广人2-新用户, 3-扫码覆盖',
brokerage_post_urls varchar(2000) default '' null comment '分销海报图地址数组',
brokerage_first_percent int default 0 not null comment '一级返佣比例',
brokerage_second_percent int default 0 not null comment '二级返佣比例',
@ -104,14 +104,15 @@ create index idx_audit_status on trade_brokerage_withdraw (status) comment '状
insert into system_dict_type(type, name)
values ('brokerage_enabled_condition', '分佣模式');
insert into system_dict_data(dict_type, label, value, sort, remark)
values ('brokerage_enabled_condition', '人人分销', 0, 0, '所有用户都可以分销'),
('brokerage_enabled_condition', '指定分销', 1, 1, '仅可后台手动设置推广员');
values ('brokerage_enabled_condition', '人人分销', 1, 1, '所有用户都可以分销'),
('brokerage_enabled_condition', '指定分销', 2, 2, '仅可后台手动设置推广员');
insert into system_dict_type(type, name)
values ('brokerage_bind_mode', '分销关系绑定模式');
insert into system_dict_data(dict_type, label, value, sort, remark)
values ('brokerage_bind_mode', '没有推广人', 0, 0, '只要用户没有推广人随时都可以绑定推广关系'),
('brokerage_bind_mode', '新用户', 1, 1, '仅新用户注册时才能绑定推广关系');
values ('brokerage_bind_mode', '没有推广人', 1, 1, '只要用户没有推广人随时都可以绑定推广关系'),
('brokerage_bind_mode', '新用户', 2, 2, '仅新用户注册时才能绑定推广关系'),
('brokerage_bind_mode', '扫码覆盖', 3, 3, '如果用户已经有推广人推广人会被变更');
insert into system_dict_type(type, name)
values ('brokerage_withdraw_type', '佣金提现类型');
@ -124,8 +125,8 @@ values ('brokerage_withdraw_type', '钱包', 1, 1),
insert into system_dict_type(type, name)
values ('brokerage_record_biz_type', '佣金记录业务类型');
insert into system_dict_data(dict_type, label, value, sort)
values ('brokerage_record_biz_type', '订单返佣', 0, 0),
('brokerage_record_biz_type', '申请提现', 1, 1);
values ('brokerage_record_biz_type', '订单返佣', 1, 1),
('brokerage_record_biz_type', '申请提现', 2, 2);
insert into system_dict_type(type, name)
values ('brokerage_record_status', '佣金记录状态');

View File

@ -77,5 +77,6 @@ public interface ErrorCodeConstants {
// ========== 分销用户 模块 1011007000 ==========
ErrorCode BROKERAGE_USER_NOT_EXISTS = new ErrorCode(1011007000, "分销用户不存在");
ErrorCode USER_FROZEN_BROKERAGE_PRICE_NOT_ENOUGH = new ErrorCode(1011007001, "用户冻结佣金({})数量不足");
}

View File

@ -15,16 +15,18 @@ import java.util.Arrays;
@Getter
public enum BrokerageBindModeEnum implements IntArrayValuable {
// TODO @疯狂要不从 1 开始
/**
* 只要用户没有推广人随时都可以绑定分销关系
*/
ANYTIME(0, "没有推广人"),
ANYTIME(1, "没有推广人"),
/**
* 仅新用户注册时才能绑定推广关系
*/
REGISTER(1, "新用户"),
// TODO @疯狂要加个 2每次扫码都覆盖
REGISTER(2, "新用户"),
/**
* 每次扫码都覆盖
*/
OVERRIDE(3, "扫码覆盖"),
;
public static final int[] ARRAYS = Arrays.stream(values()).mapToInt(BrokerageBindModeEnum::getMode).toArray();

View File

@ -15,15 +15,14 @@ import java.util.Arrays;
@Getter
public enum BrokerageEnabledConditionEnum implements IntArrayValuable {
// TODO @疯狂这个也从 1 开始哇
/**
* 所有用户都可以分销
*/
ALL(0, "人人分销"),
ALL(1, "人人分销"),
/**
* 仅可后台手动设置推广员
*/
ADMIN(1, "指定分销"),
ADMIN(2, "指定分销"),
;
public static final int[] ARRAYS = Arrays.stream(values()).mapToInt(BrokerageEnabledConditionEnum::getCondition).toArray();

View File

@ -15,9 +15,8 @@ import java.util.Arrays;
@Getter
public enum BrokerageRecordBizTypeEnum implements IntArrayValuable {
// TODO @疯狂这个也从 1 开始哇
ORDER(0, "获得推广佣金", "获得推广佣金 {}", true),
WITHDRAW(1, "提现申请", "提现申请扣除佣金 {}", false),
ORDER(1, "获得推广佣金", "获得推广佣金 {}", true),
WITHDRAW(2, "提现申请", "提现申请扣除佣金 {}", false),
;
public static final int[] ARRAYS = Arrays.stream(values()).mapToInt(BrokerageRecordBizTypeEnum::getType).toArray();

View File

@ -10,7 +10,7 @@ import cn.iocoder.yudao.module.trade.dal.dataobject.brokerage.user.TradeBrokerag
import cn.iocoder.yudao.module.trade.enums.brokerage.BrokerageRecordBizTypeEnum;
import cn.iocoder.yudao.module.trade.enums.brokerage.BrokerageRecordStatusEnum;
import cn.iocoder.yudao.module.trade.service.brokerage.record.TradeBrokerageRecordService;
import cn.iocoder.yudao.module.trade.service.brokerage.record.bo.UserBrokerageSummaryBO;
import cn.iocoder.yudao.module.trade.service.brokerage.bo.UserBrokerageSummaryBO;
import cn.iocoder.yudao.module.trade.service.brokerage.user.TradeBrokerageUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;

View File

@ -54,7 +54,6 @@ public class TradeConfigBaseVO {
@PositiveOrZero(message = "用户提现最低金额不能是负数")
private Integer brokerageWithdrawMinPrice;
// TODO @疯狂要不要做成字典按道理都可以体现对哇感觉是全局的配置哈
@Schema(description = "提现银行", requiredMode = Schema.RequiredMode.REQUIRED, example = "[0, 1]")
@NotEmpty(message = "提现银行不能为空")
private List<Integer> brokerageBankNames;

View File

@ -1,5 +1,6 @@
package cn.iocoder.yudao.module.trade.convert.brokerage.record;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.trade.controller.admin.brokerage.record.vo.TradeBrokerageRecordRespVO;
@ -29,19 +30,22 @@ public interface TradeBrokerageRecordConvert {
PageResult<TradeBrokerageRecordRespVO> convertPage(PageResult<TradeBrokerageRecordDO> page);
default TradeBrokerageRecordDO convert(TradeBrokerageUserDO user, String bizId, int brokerageFrozenDays, int brokerage, LocalDateTime unfreezeTime) {
default TradeBrokerageRecordDO convert(TradeBrokerageUserDO user, BrokerageRecordBizTypeEnum bizType, String bizId,
Integer brokerageFrozenDays, int brokerage, LocalDateTime unfreezeTime,
String title) {
brokerageFrozenDays = ObjectUtil.defaultIfNull(brokerageFrozenDays, 0);
// 不冻结时佣金直接就是结算状态
Integer status = brokerageFrozenDays > 0
? BrokerageRecordStatusEnum.WAIT_SETTLEMENT.getStatus()
: BrokerageRecordStatusEnum.SETTLEMENT.getStatus();
return new TradeBrokerageRecordDO()
.setUserId(user.getId())
.setBizType(BrokerageRecordBizTypeEnum.ORDER.getType())
.setBizType(bizType.getType())
.setBizId(bizId)
.setPrice(brokerage)
.setTotalPrice(user.getBrokeragePrice())
.setTitle(BrokerageRecordBizTypeEnum.ORDER.getTitle()) // TODO @疯狂可能 title 不是很固化会存在类似沐晴成功购买XXX JVM 实战
.setDescription(StrUtil.format(BrokerageRecordBizTypeEnum.ORDER.getDescription(), String.valueOf(brokerage / 100.0)))
.setTitle(title)
.setDescription(StrUtil.format(bizType.getDescription(), String.valueOf(brokerage / 100.0)))
.setStatus(status)
.setFrozenDays(brokerageFrozenDays)
.setUnfreezeTime(unfreezeTime);

View File

@ -5,7 +5,7 @@ import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.member.api.user.dto.MemberUserRespDTO;
import cn.iocoder.yudao.module.trade.controller.admin.brokerage.user.vo.TradeBrokerageUserRespVO;
import cn.iocoder.yudao.module.trade.dal.dataobject.brokerage.user.TradeBrokerageUserDO;
import cn.iocoder.yudao.module.trade.service.brokerage.record.bo.UserBrokerageSummaryBO;
import cn.iocoder.yudao.module.trade.service.brokerage.bo.UserBrokerageSummaryBO;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;

View File

@ -7,7 +7,7 @@ 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.record.bo.BrokerageAddReqBO;
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;
@ -277,8 +277,8 @@ public interface TradeOrderConvert {
default BrokerageAddReqBO convert(TradeOrderItemDO item, ProductSkuRespDTO sku) {
return new BrokerageAddReqBO().setBizId(String.valueOf(item.getId()))
.setPayPrice(item.getPayPrice()).setCount(item.getCount())
.setSkuFirstBrokeragePrice(sku.getSubCommissionFirstPrice())
.setSkuSecondBrokeragePrice(sku.getSubCommissionSecondPrice());
.setBasePrice(item.getPayPrice() * item.getCount())
.setFirstBrokeragePrice(sku.getSubCommissionFirstPrice())
.setSecondBrokeragePrice(sku.getSubCommissionSecondPrice());
}
}

View File

@ -5,7 +5,7 @@ import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.trade.controller.admin.brokerage.record.vo.TradeBrokerageRecordPageReqVO;
import cn.iocoder.yudao.module.trade.dal.dataobject.brokerage.record.TradeBrokerageRecordDO;
import cn.iocoder.yudao.module.trade.service.brokerage.record.bo.UserBrokerageSummaryBO;
import cn.iocoder.yudao.module.trade.service.brokerage.bo.UserBrokerageSummaryBO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
@ -43,8 +43,7 @@ public interface TradeBrokerageRecordMapper extends BaseMapperX<TradeBrokerageRe
.eq(TradeBrokerageRecordDO::getStatus, status));
}
// TODO @疯狂userId???
default TradeBrokerageRecordDO selectByUserIdAndBizTypeAndBizId(Integer bizType, String bizId) {
default TradeBrokerageRecordDO selectByBizTypeAndBizId(Integer bizType, String bizId) {
return selectOne(TradeBrokerageRecordDO::getBizType, bizType,
TradeBrokerageRecordDO::getBizId, bizId);
}

View File

@ -31,7 +31,7 @@ public interface TradeBrokerageUserMapper extends BaseMapperX<TradeBrokerageUser
* @param id 用户编号
* @param incrCount 增加佣金正数
*/
default void updateBrokeragePriceIncr(Long id, int incrCount) {
default void updateBrokeragePriceIncr(Long id, Integer incrCount) {
Assert.isTrue(incrCount > 0);
LambdaUpdateWrapper<TradeBrokerageUserDO> lambdaUpdateWrapper = new LambdaUpdateWrapper<TradeBrokerageUserDO>()
.setSql(" brokerage_price = brokerage_price + " + incrCount)
@ -46,7 +46,7 @@ public interface TradeBrokerageUserMapper extends BaseMapperX<TradeBrokerageUser
* @param id 用户编号
* @param incrCount 增加佣金负数
*/
default void updateBrokeragePriceDecr(Long id, int incrCount) {
default void updateBrokeragePriceDecr(Long id, Integer incrCount) {
Assert.isTrue(incrCount < 0);
LambdaUpdateWrapper<TradeBrokerageUserDO> lambdaUpdateWrapper = new LambdaUpdateWrapper<TradeBrokerageUserDO>()
.setSql(" brokerage_price = brokerage_price + " + incrCount) // 负数所以使用 +
@ -60,7 +60,7 @@ public interface TradeBrokerageUserMapper extends BaseMapperX<TradeBrokerageUser
* @param id 用户编号
* @param incrCount 增加冻结佣金正数
*/
default void updateFrozenBrokeragePriceIncr(Long id, int incrCount) {
default void updateFrozenBrokeragePriceIncr(Long id, Integer incrCount) {
Assert.isTrue(incrCount > 0);
LambdaUpdateWrapper<TradeBrokerageUserDO> lambdaUpdateWrapper = new LambdaUpdateWrapper<TradeBrokerageUserDO>()
.setSql(" frozen_brokerage_price = frozen_brokerage_price + " + incrCount)
@ -75,7 +75,7 @@ public interface TradeBrokerageUserMapper extends BaseMapperX<TradeBrokerageUser
* @param id 用户编号
* @param incrCount 减少冻结佣金负数
*/
default void updateFrozenBrokeragePriceDecr(Long id, int incrCount) {
default void updateFrozenBrokeragePriceDecr(Long id, Integer incrCount) {
Assert.isTrue(incrCount < 0);
LambdaUpdateWrapper<TradeBrokerageUserDO> lambdaUpdateWrapper = new LambdaUpdateWrapper<TradeBrokerageUserDO>()
.setSql(" frozen_brokerage_price = frozen_brokerage_price + " + incrCount) // 负数所以使用 +
@ -90,7 +90,7 @@ public interface TradeBrokerageUserMapper extends BaseMapperX<TradeBrokerageUser
* @param incrCount 减少冻结佣金负数
* @return 更新条数
*/
default int updateFrozenBrokeragePriceDecrAndBrokeragePriceIncr(Long id, int incrCount) {
default int updateFrozenBrokeragePriceDecrAndBrokeragePriceIncr(Long id, Integer incrCount) {
Assert.isTrue(incrCount < 0);
LambdaUpdateWrapper<TradeBrokerageUserDO> lambdaUpdateWrapper = new LambdaUpdateWrapper<TradeBrokerageUserDO>()
.setSql(" frozen_brokerage_price = frozen_brokerage_price + " + incrCount + // 负数所以使用 +

View File

@ -0,0 +1,36 @@
package cn.iocoder.yudao.module.trade.service.brokerage.bo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* 佣金 增加 Request BO
*
* @author owen
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
public class BrokerageAddReqBO {
// TODO @疯狂bo 的话也可以考虑加下 @Validated 注解校验下参数防御性下哈虽然不一定用的到
/**
* 业务ID
*/
private String bizId;
/**
* 佣金基数
*/
private Integer basePrice;
/**
* 一级佣金固定
*/
private Integer firstBrokeragePrice;
/**
* 二级佣金固定
*/
private Integer secondBrokeragePrice;
}

View File

@ -1,4 +1,4 @@
package cn.iocoder.yudao.module.trade.service.brokerage.record.bo;
package cn.iocoder.yudao.module.trade.service.brokerage.bo;
import lombok.AllArgsConstructor;
import lombok.Data;

View File

@ -3,8 +3,9 @@ package cn.iocoder.yudao.module.trade.service.brokerage.record;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.trade.controller.admin.brokerage.record.vo.TradeBrokerageRecordPageReqVO;
import cn.iocoder.yudao.module.trade.dal.dataobject.brokerage.record.TradeBrokerageRecordDO;
import cn.iocoder.yudao.module.trade.service.brokerage.record.bo.BrokerageAddReqBO;
import cn.iocoder.yudao.module.trade.service.brokerage.record.bo.UserBrokerageSummaryBO;
import cn.iocoder.yudao.module.trade.enums.brokerage.BrokerageRecordBizTypeEnum;
import cn.iocoder.yudao.module.trade.service.brokerage.bo.BrokerageAddReqBO;
import cn.iocoder.yudao.module.trade.service.brokerage.bo.UserBrokerageSummaryBO;
import java.util.List;
@ -31,23 +32,23 @@ public interface TradeBrokerageRecordService {
*/
PageResult<TradeBrokerageRecordDO> getBrokerageRecordPage(TradeBrokerageRecordPageReqVO pageReqVO);
// TODO @疯狂是不是 bizType 得加下方便未来拓展哈
/**
* 增加佣金
*
* @param userId 会员编号
* @param list 请求参数列表
* @param userId 会员编号
* @param bizType 业务类型
* @param list 请求参数列表
*/
void addBrokerage(Long userId, List<BrokerageAddReqBO> list);
void addBrokerage(Long userId, BrokerageRecordBizTypeEnum bizType, List<BrokerageAddReqBO> list);
// TODO @疯狂是不是 bizType 得加下方便未来拓展哈
/**
* 取消佣金将佣金记录状态修改为已失效
*
* @param userId 会员编号
* @param bizId 业务编号
* @param userId 会员编号
* @param bizType 业务类型
* @param bizId 业务编号
*/
void cancelBrokerage(Long userId, String bizId);
void cancelBrokerage(Long userId, BrokerageRecordBizTypeEnum bizType, String bizId);
/**
* 解冻佣金将待结算的佣金记录状态修改为已结算

View File

@ -14,8 +14,8 @@ import cn.iocoder.yudao.module.trade.dal.dataobject.config.TradeConfigDO;
import cn.iocoder.yudao.module.trade.dal.mysql.brokerage.record.TradeBrokerageRecordMapper;
import cn.iocoder.yudao.module.trade.enums.brokerage.BrokerageRecordBizTypeEnum;
import cn.iocoder.yudao.module.trade.enums.brokerage.BrokerageRecordStatusEnum;
import cn.iocoder.yudao.module.trade.service.brokerage.record.bo.BrokerageAddReqBO;
import cn.iocoder.yudao.module.trade.service.brokerage.record.bo.UserBrokerageSummaryBO;
import cn.iocoder.yudao.module.trade.service.brokerage.bo.BrokerageAddReqBO;
import cn.iocoder.yudao.module.trade.service.brokerage.bo.UserBrokerageSummaryBO;
import cn.iocoder.yudao.module.trade.service.brokerage.user.TradeBrokerageUserService;
import cn.iocoder.yudao.module.trade.service.config.TradeConfigService;
import lombok.extern.slf4j.Slf4j;
@ -57,42 +57,40 @@ public class TradeBrokerageRecordServiceImpl implements TradeBrokerageRecordServ
return tradeBrokerageRecordMapper.selectPage(pageReqVO);
}
// TODO @疯狂buyerId 要不要统一改成 userId
@Override
@Transactional(rollbackFor = Exception.class)
public void addBrokerage(Long buyerId, List<BrokerageAddReqBO> list) {
public void addBrokerage(Long userId, BrokerageRecordBizTypeEnum bizType, List<BrokerageAddReqBO> list) {
TradeConfigDO memberConfig = tradeConfigService.getTradeConfig();
// 0 未启用分销功能
// TODO @疯狂BooleanUtil.isFalse()逻辑里尽量不做 !取反这样要多思考一层
if (memberConfig == null || !BooleanUtil.isTrue(memberConfig.getBrokerageEnabled())) {
log.warn("[addBrokerage][增加佣金失败brokerageEnabled 未配置,buyerId({})", buyerId);
log.warn("[addBrokerage][增加佣金失败brokerageEnabled 未配置,userId({})", userId);
return;
}
// 1.1 获得一级推广人
TradeBrokerageUserDO firstUser = tradeBrokerageUserService.getInviteBrokerageUser(buyerId);
TradeBrokerageUserDO firstUser = tradeBrokerageUserService.getBindBrokerageUser(userId);
if (firstUser == null || !BooleanUtil.isTrue(firstUser.getBrokerageEnabled())) {
return;
}
// 1.2 计算一级分佣 // TODO 疯狂类似 1.1 1.2 的空行可以去掉一般在代码里的空行是为了逻辑分块但是分块如果太多就会导致代码里都是空行哈
addBrokerage(firstUser, list, memberConfig.getBrokerageFrozenDays(), memberConfig.getBrokerageFirstPercent(), BrokerageAddReqBO::getSkuFirstBrokeragePrice);
// 1.2 计算一级分佣
addBrokerage(firstUser, list, memberConfig.getBrokerageFrozenDays(), memberConfig.getBrokerageFirstPercent(), BrokerageAddReqBO::getFirstBrokeragePrice, bizType);
// 2.1 获得二级推广员
// TODO @疯狂这里可以加个 firstUser.getBrokerageUserId() 为空的判断 return
if (firstUser.getBrokerageUserId() == null) {
return;
}
TradeBrokerageUserDO secondUser = tradeBrokerageUserService.getBrokerageUser(firstUser.getBrokerageUserId());
if (secondUser == null || !BooleanUtil.isTrue(secondUser.getBrokerageEnabled())) {
return;
}
// 2.2 计算二级分佣
addBrokerage(secondUser, list, memberConfig.getBrokerageFrozenDays(), memberConfig.getBrokerageSecondPercent(), BrokerageAddReqBO::getSkuSecondBrokeragePrice);
addBrokerage(secondUser, list, memberConfig.getBrokerageFrozenDays(), memberConfig.getBrokerageSecondPercent(), BrokerageAddReqBO::getSecondBrokeragePrice, bizType);
}
@Override
@Transactional(rollbackFor = Exception.class)
public void cancelBrokerage(Long userId, String bizId) {
TradeBrokerageRecordDO record = tradeBrokerageRecordMapper.selectByUserIdAndBizTypeAndBizId(BrokerageRecordBizTypeEnum.ORDER.getType(), bizId);
public void cancelBrokerage(Long userId, BrokerageRecordBizTypeEnum bizType, String bizId) {
TradeBrokerageRecordDO record = tradeBrokerageRecordMapper.selectByBizTypeAndBizId(bizType.getType(), bizId);
if (record == null || ObjectUtil.notEqual(record.getUserId(), userId)) {
log.error("[cancelBrokerage][userId({})][bizId({}) 更新为已失效失败:记录不存在]", userId, bizId);
return;
@ -114,24 +112,23 @@ public class TradeBrokerageRecordServiceImpl implements TradeBrokerageRecordServ
}
}
// TODO @疯狂是不是 calculateBrokeragePrice
/**
* 计算佣金
*
* @param payPrice 订单支付金额
* @param percent 商品 SKU 设置的佣金
* @param skuBrokeragePrice 商品的佣金
* @param basePrice 佣金基数
* @param percent 佣金比例
* @param fixedBrokeragePrice 固定佣金
* @return 佣金
*/
int calculateBrokerage(Integer payPrice, Integer percent, Integer skuBrokeragePrice) {
// 1. 优先使用商品 SKU 设置的佣金
if (skuBrokeragePrice != null && skuBrokeragePrice > 0) {
return ObjectUtil.defaultIfNull(skuBrokeragePrice, 0);
int calculateBrokeragePrice(Integer basePrice, Integer percent, Integer fixedBrokeragePrice) {
// 1. 优先使用固定佣金
if (fixedBrokeragePrice != null && fixedBrokeragePrice > 0) {
return ObjectUtil.defaultIfNull(fixedBrokeragePrice, 0);
}
// 2. 根据订单支付金额计算佣金
// 2. 根据比例计算佣金
// TODO @疯狂要不要把 MoneyUtils 抽到 common 然后这里也使用这个类的方法
if (payPrice != null && payPrice > 0 && percent != null && percent > 0) {
return NumberUtil.div(NumberUtil.mul(payPrice, percent), 100, 0, RoundingMode.DOWN).intValue();
if (basePrice != null && basePrice > 0 && percent != null && percent > 0) {
return NumberUtil.div(NumberUtil.mul(basePrice, percent), 100, 0, RoundingMode.DOWN).intValue();
}
return 0;
}
@ -139,32 +136,32 @@ public class TradeBrokerageRecordServiceImpl implements TradeBrokerageRecordServ
/**
* 增加用户佣金
*
* @param user 用户
* @param list 佣金增加参数列表
* @param brokerageFrozenDays 冻结天数
* @param brokeragePercent 佣金比例
* @param skuBrokeragePriceFun 商品 SKU 设置的佣金
* @param user 用户
* @param list 佣金增加参数列表
* @param brokerageFrozenDays 冻结天数
* @param brokeragePercent 佣金比例
* @param FixedBrokeragePriceFun 固定佣金
* @param bizType 业务类型
*/
private void addBrokerage(TradeBrokerageUserDO user, List<BrokerageAddReqBO> list, Integer brokerageFrozenDays,
Integer brokeragePercent, Function<BrokerageAddReqBO, Integer> skuBrokeragePriceFun) {
Integer brokeragePercent, Function<BrokerageAddReqBO, Integer> FixedBrokeragePriceFun,
BrokerageRecordBizTypeEnum bizType) {
// 1.1 处理冻结时间
// TODO @疯狂是不是 brokerageFrozenDays != null && brokerageFrozenDays > 0 然后计算更简洁一点
brokerageFrozenDays = ObjectUtil.defaultIfNull(brokerageFrozenDays, 0);
LocalDateTime unfreezeTime = null;
if (brokerageFrozenDays > 0) {
if (brokerageFrozenDays != null && brokerageFrozenDays > 0) {
unfreezeTime = LocalDateTime.now().plusDays(brokerageFrozenDays);
}
// 1.2 计算分佣
int totalBrokerage = 0;
List<TradeBrokerageRecordDO> records = new ArrayList<>();
for (BrokerageAddReqBO item : list) {
int brokeragePerItem = calculateBrokerage(item.getPayPrice(), brokeragePercent, skuBrokeragePriceFun.apply(item));
// TODO @疯狂其实可以 brokeragePerItem <= 0 continue这样 { 层级更少代码更简洁}
if (brokeragePerItem > 0) {
int brokerage = brokeragePerItem * item.getCount();
records.add(TradeBrokerageRecordConvert.INSTANCE.convert(user, item.getBizId(), brokerageFrozenDays, brokerage, unfreezeTime));
totalBrokerage += brokerage;
int brokeragePerItem = calculateBrokeragePrice(item.getBasePrice(), brokeragePercent, FixedBrokeragePriceFun.apply(item));
if (brokeragePerItem <= 0) {
continue;
}
records.add(TradeBrokerageRecordConvert.INSTANCE.convert(user, bizType, item.getBizId(),
brokerageFrozenDays, brokeragePerItem, unfreezeTime, bizType.getTitle()));
totalBrokerage += brokeragePerItem;
}
if (CollUtil.isEmpty(records)) {
return;
@ -173,7 +170,7 @@ public class TradeBrokerageRecordServiceImpl implements TradeBrokerageRecordServ
tradeBrokerageRecordMapper.insertBatch(records);
// 2. 更新用户佣金
if (brokerageFrozenDays > 0) { // 更新用户冻结佣金
if (brokerageFrozenDays != null && brokerageFrozenDays > 0) { // 更新用户冻结佣金
tradeBrokerageUserService.updateUserFrozenBrokeragePrice(user.getId(), totalBrokerage);
} else { // 更新用户可用佣金
tradeBrokerageUserService.updateUserBrokeragePrice(user.getId(), totalBrokerage);

View File

@ -1,43 +0,0 @@
package cn.iocoder.yudao.module.trade.service.brokerage.record.bo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
// TODO @疯狂要不要 service 还是拍平就是都放在 brokerage 包下然后 bo 里面稍微分分
/**
* 佣金 增加 Request BO
*
* @author owen
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
public class BrokerageAddReqBO {
// TODO @疯狂bo 的话也可以考虑加下 @Validated 注解校验下参数防御性下哈虽然不一定用的到
/**
* 业务ID
*/
private String bizId;
// TODO @疯狂不需要 payPrice count计算成 price 就好啦
/**
* 商品支付价格
*/
private Integer payPrice;
// TODO @疯狂可以去掉 sku 更抽象一点
/**
* SKU 一级佣金
*/
private Integer skuFirstBrokeragePrice;
/**
* SKU 二级佣金
*/
private Integer skuSecondBrokeragePrice;
/**
* 购买数量
*/
private Integer count;
}

View File

@ -61,7 +61,7 @@ public interface TradeBrokerageUserService {
* @param id 用户编号
* @return 用户的推广人
*/
TradeBrokerageUserDO getInviteBrokerageUser(Long id);
TradeBrokerageUserDO getBindBrokerageUser(Long id);
/**
* 更新用户佣金
@ -69,16 +69,15 @@ public interface TradeBrokerageUserService {
* @param id 用户编号
* @param brokeragePrice 用户可用佣金
*/
void updateUserBrokeragePrice(Long id, int brokeragePrice);
void updateUserBrokeragePrice(Long id, Integer brokeragePrice);
// TODO @疯狂int 类型一般不用哈尽量都用封装类型不差这点内存哈
/**
* 更新用户冻结佣金
*
* @param id 用户编号
* @param frozenBrokeragePrice 用户冻结佣金
*/
void updateUserFrozenBrokeragePrice(Long id, int frozenBrokeragePrice);
void updateUserFrozenBrokeragePrice(Long id, Integer frozenBrokeragePrice);
/**
* 更新用户冻结佣金减少, 更新用户佣金增加
@ -86,7 +85,7 @@ public interface TradeBrokerageUserService {
* @param id 用户编号
* @param frozenBrokeragePrice 减少冻结佣金负数
*/
void updateFrozenBrokeragePriceDecrAndBrokeragePriceIncr(Long id, int frozenBrokeragePrice);
void updateFrozenBrokeragePriceDecrAndBrokeragePriceIncr(Long id, Integer frozenBrokeragePrice);
/**
* 获得推广用户数量一级

View File

@ -6,7 +6,6 @@ import cn.iocoder.yudao.module.trade.controller.admin.brokerage.user.vo.TradeBro
import cn.iocoder.yudao.module.trade.dal.dataobject.brokerage.user.TradeBrokerageUserDO;
import cn.iocoder.yudao.module.trade.dal.mysql.brokerage.user.TradeBrokerageUserMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import javax.annotation.Resource;
@ -15,8 +14,8 @@ import java.util.List;
import java.util.Optional;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.member.enums.ErrorCodeConstants.MEMBER_FROZEN_BROKERAGE_PRICE_NOT_ENOUGH;
import static cn.iocoder.yudao.module.trade.enums.ErrorCodeConstants.BROKERAGE_USER_NOT_EXISTS;
import static cn.iocoder.yudao.module.trade.enums.ErrorCodeConstants.USER_FROZEN_BROKERAGE_PRICE_NOT_ENOUGH;
/**
* 分销用户 Service 实现类
@ -65,9 +64,8 @@ public class TradeBrokerageUserServiceImpl implements TradeBrokerageUserService
}
}
// TODO @疯狂getBindBrokerageUser 会不会好点因为统一使用 Bind 替代了 Invite
@Override
public TradeBrokerageUserDO getInviteBrokerageUser(Long id) {
public TradeBrokerageUserDO getBindBrokerageUser(Long id) {
return Optional.ofNullable(id)
.map(this::getBrokerageUser)
.map(TradeBrokerageUserDO::getBrokerageUserId)
@ -75,10 +73,8 @@ public class TradeBrokerageUserServiceImpl implements TradeBrokerageUserService
.orElse(null);
}
// TODO @疯狂单个更新不用事务哈
@Override
@Transactional(rollbackFor = Exception.class)
public void updateUserBrokeragePrice(Long id, int brokeragePrice) {
public void updateUserBrokeragePrice(Long id, Integer brokeragePrice) {
if (brokeragePrice > 0) {
brokerageUserMapper.updateBrokeragePriceIncr(id, brokeragePrice);
} else if (brokeragePrice < 0) {
@ -86,10 +82,8 @@ public class TradeBrokerageUserServiceImpl implements TradeBrokerageUserService
}
}
// TODO @疯狂单个更新不用事务哈
@Override
@Transactional(rollbackFor = Exception.class)
public void updateUserFrozenBrokeragePrice(Long id, int frozenBrokeragePrice) {
public void updateUserFrozenBrokeragePrice(Long id, Integer frozenBrokeragePrice) {
if (frozenBrokeragePrice > 0) {
brokerageUserMapper.updateFrozenBrokeragePriceIncr(id, frozenBrokeragePrice);
} else if (frozenBrokeragePrice < 0) {
@ -97,15 +91,12 @@ public class TradeBrokerageUserServiceImpl implements TradeBrokerageUserService
}
}
// TODO @疯狂单个更新不用事务哈
@Override
@Transactional(rollbackFor = Exception.class)
public void updateFrozenBrokeragePriceDecrAndBrokeragePriceIncr(Long id, int frozenBrokeragePrice) {
public void updateFrozenBrokeragePriceDecrAndBrokeragePriceIncr(Long id, Integer frozenBrokeragePrice) {
Assert.isTrue(frozenBrokeragePrice < 0);
int updateRows = brokerageUserMapper.updateFrozenBrokeragePriceDecrAndBrokeragePriceIncr(id, frozenBrokeragePrice);
if (updateRows == 0) {
// TODO @疯狂挪到 trade 这变的错误码哈
throw exception(MEMBER_FROZEN_BROKERAGE_PRICE_NOT_ENOUGH);
throw exception(USER_FROZEN_BROKERAGE_PRICE_NOT_ENOUGH);
}
}

View File

@ -12,8 +12,6 @@ import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
import cn.iocoder.yudao.framework.common.util.json.JsonUtils;
import cn.iocoder.yudao.module.member.api.address.AddressApi;
import cn.iocoder.yudao.module.member.api.address.dto.AddressRespDTO;
import cn.iocoder.yudao.module.trade.service.brokerage.record.TradeBrokerageRecordService;
import cn.iocoder.yudao.module.trade.service.brokerage.record.bo.BrokerageAddReqBO;
import cn.iocoder.yudao.module.member.api.level.MemberLevelApi;
import cn.iocoder.yudao.module.member.api.point.MemberPointApi;
import cn.iocoder.yudao.module.member.api.user.MemberUserApi;
@ -49,9 +47,12 @@ import cn.iocoder.yudao.module.trade.dal.dataobject.order.TradeOrderItemDO;
import cn.iocoder.yudao.module.trade.dal.mysql.order.TradeOrderItemMapper;
import cn.iocoder.yudao.module.trade.dal.mysql.order.TradeOrderMapper;
import cn.iocoder.yudao.module.trade.enums.ErrorCodeConstants;
import cn.iocoder.yudao.module.trade.enums.brokerage.BrokerageRecordBizTypeEnum;
import cn.iocoder.yudao.module.trade.enums.delivery.DeliveryTypeEnum;
import cn.iocoder.yudao.module.trade.enums.order.*;
import cn.iocoder.yudao.module.trade.framework.order.config.TradeOrderProperties;
import cn.iocoder.yudao.module.trade.service.brokerage.record.TradeBrokerageRecordService;
import cn.iocoder.yudao.module.trade.service.brokerage.bo.BrokerageAddReqBO;
import cn.iocoder.yudao.module.trade.service.cart.CartService;
import cn.iocoder.yudao.module.trade.service.delivery.DeliveryExpressService;
import cn.iocoder.yudao.module.trade.service.message.TradeMessageService;
@ -370,7 +371,7 @@ public class TradeOrderUpdateServiceImpl implements TradeOrderUpdateService {
// 增加用户经验
getSelf().addUserExperienceAsync(order.getUserId(), order.getPayPrice(), order.getId());
// 增加用户佣金
getSelf().addBrokerageAsync(order.getUserId(), order.getId());
getSelf().addBrokerageAsync(order.getUserId(), BrokerageRecordBizTypeEnum.ORDER, order.getId());
}
/**
@ -640,7 +641,7 @@ public class TradeOrderUpdateServiceImpl implements TradeOrderUpdateService {
// 扣减用户经验
getSelf().reduceUserExperienceAsync(order.getUserId(), orderRefundPrice, afterSaleId);
// 更新分佣记录为已失效
getSelf().cancelBrokerageAsync(order.getUserId(), id);
getSelf().cancelBrokerageAsync(order.getUserId(), BrokerageRecordBizTypeEnum.ORDER, id);
}
@Override
@ -750,16 +751,16 @@ public class TradeOrderUpdateServiceImpl implements TradeOrderUpdateService {
@Async
protected void addBrokerageAsync(Long userId, Long orderId) {
protected void addBrokerageAsync(Long userId, BrokerageRecordBizTypeEnum bizType, Long orderId) {
List<TradeOrderItemDO> orderItems = tradeOrderItemMapper.selectListByOrderId(orderId);
List<BrokerageAddReqBO> list = convertList(orderItems,
item -> TradeOrderConvert.INSTANCE.convert(item, productSkuApi.getSku(item.getSkuId())));
tradeBrokerageRecordService.addBrokerage(userId, list);
tradeBrokerageRecordService.addBrokerage(userId, bizType, list);
}
@Async
protected void cancelBrokerageAsync(Long userId, Long orderItemId) {
tradeBrokerageRecordService.cancelBrokerage(userId, String.valueOf(orderItemId));
protected void cancelBrokerageAsync(Long userId, BrokerageRecordBizTypeEnum bizType, Long orderItemId) {
tradeBrokerageRecordService.cancelBrokerage(userId, bizType, String.valueOf(orderItemId));
}
/**

View File

@ -79,38 +79,38 @@ public class TradeBrokerageRecordServiceImplTest extends BaseDbUnitTest {
}
@Test
public void testCalculateBrokerage_useSkuBrokeragePrice() {
public void testCalculateBrokeragePrice_useFixedBrokeragePrice() {
// mock 数据
Integer payPrice = randomInteger();
Integer percent = randomInt(1, 101);
Integer skuBrokeragePrice = randomInt();
Integer fixedBrokeragePrice = randomInt();
// 调用
int brokerage = tradeBrokerageRecordService.calculateBrokerage(payPrice, percent, skuBrokeragePrice);
int brokerage = tradeBrokerageRecordService.calculateBrokeragePrice(payPrice, percent, fixedBrokeragePrice);
// 断言
assertEquals(brokerage, skuBrokeragePrice);
assertEquals(brokerage, fixedBrokeragePrice);
}
@Test
public void testCalculateBrokerage_usePercent() {
public void testCalculateBrokeragePrice_usePercent() {
// mock 数据
Integer payPrice = randomInteger();
Integer percent = randomInt(1, 101);
Integer skuBrokeragePrice = randomEle(new Integer[]{0, null});
System.out.println("skuBrokeragePrice=" + skuBrokeragePrice);
// 调用
int brokerage = tradeBrokerageRecordService.calculateBrokerage(payPrice, percent, skuBrokeragePrice);
int brokerage = tradeBrokerageRecordService.calculateBrokeragePrice(payPrice, percent, skuBrokeragePrice);
// 断言
assertEquals(brokerage, NumberUtil.div(NumberUtil.mul(payPrice, percent), 100, 0, RoundingMode.DOWN).intValue());
}
@Test
public void testCalculateBrokerage_equalsZero() {
public void testCalculateBrokeragePrice_equalsZero() {
// mock 数据
Integer payPrice = null;
Integer percent = null;
Integer skuBrokeragePrice = null;
// 调用
int brokerage = tradeBrokerageRecordService.calculateBrokerage(payPrice, percent, skuBrokeragePrice);
int brokerage = tradeBrokerageRecordService.calculateBrokeragePrice(payPrice, percent, skuBrokeragePrice);
// 断言
assertEquals(brokerage, 0);
}

View File

@ -13,7 +13,6 @@ public interface ErrorCodeConstants {
ErrorCode USER_NOT_EXISTS = new ErrorCode(1004001000, "用户不存在");
ErrorCode USER_MOBILE_NOT_EXISTS = new ErrorCode(1004001001, "手机号未注册用户");
ErrorCode USER_MOBILE_USED = new ErrorCode(1004001002, "修改手机失败,该手机号({})已经被使用");
ErrorCode MEMBER_FROZEN_BROKERAGE_PRICE_NOT_ENOUGH = new ErrorCode(1004001003, "用户冻结佣金({})数量不足");
// ========== AUTH 模块 1004003000 ==========
ErrorCode AUTH_LOGIN_BAD_CREDENTIALS = new ErrorCode(1004003000, "登录失败,账号密码不正确");