From 1e2c83d90f2fa6e4c95c9541559b00eae5efa772 Mon Sep 17 00:00:00 2001 From: owen Date: Sat, 26 Aug 2023 14:40:16 +0800 Subject: [PATCH] =?UTF-8?q?mall:=20=E5=95=86=E5=93=81=E8=AF=84=E8=AE=BA?= =?UTF-8?q?=E8=A1=A8=E5=A2=9E=E5=8A=A0=E5=86=97=E4=BD=99=20SKU=20=E7=9A=84?= =?UTF-8?q?=E5=9B=BE=E7=89=87=E5=9C=B0=E5=9D=80,=20=E8=A7=84=E6=A0=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sql/mysql/product_comment.sql | 14 ++++++++ .../comment/ProductCommentController.java | 11 +----- .../comment/ProductCommentConvert.java | 34 ++++++++++++------- .../dataobject/comment/ProductCommentDO.java | 9 +++++ .../comment/ProductCommentServiceImpl.java | 25 ++++++-------- .../service/level/MemberLevelServiceImpl.java | 2 +- 6 files changed, 58 insertions(+), 37 deletions(-) create mode 100644 sql/mysql/product_comment.sql diff --git a/sql/mysql/product_comment.sql b/sql/mysql/product_comment.sql new file mode 100644 index 000000000..f567e2923 --- /dev/null +++ b/sql/mysql/product_comment.sql @@ -0,0 +1,14 @@ +-- 1.冗余 SKU 图片地址, 规格 +alter table product_comment + add column sku_pic_url varchar(256) not null comment '图片地址' after sku_id; + +alter table product_comment + add column sku_properties varchar(512) null + comment '属性数组,JSON 格式 [{propertId: , valueId: }, {propertId: , valueId: }]' after sku_pic_url; + +-- 2.修复已有数据 +update product_comment pc + join product_sku ps on pc.spu_id = ps.spu_id +set pc.sku_pic_url = ps.pic_url, + pc.sku_properties = ps.properties +where pc.sku_id is not null; \ No newline at end of file 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 d8a779aa3..f44acd4ab 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,9 +5,7 @@ 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; @@ -16,10 +14,8 @@ 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 = "管理后台 - 商品评价") @@ -30,18 +26,13 @@ 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); - // 拼接返回 - List skuList = productSkuService.getSkuList( - convertSet(pageResult.getList(), ProductCommentDO::getSkuId)); - return success(ProductCommentConvert.INSTANCE.convertPage(pageResult, skuList)); + return success(ProductCommentConvert.INSTANCE.convertPage2(pageResult)); } @PutMapping("/update-visible") 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 94edc20bf..6fac8823c 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 @@ -1,5 +1,6 @@ package cn.iocoder.yudao.module.product.convert.comment; +import cn.hutool.core.collection.CollUtil; import cn.hutool.core.util.ObjectUtil; import cn.iocoder.yudao.framework.common.pojo.PageResult; import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils; @@ -91,7 +92,7 @@ public interface ProductCommentConvert { @Mapping(target = "scores", expression = "java(convertScores(createReqDTO.getDescriptionScores(), createReqDTO.getBenefitScores()))") - default ProductCommentDO convert(ProductCommentCreateReqDTO createReqDTO, ProductSpuDO spuDO, MemberUserRespDTO user) { + default ProductCommentDO convert(ProductCommentCreateReqDTO createReqDTO, ProductSpuDO spuDO, ProductSkuDO skuDO, MemberUserRespDTO user) { ProductCommentDO commentDO = convert(createReqDTO); if (user != null) { commentDO.setUserId(user.getId()); @@ -102,6 +103,10 @@ public interface ProductCommentConvert { commentDO.setSpuId(spuDO.getId()); commentDO.setSpuName(spuDO.getName()); } + if (skuDO != null) { + commentDO.setSkuPicUrl(skuDO.getPicUrl()); + commentDO.setSkuProperties(skuDO.getProperties()); + } return commentDO; } @@ -117,27 +122,32 @@ public interface ProductCommentConvert { List convertList02(List list); - default ProductCommentDO convert(ProductCommentCreateReqVO createReq, ProductSpuDO spu) { + default ProductCommentDO convert(ProductCommentCreateReqVO createReq, ProductSpuDO spuDO, ProductSkuDO skuDO) { ProductCommentDO commentDO = convert(createReq); - if (spu != null) { - commentDO.setSpuId(spu.getId()).setSpuName(spu.getName()); + if (spuDO != null) { + commentDO.setSpuId(spuDO.getId()); + commentDO.setSpuName(spuDO.getName()); + } + if (skuDO != null) { + commentDO.setSkuPicUrl(skuDO.getPicUrl()); + commentDO.setSkuProperties(skuDO.getProperties()); } return commentDO; } - default PageResult convertPage(PageResult pageResult, - List skus) { + default PageResult convertPage2(PageResult pageResult) { + Map> propertiesMap = convertMap(pageResult.getList(), + ProductCommentDO::getId, + // 这里会有NULL异常, 需要处理一下 + comment -> CollUtil.emptyIfNull(comment.getSkuProperties())); + PageResult result = convertPage(pageResult); - // 拼接数据 - Map skuMap = convertMap(skus, ProductSkuDO::getId); for (ProductCommentRespVO vo : result.getList()) { - findAndThen(skuMap, vo.getSkuId(), sku -> { - String propertyNames = sku.getProperties().stream() + findAndThen(propertiesMap, vo.getId(), properties -> { + String propertyNames = properties.stream() .map(ProductSkuDO.Property::getValueName) .filter(Objects::nonNull) .collect(Collectors.joining(" ")); - // TODO @疯狂:要不写入评论的时候,把商品图片、商品属性,都冗余进去。因为这种东西有“快照”的需求。商品后续会编辑掉 - vo.setSkuPicUrl(sku.getPicUrl()); vo.setSpuName(vo.getSpuName() + " " + propertyNames); }); } diff --git a/yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/dal/dataobject/comment/ProductCommentDO.java b/yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/dal/dataobject/comment/ProductCommentDO.java index 366b237a2..ff4e78468 100644 --- a/yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/dal/dataobject/comment/ProductCommentDO.java +++ b/yudao-module-mall/yudao-module-product-biz/src/main/java/cn/iocoder/yudao/module/product/dal/dataobject/comment/ProductCommentDO.java @@ -86,6 +86,15 @@ public class ProductCommentDO extends BaseDO { * 关联 {@link ProductSkuDO#getId()} */ private Long skuId; + /** + * 商品 SKU 图片地址 + */ + private String skuPicUrl; + /** + * 属性数组,JSON 格式 + */ + @TableField(typeHandler = ProductSkuDO.PropertyTypeHandler.class) + private List skuProperties; /** * 是否可见 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 fe2a01a93..934946109 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 @@ -53,25 +53,29 @@ public class ProductCommentServiceImpl implements ProductCommentService { @Override public void createComment(ProductCommentCreateReqVO createReqVO) { - // 校验商品 - ProductSpuDO spu = validateSpuBySkuId(createReqVO.getSkuId()); + // 校验 SKU + ProductSkuDO skuDO = validateSku(createReqVO.getSkuId()); + // 校验 SPU + ProductSpuDO spuDO = validateSpu(skuDO.getSpuId()); // 创建评论 - ProductCommentDO comment = ProductCommentConvert.INSTANCE.convert(createReqVO, spu); + ProductCommentDO comment = ProductCommentConvert.INSTANCE.convert(createReqVO, spuDO, skuDO); productCommentMapper.insert(comment); } @Override public Long createComment(ProductCommentCreateReqDTO createReqDTO) { - // 校验商品 - ProductSpuDO spuDO = validateSpuBySkuId(createReqDTO.getSkuId()); + // 校验 SKU + ProductSkuDO skuDO = validateSku(createReqDTO.getSkuId()); + // 校验 SPU + ProductSpuDO spuDO = validateSpu(skuDO.getSpuId()); // 校验评论 validateCommentExists(createReqDTO.getUserId(), createReqDTO.getOrderId()); // 获取用户详细信息 MemberUserRespDTO user = memberUserApi.getUser(createReqDTO.getUserId()); // 创建评论 - ProductCommentDO comment = ProductCommentConvert.INSTANCE.convert(createReqDTO, spuDO, user); + ProductCommentDO comment = ProductCommentConvert.INSTANCE.convert(createReqDTO, spuDO, skuDO, user); productCommentMapper.insert(comment); return comment.getId(); } @@ -79,7 +83,7 @@ public class ProductCommentServiceImpl implements ProductCommentService { /** * 判断当前订单的当前商品用户是否评价过 * - * @param userId 用户编号 + * @param userId 用户编号 * @param orderItemId 订单项编号 */ private void validateCommentExists(Long userId, Long orderItemId) { @@ -105,13 +109,6 @@ public class ProductCommentServiceImpl implements ProductCommentService { return spu; } - private ProductSpuDO validateSpuBySkuId(Long skuId) { - // 通过 sku ID 拿到 spu 相关信息 - ProductSkuDO sku = validateSku(skuId); - // 校验 spu 如果存在返回详情 - return validateSpu(sku.getSpuId()); - } - @Override public void updateCommentVisible(ProductCommentUpdateVisibleReqVO updateReqVO) { // 校验评论是否存在 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 6d4f713dc..d42f86ec0 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 @@ -200,7 +200,7 @@ public class MemberLevelServiceImpl implements MemberLevelService { if (updateReqVO.getLevelId() == null) { // 取消用户等级时,需要扣减经验 levelRecord.setExperience(-user.getExperience()); - // TODO @疯狂:这里是不是也要设置下 setUserExperience 属性; + levelRecord.setUserExperience(0); levelRecord.setDescription("管理员取消了等级"); } else { // 复制等级配置