package com.yonge.cooleshow.bbs.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.yonge.cooleshow.bbs.entity.BbsFollow;
import com.yonge.cooleshow.bbs.vo.BbsFollowVo;
import com.yonge.cooleshow.bbs.dto.search.BbsFollowSearch;
import com.yonge.cooleshow.bbs.dao.BbsFollowDao;
import com.yonge.cooleshow.bbs.service.BbsFollowService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

@Service
public class BbsFollowServiceImpl extends ServiceImpl<BbsFollowDao, BbsFollow> implements BbsFollowService {
    private final static Logger log = LoggerFactory.getLogger(BbsFollowServiceImpl.class);

    @Override
    public BbsFollowVo detail(Long id) {
        return baseMapper.detail(id);
    }

    @Override
    public IPage<BbsFollowVo> selectPage(IPage<BbsFollowVo> page, BbsFollowSearch query) {
        return page.setRecords(baseMapper.selectPage(page, query));
    }

    /**
     * @Description: 关注/取消关注
     * @Author: cy
     * @Date: 2022/6/13
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void follow(Long userId, Long followId) {
        BbsFollow bbsFollow = baseMapper.selectOne(Wrappers.<BbsFollow>lambdaQuery()
                .eq(BbsFollow::getUserId, userId)
                .eq(BbsFollow::getFriendUserId, followId));
        if (bbsFollow == null) {
            BbsFollow follow = new BbsFollow();
            follow.setUserId(userId);
            follow.setFriendUserId(followId);
            baseMapper.insert(follow);
        } else {
            baseMapper.delete(Wrappers.<BbsFollow>lambdaQuery()
                    .eq(BbsFollow::getUserId, userId)
                    .eq(BbsFollow::getFriendUserId, followId));
        }
    }
}