trade: 1.支付成功后,增加用户积分;2.退款成功后,扣减用户积分

This commit is contained in:
owen 2023-08-25 23:07:54 +08:00
parent dad56fd096
commit d4acacec60
11 changed files with 146 additions and 16 deletions

View File

@ -16,6 +16,7 @@ import cn.iocoder.yudao.module.member.api.level.MemberLevelApi;
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;
import cn.iocoder.yudao.module.pay.api.order.dto.PayOrderCreateReqDTO;
import cn.iocoder.yudao.module.pay.api.order.dto.PayOrderRespDTO;
@ -344,8 +345,8 @@ public class TradeOrderUpdateServiceImpl implements TradeOrderUpdateService {
// TODO 芋艿OrderLog
// todo 增加用户积分
// 增加用户积分
addUserPointAsync(order.getUserId(), order.getPayPrice(), order.getId());
// 增加用户经验
addUserExperienceAsync(order.getUserId(), order.getPayPrice(), order.getId());
}
@ -615,9 +616,9 @@ public class TradeOrderUpdateServiceImpl implements TradeOrderUpdateService {
// TODO 芋艿未来如果有分佣需要更新相关分佣订单为已失效
// todo 取消用户积分
// 取消用户经验
// 扣减用户积分
reduceUserPointAsync(order.getUserId(), orderRefundPrice, afterSaleId);
// 扣减用户经验
reduceUserExperienceAsync(order.getUserId(), orderRefundPrice, afterSaleId);
}
@ -673,8 +674,20 @@ public class TradeOrderUpdateServiceImpl implements TradeOrderUpdateService {
}
@Async
protected void reduceUserExperienceAsync(Long userId, Integer refundPrice, Long afterSaleId){
protected void reduceUserExperienceAsync(Long userId, Integer refundPrice, Long afterSaleId) {
int bizType = MemberExperienceBizTypeEnum.REFUND.getType();
memberLevelApi.addExperience(userId, -refundPrice, bizType, String.valueOf(afterSaleId));
}
@Async
protected void addUserPointAsync(Long userId, Integer payPrice, Long orderId) {
int bizType = MemberPointBizTypeEnum.ORDER_BUY.getType();
memberUserApi.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));
}
}

View File

@ -1,6 +1,7 @@
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,4 +58,13 @@ public interface MemberUserApi {
*/
MemberUserRespDTO getUserByMobile(String mobile);
/**
* 增加用户积分
*
* @param userId 用户编号
* @param point 积分
* @param bizType 业务类型 {@link MemberPointBizTypeEnum}
* @param bizId 业务编号
*/
void addPoint(Long userId, Integer point, Integer bizType, String bizId);
}

View File

@ -4,7 +4,7 @@ import cn.iocoder.yudao.framework.common.exception.ErrorCode;
/**
* Member 错误码枚举类
*
* <p>
* member 系统使用 1-004-000-000
*/
public interface ErrorCodeConstants {
@ -32,6 +32,7 @@ public interface ErrorCodeConstants {
//========== 积分配置 1004007000 ==========
//========== 积分记录 1004008000 ==========
ErrorCode POINT_RECORD_BIZ_NOT_SUPPORT = new ErrorCode(1004008000, "用户积分记录业务类型不支持");
//========== 签到配置 1004009000 ==========
ErrorCode SIGN_IN_CONFIG_NOT_EXISTS = new ErrorCode(1004009000, "签到天数规则不存在");

View File

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

View File

@ -1,9 +1,12 @@
package cn.iocoder.yudao.module.member.enums.point;
import cn.hutool.core.util.EnumUtil;
import cn.iocoder.yudao.framework.common.core.IntArrayValuable;
import lombok.AllArgsConstructor;
import lombok.Getter;
import java.util.Objects;
/**
* 会员积分的业务类型枚举
*
@ -13,9 +16,9 @@ import lombok.Getter;
@Getter
public enum MemberPointBizTypeEnum implements IntArrayValuable {
SIGN(1, "签到"),
ORDER_BUY(10, "订单消费"),
ORDER_CANCEL(11, "订单取消"); // 退回积分
SIGN(1, "签到", "签到获得 {} 积分", false),
ORDER_BUY(10, "订单消费", "下单获得 {} 积分", false),
ORDER_CANCEL(11, "订单取消", "退单获得 {} 积分", true); // 退回积分
/**
* 类型
@ -25,10 +28,24 @@ public enum MemberPointBizTypeEnum implements IntArrayValuable {
* 名字
*/
private final String name;
/**
* 描述
*/
private final String description;
/**
* 是否为扣减积分
*/
private final boolean isReduce;
@Override
public int[] array() {
return new int[0];
}
public static MemberPointBizTypeEnum getByType(Integer type) {
return EnumUtil.getBy(MemberPointBizTypeEnum.class,
e -> Objects.equals(type, e.getType()));
}
}

View File

@ -3,6 +3,8 @@ 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;
@ -11,6 +13,9 @@ 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 实现类
*
@ -23,6 +28,9 @@ public class MemberUserApiImpl implements MemberUserApi {
@Resource
private MemberUserService userService;
@Resource
private MemberPointRecordService memberPointRecordService;
@Override
public MemberUserRespDTO getUser(Long id) {
MemberUserDO user = userService.getUser(id);
@ -44,4 +52,13 @@ 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

@ -235,6 +235,9 @@ public class MemberLevelServiceImpl implements MemberLevelService {
if (experience == 0) {
return;
}
if (bizType.isReduce() && experience > 0) {
experience = -experience;
}
MemberUserDO user = memberUserService.getUser(userId);

View File

@ -4,6 +4,7 @@ import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.member.controller.admin.point.vo.recrod.MemberPointRecordPageReqVO;
import cn.iocoder.yudao.module.member.dal.dataobject.point.MemberPointRecordDO;
import cn.iocoder.yudao.module.member.enums.point.MemberPointBizTypeEnum;
/**
* 用户积分记录 Service 接口
@ -29,4 +30,13 @@ public interface MemberPointRecordService {
*/
PageResult<MemberPointRecordDO> getPointRecordPage(Long userId, PageParam pageVO);
/**
* 创建用户积分记录
*
* @param userId 用户ID
* @param point 变动积分
* @param bizType 业务类型
* @param bizId 业务编号
*/
void createPointRecord(Long userId, Integer point, MemberPointBizTypeEnum bizType, String bizId);
}

View File

@ -1,12 +1,18 @@
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;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
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.controller.admin.point.vo.recrod.MemberPointRecordPageReqVO;
import cn.iocoder.yudao.module.member.dal.dataobject.point.MemberPointRecordDO;
import cn.iocoder.yudao.module.member.dal.dataobject.user.MemberUserDO;
import cn.iocoder.yudao.module.member.dal.mysql.point.MemberPointRecordMapper;
import cn.iocoder.yudao.module.member.enums.point.MemberPointBizTypeEnum;
import cn.iocoder.yudao.module.member.service.user.MemberUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
@ -33,6 +39,8 @@ public class MemberPointRecordServiceImpl implements MemberPointRecordService {
@Resource
private MemberUserApi memberUserApi;
@Resource
private MemberUserService memberUserService;
@Override
public PageResult<MemberPointRecordDO> getPointRecordPage(MemberPointRecordPageReqVO pageReqVO) {
@ -55,4 +63,29 @@ public class MemberPointRecordServiceImpl implements MemberPointRecordService {
return recordMapper.selectPage(userId, pageVO);
}
@Override
public void createPointRecord(Long userId, Integer point, MemberPointBizTypeEnum bizType, String bizId) {
if (bizType.isReduce() && point > 0) {
point = -point;
}
MemberUserDO user = memberUserService.getUser(userId);
Integer userPoint = ObjectUtil.defaultIfNull(user.getPoint(), 0);
// 用户变动后的积分防止扣出负数
Integer totalPoint = NumberUtil.max(userPoint + point, 0);
// 增加积分记录
MemberPointRecordDO recordDO = new MemberPointRecordDO()
.setUserId(userId)
.setBizId(bizId)
.setBizType(bizType.getType())
.setTitle(bizType.getName())
.setDescription(StrUtil.format(bizType.getDescription(), point))
.setPoint(point)
.setTotalPoint(totalPoint);
recordMapper.insert(recordDO);
// 更新用户积分
memberUserService.updateUserPoint(userId, totalPoint);
}
}

View File

@ -158,4 +158,12 @@ public interface MemberUserService {
* @return 用户数量
*/
Long getUserCountByTagId(Long tagId);
/**
* 更新用户的积分
*
* @param userId 用户ID
* @param point 积分数量
*/
void updateUserPoint(Long userId, Integer point);
}

View File

@ -254,4 +254,9 @@ public class MemberUserServiceImpl implements MemberUserService {
return memberUserMapper.selectCountByTagId(tagId);
}
@Override
public void updateUserPoint(Long userId, Integer point) {
memberUserMapper.updateById(new MemberUserDO().setId(userId).setPoint(point));
}
}