package com.mcwl.communityCenter.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mcwl.common.constant.HttpStatus;
import com.mcwl.common.core.domain.AjaxResult;
import com.mcwl.common.core.domain.R;
import com.mcwl.common.core.domain.entity.SysUser;
import com.mcwl.common.utils.SecurityUtils;
import com.mcwl.communityCenter.domain.CommunityUser;
import com.mcwl.communityCenter.domain.Publish;
import com.mcwl.communityCenter.domain.dto.CommentDelRes;
import com.mcwl.communityCenter.domain.dto.CommentDetailRes;
import com.mcwl.communityCenter.domain.dto.CommentRes;
import com.mcwl.communityCenter.domain.vo.CommentVo;
import com.mcwl.communityCenter.domain.PublishComment;
import com.mcwl.communityCenter.domain.PublishCommentLike;
import com.mcwl.communityCenter.mapper.CommunityUserMapper;
import com.mcwl.communityCenter.mapper.PublishCommentLikeMapper;
import com.mcwl.communityCenter.mapper.PublishCommentMapper;
import com.mcwl.communityCenter.mapper.PublishMapper;
import com.mcwl.communityCenter.service.PublishCommentService;
import com.mcwl.system.service.ISysUserService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 评论区业务层
 *
 * @Author：ChenYan
 * @Project：mcwl-ai
 * @Package：com.mcwl.communityCenter.service.impl
 * @Filename：PublishCommissionServiceImpl
 * @Description TODO
 * @Date：2025/1/17 14:28
 */
@Service
@RequiredArgsConstructor
public class PublishCommentServiceImpl extends ServiceImpl<PublishCommentMapper, PublishComment> implements PublishCommentService {

    private final PublishCommentMapper publishCommentMapper;

    private final PublishCommentLikeMapper likeMapper;

    private final ISysUserService sysUserService;

    private final PublishMapper publishMapper;

    private final CommunityUserMapper communityUserMapper;

    @Override
    public R<List<CommentVo>> getComment(CommentDetailRes commentDetailRes) {
        Long tenantId = commentDetailRes.getTenantId();
        Long communityId = commentDetailRes.getCommunityId();
        Long operatorId = commentDetailRes.getOperatorId();

        // 查询评论
        List<PublishComment> publishComment = publishCommentMapper
                .selectByTenantIdAndCommunityIdAndOperatorId(tenantId, communityId, operatorId);

        List<CommentVo> commentVoList = BeanUtil.copyToList(publishComment, CommentVo.class);

        for (CommentVo commentVo : commentVoList) {
            Long userId = commentVo.getUserId();
            SysUser sysUser = sysUserService.selectUserById(userId);
            commentVo.setUserName(sysUser.getNickName());
            commentVo.setUserAvatar(sysUser.getAvatar());
        }

        return R.ok(commentVoList);
    }

    @Override
    public R<Object> addComment(CommentRes commentRes) {
        Long tenantId = commentRes.getTenantId();
        Long communityId = commentRes.getCommunityId();
        Long operatorId = commentRes.getOperatorId();

        CommunityUser communityUser = communityUserMapper.selectCommunityUser(tenantId, communityId, SecurityUtils.getUserId());

        if (Objects.isNull(communityUser)) {
            return R.fail(HttpStatus.SHOW_ERROR_MSG,"评论失败，您不是该社区成员");
        }

        if ("1".equals(communityUser.getIsBlack())) {
            return R.fail(HttpStatus.SHOW_ERROR_MSG,"评论失败，您已被拉黑");
        }

        Publish publish = publishMapper.selectByIdAndTenantIdAndCommunityId(operatorId, tenantId, communityId);
        if (Objects.isNull(publish)) {
            return R.fail(HttpStatus.SHOW_ERROR_MSG,"评论失败，该内容不存在");
        }

        PublishComment publishComment = new PublishComment();
        BeanUtil.copyProperties(commentRes, publishComment);
        publishComment.setPublishId(operatorId);
        publishComment.setUserId(SecurityUtils.getUserId());
        publishComment.setType(0);

        publishCommentMapper.insert(publishComment);

        return R.ok(null, "评论成功");
    }

    @Override
    public R<Object> removeCommentById(CommentDelRes commentDelRes) {
        Long id = commentDelRes.getId();
        Long tenantId = commentDelRes.getTenantId();
        Long communityId = commentDelRes.getCommunityId();
        Long operatorId = commentDelRes.getOperatorId();

        PublishComment publishComment = publishCommentMapper.selectByIdAndTenantIdAndCommunityIdAndOperatorId(id, tenantId, communityId, operatorId);

        if (Objects.isNull(publishComment)) {
            return R.fail(HttpStatus.SHOW_ERROR_MSG,"删除失败，该评论不存在");
        }

        publishCommentMapper.deleteByIdAndTenantIdAndCommunityIdAndOperatorId(id, tenantId, communityId, operatorId);

        return R.ok(null, "删除成功");
    }

}
