From 2132de9dd1e2edfa849794404f707700b0df679b Mon Sep 17 00:00:00 2001 From: owen Date: Fri, 25 Aug 2023 21:02:49 +0800 Subject: [PATCH] =?UTF-8?q?=E5=95=86=E5=93=81=EF=BC=9A=E5=AE=8C=E5=96=84?= =?UTF-8?q?=E5=90=8E=E5=8F=B0=E5=95=86=E5=93=81=E8=AF=84=E8=AE=BA=E5=8A=9F?= =?UTF-8?q?=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../comment/ProductCommentController.java | 14 ++++++-- .../comment/vo/ProductCommentBaseVO.java | 2 -- .../comment/vo/ProductCommentRespVO.java | 2 ++ .../comment/ProductCommentConvert.java | 36 +++++++++++++++++-- .../mysql/comment/ProductCommentMapper.java | 1 + .../comment/ProductCommentServiceImpl.java | 30 ++++++++++------ .../service/sku/ProductSkuServiceImpl.java | 4 +++ 7 files changed, 73 insertions(+), 16 deletions(-) diff --git a/yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/controller/admin/comment/ProductCommentController.java b/yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/controller/admin/comment/ProductCommentController.java index 91020a51c..bac915a0f 100644 --- a/yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/controller/admin/comment/ProductCommentController.java +++ b/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; import cn.iocoder.yudao.module.product.controller.admin.comment.vo.*; import cn.iocoder.yudao.module.product.convert.comment.ProductCommentConvert; import cn.iocoder.yudao.module.product.dal.dataobject.comment.ProductCommentDO; +import cn.iocoder.yudao.module.product.dal.dataobject.sku.ProductSkuDO; import cn.iocoder.yudao.module.product.service.comment.ProductCommentService; +import cn.iocoder.yudao.module.product.service.sku.ProductSkuService; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; import org.springframework.security.access.prepost.PreAuthorize; @@ -14,8 +16,10 @@ import org.springframework.web.bind.annotation.*; import javax.annotation.Resource; import javax.validation.Valid; +import java.util.List; import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success; +import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet; import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId; @Tag(name = "管理后台 - 商品评价") @@ -26,13 +30,19 @@ public class ProductCommentController { @Resource private ProductCommentService productCommentService; + @Resource + private ProductSkuService productSkuService; @GetMapping("/page") @Operation(summary = "获得商品评价分页") @PreAuthorize("@ss.hasPermission('product:comment:query')") public CommonResult> getCommentPage(@Valid ProductCommentPageReqVO pageVO) { PageResult pageResult = productCommentService.getCommentPage(pageVO); - return success(ProductCommentConvert.INSTANCE.convertPage(pageResult)); + + List skuList = productSkuService.getSkuList( + convertSet(pageResult.getList(), ProductCommentDO::getSkuId)); + + return success(ProductCommentConvert.INSTANCE.convertPage(pageResult, skuList)); } @PutMapping("/update-visible") @@ -51,7 +61,7 @@ public class ProductCommentController { return success(true); } - @PutMapping("/create") + @PostMapping("/create") @Operation(summary = "添加自评") @PreAuthorize("@ss.hasPermission('product:comment:update')") public CommonResult createComment(@Valid @RequestBody ProductCommentCreateReqVO createReqVO) { diff --git a/yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/controller/admin/comment/vo/ProductCommentBaseVO.java b/yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/controller/admin/comment/vo/ProductCommentBaseVO.java index 24d6a5456..f88f5ed68 100644 --- a/yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/controller/admin/comment/vo/ProductCommentBaseVO.java +++ b/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; public class ProductCommentBaseVO { @Schema(description = "评价人", requiredMode = Schema.RequiredMode.REQUIRED, example = "16868") - @NotNull(message = "评价人不能为空") private Long userId; @Schema(description = "评价订单项", requiredMode = Schema.RequiredMode.REQUIRED, example = "19292") - @NotNull(message = "评价订单项不能为空") private Long orderItemId; @Schema(description = "评价人名称", requiredMode = Schema.RequiredMode.REQUIRED, example = "小姑凉") diff --git a/yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/controller/admin/comment/vo/ProductCommentRespVO.java b/yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/controller/admin/comment/vo/ProductCommentRespVO.java index 9205e34cc..8e3c732a5 100644 --- a/yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/controller/admin/comment/vo/ProductCommentRespVO.java +++ b/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 { @NotNull(message = "商品 SPU 名称不能为空") private String spuName; + @Schema(description = "商品 SKU 图片地址", example = "https://www.iocoder.cn/yudao.jpg") + private String skuPicUrl; } diff --git a/yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/convert/comment/ProductCommentConvert.java b/yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/convert/comment/ProductCommentConvert.java index cabc79a7d..216212b2e 100644 --- a/yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/convert/comment/ProductCommentConvert.java +++ b/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; import cn.hutool.core.util.ObjectUtil; import cn.iocoder.yudao.framework.common.pojo.PageResult; import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils; -import cn.iocoder.yudao.framework.common.util.collection.MapUtils; import cn.iocoder.yudao.module.member.api.user.dto.MemberUserRespDTO; import cn.iocoder.yudao.module.product.api.comment.dto.ProductCommentCreateReqDTO; import cn.iocoder.yudao.module.product.controller.admin.comment.vo.ProductCommentCreateReqVO; @@ -23,6 +22,11 @@ import java.math.BigDecimal; import java.math.RoundingMode; import java.util.List; import java.util.Map; +import java.util.Objects; +import java.util.stream.Collectors; + +import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertMap; +import static cn.iocoder.yudao.framework.common.util.collection.MapUtils.findAndThen; /** * 商品评价 Convert @@ -60,7 +64,7 @@ public interface ProductCommentConvert { item.setUserNickname(ProductCommentDO.NICKNAME_ANONYMOUS); } // 设置 SKU 规格值 - MapUtils.findAndThen(skuMap, item.getSkuId(), + findAndThen(skuMap, item.getSkuId(), sku -> item.setSkuProperties(convertList01(sku.getProperties()))); }); return page; @@ -101,6 +105,8 @@ public interface ProductCommentConvert { return commentDO; } + @Mapping(target = "visible", constant = "true") + @Mapping(target = "replyStatus", constant = "false") @Mapping(target = "userId", constant = "0L") @Mapping(target = "orderId", constant = "0L") @Mapping(target = "orderItemId", constant = "0L") @@ -111,4 +117,30 @@ public interface ProductCommentConvert { List convertList02(List list); + default ProductCommentDO convert(ProductCommentCreateReqVO createReq, ProductSpuDO spuDO) { + ProductCommentDO commentDO = convert(createReq); + if (spuDO != null) { + commentDO.setSpuId(spuDO.getId()); + commentDO.setSpuName(spuDO.getName()); + } + return commentDO; + } + + default PageResult convertPage(PageResult pageResult, List skuList) { + Map skuMap = convertMap(skuList, ProductSkuDO::getId); + + PageResult result = convertPage(pageResult); + for (ProductCommentRespVO vo : result.getList()) { + findAndThen(skuMap, vo.getSkuId(), sku -> { + String propertyNames = sku.getProperties().stream() + .map(ProductSkuDO.Property::getValueName) + .filter(Objects::nonNull) + .collect(Collectors.joining(" ")); + + vo.setSkuPicUrl(sku.getPicUrl()); + vo.setSpuName(vo.getSpuName() + " " + propertyNames); + }); + } + return result; + } } diff --git a/yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/dal/mysql/comment/ProductCommentMapper.java b/yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/dal/mysql/comment/ProductCommentMapper.java index c933c4370..095cd655b 100644 --- a/yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/dal/mysql/comment/ProductCommentMapper.java +++ b/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 { .eqIfPresent(ProductCommentDO::getOrderId, reqVO.getOrderId()) .eqIfPresent(ProductCommentDO::getSpuId, reqVO.getSpuId()) .eqIfPresent(ProductCommentDO::getScores, reqVO.getScores()) + .eqIfPresent(ProductCommentDO::getReplyStatus, reqVO.getReplyStatus()) .betweenIfPresent(ProductCommentDO::getCreateTime, reqVO.getCreateTime()) .likeIfPresent(ProductCommentDO::getSpuName, reqVO.getSpuName()) .orderByDesc(ProductCommentDO::getId)); diff --git a/yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/service/comment/ProductCommentServiceImpl.java b/yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/service/comment/ProductCommentServiceImpl.java index 97a3d1562..05d5b02cd 100644 --- a/yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/service/comment/ProductCommentServiceImpl.java +++ b/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 { @Override @Transactional(rollbackFor = Exception.class) public void createComment(ProductCommentCreateReqVO createReqVO) { - // 校验评论 - validateComment(createReqVO.getSkuId(), createReqVO.getUserId(), createReqVO.getOrderItemId()); + // 校验商品 + ProductSpuDO spuDO = validateProduct(createReqVO.getSkuId()); - ProductCommentDO commentDO = ProductCommentConvert.INSTANCE.convert(createReqVO); + ProductCommentDO commentDO = ProductCommentConvert.INSTANCE.convert(createReqVO, spuDO); productCommentMapper.insert(commentDO); } @Override @Transactional(rollbackFor = Exception.class) public Long createComment(ProductCommentCreateReqDTO createReqDTO) { - // 通过 sku ID 拿到 spu 相关信息 - ProductSkuDO sku = productSkuService.getSku(createReqDTO.getSkuId()); - if (sku == null) { - throw exception(SKU_NOT_EXISTS); - } - // 校验 spu 如果存在返回详情 - ProductSpuDO spuDO = validateSpu(sku.getSpuId()); + // 校验商品 + ProductSpuDO spuDO = validateProduct(createReqDTO.getSkuId()); // 校验评论 validateComment(spuDO.getId(), createReqDTO.getUserId(), createReqDTO.getOrderId()); // 获取用户详细信息 @@ -116,6 +111,21 @@ public class ProductCommentServiceImpl implements ProductCommentService { } } + private ProductSpuDO validateProduct(Long skuId) { + // 通过 sku ID 拿到 spu 相关信息 + ProductSkuDO sku = validateSku(skuId); + // 校验 spu 如果存在返回详情 + return validateSpu(sku.getSpuId()); + } + + private ProductSkuDO validateSku(Long skuId) { + ProductSkuDO sku = productSkuService.getSku(skuId); + if (sku == null) { + throw exception(SKU_NOT_EXISTS); + } + return sku; + } + private ProductSpuDO validateSpu(Long spuId) { ProductSpuDO spu = productSpuService.getSpu(spuId); if (null == spu) { diff --git a/yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/service/sku/ProductSkuServiceImpl.java b/yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/service/sku/ProductSkuServiceImpl.java index 7c6c5030e..cd0ba6b46 100755 --- a/yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/service/sku/ProductSkuServiceImpl.java +++ b/yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/service/sku/ProductSkuServiceImpl.java @@ -1,6 +1,7 @@ package cn.iocoder.yudao.module.product.service.sku; import cn.hutool.core.collection.CollUtil; +import cn.hutool.core.collection.ListUtil; import cn.hutool.core.util.ObjectUtil; import cn.iocoder.yudao.module.product.api.sku.dto.ProductSkuUpdateStockReqDTO; import cn.iocoder.yudao.module.product.controller.admin.sku.vo.ProductSkuBaseVO; @@ -74,6 +75,9 @@ public class ProductSkuServiceImpl implements ProductSkuService { @Override public List getSkuList(Collection ids) { + if (CollUtil.isEmpty(ids)) { + return ListUtil.empty(); + } return productSkuMapper.selectBatchIds(ids); }