功能:完善评论测试与优惠券最优选择

This commit is contained in:
xiaowang
2026-05-16 14:28:42 +08:00
parent 1f05e0ab44
commit 4563dd2538
31 changed files with 1130 additions and 581 deletions
@@ -32,6 +32,8 @@ public class WebMvcConfig implements WebMvcConfigurer {
"/user/reset-password-by-phone",
"/product/get/**",
"/product/list/**",
"/comment/list/product/**",
"/api/comment/list/product/**",
"/shop/get/**",
"/shop/list/public",
"/shop/list/simple",
@@ -0,0 +1,49 @@
package com.xiaowang.yuminailcore.controller;
import com.xiaowang.yuminailcore.annotation.RequirePermission;
import com.xiaowang.yuminailcore.common.Result;
import com.xiaowang.yuminailcore.entity.UserDO;
import com.xiaowang.yuminailcore.service.CommentService;
import com.xiaowang.yuminailcore.support.AdminScopeHelper;
import com.xiaowang.yuminailcore.vo.CommentVO;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
@RestController
@RequestMapping("/admin/comment")
@RequiredArgsConstructor
public class AdminCommentController {
private final CommentService commentService;
private final AdminScopeHelper adminScopeHelper;
@GetMapping("/list")
@RequirePermission("comment:view")
public Result<List<CommentVO>> listComments(
HttpServletRequest request,
@RequestParam(required = false) Long productId,
@RequestParam(required = false) Long userId,
@RequestParam(required = false) Long shopId,
@RequestParam(required = false) String keyword
) {
UserDO currentUser = adminScopeHelper.getCurrentUser(request);
Long scopedShopId = adminScopeHelper.resolveShopScope(currentUser, shopId);
return Result.success(commentService.listAdminComments(productId, userId, scopedShopId, keyword));
}
@DeleteMapping("/delete/{id}")
@RequirePermission("comment:delete")
public Result<Boolean> deleteComment(HttpServletRequest request, @PathVariable Long id) {
UserDO currentUser = adminScopeHelper.getCurrentUser(request);
Long managedShopId = adminScopeHelper.resolveShopScope(currentUser, null);
return Result.success(commentService.adminDeleteComment(id, managedShopId));
}
}
@@ -7,6 +7,7 @@ import com.xiaowang.yuminailcore.service.CommentService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
/**
@@ -30,8 +31,9 @@ public class CommentController {
* @return 评论ID
*/
@PostMapping("/create")
public Result<Long> createComment(@RequestBody CommentDTO commentDTO) {
Long id = commentService.createComment(commentDTO);
public Result<Long> createComment(HttpServletRequest request, @RequestBody CommentDTO commentDTO) {
Long userId = (Long) request.getAttribute("userId");
Long id = commentService.createComment(userId, commentDTO);
return Result.success(id);
}
@@ -43,8 +45,9 @@ public class CommentController {
* @return 是否更新成功
*/
@PutMapping("/update/{id}")
public Result<Boolean> updateComment(@PathVariable Long id, @RequestBody CommentDTO commentDTO) {
boolean success = commentService.updateComment(id, commentDTO);
public Result<Boolean> updateComment(HttpServletRequest request, @PathVariable Long id, @RequestBody CommentDTO commentDTO) {
Long userId = (Long) request.getAttribute("userId");
boolean success = commentService.updateComment(id, userId, commentDTO);
return Result.success(success);
}
@@ -55,8 +58,9 @@ public class CommentController {
* @return 是否删除成功
*/
@DeleteMapping("/delete/{id}")
public Result<Boolean> deleteComment(@PathVariable Long id) {
boolean success = commentService.deleteComment(id);
public Result<Boolean> deleteComment(HttpServletRequest request, @PathVariable Long id) {
Long userId = (Long) request.getAttribute("userId");
boolean success = commentService.deleteComment(id, userId);
return Result.success(success);
}
@@ -24,6 +24,8 @@ public interface CommentService extends IService<CommentDO> {
*/
Long createComment(CommentDTO commentDTO);
Long createComment(Long currentUserId, CommentDTO commentDTO);
/**
* 更新评论
*
@@ -33,6 +35,8 @@ public interface CommentService extends IService<CommentDO> {
*/
boolean updateComment(Long id, CommentDTO commentDTO);
boolean updateComment(Long id, Long currentUserId, CommentDTO commentDTO);
/**
* 删除评论
*
@@ -41,6 +45,12 @@ public interface CommentService extends IService<CommentDO> {
*/
boolean deleteComment(Long id);
boolean deleteComment(Long id, Long currentUserId);
boolean adminDeleteComment(Long id, Long managedShopId);
List<CommentVO> listAdminComments(Long productId, Long userId, Long shopId, String keyword);
/**
* 根据ID查询评论
*
@@ -4,13 +4,25 @@ import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaowang.yuminailcore.dto.CommentDTO;
import com.xiaowang.yuminailcore.entity.CommentDO;
import com.xiaowang.yuminailcore.entity.ProductDO;
import com.xiaowang.yuminailcore.entity.ShopDO;
import com.xiaowang.yuminailcore.entity.UserDO;
import com.xiaowang.yuminailcore.mapper.CommentMapper;
import com.xiaowang.yuminailcore.mapper.ProductMapper;
import com.xiaowang.yuminailcore.mapper.ShopMapper;
import com.xiaowang.yuminailcore.mapper.UserMapper;
import com.xiaowang.yuminailcore.service.CommentService;
import com.xiaowang.yuminailcore.vo.CommentVO;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
@@ -25,15 +37,58 @@ import java.util.stream.Collectors;
public class CommentServiceImpl extends ServiceImpl<CommentMapper, CommentDO> implements CommentService {
private final CommentMapper commentMapper;
private final UserMapper userMapper;
private final ShopMapper shopMapper;
private final ProductMapper productMapper;
@Override
public Long createComment(CommentDTO commentDTO) {
if (commentDTO == null || commentDTO.getUserId() == null) {
throw new RuntimeException("请先登录后再评论");
}
return createComment(commentDTO.getUserId(), commentDTO);
}
@Override
public Long createComment(Long currentUserId, CommentDTO commentDTO) {
if (currentUserId == null) {
throw new RuntimeException("请先登录后再评论");
}
if (commentDTO == null) {
throw new RuntimeException("评论内容不能为空");
}
String content = sanitizeContent(commentDTO.getCommentContent());
Long productId = normalizeId(commentDTO.getProductId());
Long parentCommentId = normalizeId(commentDTO.getParentCommentId());
if (productId == null && parentCommentId == null) {
throw new RuntimeException("请选择要评论的作品");
}
CommentDO parentComment = null;
if (parentCommentId != null) {
parentComment = commentMapper.selectById(parentCommentId);
if (parentComment == null || Integer.valueOf(1).equals(parentComment.getIsDelete())) {
throw new RuntimeException("回复的评论不存在");
}
if (productId == null) {
productId = parentComment.getProductId();
} else if (parentComment.getProductId() != null && !productId.equals(parentComment.getProductId())) {
throw new RuntimeException("回复内容与作品不匹配");
}
}
Long shopId = normalizeId(commentDTO.getShopId());
if (shopId == null && productId != null) {
ProductDO product = productMapper.selectById(productId);
if (product != null && !Integer.valueOf(1).equals(product.getIsDelete())) {
shopId = product.getShopId();
}
}
CommentDO commentDO = new CommentDO();
commentDO.setUserId(commentDTO.getUserId());
commentDO.setShopId(commentDTO.getShopId());
commentDO.setProductId(commentDTO.getProductId());
commentDO.setCommentContent(commentDTO.getCommentContent());
commentDO.setParentCommentId(commentDTO.getParentCommentId());
commentDO.setUserId(currentUserId);
commentDO.setShopId(shopId);
commentDO.setProductId(productId);
commentDO.setCommentContent(content);
commentDO.setParentCommentId(parentCommentId);
commentDO.setLikeCount(0);
commentDO.setIsDelete(0);
commentMapper.insert(commentDO);
return commentDO.getId();
@@ -45,11 +100,8 @@ public class CommentServiceImpl extends ServiceImpl<CommentMapper, CommentDO> im
if (commentDO == null) {
return false;
}
commentDO.setUserId(commentDTO.getUserId());
commentDO.setShopId(commentDTO.getShopId());
commentDO.setProductId(commentDTO.getProductId());
commentDO.setCommentContent(commentDTO.getCommentContent());
commentDO.setParentCommentId(commentDTO.getParentCommentId());
commentDO.setCommentContent(sanitizeContent(commentDTO.getCommentContent()));
commentDO.setUpdateTime(LocalDateTime.now());
return commentMapper.updateById(commentDO) > 0;
}
@@ -63,10 +115,52 @@ public class CommentServiceImpl extends ServiceImpl<CommentMapper, CommentDO> im
return commentMapper.updateById(commentDO) > 0;
}
@Override
public boolean updateComment(Long id, Long currentUserId, CommentDTO commentDTO) {
CommentDO commentDO = commentMapper.selectById(id);
if (commentDO == null || Integer.valueOf(1).equals(commentDO.getIsDelete())) {
return false;
}
if (currentUserId == null || !currentUserId.equals(commentDO.getUserId())) {
throw new RuntimeException("只能修改自己的评论");
}
commentDO.setCommentContent(sanitizeContent(commentDTO.getCommentContent()));
commentDO.setUpdateTime(LocalDateTime.now());
return commentMapper.updateById(commentDO) > 0;
}
@Override
public boolean deleteComment(Long id, Long currentUserId) {
CommentDO commentDO = commentMapper.selectById(id);
if (commentDO == null || Integer.valueOf(1).equals(commentDO.getIsDelete())) {
return false;
}
if (currentUserId == null || !currentUserId.equals(commentDO.getUserId())) {
throw new RuntimeException("只能删除自己的评论");
}
commentDO.setIsDelete(1);
commentDO.setUpdateTime(LocalDateTime.now());
return commentMapper.updateById(commentDO) > 0;
}
@Override
public boolean adminDeleteComment(Long id, Long managedShopId) {
CommentDO commentDO = commentMapper.selectById(id);
if (commentDO == null || Integer.valueOf(1).equals(commentDO.getIsDelete())) {
return false;
}
if (managedShopId != null && !managedShopId.equals(commentDO.getShopId())) {
throw new RuntimeException("无权删除其他门店评论");
}
commentDO.setIsDelete(1);
commentDO.setUpdateTime(LocalDateTime.now());
return commentMapper.updateById(commentDO) > 0;
}
@Override
public CommentVO getCommentById(Long id) {
CommentDO commentDO = commentMapper.selectById(id);
if (commentDO == null) {
if (commentDO == null || Integer.valueOf(1).equals(commentDO.getIsDelete())) {
return null;
}
return convertToVO(commentDO);
@@ -74,10 +168,7 @@ public class CommentServiceImpl extends ServiceImpl<CommentMapper, CommentDO> im
@Override
public List<CommentVO> listAllComments() {
List<CommentDO> commentDOs = commentMapper.selectList(null);
return commentDOs.stream()
.map(this::convertToVO)
.collect(Collectors.toList());
return toCommentVOList(activeQuery().orderByDesc(CommentDO::getCreateTime));
}
@Override
@@ -85,6 +176,8 @@ public class CommentServiceImpl extends ServiceImpl<CommentMapper, CommentDO> im
List<CommentDO> commentDOs = commentMapper.selectList(
Wrappers.lambdaQuery(CommentDO.class)
.eq(CommentDO::getUserId, userId)
.eq(CommentDO::getIsDelete, 0)
.orderByDesc(CommentDO::getCreateTime)
);
return commentDOs.stream()
.map(this::convertToVO)
@@ -96,6 +189,8 @@ public class CommentServiceImpl extends ServiceImpl<CommentMapper, CommentDO> im
List<CommentDO> commentDOs = commentMapper.selectList(
Wrappers.lambdaQuery(CommentDO.class)
.eq(CommentDO::getShopId, shopId)
.eq(CommentDO::getIsDelete, 0)
.orderByDesc(CommentDO::getCreateTime)
);
return commentDOs.stream()
.map(this::convertToVO)
@@ -107,10 +202,10 @@ public class CommentServiceImpl extends ServiceImpl<CommentMapper, CommentDO> im
List<CommentDO> commentDOs = commentMapper.selectList(
Wrappers.lambdaQuery(CommentDO.class)
.eq(CommentDO::getProductId, productId)
.eq(CommentDO::getIsDelete, 0)
.orderByDesc(CommentDO::getCreateTime)
);
return commentDOs.stream()
.map(this::convertToVO)
.collect(Collectors.toList());
return buildTree(commentDOs);
}
@Override
@@ -118,12 +213,28 @@ public class CommentServiceImpl extends ServiceImpl<CommentMapper, CommentDO> im
List<CommentDO> commentDOs = commentMapper.selectList(
Wrappers.lambdaQuery(CommentDO.class)
.eq(CommentDO::getParentCommentId, parentCommentId)
.eq(CommentDO::getIsDelete, 0)
.orderByAsc(CommentDO::getCreateTime)
);
return commentDOs.stream()
.map(this::convertToVO)
.collect(Collectors.toList());
}
@Override
public List<CommentVO> listAdminComments(Long productId, Long userId, Long shopId, String keyword) {
List<CommentDO> commentDOs = commentMapper.selectList(
Wrappers.lambdaQuery(CommentDO.class)
.eq(CommentDO::getIsDelete, 0)
.eq(productId != null && productId > 0, CommentDO::getProductId, productId)
.eq(userId != null && userId > 0, CommentDO::getUserId, userId)
.eq(shopId != null && shopId > 0, CommentDO::getShopId, shopId)
.like(StringUtils.hasText(keyword), CommentDO::getCommentContent, keyword)
.orderByDesc(CommentDO::getCreateTime)
);
return commentDOs.stream().map(this::convertToVO).collect(Collectors.toList());
}
/**
* 将 DO 转换为 VO
*
@@ -141,6 +252,80 @@ public class CommentServiceImpl extends ServiceImpl<CommentMapper, CommentDO> im
commentVO.setLikeCount(commentDO.getLikeCount());
commentVO.setCreateTime(commentDO.getCreateTime());
commentVO.setUpdateTime(commentDO.getUpdateTime());
fillDisplayFields(commentVO, commentDO);
return commentVO;
}
private List<CommentVO> toCommentVOList(com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper<CommentDO> wrapper) {
return commentMapper.selectList(wrapper).stream().map(this::convertToVO).collect(Collectors.toList());
}
private List<CommentVO> buildTree(List<CommentDO> commentDOs) {
Map<Long, CommentVO> rootMap = new HashMap<>();
List<CommentVO> roots = new ArrayList<>();
List<CommentVO> replies = new ArrayList<>();
for (CommentDO commentDO : commentDOs) {
CommentVO vo = convertToVO(commentDO);
if (commentDO.getParentCommentId() == null || commentDO.getParentCommentId() <= 0) {
vo.setReplies(new ArrayList<>());
rootMap.put(commentDO.getId(), vo);
roots.add(vo);
} else {
replies.add(vo);
}
}
replies.sort(Comparator.comparing(CommentVO::getCreateTime, Comparator.nullsLast(Comparator.naturalOrder())));
for (CommentVO reply : replies) {
CommentVO root = rootMap.get(reply.getParentCommentId());
if (root != null) {
root.getReplies().add(reply);
}
}
roots.sort(Comparator.comparing(CommentVO::getCreateTime, Comparator.nullsLast(Comparator.naturalOrder())).reversed());
return roots;
}
private void fillDisplayFields(CommentVO commentVO, CommentDO commentDO) {
UserDO user = commentDO.getUserId() == null ? null : userMapper.selectById(commentDO.getUserId());
if (user != null) {
commentVO.setUserNickname(StringUtils.hasText(user.getNickname()) ? user.getNickname() : user.getAccount());
}
ShopDO shop = commentDO.getShopId() == null ? null : shopMapper.selectById(commentDO.getShopId());
if (shop != null) {
commentVO.setShopName(shop.getShopName());
}
ProductDO product = commentDO.getProductId() == null ? null : productMapper.selectById(commentDO.getProductId());
if (product != null) {
commentVO.setProductTitle(product.getTitle());
}
if (commentDO.getParentCommentId() != null && commentDO.getParentCommentId() > 0) {
CommentDO parent = commentMapper.selectById(commentDO.getParentCommentId());
if (parent != null && !Integer.valueOf(1).equals(parent.getIsDelete())) {
commentVO.setParentCommentContent(parent.getCommentContent());
}
}
}
private com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper<CommentDO> activeQuery() {
return Wrappers.lambdaQuery(CommentDO.class).eq(CommentDO::getIsDelete, 0);
}
private Long normalizeId(Long id) {
return id == null || id <= 0 ? null : id;
}
private String sanitizeContent(String content) {
if (!StringUtils.hasText(content)) {
throw new RuntimeException("评论内容不能为空");
}
String sanitized = content.replaceAll("[\\p{Cntrl}&&[^\r\n\t]]", "").trim();
sanitized = sanitized.replaceAll("<[^>]*>", "");
if (!StringUtils.hasText(sanitized)) {
throw new RuntimeException("评论内容不能为空");
}
if (sanitized.length() > 300) {
throw new RuntimeException("评论最多输入 300 个字");
}
return sanitized;
}
}
@@ -291,7 +291,7 @@ public class OrderServiceImpl implements OrderService {
CouponSelection couponSelection = resolveRequestedCoupon(orderDTO, availableCouponOptions);
if (couponSelection == null && !isExplicitNoCoupon(orderDTO)) {
couponSelection = selectBestSystemVipCoupon(availableCouponOptions, totalAmount);
couponSelection = selectBestAvailableCoupon(availableCouponOptions, totalAmount);
}
if (couponSelection != null) {
@@ -961,11 +961,15 @@ public class OrderServiceImpl implements OrderService {
);
}
private CouponSelection selectBestSystemVipCoupon(List<UserCouponVO> availableCoupons, BigDecimal totalAmount) {
private CouponSelection selectBestAvailableCoupon(List<UserCouponVO> availableCoupons, BigDecimal totalAmount) {
return availableCoupons.stream()
.filter(coupon -> COUPON_SOURCE_SYSTEM_VIP.equals(coupon.getSourceType()))
.filter(coupon -> coupon.getCouponId() != null)
.max(Comparator.comparing(coupon -> calculateCouponDiscountAmount(coupon, totalAmount)))
.map(coupon -> new CouponSelection(coupon.getSourceType(), coupon.getCouponId(), null))
.map(coupon -> new CouponSelection(
coupon.getSourceType(),
coupon.getCouponId(),
COUPON_SOURCE_USER.equals(coupon.getSourceType()) ? coupon.getId() : null
))
.orElse(null);
}
@@ -127,6 +127,8 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, UserDO> implements
seeds.put("coupon:edit", new PermissionSeed("编辑优惠券", "优惠券管理", "编辑优惠券模板"));
seeds.put("coupon:delete", new PermissionSeed("删除优惠券", "优惠券管理", "删除优惠券模板"));
seeds.put("coupon:assign", new PermissionSeed("发放优惠券", "优惠券管理", "向用户发放普通优惠券"));
seeds.put("comment:view", new PermissionSeed("查看评论", "评论管理", "查看作品评论与回复"));
seeds.put("comment:delete", new PermissionSeed("删除评论", "评论管理", "删除不合规作品评论"));
seeds.put("order:view", new PermissionSeed("查看订单", "订单管理", "查看订单列表与详情"));
seeds.put("order:process", new PermissionSeed("处理订单", "订单管理", "处理订单状态或由美甲师完结订单"));
seeds.put("order:edit", new PermissionSeed("编辑订单", "订单管理", "编辑订单信息的预留权限"));
@@ -19,6 +19,7 @@ public final class PermissionPolicy {
"permission:view", "permission:create", "permission:edit", "permission:delete", "permission:assign",
"tag:view", "tag:create", "tag:edit", "tag:delete", "tag:assign",
"coupon:view", "coupon:create", "coupon:edit", "coupon:delete", "coupon:assign",
"comment:view", "comment:delete",
"order:view", "order:process", "order:edit", "order:delete",
"nail_artist:view", "nail_artist:edit"
);
@@ -33,6 +34,7 @@ public final class PermissionPolicy {
"permission:view", "permission:assign",
"tag:view", "tag:create", "tag:edit", "tag:delete", "tag:assign",
"coupon:view", "coupon:create", "coupon:edit", "coupon:delete", "coupon:assign",
"comment:view", "comment:delete",
"order:view", "order:process", "order:edit", "order:delete",
"nail_artist:view", "nail_artist:edit"
);
@@ -45,6 +47,7 @@ public final class PermissionPolicy {
"shop:view",
"tag:view", "tag:create",
"coupon:view",
"comment:view",
"order:view", "order:process"
);
@@ -1127,6 +1127,8 @@ VALUES
('coupon:edit', '编辑优惠券', '优惠券管理', '编辑优惠券模板', NOW(), NOW(), 0),
('coupon:delete', '删除优惠券', '优惠券管理', '删除优惠券模板', NOW(), NOW(), 0),
('coupon:assign', '发放优惠券', '优惠券管理', '向用户发放普通优惠券', NOW(), NOW(), 0),
('comment:view', '查看评论', '评论管理', '查看作品评论与回复', NOW(), NOW(), 0),
('comment:delete', '删除评论', '评论管理', '删除不合规作品评论', NOW(), NOW(), 0),
('order:view', '查看订单', '订单管理', '查看订单列表与详情', NOW(), NOW(), 0),
('order:process', '处理订单', '订单管理', '处理订单状态或由美甲师完结订单', NOW(), NOW(), 0),
('order:edit', '编辑订单', '订单管理', '编辑订单信息的预留权限', NOW(), NOW(), 0),
@@ -1220,6 +1222,8 @@ INNER JOIN t_permission p
'coupon:edit',
'coupon:delete',
'coupon:assign',
'comment:view',
'comment:delete',
'order:view',
'order:process',
'order:edit',
@@ -1272,6 +1276,8 @@ INNER JOIN t_permission p
'coupon:edit',
'coupon:delete',
'coupon:assign',
'comment:view',
'comment:delete',
'order:view',
'order:process',
'order:edit',
@@ -1304,6 +1310,7 @@ INNER JOIN t_permission p
'tag:view',
'tag:create',
'coupon:view',
'comment:view',
'order:view',
'order:process'
)