!622 trade: 分销业务review代码修改

Merge pull request !622 from 疯狂的世界/brokerate
This commit is contained in:
芋道源码 2023-09-19 13:48:07 +00:00 committed by Gitee
commit 581f9d4101
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
22 changed files with 214 additions and 347 deletions

View File

@ -12,14 +12,20 @@ create table trade_config
brokerage_bank_names varchar(200) default '' not null comment '提现银行字典类型=brokerage_bank_name',
brokerage_frozen_days int default 7 not null comment '佣金冻结时间()',
brokerage_withdraw_type varchar(32) default '1,2,3,4' not null comment '提现方式1-钱包2-银行卡3-微信4-支付宝',
creator varchar(64) collate utf8mb4_unicode_ci default '' null comment '创建者',
creator varchar(64) default '' null comment '创建者',
create_time datetime default CURRENT_TIMESTAMP not null comment '创建时间',
updater varchar(64) collate utf8mb4_unicode_ci default '' null comment '更新者',
updater varchar(64) default '' null comment '更新者',
update_time datetime default CURRENT_TIMESTAMP not null on update CURRENT_TIMESTAMP comment '更新时间',
deleted bit default b'0' not null comment '是否删除',
tenant_id bigint default 0 not null comment '租户编号'
) comment '交易中心配置';
# alter table trade_brokerage_user
# add level int not null default 1 comment '等级' after frozen_price;
# alter table trade_brokerage_user
# add path varchar(2000) null comment '路径' after level;
-- 增加分销用户扩展表
create table trade_brokerage_user
(
@ -30,13 +36,16 @@ create table trade_brokerage_user
brokerage_time datetime null comment '成为分销员时间',
price int default 0 not null comment '可用佣金',
frozen_price int default 0 not null comment '冻结佣金',
creator varchar(64) collate utf8mb4_unicode_ci default '' null comment '创建者',
level int default 1 not null comment '等级',
path varchar(2000) null comment '路径',
creator varchar(64) default '' null comment '创建者',
create_time datetime default CURRENT_TIMESTAMP not null comment '创建时间',
updater varchar(64) collate utf8mb4_unicode_ci default '' null comment '更新者',
updater varchar(64) default '' null comment '更新者',
update_time datetime default CURRENT_TIMESTAMP not null on update CURRENT_TIMESTAMP comment '更新时间',
deleted bit default b'0' not null comment '是否删除',
tenant_id bigint default 0 not null comment '租户编号'
) comment '分销用户';
)
comment '分销用户';
create index idx_invite_user_id on trade_brokerage_user (bind_user_id) comment '推广员编号';
create index idx_agent on trade_brokerage_user (brokerage_enabled) comment '是否成为推广员';
@ -56,11 +65,11 @@ create table trade_brokerage_record
status tinyint default 0 not null comment '状态0-待结算1-已结算2-已取消',
frozen_days int default 0 not null comment '冻结时间',
unfreeze_time datetime null comment '解冻时间',
source_user_type tinyint not null comment '来源用户类型1-一级推广用户2-二级推广用户',
source_user_level int not null comment '来源用户等级',
source_user_id bigint not null comment '来源用户编号',
creator varchar(64) collate utf8mb4_general_ci default '' null comment '创建者',
creator varchar(64) default '' null comment '创建者',
create_time datetime default CURRENT_TIMESTAMP not null comment '创建时间',
updater varchar(64) collate utf8mb4_general_ci default '' null comment '更新者',
updater varchar(64) default '' null comment '更新者',
update_time datetime default CURRENT_TIMESTAMP not null on update CURRENT_TIMESTAMP comment '更新时间',
deleted bit default b'0' not null comment '是否删除',
tenant_id bigint default 0 not null comment '租户编号'
@ -90,9 +99,9 @@ create table trade_brokerage_withdraw
audit_reason varchar(128) null comment '审核驳回原因',
audit_time datetime null comment '审核时间',
remark varchar(500) null comment '备注',
creator varchar(64) collate utf8mb4_general_ci default '' null comment '创建者',
creator varchar(64) default '' null comment '创建者',
create_time datetime default CURRENT_TIMESTAMP not null comment '创建时间',
updater varchar(64) collate utf8mb4_general_ci default '' null comment '更新者',
updater varchar(64) default '' null comment '更新者',
update_time datetime default CURRENT_TIMESTAMP not null on update CURRENT_TIMESTAMP comment '更新时间',
deleted bit default b'0' not null comment '是否删除',
tenant_id bigint default 0 not null comment '租户编号'

View File

@ -100,6 +100,13 @@ public class LambdaQueryWrapperX<T> extends LambdaQueryWrapper<T> {
return betweenIfPresent(column, val1, val2);
}
public LambdaQueryWrapperX<T> findInSetIfPresent(SFunction<T, ?> column, Object val) {
if (val != null) {
return (LambdaQueryWrapperX<T>) super.apply("FIND_IN_SET({0}, " + columnToString(column) + ")", val);
}
return this;
}
// ========== 重写父类方法方便链式调用 ==========
@Override

View File

@ -64,14 +64,14 @@ public class ${table.className}ServiceImpl implements ${table.className}Service
@Override
public ${table.className}DO get${simpleClassName}(${primaryColumn.javaType} id) {
if (CollUtil.isEmpty(ids)) {
return ListUtil.empty();
}
return ${classNameVar}Mapper.selectById(id);
}
@Override
public List<${table.className}DO> get${simpleClassName}List(Collection<${primaryColumn.javaType}> ids) {
if (CollUtil.isEmpty(ids)) {
return ListUtil.empty();
}
return ${classNameVar}Mapper.selectBatchIds(ids);
}

View File

@ -1,52 +0,0 @@
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;
// TODO @疯狂是不是不需要这个啦
/**
* 分销 API 接口
*
* @author owen
*/
public interface BrokerageApi {
/**
* 获得分销用户
*
* @param userId 用户编号
* @return 分销用户信息
*/
BrokerageUserDTO getBrokerageUser(Long userId);
/**
* 会员绑定推广员
*
* @param userId 用户编号
* @param bindUserId 推广员编号
* @param registerTime 用户注册时间
* @return 是否绑定
*/
default boolean bindUser(@NotNull Long userId, @NotNull Long bindUserId, @NotNull LocalDateTime registerTime) {
// 注册时间在30秒内的都算新用户
// TODO @疯狂这个要不抽到 service 里哈
boolean isNewUser = LocalDateTimeUtils.afterNow(registerTime.minusSeconds(30));
return bindUser(userId, bindUserId, isNewUser);
}
/**
* 绑定推广员
*
* @param userId 用户编号
* @param bindUserId 推广员编号
* @param isNewUser 是否为新用户
* @return 是否绑定
*/
boolean bindUser(@NotNull(message = "用户编号不能为空") Long userId,
@NotNull(message = "推广员编号不能为空") Long bindUserId,
@NotNull Boolean isNewUser);
}

View File

@ -1,51 +0,0 @@
package cn.iocoder.yudao.module.trade.api.brokerage.dto;
import lombok.Data;
import java.time.LocalDateTime;
/**
* 分销用户 DTO
*
* @author owen
*/
@Data
public class BrokerageUserDTO {
/**
* 用户编号
* <p>
* 对应 MemberUserDO id 字段
*/
private Long id;
/**
* 推广员编号
* <p>
* 关联 MemberUserDO id 字段
*/
private Long bindUserId;
/**
* 推广员绑定时间
*/
private LocalDateTime bindUserTime;
/**
* 推广资格
*/
private Boolean brokerageEnabled;
/**
* 成为分销员时间
*/
private LocalDateTime brokerageTime;
/**
* 可用佣金
*/
private Integer price;
/**
* 冻结佣金
*/
private Integer frozenPrice;
}

View File

@ -1,40 +0,0 @@
package cn.iocoder.yudao.module.trade.enums.brokerage;
import cn.iocoder.yudao.framework.common.core.IntArrayValuable;
import lombok.AllArgsConstructor;
import lombok.Getter;
import java.util.Arrays;
// TODO @疯狂是不是搞成层级类似 level 这样因为本质上它是 1 2 3 级这样的关系哈
/**
* 分销用户类型枚举
*
* @author owen
*/
@AllArgsConstructor
@Getter
public enum BrokerageUserTypeEnum implements IntArrayValuable {
ALL(0, "全部"),
FIRST(1, "一级推广人"),
SECOND(2, "二级推广人"),
;
public static final int[] ARRAYS = Arrays.stream(values()).mapToInt(BrokerageUserTypeEnum::getType).toArray();
/**
* 类型
*/
private final Integer type;
/**
* 名字
*/
private final String name;
@Override
public int[] array() {
return ARRAYS;
}
}

View File

@ -1,33 +0,0 @@
package cn.iocoder.yudao.module.trade.api.brokerage;
import cn.iocoder.yudao.module.trade.api.brokerage.dto.BrokerageUserDTO;
import cn.iocoder.yudao.module.trade.convert.brokerage.user.BrokerageUserConvert;
import cn.iocoder.yudao.module.trade.service.brokerage.user.BrokerageUserService;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
import javax.annotation.Resource;
/**
* 分销 API 接口实现类
*
* @author owen
*/
@Service
@Validated
public class BrokerageApiImpl implements BrokerageApi {
@Resource
private BrokerageUserService brokerageUserService;
@Override
public BrokerageUserDTO getBrokerageUser(Long userId) {
return BrokerageUserConvert.INSTANCE.convertDTO(brokerageUserService.getBrokerageUser(userId));
}
@Override
public boolean bindUser(Long userId, Long bindUserId, Boolean isNewUser) {
return brokerageUserService.bindBrokerageUser(userId, bindUserId, isNewUser);
}
}

View File

@ -57,8 +57,8 @@ public class BrokerageRecordBaseVO {
@DateTimeFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND)
private LocalDateTime unfreezeTime;
@Schema(description = "来源用户类型")
private Integer sourceUserType;
@Schema(description = "来源用户等级")
private Integer sourceUserLevel;
@Schema(description = "来源用户编号")
private Long sourceUserId;

View File

@ -1,8 +1,6 @@
package cn.iocoder.yudao.module.trade.controller.admin.brokerage.record.vo;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.validation.InEnum;
import cn.iocoder.yudao.module.trade.enums.brokerage.BrokerageUserTypeEnum;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.EqualsAndHashCode;
@ -33,7 +31,6 @@ public class BrokerageRecordPageReqVO extends PageParam {
private LocalDateTime[] createTime;
@Schema(description = "用户类型")
@InEnum(value = BrokerageUserTypeEnum.class, message = "用户类型必须是 {value}")
private Integer sourceUserType;
private Integer sourceUserLevel;
}

View File

@ -9,7 +9,6 @@ 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;
@ -96,7 +95,7 @@ public class BrokerageUserController {
// 合计推广用户数量
Map<Long, Long> brokerageUserCountMap = convertMap(userIds,
userId -> userId,
userId -> brokerageUserService.getBrokerageUserCountByBindUserId(userId, BrokerageUserTypeEnum.ALL));
userId -> brokerageUserService.getBrokerageUserCountByBindUserId(userId, null));
// todo 合计提现

View File

@ -1,8 +1,6 @@
package cn.iocoder.yudao.module.trade.controller.admin.brokerage.user.vo;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.validation.InEnum;
import cn.iocoder.yudao.module.trade.enums.brokerage.BrokerageUserTypeEnum;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.EqualsAndHashCode;
@ -29,9 +27,8 @@ public class BrokerageUserPageReqVO extends PageParam {
@DateTimeFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND)
private LocalDateTime[] createTime;
@Schema(description = "用户类型")
@InEnum(value = BrokerageUserTypeEnum.class, message = "用户类型必须是 {value}")
private Integer userType;
@Schema(description = "用户等级")
private Integer level;
@Schema(description = "绑定时间")
@DateTimeFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND)

View File

@ -35,7 +35,7 @@ public interface BrokerageRecordConvert {
default BrokerageRecordDO convert(BrokerageUserDO user, BrokerageRecordBizTypeEnum bizType, String bizId,
Integer brokerageFrozenDays, int brokeragePrice, LocalDateTime unfreezeTime,
String title, Long sourceUserId, Integer sourceUserType) {
String title, Long sourceUserId, Integer sourceUserLevel) {
brokerageFrozenDays = ObjectUtil.defaultIfNull(brokerageFrozenDays, 0);
// 不冻结时佣金直接就是结算状态
Integer status = brokerageFrozenDays > 0
@ -47,7 +47,7 @@ public interface BrokerageRecordConvert {
.setTitle(title)
.setDescription(StrUtil.format(bizType.getDescription(), String.format("¥%.2f", brokeragePrice / 100d)))
.setStatus(status).setFrozenDays(brokerageFrozenDays).setUnfreezeTime(unfreezeTime)
.setSourceUserType(sourceUserType).setSourceUserId(sourceUserId);
.setSourceUserLevel(sourceUserLevel).setSourceUserId(sourceUserId);
}
default PageResult<BrokerageRecordRespVO> convertPage(PageResult<BrokerageRecordDO> pageResult, Map<Long, MemberUserRespDTO> userMap) {

View File

@ -3,7 +3,6 @@ package cn.iocoder.yudao.module.trade.convert.brokerage.user;
import cn.hutool.core.map.MapUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.member.api.user.dto.MemberUserRespDTO;
import cn.iocoder.yudao.module.trade.api.brokerage.dto.BrokerageUserDTO;
import cn.iocoder.yudao.module.trade.controller.admin.brokerage.user.vo.BrokerageUserRespVO;
import cn.iocoder.yudao.module.trade.dal.dataobject.brokerage.user.BrokerageUserDO;
import cn.iocoder.yudao.module.trade.service.brokerage.bo.UserBrokerageSummaryBO;
@ -56,6 +55,4 @@ public interface BrokerageUserConvert {
user -> target.setNickname(user.getNickname()).setAvatar(user.getAvatar()));
return target;
}
BrokerageUserDTO convertDTO(BrokerageUserDO brokerageUser);
}

View File

@ -29,7 +29,6 @@ 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;
@ -276,10 +275,10 @@ public interface TradeOrderConvert {
TradeOrderDO convert(TradeOrderRemarkReqVO reqVO);
default BrokerageAddReqBO convert(TradeOrderItemDO item, ProductSkuRespDTO sku) {
default BrokerageAddReqBO convert(MemberUserRespDTO user, TradeOrderItemDO item, ProductSkuRespDTO sku) {
return new BrokerageAddReqBO().setBizId(String.valueOf(item.getId())).setSourceUserId(item.getUserId())
.setBasePrice(item.getPayPrice() * item.getCount())
.setTitle(BrokerageRecordBizTypeEnum.ORDER.getTitle()) // TODO @疯狂标题类似木晴冰雪成功购买云时代的JVM原理与实战茫农成功购买深入拆解消息队列47讲
.setTitle(StrUtil.format("{}成功购买{}", user.getNickname(), item.getSpuName()))
.setFirstFixedPrice(sku.getFirstBrokerageRecord()).setSecondFixedPrice(sku.getSecondBrokerageRecord());
}

View File

@ -3,7 +3,6 @@ package cn.iocoder.yudao.module.trade.dal.dataobject.brokerage.record;
import cn.iocoder.yudao.framework.mybatis.core.dataobject.BaseDO;
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 com.baomidou.mybatisplus.annotation.KeySequence;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
@ -83,11 +82,11 @@ public class BrokerageRecordDO extends BaseDO {
private LocalDateTime unfreezeTime;
/**
* 来源用户类型
* 来源用户等级
* <p>
* 枚举 {@link BrokerageUserTypeEnum}被推广用户和 {@link #userId} 的推广层级关系
* 被推广用户和 {@link #userId} 的推广层级关系
*/
private Integer sourceUserType;
private Integer sourceUserLevel;
/**
* 来源用户编号
* <p>

View File

@ -60,4 +60,12 @@ public class BrokerageUserDO extends BaseDO {
*/
private Integer frozenPrice;
/**
* 等级
*/
private Integer level;
/**
* 路径
*/
private String path;
}

View File

@ -5,7 +5,6 @@ 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.BrokerageRecordPageReqVO;
import cn.iocoder.yudao.module.trade.dal.dataobject.brokerage.record.BrokerageRecordDO;
import cn.iocoder.yudao.module.trade.enums.brokerage.BrokerageUserTypeEnum;
import cn.iocoder.yudao.module.trade.service.brokerage.bo.UserBrokerageSummaryBO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.ibatis.annotations.Mapper;
@ -24,14 +23,12 @@ import java.util.List;
public interface BrokerageRecordMapper extends BaseMapperX<BrokerageRecordDO> {
default PageResult<BrokerageRecordDO> selectPage(BrokerageRecordPageReqVO reqVO) {
boolean sourceUserTypeCondition = reqVO.getSourceUserType() != null &&
!BrokerageUserTypeEnum.ALL.getType().equals(reqVO.getSourceUserType());
// 分页查询
return selectPage(reqVO, new LambdaQueryWrapperX<BrokerageRecordDO>()
.eqIfPresent(BrokerageRecordDO::getUserId, reqVO.getUserId())
.eqIfPresent(BrokerageRecordDO::getBizType, reqVO.getBizType())
.eqIfPresent(BrokerageRecordDO::getStatus, reqVO.getStatus())
.eq(sourceUserTypeCondition, BrokerageRecordDO::getSourceUserType, reqVO.getSourceUserType())
.eqIfPresent(BrokerageRecordDO::getSourceUserLevel, reqVO.getSourceUserLevel())
.betweenIfPresent(BrokerageRecordDO::getCreateTime, reqVO.getCreateTime())
.orderByDesc(BrokerageRecordDO::getId));
}

View File

@ -1,17 +1,15 @@
package cn.iocoder.yudao.module.trade.dal.mysql.brokerage.user;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
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.user.vo.BrokerageUserPageReqVO;
import cn.iocoder.yudao.module.trade.dal.dataobject.brokerage.user.BrokerageUserDO;
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;
import java.util.List;
/**
* 分销用户 Mapper
@ -21,35 +19,16 @@ import org.apache.ibatis.annotations.Select;
@Mapper
public interface BrokerageUserMapper extends BaseMapperX<BrokerageUserDO> {
default PageResult<BrokerageUserDO> selectPage(BrokerageUserPageReqVO reqVO) {
default PageResult<BrokerageUserDO> selectPage(BrokerageUserPageReqVO reqVO, List<Integer> levels) {
return selectPage(reqVO, new LambdaQueryWrapperX<BrokerageUserDO>()
.eqIfPresent(BrokerageUserDO::getBrokerageEnabled, reqVO.getBrokerageEnabled())
.betweenIfPresent(BrokerageUserDO::getCreateTime, reqVO.getCreateTime())
.betweenIfPresent(BrokerageUserDO::getBindUserTime, reqVO.getBindUserTime())
.and(reqVO.getBindUserId() != null, w -> buildBindUserCondition(reqVO, w))
.findInSetIfPresent(BrokerageUserDO::getPath, reqVO.getBindUserId())
.inIfPresent(BrokerageUserDO::getLevel, levels)
.orderByDesc(BrokerageUserDO::getId));
}
static void buildBindUserCondition(BrokerageUserPageReqVO reqVO, LambdaQueryWrapper<BrokerageUserDO> wrapper) {
if (BrokerageUserTypeEnum.FIRST.getType().equals(reqVO.getUserType())) {
buildFirstBindUserCondition(reqVO.getBindUserId(), wrapper);
} else if (BrokerageUserTypeEnum.SECOND.getType().equals(reqVO.getUserType())) {
buildSecondBindUserCondition(reqVO.getBindUserId(), wrapper);
} else {
// TODO @疯狂要不要把这个逻辑挪到 Service 算出子用户有哪些然后 IN
buildFirstBindUserCondition(reqVO.getBindUserId(), wrapper);
buildSecondBindUserCondition(reqVO.getBindUserId(), wrapper.or()); // 通过 or 实现多个条件
}
}
static void buildFirstBindUserCondition(Long bindUserId, LambdaQueryWrapper<BrokerageUserDO> wrapper) {
wrapper.eq(BrokerageUserDO::getBindUserId, bindUserId);
}
static void buildSecondBindUserCondition(Long bindUserId, LambdaQueryWrapper<BrokerageUserDO> wrapper) {
wrapper.inSql(BrokerageUserDO::getBindUserId, StrUtil.format("SELECT id FROM trade_brokerage_user WHERE bind_user_id = {}", bindUserId));
}
/**
* 更新用户可用佣金增加
*
@ -128,7 +107,8 @@ public interface BrokerageUserMapper extends BaseMapperX<BrokerageUserDO> {
default void updateBindUserIdAndBindUserTimeToNull(Long id) {
update(null, new LambdaUpdateWrapper<BrokerageUserDO>()
.eq(BrokerageUserDO::getId, id)
.set(BrokerageUserDO::getBindUserId, null).set(BrokerageUserDO::getBindUserTime, null));
.set(BrokerageUserDO::getBindUserId, null).set(BrokerageUserDO::getBindUserTime, null)
.set(BrokerageUserDO::getLevel, 1).set(BrokerageUserDO::getPath, ""));
}
default void updateEnabledFalseAndBrokerageTimeToNull(Long id) {
@ -137,11 +117,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);
default Long selectCountByBindUserIdAndLevelIn(Long bindUserId, List<Integer> levels) {
return selectCount(new LambdaQueryWrapperX<BrokerageUserDO>()
.findInSetIfPresent(BrokerageUserDO::getPath, bindUserId)
.inIfPresent(BrokerageUserDO::getLevel, levels));
}
@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

@ -15,7 +15,6 @@ import cn.iocoder.yudao.module.trade.dal.dataobject.config.TradeConfigDO;
import cn.iocoder.yudao.module.trade.dal.mysql.brokerage.record.BrokerageRecordMapper;
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.BrokerageAddReqBO;
import cn.iocoder.yudao.module.trade.service.brokerage.bo.UserBrokerageSummaryBO;
import cn.iocoder.yudao.module.trade.service.brokerage.user.BrokerageUserService;
@ -29,6 +28,7 @@ import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
/**
* 佣金记录 Service 实现类
@ -74,7 +74,7 @@ public class BrokerageRecordServiceImpl implements BrokerageRecordService {
}
// 1.2 计算一级分佣
addBrokerage(firstUser, list, memberConfig.getBrokerageFrozenDays(), memberConfig.getBrokerageFirstPercent(),
bizType, BrokerageUserTypeEnum.FIRST);
bizType, 1);
// 2.1 获得二级推广员
if (firstUser.getBindUserId() == null) {
@ -86,7 +86,7 @@ public class BrokerageRecordServiceImpl implements BrokerageRecordService {
}
// 2.2 计算二级分佣
addBrokerage(secondUser, list, memberConfig.getBrokerageFrozenDays(), memberConfig.getBrokerageSecondPercent(),
bizType, BrokerageUserTypeEnum.SECOND);
bizType, 2);
}
@Override
@ -142,10 +142,10 @@ public class BrokerageRecordServiceImpl implements BrokerageRecordService {
* @param brokerageFrozenDays 冻结天数
* @param brokeragePercent 佣金比例
* @param bizType 业务类型
* @param sourceUserType 来源用户类型
* @param sourceUserLevel 来源用户等级
*/
private void addBrokerage(BrokerageUserDO user, List<BrokerageAddReqBO> list, Integer brokerageFrozenDays,
Integer brokeragePercent, BrokerageRecordBizTypeEnum bizType, BrokerageUserTypeEnum sourceUserType) {
Integer brokeragePercent, BrokerageRecordBizTypeEnum bizType, Integer sourceUserLevel) {
// 1.1 处理冻结时间
LocalDateTime unfreezeTime = null;
if (brokerageFrozenDays != null && brokerageFrozenDays > 0) {
@ -157,12 +157,12 @@ public class BrokerageRecordServiceImpl implements BrokerageRecordService {
for (BrokerageAddReqBO item : list) {
// 计算金额
Integer fixedPrice;
if (BrokerageUserTypeEnum.FIRST.equals(sourceUserType)) {
if (Objects.equals(sourceUserLevel, 1)) {
fixedPrice = item.getFirstFixedPrice();
} else if (BrokerageUserTypeEnum.SECOND.equals(sourceUserType)) {
} else if (Objects.equals(sourceUserLevel, 2)) {
fixedPrice = item.getSecondFixedPrice();
} else {
throw new IllegalArgumentException(StrUtil.format("来源用户({}) 不合法", sourceUserType));
throw new IllegalArgumentException(StrUtil.format("用户等级({}) 不合法", sourceUserLevel));
}
int brokeragePrice = calculatePrice(item.getBasePrice(), brokeragePercent, fixedPrice);
if (brokeragePrice <= 0) {
@ -172,7 +172,7 @@ public class BrokerageRecordServiceImpl implements BrokerageRecordService {
// 创建记录实体
records.add(BrokerageRecordConvert.INSTANCE.convert(user, bizType, item.getBizId(),
brokerageFrozenDays, brokeragePrice, unfreezeTime, item.getTitle(),
item.getSourceUserId(), sourceUserType.getType()));
item.getSourceUserId(), sourceUserLevel));
}
if (CollUtil.isEmpty(records)) {
return;

View File

@ -1,10 +1,12 @@
package cn.iocoder.yudao.module.trade.service.brokerage.user;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils;
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 javax.validation.constraints.NotNull;
import java.time.LocalDateTime;
import java.util.Collection;
import java.util.List;
@ -91,10 +93,25 @@ public interface BrokerageUserService {
* 获得推广用户数量
*
* @param bindUserId 绑定的推广员编号
* @param userType 用户类型
* @param level 推广用户等级
* @return 推广用户数量
*/
Long getBrokerageUserCountByBindUserId(Long bindUserId, BrokerageUserTypeEnum userType);
Long getBrokerageUserCountByBindUserId(Long bindUserId, Integer level);
/**
* 会员绑定推广员
*
* @param userId 用户编号
* @param bindUserId 推广员编号
* @param registerTime 用户注册时间
* @return 是否绑定
*/
default boolean bindBrokerageUser(@NotNull Long userId, @NotNull Long bindUserId, @NotNull LocalDateTime registerTime) {
// 注册时间在30秒内的都算新用户
boolean isNewUser = LocalDateTimeUtils.afterNow(registerTime.minusSeconds(30));
return bindBrokerageUser(userId, bindUserId, isNewUser);
}
/**
* 会员绑定推广员

View File

@ -1,7 +1,10 @@
package cn.iocoder.yudao.module.trade.service.brokerage.user;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.BooleanUtil;
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.user.vo.BrokerageUserPageReqVO;
import cn.iocoder.yudao.module.trade.dal.dataobject.brokerage.user.BrokerageUserDO;
@ -9,17 +12,13 @@ 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;
import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.*;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.trade.enums.ErrorCodeConstants.*;
@ -51,7 +50,8 @@ public class BrokerageUserServiceImpl implements BrokerageUserService {
@Override
public PageResult<BrokerageUserDO> getBrokerageUserPage(BrokerageUserPageReqVO pageReqVO) {
return brokerageUserMapper.selectPage(pageReqVO);
List<Integer> levels = buildUserQueryLevels(pageReqVO.getBindUserId(), pageReqVO.getLevel());
return brokerageUserMapper.selectPage(pageReqVO, levels);
}
@Override
@ -66,10 +66,14 @@ public class BrokerageUserServiceImpl implements BrokerageUserService {
return;
}
// 绑定关系未发生变化
if (Objects.equals(brokerageUser.getBindUserId(), bindUserId)) {
return;
}
// 情况二修改推广员
validateCanBindUser(brokerageUser, bindUserId);
brokerageUserMapper.updateById(new BrokerageUserDO().setId(id)
.setBindUserId(bindUserId).setBindUserTime(LocalDateTime.now()));
brokerageUserMapper.updateById(fillBindUserData(bindUserId, new BrokerageUserDO().setId(id)));
}
@Override
@ -132,19 +136,12 @@ public class BrokerageUserServiceImpl implements BrokerageUserService {
}
@Override
public Long getBrokerageUserCountByBindUserId(Long bindUserId, BrokerageUserTypeEnum userType) {
switch (userType) {
case ALL: // TODO @疯狂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:
public Long getBrokerageUserCountByBindUserId(Long bindUserId, Integer level) {
List<Integer> levels = buildUserQueryLevels(bindUserId, level);
if (CollUtil.isEmpty(levels)) {
return 0L;
}
return brokerageUserMapper.selectCountByBindUserIdAndLevelIn(bindUserId, levels);
}
@Override
@ -171,14 +168,30 @@ public class BrokerageUserServiceImpl implements BrokerageUserService {
brokerageUser.setBrokerageEnabled(true).setBrokerageTime(LocalDateTime.now());
}
brokerageUser.setBindUserId(bindUserId).setBindUserTime(LocalDateTime.now());
brokerageUserMapper.insert(brokerageUser);
brokerageUserMapper.insert(fillBindUserData(bindUserId, brokerageUser));
} else {
brokerageUserMapper.updateById(new BrokerageUserDO().setId(userId)
.setBindUserId(bindUserId).setBindUserTime(LocalDateTime.now()));
brokerageUserMapper.updateById(fillBindUserData(bindUserId, new BrokerageUserDO().setId(userId)));
}
return true;
}
private BrokerageUserDO fillBindUserData(Long bindUserId, BrokerageUserDO brokerageUser) {
BrokerageUserDO bindUser = getBrokerageUser(bindUserId);
Integer bindUserLevel = 0;
String bindUserPath = "";
if (bindUser != null) {
bindUserLevel = ObjectUtil.defaultIfNull(bindUser.getLevel(), 0);
bindUserPath = bindUser.getPath();
}
String path = StrUtil.isEmpty(bindUserPath)
? String.valueOf(bindUserId)
: String.format("%s,%s", bindUserPath, bindUserId);
return brokerageUser.setBindUserId(bindUserId).setBindUserTime(LocalDateTime.now())
.setLevel(bindUserLevel + 1).setPath(path);
}
@Override
public Boolean getUserBrokerageEnabled(Long userId) {
// 全局分销功能是否开启
@ -231,11 +244,29 @@ public class BrokerageUserServiceImpl implements BrokerageUserService {
throw exception(BROKERAGE_BIND_SELF);
}
// TODO @疯狂这块是不是一直查询到根节点中间不允许出现自己就是不能形成环虽然目前是 2 但是未来可能会改多级 = = 环的话就会存在问题哈
// A->B->A下级不能绑定自己的上级, A->B->C->A可以!!
if (Objects.equals(user.getId(), bindUser.getBindUserId())) {
// 下级不能绑定自己的上级
if (StrUtil.split(bindUser.getPath(), ",").contains(String.valueOf(user.getId()))) {
throw exception(BROKERAGE_BIND_LOOP);
}
}
private List<Integer> buildUserQueryLevels(Long bindUserId, Integer level) {
List<Integer> levels = new ArrayList<>(2);
BrokerageUserDO bindUser = getBrokerageUser(bindUserId);
if (bindUser == null) {
return levels;
}
if (level == null) {
// 默认查两层
levels.add(bindUser.getLevel() + 1);
levels.add(bindUser.getLevel() + 2);
} else {
levels.add(bindUser.getLevel() + level);
}
return levels;
}
}

View File

@ -12,6 +12,8 @@ import cn.iocoder.yudao.module.member.api.address.AddressApi;
import cn.iocoder.yudao.module.member.api.address.dto.AddressRespDTO;
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;
import cn.iocoder.yudao.module.member.api.user.dto.MemberUserRespDTO;
import cn.iocoder.yudao.module.member.enums.MemberExperienceBizTypeEnum;
import cn.iocoder.yudao.module.member.enums.point.MemberPointBizTypeEnum;
import cn.iocoder.yudao.module.pay.api.order.PayOrderApi;
@ -115,6 +117,8 @@ public class TradeOrderUpdateServiceImpl implements TradeOrderUpdateService {
@Resource
private BargainRecordApi bargainRecordApi;
@Resource
private MemberUserApi memberUserApi;
@Resource
private MemberLevelApi memberLevelApi;
@Resource
private MemberPointApi memberPointApi;
@ -781,9 +785,12 @@ public class TradeOrderUpdateServiceImpl implements TradeOrderUpdateService {
@Async
protected void addBrokerageAsync(Long userId, Long orderId) {
MemberUserRespDTO user = memberUserApi.getUser(userId);
Assert.notNull(user);
List<TradeOrderItemDO> orderItems = tradeOrderItemMapper.selectListByOrderId(orderId);
List<BrokerageAddReqBO> list = convertList(orderItems,
item -> TradeOrderConvert.INSTANCE.convert(item, productSkuApi.getSku(item.getSkuId())));
item -> TradeOrderConvert.INSTANCE.convert(user, item, productSkuApi.getSku(item.getSkuId())));
brokerageRecordService.addBrokerage(userId, BrokerageRecordBizTypeEnum.ORDER, list);
}