TCommentUserAuditServiceImpl.java 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. package com.ylx.massage.service.impl;
  2. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  3. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  4. import com.ylx.common.exception.ServiceException;
  5. import com.ylx.common.utils.StringUtils;
  6. import com.ylx.massage.domain.CommentUserAudit;
  7. import com.ylx.massage.domain.TCommentUser;
  8. import com.ylx.massage.mapper.TCommentUserAuditMapper;
  9. import com.ylx.massage.service.TCommentUserAuditService;
  10. import com.ylx.massage.service.TCommentUserService;
  11. import org.springframework.stereotype.Service;
  12. import org.springframework.transaction.annotation.Transactional;
  13. import javax.annotation.Resource;
  14. import java.util.Date;
  15. import java.util.List;
  16. /**
  17. * 用户评论审核服务实现类
  18. * <p>
  19. * 提供用户评论审核功能的具体实现,包括审核通过、审核拒绝等操作。
  20. * </p>
  21. *
  22. * @author ylx
  23. * @version 1.0
  24. * @since 2024
  25. */
  26. @Service
  27. public class TCommentUserAuditServiceImpl extends ServiceImpl<TCommentUserAuditMapper, CommentUserAudit> implements TCommentUserAuditService {
  28. @Resource
  29. private TCommentUserService tCommentUserService;
  30. /**
  31. * 审核用户评论
  32. * <p>
  33. * 根据审核结果更新评论状态,并记录审核信息到审核表。
  34. * 审核通过后,评论状态更新为"通过";
  35. * 审核拒绝后,评论状态更新为"拒绝",并可记录拒绝原因。
  36. * </p>
  37. *
  38. * @param commentId 评论ID,用于标识待审核的评论
  39. * @param auditStatus 审核状态:1-通过,2-拒绝
  40. * @param auditReason 审核原因/拒绝理由,审核拒绝时必填,审核通过时可为空
  41. * @param auditorId 审核人ID,当前登录管理员的ID
  42. * @param auditorName 审核人姓名,当前登录管理员的姓名
  43. * @return 审核是否成功
  44. */
  45. @Override
  46. @Transactional(rollbackFor = Exception.class)
  47. public Boolean auditComment(String commentId, Integer auditStatus, String auditReason, String auditorId, String auditorName) {
  48. // 参数校验
  49. if (StringUtils.isBlank(commentId)) {
  50. throw new ServiceException("评论ID不能为空");
  51. }
  52. if (auditStatus == null || (auditStatus != 1 && auditStatus != 2)) {
  53. throw new ServiceException("审核状态无效,必须为1(通过)或2(拒绝)");
  54. }
  55. if (auditStatus == 2 && StringUtils.isBlank(auditReason)) {
  56. throw new ServiceException("审核拒绝时必须填写拒绝理由");
  57. }
  58. if (StringUtils.isBlank(auditorId) || StringUtils.isBlank(auditorName)) {
  59. throw new ServiceException("审核人信息不能为空");
  60. }
  61. // 查询评论是否存在
  62. TCommentUser commentUser = tCommentUserService.getById(commentId);
  63. if (commentUser == null) {
  64. throw new ServiceException("评论不存在");
  65. }
  66. // 查询审核记录是否存在
  67. LambdaQueryWrapper<CommentUserAudit> queryWrapper = new LambdaQueryWrapper<>();
  68. queryWrapper.eq(CommentUserAudit::getCommentId, commentId);
  69. CommentUserAudit auditRecord = this.getOne(queryWrapper);
  70. if (auditRecord == null) {
  71. throw new ServiceException("审核记录不存在");
  72. }
  73. // 检查是否已审核
  74. if (auditRecord.getAuditStatus() != 0) {
  75. throw new ServiceException("该评论已审核,请勿重复操作");
  76. }
  77. // 更新审核记录
  78. auditRecord.setAuditStatus(auditStatus);
  79. auditRecord.setAuditReason(auditReason);
  80. auditRecord.setAuditorId(auditorId);
  81. auditRecord.setAuditorName(auditorName);
  82. auditRecord.setAuditTime(new Date());
  83. auditRecord.setUpdateTime(new Date());
  84. boolean updateResult = this.updateById(auditRecord);
  85. if (!updateResult) {
  86. throw new ServiceException("审核记录更新失败");
  87. }
  88. return true;
  89. }
  90. /**
  91. * 批量审核用户评论
  92. * <p>
  93. * 批量审核多条用户评论,支持统一通过或拒绝。
  94. * 对每条评论进行独立的验证和处理,记录详细的审核结果。
  95. * 返回审核结果统计,包括成功数量、失败数量和失败详情。
  96. * </p>
  97. *
  98. * @param commentIds 评论ID列表,用于标识待审核的多条评论
  99. * @param auditStatus 审核状态:1-通过,2-拒绝
  100. * @param auditReason 审核原因/拒绝理由,审核拒绝时必填,审核通过时可为空
  101. * @param auditorId 审核人ID,当前登录管理员的ID
  102. * @param auditorName 审核人姓名,当前登录管理员的姓名
  103. * @return 审核结果对象,包含成功数量、失败数量和失败详情
  104. */
  105. @Override
  106. @Transactional(rollbackFor = Exception.class)
  107. public BatchAuditResult batchAuditComments(List<String> commentIds, Integer auditStatus, String auditReason, String auditorId, String auditorName) {
  108. // 创建批量审核结果对象
  109. BatchAuditResult result = new BatchAuditResult();
  110. // 基础参数校验
  111. if (commentIds == null || commentIds.isEmpty()) {
  112. throw new ServiceException("评论ID列表不能为空");
  113. }
  114. if (auditStatus == null || (auditStatus != 1 && auditStatus != 2)) {
  115. throw new ServiceException("审核状态无效,必须为1(通过)或2(拒绝)");
  116. }
  117. if (auditStatus == 2 && StringUtils.isBlank(auditReason)) {
  118. throw new ServiceException("审核拒绝时必须填写拒绝理由");
  119. }
  120. if (StringUtils.isBlank(auditorId) || StringUtils.isBlank(auditorName)) {
  121. throw new ServiceException("审核人信息不能为空");
  122. }
  123. // 记录当前审核时间
  124. Date auditTime = new Date();
  125. // 遍历评论ID列表,逐个进行审核
  126. for (String commentId : commentIds) {
  127. try {
  128. // 参数校验:跳过空白的评论ID
  129. if (StringUtils.isBlank(commentId)) {
  130. result.addFailDetail(commentId, "评论ID为空");
  131. continue;
  132. }
  133. // 查询评论是否存在
  134. TCommentUser commentUser = tCommentUserService.getById(commentId);
  135. if (commentUser == null) {
  136. result.addFailDetail(commentId, "评论不存在");
  137. continue;
  138. }
  139. // 查询审核记录是否存在
  140. LambdaQueryWrapper<CommentUserAudit> queryWrapper = new LambdaQueryWrapper<>();
  141. queryWrapper.eq(CommentUserAudit::getCommentId, commentId);
  142. CommentUserAudit auditRecord = this.getOne(queryWrapper);
  143. if (auditRecord == null) {
  144. result.addFailDetail(commentId, "审核记录不存在");
  145. continue;
  146. }
  147. // 检查是否已审核
  148. if (auditRecord.getAuditStatus() != 0) {
  149. result.addFailDetail(commentId, "该评论已审核,请勿重复操作");
  150. continue;
  151. }
  152. // 更新审核记录
  153. auditRecord.setAuditStatus(auditStatus);
  154. auditRecord.setAuditReason(auditReason);
  155. auditRecord.setAuditorId(auditorId);
  156. auditRecord.setAuditorName(auditorName);
  157. auditRecord.setAuditTime(auditTime);
  158. auditRecord.setUpdateTime(new Date());
  159. boolean updateResult = this.updateById(auditRecord);
  160. if (updateResult) {
  161. result.incrementSuccess();
  162. } else {
  163. result.addFailDetail(commentId, "审核记录更新失败");
  164. }
  165. } catch (Exception e) {
  166. // 捕获单个评论审核的异常,继续处理其他评论
  167. result.addFailDetail(commentId, "审核失败:" + e.getMessage());
  168. }
  169. }
  170. return result;
  171. }
  172. }