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

@ -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));
}
}