package com.mcwl.communityCenter.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mcwl.common.constant.HttpStatus;
import com.mcwl.common.core.domain.R;
import com.mcwl.common.exception.ServiceException;
import com.mcwl.common.utils.SecurityUtils;
import com.mcwl.common.utils.StringUtils;
import com.mcwl.communityCenter.constant.AdviceConstant;
import com.mcwl.communityCenter.domain.*;
import com.mcwl.communityCenter.domain.dto.CommentLikeDetailRes;
import com.mcwl.communityCenter.domain.dto.PublishCommentLikeRes;
import com.mcwl.communityCenter.mapper.CommunityAdviceMapper;
import com.mcwl.communityCenter.mapper.CommunityUserMapper;
import com.mcwl.communityCenter.mapper.PublishCommentLikeMapper;
import com.mcwl.communityCenter.mapper.PublishCommentMapper;
import com.mcwl.communityCenter.service.CommunityAdviceService;
import com.mcwl.communityCenter.service.PublishCommentLikeService;
import com.mcwl.communityCenter.service.PublishCommentService;
import lombok.RequiredArgsConstructor;
import net.bytebuddy.implementation.bytecode.Throw;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Objects;

/**
 * 评论区点赞业务层
 *
 * @Author：ChenYan
 * @Project：mcwl-ai
 * @Package：com.mcwl.communityCenter.service.impl
 * @Filename：PublishCommissionLikeServiceImpl
 * @Description TODO
 * @Date：2025/1/17 14:25
 */
@Service
@RequiredArgsConstructor
public class PublishCommentLikeServiceImpl extends ServiceImpl<PublishCommentLikeMapper, PublishCommentLike> implements PublishCommentLikeService {

    private final PublishCommentMapper publishCommentMapper;

    private final CommunityUserMapper communityUserMapper;

    private final CommunityAdviceMapper  communityAdviceMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public R<Integer> like(PublishCommentLikeRes publishCommentLikeRes) {
        Long tenantId = publishCommentLikeRes.getTenantId();
        Long communityId = publishCommentLikeRes.getCommunityId();
        Long publishId = publishCommentLikeRes.getPublishId();
        Long commentId = publishCommentLikeRes.getCommentId();

        PublishComment publishComment = publishCommentMapper.selectByIdAndTenantIdAndCommunityIdAndPublishId(commentId, tenantId, communityId, publishId);

        if (Objects.isNull(publishComment)) {
            throw new ServiceException("点赞失败，该内容不存在", HttpStatus.SHOW_ERROR_MSG);
        }


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

        if (Objects.isNull(communityUser)) {
            throw new ServiceException("点赞失败，您不是该社区成员", HttpStatus.SHOW_ERROR_MSG);
        }

        if ("1".equals(communityUser.getIsBlack())) {
            throw new ServiceException("点赞失败，您已被拉黑", HttpStatus.SHOW_ERROR_MSG);
        }

        PublishCommentLike publishCommentLike = baseMapper.selectLike(publishCommentLikeRes, SecurityUtils.getUserId());

        if (Objects.isNull(publishCommentLike)) {
            publishCommentLike = new PublishCommentLike();
            publishCommentLike.setTenantId(tenantId);
            publishCommentLike.setCommunityId(communityId);
            publishCommentLike.setPublishId(publishId);
            publishCommentLike.setPublishCommentId(commentId);
            publishCommentLike.setUserId(SecurityUtils.getUserId());
            baseMapper.insert(publishCommentLike);

            publishComment.setLikeNum(publishComment.getLikeNum() + 1);
            publishCommentMapper.updateById(publishComment);
            this.addLikeAdvice(publishComment);
            return R.ok(publishComment.getLikeNum());
        }

        if (Objects.equals(publishCommentLike.getDelFlag(), "0")) {
            publishCommentLike.setDelFlag("2");
            int likeNum = publishComment.getLikeNum() - 1;
            likeNum = Math.max(likeNum, 0);
            publishComment.setLikeNum(likeNum);
        } else {
            publishCommentLike.setDelFlag("0");
            publishComment.setLikeNum(publishComment.getLikeNum() + 1);
            this.addLikeAdvice(publishComment);
        }

        baseMapper.updateDelFlagById(publishCommentLike);
        publishCommentMapper.updateById(publishComment);

        return R.ok(publishComment.getLikeNum());

    }

    @Override
    public PublishCommentLike publishCommentLike(CommentLikeDetailRes commentLikeDetailRes, Long userId) {
        return baseMapper.publishCommentLike(commentLikeDetailRes, userId);
    }

    private void addLikeAdvice(PublishComment publishComment) {
        CommunityAdvice communityAdvice = new CommunityAdvice();

        communityAdvice.setTenantId(publishComment.getTenantId());
        communityAdvice.setCommunityId(publishComment.getCommunityId());
        communityAdvice.setSendUserId(SecurityUtils.getUserId());
        communityAdvice.setAdviceType(AdviceConstant.LIKE);
        communityAdvice.setUserId(publishComment.getUserId());
        communityAdvice.setContent(StringUtils.format("{}点赞了你的评论{}",
                SecurityUtils.getLoginUser().getUser().getNickName(), publishComment.getContent()));

        communityAdviceMapper.insert(communityAdvice);
    }


}
