package shop.alien.lawyer.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import shop.alien.entity.result.R;
import shop.alien.entity.store.LifeLikeRecord;
import shop.alien.entity.store.OrderReview;
import shop.alien.entity.store.ReviewComment;
import shop.alien.entity.store.dto.LawyerReplyDto;
import shop.alien.entity.store.dto.ReviewCommentRequestDto;
import shop.alien.entity.store.dto.ReviewReplyDto;
import shop.alien.entity.store.vo.ReviewCommentVo;
import shop.alien.lawyer.service.OrderReviewService;
import shop.alien.lawyer.service.ReviewCommentService;
import shop.alien.lawyer.util.LawyerLikeRecordIdentityHelper;
import shop.alien.lawyer.util.LawyerLikeRecordIdentityHelper.LawyerLikerScope;
import shop.alien.mapper.LawyerUserMapper;
import shop.alien.mapper.LifeLikeRecordMapper;
import shop.alien.mapper.ReviewCommentMapper;
import shop.alien.entity.store.LawyerUser;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 评价评论 服务实现类
 *
 * @author system
 * @since 2025-01-XX
 */
@Slf4j
@Transactional
@Service
public class ReviewCommentServiceImpl extends ServiceImpl<ReviewCommentMapper, ReviewComment> implements ReviewCommentService {

    private final ReviewCommentMapper reviewCommentMapper;
    private final OrderReviewService orderReviewService;
    private final LifeLikeRecordMapper lifeLikeRecordMapper;
    private final LawyerUserMapper lawyerUserMapper;
    private final LawyerLikeRecordIdentityHelper lifeLikeIdentityHelper;

    public ReviewCommentServiceImpl(ReviewCommentMapper reviewCommentMapper,
                                    @Lazy OrderReviewService orderReviewService,
                                    LifeLikeRecordMapper lifeLikeRecordMapper,
                                    LawyerUserMapper lawyerUserMapper,
                                    LawyerLikeRecordIdentityHelper lifeLikeIdentityHelper) {
        this.reviewCommentMapper = reviewCommentMapper;
        this.orderReviewService = orderReviewService;
        this.lifeLikeRecordMapper = lifeLikeRecordMapper;
        this.lawyerUserMapper = lawyerUserMapper;
        this.lifeLikeIdentityHelper = lifeLikeIdentityHelper;
    }

    /**
     * 判断用户类型：1-普通用户，2-律师
     *
     * @param userId 用户ID
     * @return 用户类型：1-普通用户，2-律师
     */
    private Integer getUserType(Integer userId) {
        if (userId == null) {
            return 1; // 默认为普通用户
        }
        // 查询律师表，如果存在且未删除，则为律师
        LawyerUser lawyerUser = lawyerUserMapper.selectById(userId);
        if (lawyerUser != null && (lawyerUser.getDeleteFlag() == null || lawyerUser.getDeleteFlag() == 0)) {
            return 2; // 律师
        }
        return 1; // 普通用户
    }

    @Override
    public R<ReviewComment> createComment(ReviewComment comment) {
        log.info("ReviewCommentServiceImpl.createComment?comment={}", comment);
        // 参数校验
        if (comment == null) {
            return R.fail("评论信息不能为空");
        }
        if (comment.getReviewId() == null) {
            return R.fail("评价ID不能为空");
        }
        if (comment.getCommentContent() == null || comment.getCommentContent().trim().isEmpty()) {
            return R.fail("评论内容不能为空");
        }
        Integer userId = comment.getSendUserId();
        if (userId == null) {
            return R.fail("用户ID不能为空");
        }

        // 验证评价是否存在
        OrderReview review = orderReviewService.getById(comment.getReviewId());
        if (review == null || review.getDeleteFlag() == 1) {
            return R.fail("评价不存在或已删除");
        }

        // 验证用户类型参数：必须传入
        if (comment.getSendUserType() == null) {
            return R.fail("发送用户类型不能为空");
        }
        if (comment.getReceiveUserType() == null) {
            return R.fail("接收用户类型不能为空");
        }

        // 权限校验：如果是律师（sendUserType=2），只能回复自己订单的评价
        if (comment.getSendUserType() == 2) {
            if (!review.getLawyerUserId().equals(comment.getSendUserId())) {
                return R.fail("只能回复自己的订单评价");
            }
        }

        // 设置评论属性
        // 接收用户ID为评价的创建者（如果未设置）
        if (comment.getReceiveUserId() == null) {
            comment.setReceiveUserId(review.getUserId());
        }
        // 设置默认值
        if (comment.getLikeCount() == null) {
            comment.setLikeCount(0);
        }
        if (comment.getReplyCount() == null) {
            comment.setReplyCount(0);
        }
        if (comment.getHeadType() == null) {
            comment.setHeadType(0); // 0:是首评
        }
        comment.setCreatedUserId(userId);
        comment.setCreatedTime(new Date());

        boolean success = this.save(comment);
        if (success) {
            // 更新评价的评论数（包括首评论和子评论）
            review.setCommentCount((review.getCommentCount() == null ? 0 : review.getCommentCount()) + 1);
            orderReviewService.updateById(review);
            log.info("更新评价评论数成功，评价ID={}, 评论数={}", comment.getReviewId(), review.getCommentCount());

            log.info("创建评论成功，评论ID={}", comment.getId());
            return R.data(comment, "评论成功");
        } else {
            log.error("创建评论失败");
            return R.fail("创建评论失败");
        }
    }

    @Override
    public R<List<ReviewCommentVo>> getCommentListByReviewId(Integer reviewId, Integer currentUserId) {
        log.info("ReviewCommentServiceImpl.getCommentListByReviewId?reviewId={}, currentUserId={}", reviewId, currentUserId);

        if (reviewId == null) {
            return R.fail("评价ID不能为空");
        }

        List<ReviewCommentVo> comments = reviewCommentMapper.getCommentListByReviewId(reviewId, currentUserId);

        // 为每个首评加载回复列表
        if (comments != null && !comments.isEmpty()) {
            for (ReviewCommentVo comment : comments) {
                List<ReviewCommentVo> replies = reviewCommentMapper.getReplyListByHeadId(comment.getId(), currentUserId);
                comment.setReplies(replies);
            }
        }

        return R.data(comments);
    }

    @Override
    public R<Boolean> likeComment(ReviewCommentRequestDto requestDto) {
        log.info("ReviewCommentServiceImpl.likeComment?requestDto={}", requestDto);

        Integer commentId = requestDto.getCommentId();
        Integer userId = requestDto.getUserId();

        if (commentId == null) {
            return R.fail("评论ID不能为空");
        }
        if (userId == null) {
            return R.fail("用户ID不能为空");
        }

        // 验证评论是否存在
        ReviewComment comment = this.getById(commentId);
        if (comment == null || comment.getDeleteFlag() == 1) {
            return R.fail("评论不存在或已删除");
        }

        // 检查是否已点赞
        LawyerLikerScope likerScope = lifeLikeIdentityHelper.fromLifeUserId(userId);
        LambdaQueryWrapper<LifeLikeRecord> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(LifeLikeRecord::getType, "8")
                .eq(LifeLikeRecord::getHuifuId, String.valueOf(commentId))
                .eq(LifeLikeRecord::getDeleteFlag, 0);
        lifeLikeIdentityHelper.applyLikerFilter(queryWrapper, likerScope);
        List<LifeLikeRecord> records = lifeLikeRecordMapper.selectList(queryWrapper);

        if (CollectionUtils.isEmpty(records)) {
            // 插入点赞记录
            LifeLikeRecord likeRecord = new LifeLikeRecord();
            likeRecord.setDianzanId(String.valueOf(userId));
            lifeLikeIdentityHelper.normalizeBeforeSave(likeRecord);
            likeRecord.setHuifuId(String.valueOf(commentId));
            likeRecord.setType("8");
            likeRecord.setCreatedTime(new Date());
            likeRecord.setCreatedUserId(userId);
            lifeLikeRecordMapper.insert(likeRecord);

            // 更新评论点赞数
            LambdaUpdateWrapper<ReviewComment> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.eq(ReviewComment::getId, commentId);
            updateWrapper.setSql("like_count = like_count + 1");
            int result = reviewCommentMapper.update(null, updateWrapper);

            if (result > 0) {
                log.info("点赞评论成功，评论ID={}", commentId);
                return R.data(true, "点赞成功");
            } else {
                return R.fail("点赞失败");
            }
        } else {
            return R.data(true, "已点赞");
        }
    }

    @Override
    public R<Boolean> cancelLikeComment(ReviewCommentRequestDto requestDto) {
        log.info("ReviewCommentServiceImpl.cancelLikeComment?requestDto={}", requestDto);

        Integer commentId = requestDto.getCommentId();
        Integer userId = requestDto.getUserId();
        if (commentId == null) {
            return R.fail("评论ID不能为空");
        }
        if (userId == null) {
            return R.fail("用户ID不能为空");
        }

        // 查询点赞记录
        LawyerLikerScope likerScope = lifeLikeIdentityHelper.fromLifeUserId(userId);
        LambdaQueryWrapper<LifeLikeRecord> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(LifeLikeRecord::getType, "8")
                .eq(LifeLikeRecord::getHuifuId, String.valueOf(commentId))
                .eq(LifeLikeRecord::getDeleteFlag, 0);
        lifeLikeIdentityHelper.applyLikerFilter(queryWrapper, likerScope);
        List<LifeLikeRecord> records = lifeLikeRecordMapper.selectList(queryWrapper);

        if (!CollectionUtils.isEmpty(records)) {
            // 删除点赞记录（逻辑删除）
            for (LifeLikeRecord record : records) {
                lifeLikeRecordMapper.deleteById(record.getId());
            }

            // 更新评论点赞数（移除 gt 条件，确保即使为 0 也能正确更新）
            LambdaUpdateWrapper<ReviewComment> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.eq(ReviewComment::getId, commentId);
            updateWrapper.setSql("like_count = GREATEST(0, like_count - 1)");
            int result = reviewCommentMapper.update(null, updateWrapper);

            if (result > 0) {
                log.info("取消点赞评论成功，评论ID={}", commentId);
                return R.data(true, "取消点赞成功");
            } else {
                return R.fail("取消点赞失败");
            }
        } else {
            return R.data(true, "未点赞");
        }
    }

    @Override
    public R<ReviewComment> createReply(ReviewReplyDto replyDto) {
        log.info("ReviewCommentServiceImpl.createReply?replyDto={}", replyDto);

        // 参数校验
        if (replyDto == null) {
            return R.fail("回复信息不能为空");
        }
        if (replyDto.getCommentId() == null) {
            return R.fail("评论ID不能为空");
        }
        if (replyDto.getReplyContent() == null || replyDto.getReplyContent().trim().isEmpty()) {
            return R.fail("回复内容不能为空");
        }
        Integer userId = replyDto.getUserId();
        if (userId == null) {
            return R.fail("用户ID不能为空");
        }

        // 验证首评是否存在
        ReviewComment headComment = this.getById(replyDto.getCommentId());
        if (headComment == null || headComment.getDeleteFlag() == 1) {
            return R.fail("评论不存在或已删除");
        }
        if (headComment.getHeadType() != 0) {
            return R.fail("只能回复首评");
        }

        // 校验用户类型参数：必须传入
        if (replyDto.getSendUserType() == null) {
            return R.fail("发送用户类型不能为空");
        }
        if (replyDto.getReceiveUserType() == null) {
            return R.fail("接收用户类型不能为空");
        }

        // 权限校验：如果是律师（sendUserType=2），只能回复自己订单评价下的评论
        if (replyDto.getSendUserType() == 2) {
            OrderReview review = orderReviewService.getById(headComment.getReviewId());
            if (review == null || review.getDeleteFlag() == 1) {
                return R.fail("评价不存在或已删除");
            }
            if (!review.getLawyerUserId().equals(userId)) {
                return R.fail("只能回复自己订单评价下的评论");
            }
        }

        // 创建回复
        ReviewComment reply = new ReviewComment();
        reply.setReviewId(headComment.getReviewId());
        reply.setSendUserId(userId);
        Integer receiveUserId = replyDto.getReplyToUserId() != null ? replyDto.getReplyToUserId() : headComment.getSendUserId();
        reply.setReceiveUserId(receiveUserId);
        // 设置用户类型：Controller层已经校验过，直接使用
        reply.setSendUserType(replyDto.getSendUserType());
        reply.setReceiveUserType(replyDto.getReceiveUserType());
        reply.setCommentContent(replyDto.getReplyContent());
        reply.setLikeCount(0);
        reply.setReplyCount(0);
        reply.setHeadType(1); // 1:是回复
        reply.setHeadId(replyDto.getCommentId()); // 指向首评ID
        reply.setCreatedUserId(userId);
        reply.setCreatedTime(new Date());

        boolean success = this.save(reply);
        if (success) {
            // 更新首评的回复数
            headComment.setReplyCount((headComment.getReplyCount() == null ? 0 : headComment.getReplyCount()) + 1);
            this.updateById(headComment);

            // 更新评价的评论数（包括子评论）
            OrderReview review = orderReviewService.getById(reply.getReviewId());
            if (review != null) {
                review.setCommentCount((review.getCommentCount() == null ? 0 : review.getCommentCount()) + 1);
                orderReviewService.updateById(review);
                log.info("更新评价评论数成功，评价ID={}, 评论数={}", reply.getReviewId(), review.getCommentCount());
            }

            log.info("创建回复成功，回复ID={}", reply.getId());
            return R.data(reply, "回复成功");
        } else {
            log.error("创建回复失败");
            return R.fail("创建回复失败");
        }
    }

    @Override
    public R<List<ReviewCommentVo>> getReplyListByHeadId(Integer headId, Integer currentUserId) {
        log.info("ReviewCommentServiceImpl.getReplyListByHeadId?headId={}, currentUserId={}", headId, currentUserId);

        if (headId == null) {
            return R.fail("首评ID不能为空");
        }

        List<ReviewCommentVo> replies = reviewCommentMapper.getReplyListByHeadId(headId, currentUserId);
        return R.data(replies);
    }

    @Override
    public R<Boolean> deleteReply(Integer replyId, Integer userId) {
        log.info("ReviewCommentServiceImpl.deleteReply?replyId={}, userId={}", replyId, userId);

        if (replyId == null) {
            return R.fail("回复ID不能为空");
        }
        if (userId == null) {
            return R.fail("用户ID不能为空");
        }

        // 查询回复
        ReviewComment reply = this.getById(replyId);
        if (reply == null) {
            return R.fail("回复不存在");
        }
        if (reply.getHeadType() != 1) {
            return R.fail("该记录不是回复");
        }

        // 验证是否为回复用户
        if (!reply.getSendUserId().equals(userId)) {
            return R.fail("只能删除自己的回复");
        }

        // 删除回复（逻辑删除）
        boolean success = this.removeById(reply.getId());

        if (success) {
            // 更新首评的回复数
            ReviewComment headComment = this.getById(reply.getHeadId());
            if (headComment != null) {
                headComment.setReplyCount((headComment.getReplyCount() == null ? 0 : headComment.getReplyCount()) - 1);
                headComment.setReplyCount(Math.max(0, headComment.getReplyCount())); // 确保回复数不为负数
                this.updateById(headComment);
            }

            // 更新评价的评论数（包括子评论）
            OrderReview review = orderReviewService.getById(reply.getReviewId());
            if (review != null) {
                Integer currentCount = review.getCommentCount() == null ? 0 : review.getCommentCount();
                review.setCommentCount(Math.max(0, currentCount - 1)); // 确保评论数不为负数
                orderReviewService.updateById(review);
                log.info("更新评价评论数成功，评价ID={}, 原评论数={}, 新评论数={}",
                        reply.getReviewId(), currentCount, review.getCommentCount());
            }

            log.info("删除回复成功，回复ID={}", replyId);
            return R.data(true, "删除成功");
        } else {
            log.error("删除回复失败，回复ID={}", replyId);
            return R.fail("删除回复失败");
        }
    }


    @Override
    public R<Boolean> deleteReviewComment(ReviewComment reviewComment) {
        Integer id = reviewComment.getId();
        Integer userId = reviewComment.getUserId();
        log.info("ReviewCommentServiceImpl.deleteReviewComment?id={}, userId={}", id, userId);
        if (id == null) {
            return R.fail("评论ID不能为空");
        }

        // 查询评论
        ReviewComment comment = this.getById(id);
        if (comment == null) {
            return R.fail("评论不存在");
        }

        // 当userId有值时，验证是否为评论用户（只能删除自己的评论）
        if (userId != null) {
            if (!comment.getSendUserId().equals(userId)) {
                return R.fail("只能删除自己的评论");
            }
        }

        // 计算需要减少的评论数
        int commentCountToReduce = 1; // 至少减少1（当前评论本身）

        // 判断是否为首评论（headType == 0 表示是首评论）
        if (comment.getHeadType() != null && comment.getHeadType() == 0) {
            // 如果是首评论，需要先删除所有子评论
            LambdaQueryWrapper<ReviewComment> childQueryWrapper = new LambdaQueryWrapper<>();
            childQueryWrapper.eq(ReviewComment::getHeadId, id)
                    .eq(ReviewComment::getHeadType, 1)
                    .eq(ReviewComment::getDeleteFlag, 0);
            List<ReviewComment> childComments = this.list(childQueryWrapper);

            if (!CollectionUtils.isEmpty(childComments)) {
                // 批量删除子评论
                List<Integer> childIds = childComments.stream()
                        .map(ReviewComment::getId)
                        .collect(Collectors.toList());
                boolean deleteChildrenResult = this.removeByIds(childIds);
                if (!deleteChildrenResult) {
                    log.warn("删除子评论失败，首评论ID={}, 子评论数量={}", id, childIds.size());
                    return R.fail("删除子评论失败");
                }
                // 增加需要减少的评论数（包括所有子评论）
                commentCountToReduce += childComments.size();
                log.info("删除首评论下的子评论成功，首评论ID={}, 子评论数量={}", id, childIds.size());
            }
        }

        // 删除评论本身（物理删除）
        boolean result = this.removeById(id);
        if (result) {
            // 更新评价的评论数（包括首评论和子评论）
            OrderReview review = orderReviewService.getById(comment.getReviewId());
            if (review != null) {
                Integer currentCount = review.getCommentCount() == null ? 0 : review.getCommentCount();
                review.setCommentCount(Math.max(0, currentCount - commentCountToReduce)); // 确保评论数不为负数
                orderReviewService.updateById(review);
                log.info("更新评价评论数成功，评价ID={}, 原评论数={}, 减少数量={}, 新评论数={}",
                        comment.getReviewId(), currentCount, commentCountToReduce, review.getCommentCount());
            }

            if (userId != null) {
                log.info("删除评论成功，评论ID={}, userId={}, 是否首评论={}, 减少评论数={}", id, userId,
                        comment.getHeadType() != null && comment.getHeadType() == 0, commentCountToReduce);
            } else {
                log.info("管理员删除评论成功，评论ID={}, 是否首评论={}, 减少评论数={}", id,
                        comment.getHeadType() != null && comment.getHeadType() == 0, commentCountToReduce);
            }
            return R.success("删除成功");
        }
        log.warn("删除评论失败，评论ID={}, userId={}", id, userId);
        return R.fail("删除失败");
    }

    /**
     * @deprecated 已废弃，请使用 createComment 或 createReply 接口
     */
    @Deprecated
    @Override
    public R<ReviewComment> lawyerReply(LawyerReplyDto replyDto) {
        log.warn("【废弃方法】ReviewCommentServiceImpl.lawyerReply 已废弃，请使用 createComment 或 createReply 方法");
        log.info("ReviewCommentServiceImpl.lawyerReply?replyDto={}", replyDto);

        // 参数校验
        if (replyDto == null) {
            return R.fail("回复信息不能为空");
        }
        if (replyDto.getReplyType() == null) {
            return R.fail("回复类型不能为空");
        }
        if (replyDto.getLawyerUserId() == null) {
            return R.fail("律师用户ID不能为空");
        }
        if (replyDto.getReplyContent() == null || replyDto.getReplyContent().trim().isEmpty()) {
            return R.fail("回复内容不能为空");
        }

        ReviewComment reply = new ReviewComment();
        Integer receiveUserId = null;
        Integer reviewId = null;

        // 根据回复类型处理
        if (replyDto.getReplyType() == 1) {
            // 回复评价
            if (replyDto.getReviewId() == null) {
                return R.fail("评价ID不能为空");
            }

            // 验证评价是否存在
            OrderReview review = orderReviewService.getById(replyDto.getReviewId());
            if (review == null || review.getDeleteFlag() == 1) {
                return R.fail("评价不存在或已删除");
            }

            // 验证律师权限：必须是该评价对应的律师
            if (!review.getLawyerUserId().equals(replyDto.getLawyerUserId())) {
                return R.fail("只能回复自己的订单评价");
            }

            reviewId = review.getId();
            receiveUserId = replyDto.getReplyToUserId() != null ? replyDto.getReplyToUserId() : review.getUserId();
            reply.setReviewId(reviewId);
            reply.setHeadType(0); // 0:是首评（对评价的直接回复）
            reply.setHeadId(null);

        } else if (replyDto.getReplyType() == 2) {
            // 回复评论
            if (replyDto.getCommentId() == null) {
                return R.fail("评论ID不能为空");
            }

            // 验证评论是否存在
            ReviewComment comment = this.getById(replyDto.getCommentId());
            if (comment == null || comment.getDeleteFlag() == 1) {
                return R.fail("评论不存在或已删除");
            }

            // 获取评价信息，验证律师权限
            OrderReview review = orderReviewService.getById(comment.getReviewId());
            if (review == null || review.getDeleteFlag() == 1) {
                return R.fail("评价不存在或已删除");
            }

            // 验证律师权限：必须是该评价对应的律师
            if (!review.getLawyerUserId().equals(replyDto.getLawyerUserId())) {
                return R.fail("只能回复自己订单评价下的评论");
            }

            // 如果评论是首评，则回复首评；如果评论是回复，则需要回复首评
            Integer headCommentId = comment.getHeadType() == 0 ? comment.getId() : comment.getHeadId();
            ReviewComment headComment = this.getById(headCommentId);
            if (headComment == null || headComment.getDeleteFlag() == 1) {
                return R.fail("首评不存在或已删除");
            }

            reviewId = comment.getReviewId();
            receiveUserId = replyDto.getReplyToUserId() != null ? replyDto.getReplyToUserId() : comment.getSendUserId();
            reply.setReviewId(reviewId);
            reply.setHeadType(1); // 1:是回复
            reply.setHeadId(headCommentId); // 指向首评ID

        } else {
            return R.fail("回复类型错误，只能是1（回复评价）或2（回复评论）");
        }

        // 创建回复
        reply.setSendUserId(replyDto.getLawyerUserId());
        reply.setReceiveUserId(receiveUserId);
        // 验证用户类型参数：必须传入
        if (replyDto.getSendUserType() == null) {
            return R.fail("发送用户类型不能为空");
        }
        if (replyDto.getReceiveUserType() == null) {
            return R.fail("接收用户类型不能为空");
        }
        reply.setSendUserType(replyDto.getSendUserType());
        reply.setReceiveUserType(replyDto.getReceiveUserType());
        reply.setCommentContent(replyDto.getReplyContent());
        reply.setLikeCount(0);
        reply.setReplyCount(0);
        reply.setCreatedUserId(replyDto.getLawyerUserId());
        reply.setCreatedTime(new Date());

        boolean success = this.save(reply);
        if (success) {
            // 如果是回复评论，更新首评的回复数
            if (replyDto.getReplyType() == 2 && reply.getHeadId() != null) {
                ReviewComment headComment = this.getById(reply.getHeadId());
                if (headComment != null) {
                    headComment.setReplyCount((headComment.getReplyCount() == null ? 0 : headComment.getReplyCount()) + 1);
                    this.updateById(headComment);
                }
            }

            // 更新评价的评论数
            OrderReview review = orderReviewService.getById(reviewId);
            if (review != null) {
                review.setCommentCount((review.getCommentCount() == null ? 0 : review.getCommentCount()) + 1);
                orderReviewService.updateById(review);
                log.info("更新评价评论数成功，评价ID={}, 评论数={}", reviewId, review.getCommentCount());
            }

            log.info("律师回复成功，回复ID={}, 律师ID={}", reply.getId(), replyDto.getLawyerUserId());
            return R.data(reply, "回复成功");
        } else {
            log.error("律师回复失败");
            return R.fail("回复失败");
        }
    }
}

