Browse Source

feat(shop.review.list): S-cfc-499 商品详情页评价列表接口实现使红单测转绿

- ProductReviewController 新增 POST /api/product/review/list(公开只读,无 userId 鉴权,缺省 page=1/size=20)
- ProductReviewService(Impl) 新增 listByProduct(Long,int,int),聚合 {total, avgRating, records}
- JwtInterceptor.PUBLIC_PATHS 登记 /api/product/review/list 支持匿名可读
- ProductReviewListContractTest 14/14 全绿(原 8 红 → 0 红)
Sisyphus 4 hours ago
parent
commit
ee86e837e4

+ 1 - 0
cfc-backend/src/main/java/com/etotem/cfc/config/JwtInterceptor.java

@@ -58,6 +58,7 @@ private static final String[] PUBLIC_PATHS = {
          "/api/auth/internal/",
          "/api/product/list",
          "/api/product/detail",
+         "/api/product/review/list",          // R3 shop.review.list:商品详情页评价列表,公开只读匿名可看
          "/api/special-price/info",
          "/api/product/type-list",
          "/api/product/related-products",

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

@@ -53,4 +53,22 @@ public class ProductReviewController {
         int size = params.get("size") instanceof Number ? ((Number) params.get("size")).intValue() : 10;
         return Result.success(reviewService.my(userId, page, size));
     }
+
+    /**
+     * R3 商品详情页评价列表(能力键 {@code shop.review.list},公开只读,匿名可看)。
+     *
+     * <p>设计 §3.1 R3:入参 {@code {productId, page, size}}(分页默认 page=1 / size=20,§9),
+     * 出参 {@code Result<Map>}:{@code {total, avgRating, records[]}}。
+     * 本接口不携带 {@code @RequestAttribute("userId")},路径已登记 {@code JwtInterceptor.PUBLIC_PATHS}。</p>
+     */
+    @PostMapping("/list")
+    public Result<Map<String, Object>> list(@RequestBody Map<String, Object> params) {
+        Long productId = params != null && params.get("productId") != null
+                ? Long.valueOf(params.get("productId").toString()) : null;
+        int page = params != null && params.get("page") instanceof Number
+                ? ((Number) params.get("page")).intValue() : 1;
+        int size = params != null && params.get("size") instanceof Number
+                ? ((Number) params.get("size")).intValue() : 20;
+        return Result.success(reviewService.listByProduct(productId, page, size));
+    }
 }

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

@@ -48,4 +48,19 @@ public interface ProductReviewService {
      * @return Map:{reviews[], reviewableOrders[], reviewableCount}
      */
     Map<String, Object> my(Long userId, int page, int size);
+
+    /**
+     * 商品详情页评价列表(R3,能力键 {@code shop.review.list},公开只读)。
+     *
+     * <p>设计 §3.1 R3:出参 {@code {total, avgRating, records[]}}——
+     * {@code total} / {@code avgRating} 来自 {@link com.etotem.cfc.mapper.ProductReviewMapper#statsByProduct(Long)}
+     * 聚合,{@code records} 来自 {@link com.etotem.cfc.mapper.ProductReviewMapper#listByProduct(Long, int, int)}
+     * 分页(仅展示态 status=1,按 review_at DESC 倒序)。与 {@code my} 不同,本方法不依赖登录态。</p>
+     *
+     * @param productId 商品主键
+     * @param page      页码(从 1 开始)
+     * @param size      每页条数
+     * @return Map:{total, avgRating, records}
+     */
+    Map<String, Object> listByProduct(Long productId, int page, int size);
 }

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

@@ -198,6 +198,37 @@ public class ProductReviewServiceImpl extends ServiceImpl<ProductReviewMapper, P
         return result;
     }
 
+    /**
+     * 商品详情页评价列表(R3,能力键 {@code shop.review.list},公开只读)。
+     *
+     * <p>读 {@link ProductReviewMapper#statsByProduct} 聚合 total + avgRating,
+     * 读 {@link ProductReviewMapper#listByProduct} 分页 records,组装只读视图
+     * {@code {total, avgRating, records}}。仅展示态 status=1、按 review_at DESC 倒序由 Mapper XML 保证。</p>
+     */
+    @Override
+    public Map<String, Object> listByProduct(Long productId, int page, int size) {
+        int pageNum = Math.max(page, 1);
+        int pageSize = Math.max(size, 1);
+        int offset = (pageNum - 1) * pageSize;
+
+        Map<String, Object> result = new HashMap<>();
+
+        // 1) 星级聚合:total + avgRating(Mapper XML 已限定 status=1 且 AVG(rating))
+        Map<String, Object> stats = reviewMapper.statsByProduct(productId);
+        if (stats != null) {
+            result.put("total", stats.get("total"));
+            result.put("avgRating", stats.get("avgRating"));
+        } else {
+            result.put("total", 0L);
+            result.put("avgRating", null);
+        }
+
+        // 2) 评价记录分页(含商家回复,按 review_at DESC 倒序)
+        List<ProductReview> records = reviewMapper.listByProduct(productId, offset, pageSize);
+        result.put("records", records == null ? Collections.emptyList() : records);
+        return result;
+    }
+
     /**
      * 90 天评价窗口判定:confirmTime 存在且 >= now - 90d。
      */