From d4acacec60c137aa3ab916357f176f65b406d0f8 Mon Sep 17 00:00:00 2001 From: owen Date: Fri, 25 Aug 2023 23:07:54 +0800 Subject: [PATCH] =?UTF-8?q?trade:=201.=E6=94=AF=E4=BB=98=E6=88=90=E5=8A=9F?= =?UTF-8?q?=E5=90=8E=EF=BC=8C=E5=A2=9E=E5=8A=A0=E7=94=A8=E6=88=B7=E7=A7=AF?= =?UTF-8?q?=E5=88=86=EF=BC=9B2.=E9=80=80=E6=AC=BE=E6=88=90=E5=8A=9F?= =?UTF-8?q?=E5=90=8E=EF=BC=8C=E6=89=A3=E5=87=8F=E7=94=A8=E6=88=B7=E7=A7=AF?= =?UTF-8?q?=E5=88=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../order/TradeOrderUpdateServiceImpl.java | 25 ++++++++++---- .../module/member/api/user/MemberUserApi.java | 10 ++++++ .../member/enums/ErrorCodeConstants.java | 3 +- .../enums/MemberExperienceBizTypeEnum.java | 25 ++++++++++---- .../enums/point/MemberPointBizTypeEnum.java | 23 +++++++++++-- .../member/api/user/MemberUserApiImpl.java | 17 ++++++++++ .../service/level/MemberLevelServiceImpl.java | 3 ++ .../point/MemberPointRecordService.java | 10 ++++++ .../point/MemberPointRecordServiceImpl.java | 33 +++++++++++++++++++ .../service/user/MemberUserService.java | 8 +++++ .../service/user/MemberUserServiceImpl.java | 5 +++ 11 files changed, 146 insertions(+), 16 deletions(-) diff --git a/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/service/order/TradeOrderUpdateServiceImpl.java b/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/service/order/TradeOrderUpdateServiceImpl.java index 2de6601316..a9f61f1984 100644 --- a/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/service/order/TradeOrderUpdateServiceImpl.java +++ b/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/service/order/TradeOrderUpdateServiceImpl.java @@ -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)); + } } diff --git a/yudao-module-member/yudao-module-member-api/src/main/java/cn/iocoder/yudao/module/member/api/user/MemberUserApi.java b/yudao-module-member/yudao-module-member-api/src/main/java/cn/iocoder/yudao/module/member/api/user/MemberUserApi.java index 5743493e3c..90445ffa64 100644 --- a/yudao-module-member/yudao-module-member-api/src/main/java/cn/iocoder/yudao/module/member/api/user/MemberUserApi.java +++ b/yudao-module-member/yudao-module-member-api/src/main/java/cn/iocoder/yudao/module/member/api/user/MemberUserApi.java @@ -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); } diff --git a/yudao-module-member/yudao-module-member-api/src/main/java/cn/iocoder/yudao/module/member/enums/ErrorCodeConstants.java b/yudao-module-member/yudao-module-member-api/src/main/java/cn/iocoder/yudao/module/member/enums/ErrorCodeConstants.java index 724ba0c5ed..ba283ab0ce 100644 --- a/yudao-module-member/yudao-module-member-api/src/main/java/cn/iocoder/yudao/module/member/enums/ErrorCodeConstants.java +++ b/yudao-module-member/yudao-module-member-api/src/main/java/cn/iocoder/yudao/module/member/enums/ErrorCodeConstants.java @@ -4,7 +4,7 @@ import cn.iocoder.yudao.framework.common.exception.ErrorCode; /** * Member 错误码枚举类 - * + *

* 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, "签到天数规则不存在"); diff --git a/yudao-module-member/yudao-module-member-api/src/main/java/cn/iocoder/yudao/module/member/enums/MemberExperienceBizTypeEnum.java b/yudao-module-member/yudao-module-member-api/src/main/java/cn/iocoder/yudao/module/member/enums/MemberExperienceBizTypeEnum.java index fcd0f6e7af..a287b0cdc6 100644 --- a/yudao-module-member/yudao-module-member-api/src/main/java/cn/iocoder/yudao/module/member/enums/MemberExperienceBizTypeEnum.java +++ b/yudao-module-member/yudao-module-member-api/src/main/java/cn/iocoder/yudao/module/member/enums/MemberExperienceBizTypeEnum.java @@ -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, diff --git a/yudao-module-member/yudao-module-member-api/src/main/java/cn/iocoder/yudao/module/member/enums/point/MemberPointBizTypeEnum.java b/yudao-module-member/yudao-module-member-api/src/main/java/cn/iocoder/yudao/module/member/enums/point/MemberPointBizTypeEnum.java index 65e4c6c34b..80d8fd767d 100644 --- a/yudao-module-member/yudao-module-member-api/src/main/java/cn/iocoder/yudao/module/member/enums/point/MemberPointBizTypeEnum.java +++ b/yudao-module-member/yudao-module-member-api/src/main/java/cn/iocoder/yudao/module/member/enums/point/MemberPointBizTypeEnum.java @@ -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())); + } + } diff --git a/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/api/user/MemberUserApiImpl.java b/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/api/user/MemberUserApiImpl.java index 8da857c6d7..898b7fe9c7 100644 --- a/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/api/user/MemberUserApiImpl.java +++ b/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/api/user/MemberUserApiImpl.java @@ -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); + } + } diff --git a/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/service/level/MemberLevelServiceImpl.java b/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/service/level/MemberLevelServiceImpl.java index d464655543..9169b04fdc 100644 --- a/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/service/level/MemberLevelServiceImpl.java +++ b/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/service/level/MemberLevelServiceImpl.java @@ -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); diff --git a/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/service/point/MemberPointRecordService.java b/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/service/point/MemberPointRecordService.java index 971cb72745..74e91880f2 100644 --- a/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/service/point/MemberPointRecordService.java +++ b/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/service/point/MemberPointRecordService.java @@ -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 getPointRecordPage(Long userId, PageParam pageVO); + /** + * 创建用户积分记录 + * + * @param userId 用户ID + * @param point 变动积分 + * @param bizType 业务类型 + * @param bizId 业务编号 + */ + void createPointRecord(Long userId, Integer point, MemberPointBizTypeEnum bizType, String bizId); } diff --git a/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/service/point/MemberPointRecordServiceImpl.java b/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/service/point/MemberPointRecordServiceImpl.java index 52257183da..9516981bb8 100644 --- a/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/service/point/MemberPointRecordServiceImpl.java +++ b/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/service/point/MemberPointRecordServiceImpl.java @@ -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 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); + } + } diff --git a/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/service/user/MemberUserService.java b/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/service/user/MemberUserService.java index 7a0c13380b..a1161d6686 100644 --- a/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/service/user/MemberUserService.java +++ b/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/service/user/MemberUserService.java @@ -158,4 +158,12 @@ public interface MemberUserService { * @return 用户数量 */ Long getUserCountByTagId(Long tagId); + + /** + * 更新用户的积分 + * + * @param userId 用户ID + * @param point 积分数量 + */ + void updateUserPoint(Long userId, Integer point); } diff --git a/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/service/user/MemberUserServiceImpl.java b/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/service/user/MemberUserServiceImpl.java index 152f87cad5..c5b674cc8a 100644 --- a/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/service/user/MemberUserServiceImpl.java +++ b/yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/service/user/MemberUserServiceImpl.java @@ -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)); + } + }