Commit b75d777670d6f4fdd9c1b4e381f8aa57a4219532
1 parent
f8952f8e
code review:订单评论、订单积分的变更
Showing
11 changed files
with
105 additions
and
113 deletions
yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/controller/admin/comment/ProductCommentController.java
| ... | ... | @@ -38,10 +38,9 @@ public class ProductCommentController { |
| 38 | 38 | @PreAuthorize("@ss.hasPermission('product:comment:query')") |
| 39 | 39 | public CommonResult<PageResult<ProductCommentRespVO>> getCommentPage(@Valid ProductCommentPageReqVO pageVO) { |
| 40 | 40 | PageResult<ProductCommentDO> pageResult = productCommentService.getCommentPage(pageVO); |
| 41 | - | |
| 41 | + // 拼接返回 | |
| 42 | 42 | List<ProductSkuDO> skuList = productSkuService.getSkuList( |
| 43 | 43 | convertSet(pageResult.getList(), ProductCommentDO::getSkuId)); |
| 44 | - | |
| 45 | 44 | return success(ProductCommentConvert.INSTANCE.convertPage(pageResult, skuList)); |
| 46 | 45 | } |
| 47 | 46 | ... | ... |
yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/convert/comment/ProductCommentConvert.java
| ... | ... | @@ -117,26 +117,26 @@ public interface ProductCommentConvert { |
| 117 | 117 | |
| 118 | 118 | List<AppProductCommentRespVO> convertList02(List<ProductCommentDO> list); |
| 119 | 119 | |
| 120 | - default ProductCommentDO convert(ProductCommentCreateReqVO createReq, ProductSpuDO spuDO) { | |
| 120 | + default ProductCommentDO convert(ProductCommentCreateReqVO createReq, ProductSpuDO spu) { | |
| 121 | 121 | ProductCommentDO commentDO = convert(createReq); |
| 122 | - if (spuDO != null) { | |
| 123 | - commentDO.setSpuId(spuDO.getId()); | |
| 124 | - commentDO.setSpuName(spuDO.getName()); | |
| 122 | + if (spu != null) { | |
| 123 | + commentDO.setSpuId(spu.getId()).setSpuName(spu.getName()); | |
| 125 | 124 | } |
| 126 | 125 | return commentDO; |
| 127 | 126 | } |
| 128 | 127 | |
| 129 | - default PageResult<ProductCommentRespVO> convertPage(PageResult<ProductCommentDO> pageResult, List<ProductSkuDO> skuList) { | |
| 130 | - Map<Long, ProductSkuDO> skuMap = convertMap(skuList, ProductSkuDO::getId); | |
| 131 | - | |
| 128 | + default PageResult<ProductCommentRespVO> convertPage(PageResult<ProductCommentDO> pageResult, | |
| 129 | + List<ProductSkuDO> skus) { | |
| 132 | 130 | PageResult<ProductCommentRespVO> result = convertPage(pageResult); |
| 131 | + // 拼接数据 | |
| 132 | + Map<Long, ProductSkuDO> skuMap = convertMap(skus, ProductSkuDO::getId); | |
| 133 | 133 | for (ProductCommentRespVO vo : result.getList()) { |
| 134 | 134 | findAndThen(skuMap, vo.getSkuId(), sku -> { |
| 135 | 135 | String propertyNames = sku.getProperties().stream() |
| 136 | 136 | .map(ProductSkuDO.Property::getValueName) |
| 137 | 137 | .filter(Objects::nonNull) |
| 138 | 138 | .collect(Collectors.joining(" ")); |
| 139 | - | |
| 139 | + // TODO @疯狂:要不写入评论的时候,把商品图片、商品属性,都冗余进去。因为这种东西有“快照”的需求。商品后续会编辑掉 | |
| 140 | 140 | vo.setSkuPicUrl(sku.getPicUrl()); |
| 141 | 141 | vo.setSpuName(vo.getSpuName() + " " + propertyNames); |
| 142 | 142 | }); | ... | ... |
yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/dal/mysql/comment/ProductCommentMapper.java
| 1 | 1 | package cn.iocoder.yudao.module.product.dal.mysql.comment; |
| 2 | 2 | |
| 3 | - | |
| 4 | 3 | import cn.hutool.core.util.ObjectUtil; |
| 5 | 4 | import cn.iocoder.yudao.framework.common.pojo.PageParam; |
| 6 | 5 | import cn.iocoder.yudao.framework.common.pojo.PageResult; |
| ... | ... | @@ -54,11 +53,10 @@ public interface ProductCommentMapper extends BaseMapperX<ProductCommentDO> { |
| 54 | 53 | return selectPage(reqVO, queryWrapper); |
| 55 | 54 | } |
| 56 | 55 | |
| 57 | - default ProductCommentDO selectByUserIdAndOrderItemIdAndSpuId(Long userId, Long orderItemId, Long skuId) { | |
| 56 | + default ProductCommentDO selectByUserIdAndOrderItemId(Long userId, Long orderItemId) { | |
| 58 | 57 | return selectOne(new LambdaQueryWrapperX<ProductCommentDO>() |
| 59 | 58 | .eq(ProductCommentDO::getUserId, userId) |
| 60 | - .eq(ProductCommentDO::getOrderItemId, orderItemId) | |
| 61 | - .eq(ProductCommentDO::getSpuId, skuId)); | |
| 59 | + .eq(ProductCommentDO::getOrderItemId, orderItemId)); | |
| 62 | 60 | } |
| 63 | 61 | |
| 64 | 62 | default Long selectCountBySpuId(Long spuId, Boolean visible, Integer type) { | ... | ... |
yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/service/comment/ProductCommentService.java
| ... | ... | @@ -25,12 +25,21 @@ import java.util.List; |
| 25 | 25 | public interface ProductCommentService { |
| 26 | 26 | |
| 27 | 27 | /** |
| 28 | - * 获得商品评价分页 | |
| 28 | + * 创建商品评论 | |
| 29 | + * 后台管理员创建评论使用 | |
| 29 | 30 | * |
| 30 | - * @param pageReqVO 分页查询 | |
| 31 | - * @return 商品评价分页 | |
| 31 | + * @param createReqVO 商品评价创建 Request VO 对象 | |
| 32 | 32 | */ |
| 33 | - PageResult<ProductCommentDO> getCommentPage(ProductCommentPageReqVO pageReqVO); | |
| 33 | + void createComment(ProductCommentCreateReqVO createReqVO); | |
| 34 | + | |
| 35 | + /** | |
| 36 | + * 创建评论 | |
| 37 | + * 创建商品评论 APP 端创建商品评论使用 | |
| 38 | + * | |
| 39 | + * @param createReqDTO 创建请求 dto | |
| 40 | + * @return 返回评论 id | |
| 41 | + */ | |
| 42 | + Long createComment(ProductCommentCreateReqDTO createReqDTO); | |
| 34 | 43 | |
| 35 | 44 | /** |
| 36 | 45 | * 修改评论是否可见 |
| ... | ... | @@ -43,35 +52,26 @@ public interface ProductCommentService { |
| 43 | 52 | * 商家回复 |
| 44 | 53 | * |
| 45 | 54 | * @param replyVO 商家回复 |
| 46 | - * @param loginUserId 管理后台商家登陆人 ID | |
| 55 | + * @param userId 管理后台商家登陆人 ID | |
| 47 | 56 | */ |
| 48 | - void replyComment(ProductCommentReplyReqVO replyVO, Long loginUserId); | |
| 57 | + void replyComment(ProductCommentReplyReqVO replyVO, Long userId); | |
| 49 | 58 | |
| 50 | 59 | /** |
| 51 | - * 获得商品评价分页 | |
| 60 | + * 【管理员】获得商品评价分页 | |
| 52 | 61 | * |
| 53 | - * @param pageVO 分页查询 | |
| 54 | - * @param visible 是否可见 | |
| 62 | + * @param pageReqVO 分页查询 | |
| 55 | 63 | * @return 商品评价分页 |
| 56 | 64 | */ |
| 57 | - PageResult<ProductCommentDO> getCommentPage(AppCommentPageReqVO pageVO, Boolean visible); | |
| 65 | + PageResult<ProductCommentDO> getCommentPage(ProductCommentPageReqVO pageReqVO); | |
| 58 | 66 | |
| 59 | 67 | /** |
| 60 | - * 创建商品评论 | |
| 61 | - * 后台管理员创建评论使用 | |
| 68 | + * 【会员】获得商品评价分页 | |
| 62 | 69 | * |
| 63 | - * @param createReqVO 商品评价创建 Request VO 对象 | |
| 64 | - */ | |
| 65 | - void createComment(ProductCommentCreateReqVO createReqVO); | |
| 66 | - | |
| 67 | - /** | |
| 68 | - * 创建评论 | |
| 69 | - * 创建商品评论 APP 端创建商品评论使用 | |
| 70 | - * | |
| 71 | - * @param createReqDTO 创建请求 dto | |
| 72 | - * @return 返回评论 id | |
| 70 | + * @param pageVO 分页查询 | |
| 71 | + * @param visible 是否可见 | |
| 72 | + * @return 商品评价分页 | |
| 73 | 73 | */ |
| 74 | - Long createComment(ProductCommentCreateReqDTO createReqDTO); | |
| 74 | + PageResult<ProductCommentDO> getCommentPage(AppCommentPageReqVO pageVO, Boolean visible); | |
| 75 | 75 | |
| 76 | 76 | /** |
| 77 | 77 | * 获得商品的评价统计 | ... | ... |
yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/service/comment/ProductCommentServiceImpl.java
| ... | ... | @@ -20,7 +20,6 @@ import cn.iocoder.yudao.module.product.service.sku.ProductSkuService; |
| 20 | 20 | import cn.iocoder.yudao.module.product.service.spu.ProductSpuService; |
| 21 | 21 | import org.springframework.context.annotation.Lazy; |
| 22 | 22 | import org.springframework.stereotype.Service; |
| 23 | -import org.springframework.transaction.annotation.Transactional; | |
| 24 | 23 | import org.springframework.validation.annotation.Validated; |
| 25 | 24 | |
| 26 | 25 | import javax.annotation.Resource; |
| ... | ... | @@ -53,71 +52,43 @@ public class ProductCommentServiceImpl implements ProductCommentService { |
| 53 | 52 | private MemberUserApi memberUserApi; |
| 54 | 53 | |
| 55 | 54 | @Override |
| 56 | - @Transactional(rollbackFor = Exception.class) | |
| 57 | - public void updateCommentVisible(ProductCommentUpdateVisibleReqVO updateReqVO) { | |
| 58 | - // 校验评论是否存在 | |
| 59 | - ProductCommentDO productCommentDO = validateCommentExists(updateReqVO.getId()); | |
| 60 | - productCommentDO.setVisible(updateReqVO.getVisible()); | |
| 61 | - | |
| 62 | - // 更新可见状态 | |
| 63 | - productCommentMapper.updateById(productCommentDO); | |
| 64 | - } | |
| 65 | - | |
| 66 | - @Override | |
| 67 | - @Transactional(rollbackFor = Exception.class) | |
| 68 | - public void replyComment(ProductCommentReplyReqVO replyVO, Long loginUserId) { | |
| 69 | - // 校验评论是否存在 | |
| 70 | - ProductCommentDO productCommentDO = validateCommentExists(replyVO.getId()); | |
| 71 | - productCommentDO.setReplyTime(LocalDateTime.now()); | |
| 72 | - productCommentDO.setReplyUserId(loginUserId); | |
| 73 | - productCommentDO.setReplyStatus(Boolean.TRUE); | |
| 74 | - productCommentDO.setReplyContent(replyVO.getReplyContent()); | |
| 75 | - | |
| 76 | - // 回复评论 | |
| 77 | - productCommentMapper.updateById(productCommentDO); | |
| 78 | - } | |
| 79 | - | |
| 80 | - @Override | |
| 81 | - @Transactional(rollbackFor = Exception.class) | |
| 82 | 55 | public void createComment(ProductCommentCreateReqVO createReqVO) { |
| 83 | 56 | // 校验商品 |
| 84 | - ProductSpuDO spuDO = validateProduct(createReqVO.getSkuId()); | |
| 57 | + ProductSpuDO spu = validateSpuBySkuId(createReqVO.getSkuId()); | |
| 85 | 58 | |
| 86 | - ProductCommentDO commentDO = ProductCommentConvert.INSTANCE.convert(createReqVO, spuDO); | |
| 87 | - productCommentMapper.insert(commentDO); | |
| 59 | + // 创建评论 | |
| 60 | + ProductCommentDO comment = ProductCommentConvert.INSTANCE.convert(createReqVO, spu); | |
| 61 | + productCommentMapper.insert(comment); | |
| 88 | 62 | } |
| 89 | 63 | |
| 90 | 64 | @Override |
| 91 | - @Transactional(rollbackFor = Exception.class) | |
| 92 | 65 | public Long createComment(ProductCommentCreateReqDTO createReqDTO) { |
| 93 | 66 | // 校验商品 |
| 94 | - ProductSpuDO spuDO = validateProduct(createReqDTO.getSkuId()); | |
| 67 | + ProductSpuDO spuDO = validateSpuBySkuId(createReqDTO.getSkuId()); | |
| 95 | 68 | // 校验评论 |
| 96 | - validateComment(spuDO.getId(), createReqDTO.getUserId(), createReqDTO.getOrderId()); | |
| 69 | + validateCommentExists(createReqDTO.getUserId(), createReqDTO.getOrderId()); | |
| 97 | 70 | // 获取用户详细信息 |
| 98 | 71 | MemberUserRespDTO user = memberUserApi.getUser(createReqDTO.getUserId()); |
| 99 | 72 | |
| 100 | 73 | // 创建评论 |
| 101 | - ProductCommentDO commentDO = ProductCommentConvert.INSTANCE.convert(createReqDTO, spuDO, user); | |
| 102 | - productCommentMapper.insert(commentDO); | |
| 103 | - return commentDO.getId(); | |
| 74 | + ProductCommentDO comment = ProductCommentConvert.INSTANCE.convert(createReqDTO, spuDO, user); | |
| 75 | + productCommentMapper.insert(comment); | |
| 76 | + return comment.getId(); | |
| 104 | 77 | } |
| 105 | 78 | |
| 106 | - private void validateComment(Long skuId, Long userId, Long orderItemId) { | |
| 107 | - // 判断当前订单的当前商品用户是否评价过 | |
| 108 | - ProductCommentDO exist = productCommentMapper.selectByUserIdAndOrderItemIdAndSpuId(userId, orderItemId, skuId); | |
| 109 | - if (null != exist) { | |
| 79 | + /** | |
| 80 | + * 判断当前订单的当前商品用户是否评价过 | |
| 81 | + * | |
| 82 | + * @param userId 用户编号 | |
| 83 | + * @param orderItemId 订单项编号 | |
| 84 | + */ | |
| 85 | + private void validateCommentExists(Long userId, Long orderItemId) { | |
| 86 | + ProductCommentDO exist = productCommentMapper.selectByUserIdAndOrderItemId(userId, orderItemId); | |
| 87 | + if (exist != null) { | |
| 110 | 88 | throw exception(COMMENT_ORDER_EXISTS); |
| 111 | 89 | } |
| 112 | 90 | } |
| 113 | 91 | |
| 114 | - private ProductSpuDO validateProduct(Long skuId) { | |
| 115 | - // 通过 sku ID 拿到 spu 相关信息 | |
| 116 | - ProductSkuDO sku = validateSku(skuId); | |
| 117 | - // 校验 spu 如果存在返回详情 | |
| 118 | - return validateSpu(sku.getSpuId()); | |
| 119 | - } | |
| 120 | - | |
| 121 | 92 | private ProductSkuDO validateSku(Long skuId) { |
| 122 | 93 | ProductSkuDO sku = productSkuService.getSku(skuId); |
| 123 | 94 | if (sku == null) { |
| ... | ... | @@ -134,6 +105,33 @@ public class ProductCommentServiceImpl implements ProductCommentService { |
| 134 | 105 | return spu; |
| 135 | 106 | } |
| 136 | 107 | |
| 108 | + private ProductSpuDO validateSpuBySkuId(Long skuId) { | |
| 109 | + // 通过 sku ID 拿到 spu 相关信息 | |
| 110 | + ProductSkuDO sku = validateSku(skuId); | |
| 111 | + // 校验 spu 如果存在返回详情 | |
| 112 | + return validateSpu(sku.getSpuId()); | |
| 113 | + } | |
| 114 | + | |
| 115 | + @Override | |
| 116 | + public void updateCommentVisible(ProductCommentUpdateVisibleReqVO updateReqVO) { | |
| 117 | + // 校验评论是否存在 | |
| 118 | + validateCommentExists(updateReqVO.getId()); | |
| 119 | + | |
| 120 | + // 更新可见状态 | |
| 121 | + productCommentMapper.updateById(new ProductCommentDO().setId(updateReqVO.getId()) | |
| 122 | + .setVisible(true)); | |
| 123 | + } | |
| 124 | + | |
| 125 | + @Override | |
| 126 | + public void replyComment(ProductCommentReplyReqVO replyVO, Long userId) { | |
| 127 | + // 校验评论是否存在 | |
| 128 | + validateCommentExists(replyVO.getId()); | |
| 129 | + // 回复评论 | |
| 130 | + productCommentMapper.updateById(new ProductCommentDO().setId(replyVO.getId()) | |
| 131 | + .setReplyTime(LocalDateTime.now()).setReplyUserId(userId) | |
| 132 | + .setReplyStatus(Boolean.TRUE).setReplyContent(replyVO.getReplyContent())); | |
| 133 | + } | |
| 134 | + | |
| 137 | 135 | private ProductCommentDO validateCommentExists(Long id) { |
| 138 | 136 | ProductCommentDO productComment = productCommentMapper.selectById(id); |
| 139 | 137 | if (productComment == null) { | ... | ... |
yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/service/order/TradeOrderUpdateServiceImpl.java
| ... | ... | @@ -667,6 +667,7 @@ public class TradeOrderUpdateServiceImpl implements TradeOrderUpdateService { |
| 667 | 667 | TradeOrderItemAfterSaleStatusEnum.SUCCESS.getStatus())); |
| 668 | 668 | } |
| 669 | 669 | |
| 670 | + // TODO @疯狂:直接 this 调用,async 不生效哈。全局搜下 getSelf(); | |
| 670 | 671 | @Async |
| 671 | 672 | protected void addUserExperienceAsync(Long userId, Integer payPrice, Long orderId) { |
| 672 | 673 | int bizType = MemberExperienceBizTypeEnum.ORDER.getType(); | ... | ... |
yudao-module-member/yudao-module-member-api/src/main/java/cn/iocoder/yudao/module/member/api/user/MemberUserApi.java
| ... | ... | @@ -58,6 +58,7 @@ public interface MemberUserApi { |
| 58 | 58 | */ |
| 59 | 59 | MemberUserRespDTO getUserByMobile(String mobile); |
| 60 | 60 | |
| 61 | + // TODO @疯狂:是不是新的类,MemberPointApi? | |
| 61 | 62 | /** |
| 62 | 63 | * 增加用户积分 |
| 63 | 64 | * |
| ... | ... | @@ -67,4 +68,5 @@ public interface MemberUserApi { |
| 67 | 68 | * @param bizId 业务编号 |
| 68 | 69 | */ |
| 69 | 70 | void addPoint(Long userId, Integer point, Integer bizType, String bizId); |
| 71 | + | |
| 70 | 72 | } | ... | ... |
yudao-module-member/yudao-module-member-api/src/main/java/cn/iocoder/yudao/module/member/enums/point/MemberPointBizTypeEnum.java
| ... | ... | @@ -32,6 +32,7 @@ public enum MemberPointBizTypeEnum implements IntArrayValuable { |
| 32 | 32 | * 描述 |
| 33 | 33 | */ |
| 34 | 34 | private final String description; |
| 35 | + // TODO @疯狂:改成 add 会好点。一个是属性我们尽量不要 isXXX;另外尽量正向思维,不取反; | |
| 35 | 36 | /** |
| 36 | 37 | * 是否为扣减积分 |
| 37 | 38 | */ |
| ... | ... | @@ -42,7 +43,6 @@ public enum MemberPointBizTypeEnum implements IntArrayValuable { |
| 42 | 43 | return new int[0]; |
| 43 | 44 | } |
| 44 | 45 | |
| 45 | - | |
| 46 | 46 | public static MemberPointBizTypeEnum getByType(Integer type) { |
| 47 | 47 | return EnumUtil.getBy(MemberPointBizTypeEnum.class, |
| 48 | 48 | e -> Objects.equals(type, e.getType())); | ... | ... |
yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/service/level/MemberLevelServiceImpl.java
| ... | ... | @@ -99,7 +99,6 @@ public class MemberLevelServiceImpl implements MemberLevelService { |
| 99 | 99 | if (ObjUtil.notEqual(levelDO.getName(), name)) { |
| 100 | 100 | continue; |
| 101 | 101 | } |
| 102 | - | |
| 103 | 102 | if (id == null || !id.equals(levelDO.getId())) { |
| 104 | 103 | throw exception(LEVEL_NAME_EXISTS, levelDO.getName()); |
| 105 | 104 | } |
| ... | ... | @@ -151,7 +150,6 @@ public class MemberLevelServiceImpl implements MemberLevelService { |
| 151 | 150 | validateExperienceOutRange(list, id, level, experience); |
| 152 | 151 | } |
| 153 | 152 | |
| 154 | - // TODO 有 Service 提供接口哈,不直接调用对方的 memberUserMapper | |
| 155 | 153 | @VisibleForTesting |
| 156 | 154 | void validateLevelHasUser(Long id) { |
| 157 | 155 | Long count = memberUserService.getUserCountByLevelId(id); |
| ... | ... | @@ -195,37 +193,36 @@ public class MemberLevelServiceImpl implements MemberLevelService { |
| 195 | 193 | return; |
| 196 | 194 | } |
| 197 | 195 | |
| 198 | - // 记录等级变动 | |
| 196 | + // 1. 记录等级变动 | |
| 199 | 197 | MemberLevelRecordDO levelRecord = new MemberLevelRecordDO() |
| 200 | - .setUserId(user.getId()) | |
| 201 | - .setRemark(updateReqVO.getReason()); | |
| 198 | + .setUserId(user.getId()).setRemark(updateReqVO.getReason()); | |
| 202 | 199 | MemberLevelDO memberLevel = null; |
| 203 | 200 | if (updateReqVO.getLevelId() == null) { |
| 204 | - // 取消用户等级时,为扣减经验 | |
| 201 | + // 取消用户等级时,需要扣减经验 | |
| 205 | 202 | levelRecord.setExperience(-user.getExperience()); |
| 203 | + // TODO @疯狂:这里是不是也要设置下 setUserExperience 属性; | |
| 206 | 204 | levelRecord.setDescription("管理员取消了等级"); |
| 207 | 205 | } else { |
| 208 | - memberLevel = validateLevelExists(updateReqVO.getLevelId()); | |
| 209 | 206 | // 复制等级配置 |
| 207 | + memberLevel = validateLevelExists(updateReqVO.getLevelId()); | |
| 210 | 208 | MemberLevelRecordConvert.INSTANCE.copyTo(memberLevel, levelRecord); |
| 211 | 209 | // 变动经验值 = 等级的升级经验 - 会员当前的经验;正数为增加经验,负数为扣减经验 |
| 212 | 210 | levelRecord.setExperience(memberLevel.getExperience() - user.getExperience()); |
| 213 | - // 会员当前的经验 = 等级的升级经验 | |
| 214 | - levelRecord.setUserExperience(memberLevel.getExperience()); | |
| 211 | + levelRecord.setUserExperience(memberLevel.getExperience()); // 会员当前的经验 = 等级的升级经验 | |
| 215 | 212 | levelRecord.setDescription("管理员调整为:" + memberLevel.getName()); |
| 216 | 213 | } |
| 217 | 214 | memberLevelRecordService.createLevelRecord(levelRecord); |
| 218 | 215 | |
| 219 | - // 记录会员经验变动 | |
| 216 | + // 2. 记录会员经验变动 | |
| 220 | 217 | memberExperienceRecordService.createExperienceRecord(user.getId(), |
| 221 | 218 | levelRecord.getExperience(), levelRecord.getUserExperience(), |
| 222 | 219 | MemberExperienceBizTypeEnum.ADMIN, String.valueOf(MemberExperienceBizTypeEnum.ADMIN.getType())); |
| 223 | 220 | |
| 224 | - // 更新会员表上的等级编号、经验值 | |
| 221 | + // 3. 更新会员表上的等级编号、经验值 | |
| 225 | 222 | memberUserService.updateUserLevel(user.getId(), updateReqVO.getLevelId(), |
| 226 | 223 | levelRecord.getUserExperience()); |
| 227 | 224 | |
| 228 | - // 给会员发送等级变动消息 | |
| 225 | + // 4. 给会员发送等级变动消息 | |
| 229 | 226 | notifyMemberLevelChange(user.getId(), memberLevel); |
| 230 | 227 | } |
| 231 | 228 | |
| ... | ... | @@ -239,31 +236,27 @@ public class MemberLevelServiceImpl implements MemberLevelService { |
| 239 | 236 | experience = -experience; |
| 240 | 237 | } |
| 241 | 238 | |
| 239 | + // 1. 创建经验记录 | |
| 242 | 240 | MemberUserDO user = memberUserService.getUser(userId); |
| 243 | - | |
| 244 | - int userExperience = NumberUtil.max(user.getExperience() + experience, 0); | |
| 241 | + int userExperience = NumberUtil.max(user.getExperience() + experience, 0); // 防止扣出负数 | |
| 245 | 242 | MemberLevelRecordDO levelRecord = new MemberLevelRecordDO() |
| 246 | 243 | .setUserId(user.getId()) |
| 247 | 244 | .setExperience(experience) |
| 248 | - // 防止扣出负数 | |
| 249 | 245 | .setUserExperience(userExperience); |
| 250 | - | |
| 251 | - // 创建经验记录 | |
| 252 | 246 | memberExperienceRecordService.createExperienceRecord(userId, experience, userExperience, |
| 253 | 247 | bizType, bizId); |
| 254 | 248 | |
| 255 | - // 计算会员等级 | |
| 249 | + // 2.1 保存等级变更记录 | |
| 256 | 250 | MemberLevelDO newLevel = calculateNewLevel(user, userExperience); |
| 257 | 251 | if (newLevel != null) { |
| 258 | - // 复制等级配置 | |
| 259 | 252 | MemberLevelRecordConvert.INSTANCE.copyTo(newLevel, levelRecord); |
| 260 | - // 保存等级变更记录 | |
| 261 | 253 | memberLevelRecordService.createLevelRecord(levelRecord); |
| 262 | - // 给会员发送等级变动消息 | |
| 254 | + | |
| 255 | + // 2.2 给会员发送等级变动消息 | |
| 263 | 256 | notifyMemberLevelChange(userId, newLevel); |
| 264 | 257 | } |
| 265 | 258 | |
| 266 | - // 更新会员表上的等级编号、经验值 | |
| 259 | + // 3. 更新会员表上的等级编号、经验值 | |
| 267 | 260 | memberUserService.updateUserLevel(user.getId(), levelRecord.getLevelId(), userExperience); |
| 268 | 261 | } |
| 269 | 262 | |
| ... | ... | @@ -301,4 +294,5 @@ public class MemberLevelServiceImpl implements MemberLevelService { |
| 301 | 294 | private void notifyMemberLevelChange(Long userId, MemberLevelDO level) { |
| 302 | 295 | //todo: 给会员发消息 |
| 303 | 296 | } |
| 297 | + | |
| 304 | 298 | } | ... | ... |
yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/service/point/MemberPointRecordServiceImpl.java
| ... | ... | @@ -72,22 +72,22 @@ public class MemberPointRecordServiceImpl implements MemberPointRecordService { |
| 72 | 72 | public void createPointRecord(Long userId, Integer point, MemberPointBizTypeEnum bizType, String bizId) { |
| 73 | 73 | MemberPointConfigDO pointConfig = memberPointConfigService.getPointConfig(); |
| 74 | 74 | if (pointConfig == null || pointConfig.getTradeGivePoint() == null) { |
| 75 | - log.warn("增加积分失败:积分配置”1 元赠送多少分“未设置, userId={}, point={}, bizType={}, bizId={}", | |
| 75 | + log.error("[createPointRecord][增加积分失败:tradeGivePoint 未配置,userId({}) point({}) bizType({}) bizId({})]", | |
| 76 | 76 | userId, point, bizType.getType(), bizId); |
| 77 | 77 | return; |
| 78 | 78 | } |
| 79 | 79 | |
| 80 | - // 根据配置的比例,换算实际的积分 | |
| 80 | + // 1. 根据配置的比例,换算实际的积分 | |
| 81 | 81 | point = point * pointConfig.getTradeGivePoint(); |
| 82 | 82 | if (bizType.isReduce() && point > 0) { |
| 83 | 83 | point = -point; |
| 84 | 84 | } |
| 85 | 85 | |
| 86 | + // 2. 增加积分记录 | |
| 86 | 87 | MemberUserDO user = memberUserService.getUser(userId); |
| 87 | 88 | Integer userPoint = ObjectUtil.defaultIfNull(user.getPoint(), 0); |
| 88 | - // 用户变动后的积分,防止扣出负数 | |
| 89 | + // 用户变动后的积分,防止扣出负数 TODO 疯狂:积分是不是允许扣到负数。因为它是跟有钱有关的东西,不能让商家出现资金损失 | |
| 89 | 90 | Integer totalPoint = NumberUtil.max(userPoint + point, 0); |
| 90 | - // 增加积分记录 | |
| 91 | 91 | MemberPointRecordDO recordDO = new MemberPointRecordDO() |
| 92 | 92 | .setUserId(userId) |
| 93 | 93 | .setBizId(bizId) |
| ... | ... | @@ -98,7 +98,7 @@ public class MemberPointRecordServiceImpl implements MemberPointRecordService { |
| 98 | 98 | .setTotalPoint(totalPoint); |
| 99 | 99 | recordMapper.insert(recordDO); |
| 100 | 100 | |
| 101 | - // 更新用户积分 | |
| 101 | + // 3. 更新用户积分 | |
| 102 | 102 | memberUserService.updateUserPoint(userId, totalPoint); |
| 103 | 103 | } |
| 104 | 104 | ... | ... |
yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/service/user/MemberUserService.java