package com.ruoyi.cai.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.cai.domain.UserCount;
import com.ruoyi.cai.domain.UserFollow;
import com.ruoyi.cai.dto.app.query.StarQuery;
import com.ruoyi.cai.dto.app.query.StarOrVisitorReq;
import com.ruoyi.cai.dto.app.vo.user.UserStarOrVisitorList;
import com.ruoyi.cai.mapper.UserFollowMapper;
import com.ruoyi.cai.service.UserCountService;
import com.ruoyi.cai.service.UserFollowService;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.helper.LoginHelper;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 关注表Service业务层处理
 *
 * @author 77
 * @date 2023-12-23
 */
@RequiredArgsConstructor
@Service
public class UserFollowServiceImpl extends ServiceImpl<UserFollowMapper, UserFollow> implements UserFollowService {

    @Autowired
    private UserCountService userCountService;

    @Override
    public Long getFansNumByUserId(Long userId){
        UserCount count = userCountService.getByUserId(userId);
        return count.getFansCount();
    }

    @Override
    public boolean checkStar(Long userId,Long followUserId){
        long count = this.count(Wrappers.lambdaQuery(UserFollow.class)
                .eq(UserFollow::getFollowUser, followUserId)
                .eq(UserFollow::getUserId, userId));
        if(count > 0){
            return true;
        }
        return false;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean star(StarOrVisitorReq starOrVisitorReq) {
        Long fromUserId = LoginHelper.getUserId();
        this.remove(Wrappers.lambdaQuery(UserFollow.class)
                .eq(UserFollow::getUserId,fromUserId)
                .eq(UserFollow::getFollowUser, starOrVisitorReq.getToUserId()));
        UserFollow userFollow = new UserFollow();
        userFollow.setUserId(fromUserId);
        userFollow.setFollowUser(starOrVisitorReq.getToUserId());
        this.save(userFollow);
        userCountService.star(fromUserId, starOrVisitorReq.getToUserId());
        return true;
    }

    @Override
    public boolean unStar(StarOrVisitorReq starOrVisitorReq) {
        Long fromUserId = LoginHelper.getUserId();
        this.remove(Wrappers.lambdaQuery(UserFollow.class)
                .eq(UserFollow::getUserId,fromUserId)
                .eq(UserFollow::getFollowUser, starOrVisitorReq.getToUserId()));
        userCountService.star(fromUserId, starOrVisitorReq.getToUserId());
        return true;
    }

    @Override
    public Page<UserStarOrVisitorList> pageApp(PageQuery pageQuery, StarQuery query) {
        query.setUserId(LoginHelper.getUserId());
        return baseMapper.pageApp(pageQuery.build(),query);
    }
}
