|
|
@@ -4,8 +4,10 @@ import com.alibaba.fastjson.JSON;
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
import com.etotem.cfc.dto.ProductReviewRequest;
|
|
|
import com.etotem.cfc.dto.ProductReviewResponse;
|
|
|
+import com.etotem.cfc.entity.Product;
|
|
|
import com.etotem.cfc.entity.ProductOrder;
|
|
|
import com.etotem.cfc.entity.ProductReview;
|
|
|
+import com.etotem.cfc.mapper.ProductMapper;
|
|
|
import com.etotem.cfc.mapper.ProductOrderMapper;
|
|
|
import com.etotem.cfc.mapper.ProductReviewMapper;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
@@ -43,6 +45,9 @@ public class ProductReviewServiceImpl extends ServiceImpl<ProductReviewMapper, P
|
|
|
@Resource
|
|
|
private ProductOrderMapper orderMapper;
|
|
|
|
|
|
+ @Resource
|
|
|
+ private ProductMapper productMapper;
|
|
|
+
|
|
|
@Override
|
|
|
@Transactional
|
|
|
public String createReview(ProductReviewRequest request, Long reviewerId) {
|
|
|
@@ -198,6 +203,81 @@ public class ProductReviewServiceImpl extends ServiceImpl<ProductReviewMapper, P
|
|
|
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},公开只读)。
|
|
|
+ *
|
|
|
+ * <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。
|
|
|
*/
|