商品:完善后台商品评论功能

This commit is contained in:
owen 2023-08-25 21:02:49 +08:00
parent da8b2eab6d
commit 2132de9dd1
7 changed files with 73 additions and 16 deletions

View File

@ -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<PageResult<ProductCommentRespVO>> getCommentPage(@Valid ProductCommentPageReqVO pageVO) {
PageResult<ProductCommentDO> pageResult = productCommentService.getCommentPage(pageVO);
return success(ProductCommentConvert.INSTANCE.convertPage(pageResult));
List<ProductSkuDO> 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<Boolean> createComment(@Valid @RequestBody ProductCommentCreateReqVO createReqVO) {

View File

@ -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 = "小姑凉")

View File

@ -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;
}

View File

@ -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<AppProductCommentRespVO> convertList02(List<ProductCommentDO> 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<ProductCommentRespVO> convertPage(PageResult<ProductCommentDO> pageResult, List<ProductSkuDO> skuList) {
Map<Long, ProductSkuDO> skuMap = convertMap(skuList, ProductSkuDO::getId);
PageResult<ProductCommentRespVO> 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;
}
}

View File

@ -20,6 +20,7 @@ public interface ProductCommentMapper extends BaseMapperX<ProductCommentDO> {
.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));

View File

@ -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) {

View File

@ -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<ProductSkuDO> getSkuList(Collection<Long> ids) {
if (CollUtil.isEmpty(ids)) {
return ListUtil.empty();
}
return productSkuMapper.selectBatchIds(ids);
}