Browse Source

feat(shop.review.reply): S-cfc-500 落地商家回复评价 R4 使红测转绿

- Controller 新增 POST /api/product/review/reply(@PostMapping + @RequestAttribute userId + Result<String>)
- Service/Impl 新增 reply(Long reviewId, String replyText, Long operatorId):入参守卫(≤500字/非空/reviewId非空) + 评价不存在拒绝且不写库 + product.vendorId==operatorId 商家校验 + 一评一回复覆盖式写回 replyText/repliedAt/replyBy
- 验证:ProductReviewReplyContractTest 14/14 绿(原 10 红),mvn -DskipTests compile 通过
zxyj-langgraph 4 hours ago
parent
commit
37c2f7c8ad

+ 10 - 0
cfc-backend/src/main/java/com/etotem/cfc/controller/product/ProductReviewController.java

@@ -71,4 +71,14 @@ public class ProductReviewController {
                 ? ((Number) params.get("size")).intValue() : 20;
                 ? ((Number) params.get("size")).intValue() : 20;
         return Result.success(reviewService.listByProduct(productId, page, size));
         return Result.success(reviewService.listByProduct(productId, page, size));
     }
     }
+
+    @PostMapping("/reply")
+    public Result<String> reply(@RequestBody Map<String, Object> params,
+                                @RequestAttribute("userId") Long userId) {
+        Long reviewId = params != null && params.get("reviewId") != null
+                ? Long.valueOf(params.get("reviewId").toString()) : null;
+        String replyText = params != null && params.get("replyText") != null
+                ? params.get("replyText").toString() : null;
+        return Result.success(reviewService.reply(reviewId, replyText, userId));
+    }
 }
 }

+ 14 - 0
cfc-backend/src/main/java/com/etotem/cfc/service/ProductReviewService.java

@@ -63,4 +63,18 @@ public interface ProductReviewService {
      * @return Map:{total, avgRating, records}
      * @return Map:{total, avgRating, records}
      */
      */
     Map<String, Object> listByProduct(Long productId, int page, int size);
     Map<String, Object> listByProduct(Long productId, int page, int size);
+
+    /**
+     * 商家回复商品评价(R4,能力键 {@code shop.review.reply})。
+     *
+     * <p>设计 §3.1 R4:鉴权=登录商家本人({@code product.vendorId==operatorId}),入参
+     * {@code {reviewId, replyText(≤500)}};一评一回复覆盖式写回
+     * {@code replyText/repliedAt/replyBy};无重复/多次回复历史。</p>
+     *
+     * @param reviewId   评价主键(必填)
+     * @param replyText  回复文字(≤500 字)
+     * @param operatorId 登录商家 userId(须为评价所属商品的商家)
+     * @return 成功消息;校验不通过抛 {@link RuntimeException}
+     */
+    String reply(Long reviewId, String replyText, Long operatorId);
 }
 }

+ 49 - 0
cfc-backend/src/main/java/com/etotem/cfc/service/ProductReviewServiceImpl.java

@@ -4,8 +4,10 @@ import com.alibaba.fastjson.JSON;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import com.etotem.cfc.dto.ProductReviewRequest;
 import com.etotem.cfc.dto.ProductReviewRequest;
 import com.etotem.cfc.dto.ProductReviewResponse;
 import com.etotem.cfc.dto.ProductReviewResponse;
+import com.etotem.cfc.entity.Product;
 import com.etotem.cfc.entity.ProductOrder;
 import com.etotem.cfc.entity.ProductOrder;
 import com.etotem.cfc.entity.ProductReview;
 import com.etotem.cfc.entity.ProductReview;
+import com.etotem.cfc.mapper.ProductMapper;
 import com.etotem.cfc.mapper.ProductOrderMapper;
 import com.etotem.cfc.mapper.ProductOrderMapper;
 import com.etotem.cfc.mapper.ProductReviewMapper;
 import com.etotem.cfc.mapper.ProductReviewMapper;
 import org.springframework.stereotype.Service;
 import org.springframework.stereotype.Service;
@@ -43,6 +45,9 @@ public class ProductReviewServiceImpl extends ServiceImpl<ProductReviewMapper, P
     @Resource
     @Resource
     private ProductOrderMapper orderMapper;
     private ProductOrderMapper orderMapper;
 
 
+    @Resource
+    private ProductMapper productMapper;
+
     @Override
     @Override
     @Transactional
     @Transactional
     public String createReview(ProductReviewRequest request, Long reviewerId) {
     public String createReview(ProductReviewRequest request, Long reviewerId) {
@@ -198,6 +203,50 @@ public class ProductReviewServiceImpl extends ServiceImpl<ProductReviewMapper, P
         return result;
         return result;
     }
     }
 
 
+    @Override
+    @Transactional
+    public String reply(Long reviewId, String replyText, Long operatorId) {
+        // 入参守卫(顺序:先校验,再查库,避免无效请求触达 DB)
+        if (reviewId == null) {
+            throw new RuntimeException("评价ID不能为空");
+        }
+        if (replyText == null || replyText.trim().isEmpty()) {
+            throw new RuntimeException("回复内容不能为空");
+        }
+        if (replyText.length() > 500) {
+            throw new RuntimeException("商家回复不能超过500字");
+        }
+        if (operatorId == null) {
+            throw new RuntimeException("登录商家身份缺失");
+        }
+
+        // 评价存在性:不存在直接拒绝,不写库
+        ProductReview review = reviewMapper.selectById(reviewId);
+        if (review == null) {
+            throw new RuntimeException("评价不存在");
+        }
+
+        // 鉴权:评价对应商品须属于当前登录商家(product.vendorId == operatorId)
+        Long productId = review.getProductId();
+        if (productId == null) {
+            throw new RuntimeException("评价未关联商品,无法回复");
+        }
+        Product product = productMapper.selectById(productId);
+        if (product == null || product.getVendorId() == null
+                || !product.getVendorId().equals(operatorId)) {
+            throw new RuntimeException("无权回复非本人商品的评价");
+        }
+
+        // 一评一回复覆盖式更新(P2 再议多次回复)
+        Date now = new Date();
+        review.setReplyText(replyText);
+        review.setRepliedAt(now);
+        review.setReplyBy(operatorId);
+        review.setUpdatedAt(now);
+        reviewMapper.updateById(review);
+        return "回复成功";
+    }
+
     /**
     /**
      * 商品详情页评价列表(R3,能力键 {@code shop.review.list},公开只读)。
      * 商品详情页评价列表(R3,能力键 {@code shop.review.list},公开只读)。
      *
      *