package com.mcwl.communityCenter.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mcwl.common.constant.HttpStatus;
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.PublishCommentLikeRes;
import com.mcwl.communityCenter.domain.dto.PublishLikeRes;
import com.mcwl.communityCenter.mapper.*;
import com.mcwl.communityCenter.service.PublishCommentLikeService;
import com.mcwl.communityCenter.service.PublishLikeService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Objects;

/**
 * 评论区点赞业务层
 */
@Service
@RequiredArgsConstructor
public class PublishLikeServiceImpl extends ServiceImpl<PublishLikeMapper, PublishLike> implements PublishLikeService {

    private final PublishMapper publishMapper;

    private final CommunityAdviceMapper communityAdviceMapper;

    private final CommunityUserMapper communityUserMapper;


    @Override
    public void like(PublishLikeRes publishLikeRes) {

        Long publishId = publishLikeRes.getPublishId();
        Long tenantId = publishLikeRes.getTenantId();
        Long communityId = publishLikeRes.getCommunityId();
        Publish publish = publishMapper.selectByIdAndTenantIdAndCommunityId(publishId,
                tenantId,
                communityId);

        if (Objects.isNull(publish)) {
            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);
        }


        PublishLike publishLike = baseMapper.selectPublishLike(publishLikeRes, SecurityUtils.getUserId());

        if (Objects.isNull(publishLike)) {
            publishLike = PublishLike.builder()
                    .tenantId(tenantId)
                    .communityId(communityId)
                    .publishId(publishId)
                    .userId(SecurityUtils.getUserId())
                    .build();
            baseMapper.insert(publishLike);

            publish.setLikeNum(publish.getLikeNum() + 1);
            publishMapper.updateLikeNum(publish);
            this.addLikeAdvice(publish);
            return;
        }

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

        baseMapper.updateDelFlagById(publishLike);
        publishMapper.updateLikeNum(publish);

    }

    private void addLikeAdvice(Publish publish) {
        CommunityAdvice communityAdvice = new CommunityAdvice();

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

        communityAdviceMapper.insert(communityAdvice);
    }
}
