Commit 2132de9dd1e2edfa849794404f707700b0df679b
1 parent
da8b2eab
商品:完善后台商品评论功能
Showing
7 changed files
with
73 additions
and
16 deletions
yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/controller/admin/comment/ProductCommentController.java
| @@ -5,7 +5,9 @@ import cn.iocoder.yudao.framework.common.pojo.PageResult; | @@ -5,7 +5,9 @@ import cn.iocoder.yudao.framework.common.pojo.PageResult; | ||
| 5 | import cn.iocoder.yudao.module.product.controller.admin.comment.vo.*; | 5 | import cn.iocoder.yudao.module.product.controller.admin.comment.vo.*; |
| 6 | import cn.iocoder.yudao.module.product.convert.comment.ProductCommentConvert; | 6 | import cn.iocoder.yudao.module.product.convert.comment.ProductCommentConvert; |
| 7 | import cn.iocoder.yudao.module.product.dal.dataobject.comment.ProductCommentDO; | 7 | import cn.iocoder.yudao.module.product.dal.dataobject.comment.ProductCommentDO; |
| 8 | +import cn.iocoder.yudao.module.product.dal.dataobject.sku.ProductSkuDO; | ||
| 8 | import cn.iocoder.yudao.module.product.service.comment.ProductCommentService; | 9 | import cn.iocoder.yudao.module.product.service.comment.ProductCommentService; |
| 10 | +import cn.iocoder.yudao.module.product.service.sku.ProductSkuService; | ||
| 9 | import io.swagger.v3.oas.annotations.Operation; | 11 | import io.swagger.v3.oas.annotations.Operation; |
| 10 | import io.swagger.v3.oas.annotations.tags.Tag; | 12 | import io.swagger.v3.oas.annotations.tags.Tag; |
| 11 | import org.springframework.security.access.prepost.PreAuthorize; | 13 | import org.springframework.security.access.prepost.PreAuthorize; |
| @@ -14,8 +16,10 @@ import org.springframework.web.bind.annotation.*; | @@ -14,8 +16,10 @@ import org.springframework.web.bind.annotation.*; | ||
| 14 | 16 | ||
| 15 | import javax.annotation.Resource; | 17 | import javax.annotation.Resource; |
| 16 | import javax.validation.Valid; | 18 | import javax.validation.Valid; |
| 19 | +import java.util.List; | ||
| 17 | 20 | ||
| 18 | import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success; | 21 | import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success; |
| 22 | +import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet; | ||
| 19 | import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId; | 23 | import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId; |
| 20 | 24 | ||
| 21 | @Tag(name = "管理后台 - 商品评价") | 25 | @Tag(name = "管理后台 - 商品评价") |
| @@ -26,13 +30,19 @@ public class ProductCommentController { | @@ -26,13 +30,19 @@ public class ProductCommentController { | ||
| 26 | 30 | ||
| 27 | @Resource | 31 | @Resource |
| 28 | private ProductCommentService productCommentService; | 32 | private ProductCommentService productCommentService; |
| 33 | + @Resource | ||
| 34 | + private ProductSkuService productSkuService; | ||
| 29 | 35 | ||
| 30 | @GetMapping("/page") | 36 | @GetMapping("/page") |
| 31 | @Operation(summary = "获得商品评价分页") | 37 | @Operation(summary = "获得商品评价分页") |
| 32 | @PreAuthorize("@ss.hasPermission('product:comment:query')") | 38 | @PreAuthorize("@ss.hasPermission('product:comment:query')") |
| 33 | public CommonResult<PageResult<ProductCommentRespVO>> getCommentPage(@Valid ProductCommentPageReqVO pageVO) { | 39 | public CommonResult<PageResult<ProductCommentRespVO>> getCommentPage(@Valid ProductCommentPageReqVO pageVO) { |
| 34 | PageResult<ProductCommentDO> pageResult = productCommentService.getCommentPage(pageVO); | 40 | PageResult<ProductCommentDO> pageResult = productCommentService.getCommentPage(pageVO); |
| 35 | - return success(ProductCommentConvert.INSTANCE.convertPage(pageResult)); | 41 | + |
| 42 | + List<ProductSkuDO> skuList = productSkuService.getSkuList( | ||
| 43 | + convertSet(pageResult.getList(), ProductCommentDO::getSkuId)); | ||
| 44 | + | ||
| 45 | + return success(ProductCommentConvert.INSTANCE.convertPage(pageResult, skuList)); | ||
| 36 | } | 46 | } |
| 37 | 47 | ||
| 38 | @PutMapping("/update-visible") | 48 | @PutMapping("/update-visible") |
| @@ -51,7 +61,7 @@ public class ProductCommentController { | @@ -51,7 +61,7 @@ public class ProductCommentController { | ||
| 51 | return success(true); | 61 | return success(true); |
| 52 | } | 62 | } |
| 53 | 63 | ||
| 54 | - @PutMapping("/create") | 64 | + @PostMapping("/create") |
| 55 | @Operation(summary = "添加自评") | 65 | @Operation(summary = "添加自评") |
| 56 | @PreAuthorize("@ss.hasPermission('product:comment:update')") | 66 | @PreAuthorize("@ss.hasPermission('product:comment:update')") |
| 57 | public CommonResult<Boolean> createComment(@Valid @RequestBody ProductCommentCreateReqVO createReqVO) { | 67 | public CommonResult<Boolean> createComment(@Valid @RequestBody ProductCommentCreateReqVO createReqVO) { |
yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/controller/admin/comment/vo/ProductCommentBaseVO.java
| @@ -11,11 +11,9 @@ import java.util.List; | @@ -11,11 +11,9 @@ import java.util.List; | ||
| 11 | public class ProductCommentBaseVO { | 11 | public class ProductCommentBaseVO { |
| 12 | 12 | ||
| 13 | @Schema(description = "评价人", requiredMode = Schema.RequiredMode.REQUIRED, example = "16868") | 13 | @Schema(description = "评价人", requiredMode = Schema.RequiredMode.REQUIRED, example = "16868") |
| 14 | - @NotNull(message = "评价人不能为空") | ||
| 15 | private Long userId; | 14 | private Long userId; |
| 16 | 15 | ||
| 17 | @Schema(description = "评价订单项", requiredMode = Schema.RequiredMode.REQUIRED, example = "19292") | 16 | @Schema(description = "评价订单项", requiredMode = Schema.RequiredMode.REQUIRED, example = "19292") |
| 18 | - @NotNull(message = "评价订单项不能为空") | ||
| 19 | private Long orderItemId; | 17 | private Long orderItemId; |
| 20 | 18 | ||
| 21 | @Schema(description = "评价人名称", requiredMode = Schema.RequiredMode.REQUIRED, example = "小姑凉") | 19 | @Schema(description = "评价人名称", requiredMode = Schema.RequiredMode.REQUIRED, example = "小姑凉") |
yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/controller/admin/comment/vo/ProductCommentRespVO.java
| @@ -52,4 +52,6 @@ public class ProductCommentRespVO extends ProductCommentBaseVO { | @@ -52,4 +52,6 @@ public class ProductCommentRespVO extends ProductCommentBaseVO { | ||
| 52 | @NotNull(message = "商品 SPU 名称不能为空") | 52 | @NotNull(message = "商品 SPU 名称不能为空") |
| 53 | private String spuName; | 53 | private String spuName; |
| 54 | 54 | ||
| 55 | + @Schema(description = "商品 SKU 图片地址", example = "https://www.iocoder.cn/yudao.jpg") | ||
| 56 | + private String skuPicUrl; | ||
| 55 | } | 57 | } |
yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/convert/comment/ProductCommentConvert.java
| @@ -3,7 +3,6 @@ package cn.iocoder.yudao.module.product.convert.comment; | @@ -3,7 +3,6 @@ package cn.iocoder.yudao.module.product.convert.comment; | ||
| 3 | import cn.hutool.core.util.ObjectUtil; | 3 | import cn.hutool.core.util.ObjectUtil; |
| 4 | import cn.iocoder.yudao.framework.common.pojo.PageResult; | 4 | import cn.iocoder.yudao.framework.common.pojo.PageResult; |
| 5 | import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils; | 5 | import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils; |
| 6 | -import cn.iocoder.yudao.framework.common.util.collection.MapUtils; | ||
| 7 | import cn.iocoder.yudao.module.member.api.user.dto.MemberUserRespDTO; | 6 | import cn.iocoder.yudao.module.member.api.user.dto.MemberUserRespDTO; |
| 8 | import cn.iocoder.yudao.module.product.api.comment.dto.ProductCommentCreateReqDTO; | 7 | import cn.iocoder.yudao.module.product.api.comment.dto.ProductCommentCreateReqDTO; |
| 9 | import cn.iocoder.yudao.module.product.controller.admin.comment.vo.ProductCommentCreateReqVO; | 8 | import cn.iocoder.yudao.module.product.controller.admin.comment.vo.ProductCommentCreateReqVO; |
| @@ -23,6 +22,11 @@ import java.math.BigDecimal; | @@ -23,6 +22,11 @@ import java.math.BigDecimal; | ||
| 23 | import java.math.RoundingMode; | 22 | import java.math.RoundingMode; |
| 24 | import java.util.List; | 23 | import java.util.List; |
| 25 | import java.util.Map; | 24 | import java.util.Map; |
| 25 | +import java.util.Objects; | ||
| 26 | +import java.util.stream.Collectors; | ||
| 27 | + | ||
| 28 | +import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertMap; | ||
| 29 | +import static cn.iocoder.yudao.framework.common.util.collection.MapUtils.findAndThen; | ||
| 26 | 30 | ||
| 27 | /** | 31 | /** |
| 28 | * 商品评价 Convert | 32 | * 商品评价 Convert |
| @@ -60,7 +64,7 @@ public interface ProductCommentConvert { | @@ -60,7 +64,7 @@ public interface ProductCommentConvert { | ||
| 60 | item.setUserNickname(ProductCommentDO.NICKNAME_ANONYMOUS); | 64 | item.setUserNickname(ProductCommentDO.NICKNAME_ANONYMOUS); |
| 61 | } | 65 | } |
| 62 | // 设置 SKU 规格值 | 66 | // 设置 SKU 规格值 |
| 63 | - MapUtils.findAndThen(skuMap, item.getSkuId(), | 67 | + findAndThen(skuMap, item.getSkuId(), |
| 64 | sku -> item.setSkuProperties(convertList01(sku.getProperties()))); | 68 | sku -> item.setSkuProperties(convertList01(sku.getProperties()))); |
| 65 | }); | 69 | }); |
| 66 | return page; | 70 | return page; |
| @@ -101,6 +105,8 @@ public interface ProductCommentConvert { | @@ -101,6 +105,8 @@ public interface ProductCommentConvert { | ||
| 101 | return commentDO; | 105 | return commentDO; |
| 102 | } | 106 | } |
| 103 | 107 | ||
| 108 | + @Mapping(target = "visible", constant = "true") | ||
| 109 | + @Mapping(target = "replyStatus", constant = "false") | ||
| 104 | @Mapping(target = "userId", constant = "0L") | 110 | @Mapping(target = "userId", constant = "0L") |
| 105 | @Mapping(target = "orderId", constant = "0L") | 111 | @Mapping(target = "orderId", constant = "0L") |
| 106 | @Mapping(target = "orderItemId", constant = "0L") | 112 | @Mapping(target = "orderItemId", constant = "0L") |
| @@ -111,4 +117,30 @@ public interface ProductCommentConvert { | @@ -111,4 +117,30 @@ public interface ProductCommentConvert { | ||
| 111 | 117 | ||
| 112 | List<AppProductCommentRespVO> convertList02(List<ProductCommentDO> list); | 118 | List<AppProductCommentRespVO> convertList02(List<ProductCommentDO> list); |
| 113 | 119 | ||
| 120 | + default ProductCommentDO convert(ProductCommentCreateReqVO createReq, ProductSpuDO spuDO) { | ||
| 121 | + ProductCommentDO commentDO = convert(createReq); | ||
| 122 | + if (spuDO != null) { | ||
| 123 | + commentDO.setSpuId(spuDO.getId()); | ||
| 124 | + commentDO.setSpuName(spuDO.getName()); | ||
| 125 | + } | ||
| 126 | + return commentDO; | ||
| 127 | + } | ||
| 128 | + | ||
| 129 | + default PageResult<ProductCommentRespVO> convertPage(PageResult<ProductCommentDO> pageResult, List<ProductSkuDO> skuList) { | ||
| 130 | + Map<Long, ProductSkuDO> skuMap = convertMap(skuList, ProductSkuDO::getId); | ||
| 131 | + | ||
| 132 | + PageResult<ProductCommentRespVO> result = convertPage(pageResult); | ||
| 133 | + for (ProductCommentRespVO vo : result.getList()) { | ||
| 134 | + findAndThen(skuMap, vo.getSkuId(), sku -> { | ||
| 135 | + String propertyNames = sku.getProperties().stream() | ||
| 136 | + .map(ProductSkuDO.Property::getValueName) | ||
| 137 | + .filter(Objects::nonNull) | ||
| 138 | + .collect(Collectors.joining(" ")); | ||
| 139 | + | ||
| 140 | + vo.setSkuPicUrl(sku.getPicUrl()); | ||
| 141 | + vo.setSpuName(vo.getSpuName() + " " + propertyNames); | ||
| 142 | + }); | ||
| 143 | + } | ||
| 144 | + return result; | ||
| 145 | + } | ||
| 114 | } | 146 | } |
yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/dal/mysql/comment/ProductCommentMapper.java
| @@ -20,6 +20,7 @@ public interface ProductCommentMapper extends BaseMapperX<ProductCommentDO> { | @@ -20,6 +20,7 @@ public interface ProductCommentMapper extends BaseMapperX<ProductCommentDO> { | ||
| 20 | .eqIfPresent(ProductCommentDO::getOrderId, reqVO.getOrderId()) | 20 | .eqIfPresent(ProductCommentDO::getOrderId, reqVO.getOrderId()) |
| 21 | .eqIfPresent(ProductCommentDO::getSpuId, reqVO.getSpuId()) | 21 | .eqIfPresent(ProductCommentDO::getSpuId, reqVO.getSpuId()) |
| 22 | .eqIfPresent(ProductCommentDO::getScores, reqVO.getScores()) | 22 | .eqIfPresent(ProductCommentDO::getScores, reqVO.getScores()) |
| 23 | + .eqIfPresent(ProductCommentDO::getReplyStatus, reqVO.getReplyStatus()) | ||
| 23 | .betweenIfPresent(ProductCommentDO::getCreateTime, reqVO.getCreateTime()) | 24 | .betweenIfPresent(ProductCommentDO::getCreateTime, reqVO.getCreateTime()) |
| 24 | .likeIfPresent(ProductCommentDO::getSpuName, reqVO.getSpuName()) | 25 | .likeIfPresent(ProductCommentDO::getSpuName, reqVO.getSpuName()) |
| 25 | .orderByDesc(ProductCommentDO::getId)); | 26 | .orderByDesc(ProductCommentDO::getId)); |
yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/service/comment/ProductCommentServiceImpl.java
| @@ -80,23 +80,18 @@ public class ProductCommentServiceImpl implements ProductCommentService { | @@ -80,23 +80,18 @@ public class ProductCommentServiceImpl implements ProductCommentService { | ||
| 80 | @Override | 80 | @Override |
| 81 | @Transactional(rollbackFor = Exception.class) | 81 | @Transactional(rollbackFor = Exception.class) |
| 82 | public void createComment(ProductCommentCreateReqVO createReqVO) { | 82 | public void createComment(ProductCommentCreateReqVO createReqVO) { |
| 83 | - // 校验评论 | ||
| 84 | - validateComment(createReqVO.getSkuId(), createReqVO.getUserId(), createReqVO.getOrderItemId()); | 83 | + // 校验商品 |
| 84 | + ProductSpuDO spuDO = validateProduct(createReqVO.getSkuId()); | ||
| 85 | 85 | ||
| 86 | - ProductCommentDO commentDO = ProductCommentConvert.INSTANCE.convert(createReqVO); | 86 | + ProductCommentDO commentDO = ProductCommentConvert.INSTANCE.convert(createReqVO, spuDO); |
| 87 | productCommentMapper.insert(commentDO); | 87 | productCommentMapper.insert(commentDO); |
| 88 | } | 88 | } |
| 89 | 89 | ||
| 90 | @Override | 90 | @Override |
| 91 | @Transactional(rollbackFor = Exception.class) | 91 | @Transactional(rollbackFor = Exception.class) |
| 92 | public Long createComment(ProductCommentCreateReqDTO createReqDTO) { | 92 | public Long createComment(ProductCommentCreateReqDTO createReqDTO) { |
| 93 | - // 通过 sku ID 拿到 spu 相关信息 | ||
| 94 | - ProductSkuDO sku = productSkuService.getSku(createReqDTO.getSkuId()); | ||
| 95 | - if (sku == null) { | ||
| 96 | - throw exception(SKU_NOT_EXISTS); | ||
| 97 | - } | ||
| 98 | - // 校验 spu 如果存在返回详情 | ||
| 99 | - ProductSpuDO spuDO = validateSpu(sku.getSpuId()); | 93 | + // 校验商品 |
| 94 | + ProductSpuDO spuDO = validateProduct(createReqDTO.getSkuId()); | ||
| 100 | // 校验评论 | 95 | // 校验评论 |
| 101 | validateComment(spuDO.getId(), createReqDTO.getUserId(), createReqDTO.getOrderId()); | 96 | validateComment(spuDO.getId(), createReqDTO.getUserId(), createReqDTO.getOrderId()); |
| 102 | // 获取用户详细信息 | 97 | // 获取用户详细信息 |
| @@ -116,6 +111,21 @@ public class ProductCommentServiceImpl implements ProductCommentService { | @@ -116,6 +111,21 @@ public class ProductCommentServiceImpl implements ProductCommentService { | ||
| 116 | } | 111 | } |
| 117 | } | 112 | } |
| 118 | 113 | ||
| 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 | + private ProductSkuDO validateSku(Long skuId) { | ||
| 122 | + ProductSkuDO sku = productSkuService.getSku(skuId); | ||
| 123 | + if (sku == null) { | ||
| 124 | + throw exception(SKU_NOT_EXISTS); | ||
| 125 | + } | ||
| 126 | + return sku; | ||
| 127 | + } | ||
| 128 | + | ||
| 119 | private ProductSpuDO validateSpu(Long spuId) { | 129 | private ProductSpuDO validateSpu(Long spuId) { |
| 120 | ProductSpuDO spu = productSpuService.getSpu(spuId); | 130 | ProductSpuDO spu = productSpuService.getSpu(spuId); |
| 121 | if (null == spu) { | 131 | if (null == spu) { |
yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/service/sku/ProductSkuServiceImpl.java
| 1 | package cn.iocoder.yudao.module.product.service.sku; | 1 | package cn.iocoder.yudao.module.product.service.sku; |
| 2 | 2 | ||
| 3 | import cn.hutool.core.collection.CollUtil; | 3 | import cn.hutool.core.collection.CollUtil; |
| 4 | +import cn.hutool.core.collection.ListUtil; | ||
| 4 | import cn.hutool.core.util.ObjectUtil; | 5 | import cn.hutool.core.util.ObjectUtil; |
| 5 | import cn.iocoder.yudao.module.product.api.sku.dto.ProductSkuUpdateStockReqDTO; | 6 | import cn.iocoder.yudao.module.product.api.sku.dto.ProductSkuUpdateStockReqDTO; |
| 6 | import cn.iocoder.yudao.module.product.controller.admin.sku.vo.ProductSkuBaseVO; | 7 | import cn.iocoder.yudao.module.product.controller.admin.sku.vo.ProductSkuBaseVO; |
| @@ -74,6 +75,9 @@ public class ProductSkuServiceImpl implements ProductSkuService { | @@ -74,6 +75,9 @@ public class ProductSkuServiceImpl implements ProductSkuService { | ||
| 74 | 75 | ||
| 75 | @Override | 76 | @Override |
| 76 | public List<ProductSkuDO> getSkuList(Collection<Long> ids) { | 77 | public List<ProductSkuDO> getSkuList(Collection<Long> ids) { |
| 78 | + if (CollUtil.isEmpty(ids)) { | ||
| 79 | + return ListUtil.empty(); | ||
| 80 | + } | ||
| 77 | return productSkuMapper.selectBatchIds(ids); | 81 | return productSkuMapper.selectBatchIds(ids); |
| 78 | } | 82 | } |
| 79 | 83 |