1.用户积分变动方法, 提取到单独的接口中
2.规范代码
This commit is contained in:
owen 2023-08-26 13:52:20 +08:00
parent f6f33a07d0
commit 02abe86253
9 changed files with 91 additions and 54 deletions

View File

@ -5,6 +5,7 @@ import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.spring.SpringUtil;
import cn.iocoder.yudao.framework.common.core.KeyValue;
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
import cn.iocoder.yudao.framework.common.enums.TerminalEnum;
@ -13,6 +14,7 @@ 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.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;
@ -110,6 +112,8 @@ public class TradeOrderUpdateServiceImpl implements TradeOrderUpdateService {
private MemberUserApi memberUserApi;
@Resource
private MemberLevelApi memberLevelApi;
@Resource
private MemberPointApi memberPointApi;
@Resource
private ProductCommentApi productCommentApi;
@ -346,9 +350,9 @@ public class TradeOrderUpdateServiceImpl implements TradeOrderUpdateService {
// TODO 芋艿OrderLog
// 增加用户积分
addUserPointAsync(order.getUserId(), order.getPayPrice(), order.getId());
getSelf().addUserPointAsync(order.getUserId(), order.getPayPrice(), order.getId());
// 增加用户经验
addUserExperienceAsync(order.getUserId(), order.getPayPrice(), order.getId());
getSelf().addUserExperienceAsync(order.getUserId(), order.getPayPrice(), order.getId());
}
/**
@ -617,9 +621,9 @@ public class TradeOrderUpdateServiceImpl implements TradeOrderUpdateService {
// TODO 芋艿未来如果有分佣需要更新相关分佣订单为已失效
// 扣减用户积分
reduceUserPointAsync(order.getUserId(), orderRefundPrice, afterSaleId);
getSelf().reduceUserPointAsync(order.getUserId(), orderRefundPrice, afterSaleId);
// 扣减用户经验
reduceUserExperienceAsync(order.getUserId(), orderRefundPrice, afterSaleId);
getSelf().reduceUserExperienceAsync(order.getUserId(), orderRefundPrice, afterSaleId);
}
@Override
@ -667,7 +671,6 @@ public class TradeOrderUpdateServiceImpl implements TradeOrderUpdateService {
TradeOrderItemAfterSaleStatusEnum.SUCCESS.getStatus()));
}
// TODO @疯狂直接 this 调用async 不生效哈全局搜下 getSelf();
@Async
protected void addUserExperienceAsync(Long userId, Integer payPrice, Long orderId) {
int bizType = MemberExperienceBizTypeEnum.ORDER.getType();
@ -683,12 +686,22 @@ public class TradeOrderUpdateServiceImpl implements TradeOrderUpdateService {
@Async
protected void addUserPointAsync(Long userId, Integer payPrice, Long orderId) {
int bizType = MemberPointBizTypeEnum.ORDER_BUY.getType();
memberUserApi.addPoint(userId, payPrice, bizType, String.valueOf(orderId));
memberPointApi.addPoint(userId, payPrice, bizType, String.valueOf(orderId));
}
@Async
protected void reduceUserPointAsync(Long userId, Integer refundPrice, Long afterSaleId) {
int bizType = MemberPointBizTypeEnum.ORDER_CANCEL.getType();
memberUserApi.addPoint(userId, -refundPrice, bizType, String.valueOf(afterSaleId));
memberPointApi.addPoint(userId, -refundPrice, bizType, String.valueOf(afterSaleId));
}
/**
* 获得自身的代理对象解决 AOP 生效问题
*
* @return 自己
*/
private TradeOrderUpdateServiceImpl getSelf() {
return SpringUtil.getBean(getClass());
}
}

View File

@ -0,0 +1,22 @@
package cn.iocoder.yudao.module.member.api.point;
import cn.iocoder.yudao.module.member.enums.point.MemberPointBizTypeEnum;
/**
* 用户积分的 API 接口
*
* @author owen
*/
public interface MemberPointApi {
/**
* 增加用户积分
*
* @param userId 用户编号
* @param point 积分
* @param bizType 业务类型 {@link MemberPointBizTypeEnum}
* @param bizId 业务编号
*/
void addPoint(Long userId, Integer point, Integer bizType, String bizId);
}

View File

@ -1,7 +1,6 @@
package cn.iocoder.yudao.module.member.api.user;
import cn.iocoder.yudao.module.member.api.user.dto.MemberUserRespDTO;
import cn.iocoder.yudao.module.member.enums.point.MemberPointBizTypeEnum;
import java.util.Collection;
import java.util.List;
@ -57,16 +56,4 @@ public interface MemberUserApi {
* @return 用户信息
*/
MemberUserRespDTO getUserByMobile(String mobile);
// TODO @疯狂是不是新的类MemberPointApi
/**
* 增加用户积分
*
* @param userId 用户编号
* @param point 积分
* @param bizType 业务类型 {@link MemberPointBizTypeEnum}
* @param bizId 业务编号
*/
void addPoint(Long userId, Integer point, Integer bizType, String bizId);
}

View File

@ -18,12 +18,12 @@ public enum MemberExperienceBizTypeEnum {
/**
* 管理员调整邀请新用户下单退单签到抽奖
*/
ADMIN(0, "管理员调整", "管理员调整获得 {} 经验", false),
INVITE_REGISTER(1, "邀新奖励", "邀请好友获得 {} 经验", false),
ORDER(2, "下单奖励", "下单获得 {} 经验", false),
REFUND(3, "退单扣除", "退单获得 {} 经验", true),
SIGN_IN(4, "签到奖励", "签到获得 {} 经验", false),
LOTTERY(5, "抽奖奖励", "抽奖获得 {} 经验", false),
ADMIN(0, "管理员调整", "管理员调整获得 {} 经验", true),
INVITE_REGISTER(1, "邀新奖励", "邀请好友获得 {} 经验", true),
ORDER(2, "下单奖励", "下单获得 {} 经验", true),
REFUND(3, "退单扣除", "退单获得 {} 经验", false),
SIGN_IN(4, "签到奖励", "签到获得 {} 经验", true),
LOTTERY(5, "抽奖奖励", "抽奖获得 {} 经验", true),
;
/**
@ -41,7 +41,7 @@ public enum MemberExperienceBizTypeEnum {
/**
* 是否为扣减积分
*/
private final boolean isReduce;
private final boolean add;
public static MemberExperienceBizTypeEnum getByType(Integer type) {
return EnumUtil.getBy(MemberExperienceBizTypeEnum.class,

View File

@ -16,9 +16,9 @@ import java.util.Objects;
@Getter
public enum MemberPointBizTypeEnum implements IntArrayValuable {
SIGN(1, "签到", "签到获得 {} 积分", false),
ORDER_BUY(10, "订单消费", "下单获得 {} 积分", false),
ORDER_CANCEL(11, "订单取消", "退单获得 {} 积分", true); // 退回积分
SIGN(1, "签到", "签到获得 {} 积分", true),
ORDER_BUY(10, "订单消费", "下单获得 {} 积分", true),
ORDER_CANCEL(11, "订单取消", "退单获得 {} 积分", false); // 退回积分
/**
* 类型
@ -32,11 +32,10 @@ public enum MemberPointBizTypeEnum implements IntArrayValuable {
* 描述
*/
private final String description;
// TODO @疯狂改成 add 会好点一个是属性我们尽量不要 isXXX另外尽量正向思维不取反
/**
* 是否为扣减积分
*/
private final boolean isReduce;
private final boolean add;
@Override
public int[] array() {

View File

@ -0,0 +1,34 @@
package cn.iocoder.yudao.module.member.api.point;
import cn.iocoder.yudao.module.member.enums.point.MemberPointBizTypeEnum;
import cn.iocoder.yudao.module.member.service.point.MemberPointRecordService;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
import javax.annotation.Resource;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.member.enums.ErrorCodeConstants.POINT_RECORD_BIZ_NOT_SUPPORT;
/**
* 用户积分的 API 实现类
*
* @author owen
*/
@Service
@Validated
public class MemberPointApiImpl implements MemberPointApi {
@Resource
private MemberPointRecordService memberPointRecordService;
@Override
public void addPoint(Long userId, Integer point, Integer bizType, String bizId) {
MemberPointBizTypeEnum bizTypeEnum = MemberPointBizTypeEnum.getByType(bizType);
if (bizTypeEnum == null) {
throw exception(POINT_RECORD_BIZ_NOT_SUPPORT);
}
memberPointRecordService.createPointRecord(userId, point, bizTypeEnum, bizId);
}
}

View File

@ -3,8 +3,6 @@ package cn.iocoder.yudao.module.member.api.user;
import cn.iocoder.yudao.module.member.api.user.dto.MemberUserRespDTO;
import cn.iocoder.yudao.module.member.convert.user.MemberUserConvert;
import cn.iocoder.yudao.module.member.dal.dataobject.user.MemberUserDO;
import cn.iocoder.yudao.module.member.enums.point.MemberPointBizTypeEnum;
import cn.iocoder.yudao.module.member.service.point.MemberPointRecordService;
import cn.iocoder.yudao.module.member.service.user.MemberUserService;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
@ -13,9 +11,6 @@ import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.member.enums.ErrorCodeConstants.POINT_RECORD_BIZ_NOT_SUPPORT;
/**
* 会员用户的 API 实现类
*
@ -28,9 +23,6 @@ public class MemberUserApiImpl implements MemberUserApi {
@Resource
private MemberUserService userService;
@Resource
private MemberPointRecordService memberPointRecordService;
@Override
public MemberUserRespDTO getUser(Long id) {
MemberUserDO user = userService.getUser(id);
@ -52,13 +44,4 @@ public class MemberUserApiImpl implements MemberUserApi {
return MemberUserConvert.INSTANCE.convert2(userService.getUserByMobile(mobile));
}
@Override
public void addPoint(Long userId, Integer point, Integer bizType, String bizId) {
MemberPointBizTypeEnum bizTypeEnum = MemberPointBizTypeEnum.getByType(bizType);
if (bizTypeEnum == null) {
throw exception(POINT_RECORD_BIZ_NOT_SUPPORT);
}
memberPointRecordService.createPointRecord(userId, point, bizTypeEnum, bizId);
}
}

View File

@ -232,7 +232,7 @@ public class MemberLevelServiceImpl implements MemberLevelService {
if (experience == 0) {
return;
}
if (bizType.isReduce() && experience > 0) {
if (!bizType.isAdd() && experience > 0) {
experience = -experience;
}

View File

@ -1,6 +1,5 @@
package cn.iocoder.yudao.module.member.service.point;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
@ -75,15 +74,15 @@ public class MemberPointRecordServiceImpl implements MemberPointRecordService {
// 1. 根据配置的比例换算实际的积分
point = point * pointConfig.getTradeGivePoint();
if (bizType.isReduce() && point > 0) {
if (!bizType.isAdd() && point > 0) {
point = -point;
}
// 2. 增加积分记录
MemberUserDO user = memberUserService.getUser(userId);
Integer userPoint = ObjectUtil.defaultIfNull(user.getPoint(), 0);
// 用户变动后的积分防止扣出负数 TODO 疯狂积分是不是允许扣到负数因为它是跟有钱有关的东西不能让商家出现资金损失
Integer totalPoint = NumberUtil.max(userPoint + point, 0);
// 用户变动后的积分
Integer totalPoint = userPoint + point;
MemberPointRecordDO recordDO = new MemberPointRecordDO()
.setUserId(userId)
.setBizId(bizId)