package com.ylx.massage.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ylx.common.exception.ServiceException;
import com.ylx.common.utils.StringUtils;
import com.ylx.massage.domain.CommentUserAudit;
import com.ylx.massage.domain.TCommentUser;
import com.ylx.massage.mapper.TCommentUserAuditMapper;
import com.ylx.massage.service.TCommentUserAuditService;
import com.ylx.massage.service.TCommentUserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
/**
* 用户评论审核服务实现类
*
* 提供用户评论审核功能的具体实现,包括审核通过、审核拒绝等操作。
*
*
* @author ylx
* @version 1.0
* @since 2024
*/
@Service
public class TCommentUserAuditServiceImpl extends ServiceImpl implements TCommentUserAuditService {
@Resource
private TCommentUserService tCommentUserService;
/**
* 审核用户评论
*
* 根据审核结果更新评论状态,并记录审核信息到审核表。
* 审核通过后,评论状态更新为"通过";
* 审核拒绝后,评论状态更新为"拒绝",并可记录拒绝原因。
*
*
* @param commentId 评论ID,用于标识待审核的评论
* @param auditStatus 审核状态:1-通过,2-拒绝
* @param auditReason 审核原因/拒绝理由,审核拒绝时必填,审核通过时可为空
* @param auditorId 审核人ID,当前登录管理员的ID
* @param auditorName 审核人姓名,当前登录管理员的姓名
* @return 审核是否成功
*/
@Override
@Transactional(rollbackFor = Exception.class)
public Boolean auditComment(String commentId, Integer auditStatus, String auditReason, String auditorId, String auditorName) {
// 参数校验
if (StringUtils.isBlank(commentId)) {
throw new ServiceException("评论ID不能为空");
}
if (auditStatus == null || (auditStatus != 1 && auditStatus != 2)) {
throw new ServiceException("审核状态无效,必须为1(通过)或2(拒绝)");
}
if (auditStatus == 2 && StringUtils.isBlank(auditReason)) {
throw new ServiceException("审核拒绝时必须填写拒绝理由");
}
if (StringUtils.isBlank(auditorId) || StringUtils.isBlank(auditorName)) {
throw new ServiceException("审核人信息不能为空");
}
// 查询评论是否存在
TCommentUser commentUser = tCommentUserService.getById(commentId);
if (commentUser == null) {
throw new ServiceException("评论不存在");
}
// 查询审核记录是否存在
LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(CommentUserAudit::getCommentId, commentId);
CommentUserAudit auditRecord = this.getOne(queryWrapper);
if (auditRecord == null) {
throw new ServiceException("审核记录不存在");
}
// 检查是否已审核
if (auditRecord.getAuditStatus() != 0) {
throw new ServiceException("该评论已审核,请勿重复操作");
}
// 更新审核记录
auditRecord.setAuditStatus(auditStatus);
auditRecord.setAuditReason(auditReason);
auditRecord.setAuditorId(auditorId);
auditRecord.setAuditorName(auditorName);
auditRecord.setAuditTime(new Date());
auditRecord.setUpdateTime(new Date());
boolean updateResult = this.updateById(auditRecord);
if (!updateResult) {
throw new ServiceException("审核记录更新失败");
}
return true;
}
/**
* 批量审核用户评论
*
* 批量审核多条用户评论,支持统一通过或拒绝。
* 对每条评论进行独立的验证和处理,记录详细的审核结果。
* 返回审核结果统计,包括成功数量、失败数量和失败详情。
*
*
* @param commentIds 评论ID列表,用于标识待审核的多条评论
* @param auditStatus 审核状态:1-通过,2-拒绝
* @param auditReason 审核原因/拒绝理由,审核拒绝时必填,审核通过时可为空
* @param auditorId 审核人ID,当前登录管理员的ID
* @param auditorName 审核人姓名,当前登录管理员的姓名
* @return 审核结果对象,包含成功数量、失败数量和失败详情
*/
@Override
@Transactional(rollbackFor = Exception.class)
public BatchAuditResult batchAuditComments(List commentIds, Integer auditStatus, String auditReason, String auditorId, String auditorName) {
// 创建批量审核结果对象
BatchAuditResult result = new BatchAuditResult();
// 基础参数校验
if (commentIds == null || commentIds.isEmpty()) {
throw new ServiceException("评论ID列表不能为空");
}
if (auditStatus == null || (auditStatus != 1 && auditStatus != 2)) {
throw new ServiceException("审核状态无效,必须为1(通过)或2(拒绝)");
}
if (auditStatus == 2 && StringUtils.isBlank(auditReason)) {
throw new ServiceException("审核拒绝时必须填写拒绝理由");
}
if (StringUtils.isBlank(auditorId) || StringUtils.isBlank(auditorName)) {
throw new ServiceException("审核人信息不能为空");
}
// 记录当前审核时间
Date auditTime = new Date();
// 遍历评论ID列表,逐个进行审核
for (String commentId : commentIds) {
try {
// 参数校验:跳过空白的评论ID
if (StringUtils.isBlank(commentId)) {
result.addFailDetail(commentId, "评论ID为空");
continue;
}
// 查询评论是否存在
TCommentUser commentUser = tCommentUserService.getById(commentId);
if (commentUser == null) {
result.addFailDetail(commentId, "评论不存在");
continue;
}
// 查询审核记录是否存在
LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(CommentUserAudit::getCommentId, commentId);
CommentUserAudit auditRecord = this.getOne(queryWrapper);
if (auditRecord == null) {
result.addFailDetail(commentId, "审核记录不存在");
continue;
}
// 检查是否已审核
if (auditRecord.getAuditStatus() != 0) {
result.addFailDetail(commentId, "该评论已审核,请勿重复操作");
continue;
}
// 更新审核记录
auditRecord.setAuditStatus(auditStatus);
auditRecord.setAuditReason(auditReason);
auditRecord.setAuditorId(auditorId);
auditRecord.setAuditorName(auditorName);
auditRecord.setAuditTime(auditTime);
auditRecord.setUpdateTime(new Date());
boolean updateResult = this.updateById(auditRecord);
if (updateResult) {
result.incrementSuccess();
} else {
result.addFailDetail(commentId, "审核记录更新失败");
}
} catch (Exception e) {
// 捕获单个评论审核的异常,继续处理其他评论
result.addFailDetail(commentId, "审核失败:" + e.getMessage());
}
}
return result;
}
}