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.*;
import com.ruoyi.cai.dto.admin.vo.UserCountAdminVo;
import com.ruoyi.cai.executor.ExecutorConstant;
import com.ruoyi.cai.mapper.UserCountMapper;
import com.ruoyi.cai.mapper.UserGiftMapper;
import com.ruoyi.cai.service.*;
import com.ruoyi.common.core.domain.PageQuery;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 用户信息数量统计Service业务层处理
 *
 * @author 77
 * @date 2023-12-24
 */
@RequiredArgsConstructor
@Service
public class UserCountServiceImpl extends ServiceImpl<UserCountMapper, UserCount> implements UserCountService {

    @Autowired
    private UserVisitorService userVisitorService;
    @Autowired
    private UserFollowService userFollowService;
    @Autowired
    private UserGiftService userGiftService;
    @Resource
    private UserGiftMapper userGiftMapper;
    @Autowired
    private UserAlbumService userAlbumService;
    @Autowired
    private DynamicService dynamicService;

    @Override
    public void star(Long fromUserId, Long toUserId) {
        baseMapper.starIncs(fromUserId,0,1);
        baseMapper.starIncs(toUserId,1,0);
    }

    @Override
    public void unStar(Long fromUserId, Long toUserId) {
        baseMapper.starIncs(fromUserId,0,-1);
        baseMapper.starIncs(toUserId,-1,0);
    }

    @Override
    public void visitor(Long fromUserId, Long toUserId) {
        baseMapper.visitorIncs(fromUserId,1,0);
        baseMapper.visitorIncs(toUserId,0,-1);
    }

    @Override
    public void reset(Long userId) {
        // 粉丝总数
        long fans = userFollowService.count(Wrappers.lambdaQuery(UserFollow.class).eq(UserFollow::getFollowUser, userId));
        // 关注数
        long follow = userFollowService.count(Wrappers.lambdaQuery(UserFollow.class).eq(UserFollow::getUserId, userId));
        // 足迹数
        long foot = userVisitorService.count(Wrappers.lambdaQuery(UserVisitor.class).eq(UserVisitor::getVisitorId, userId));
        // 访客数
        long visitor = userVisitorService.count(Wrappers.lambdaQuery(UserVisitor.class).eq(UserVisitor::getUserId, userId));
        // 相册数量(包含待审核中)
        long photo = userAlbumService.count(Wrappers.lambdaQuery(UserAlbum.class).eq(UserAlbum::getUserId, userId)
                .ne(UserAlbum::getStatus, 2));
        // 收到的礼物数量
        long receiveGiftCount = userGiftMapper.countGiftNum(null,userId);
        // 送出的礼物数量
        long giveGiftCount = userGiftMapper.countGiftNum(userId,null);
        UserCount update = new UserCount();
        update.setUserId(userId);
        update.setFansCount(fans);
        update.setFollowCount(follow);
        update.setFootCount(foot);
        update.setVisitorCount(visitor);
        update.setPhotoCount(photo);
        long count = dynamicService.count(Wrappers.lambdaQuery(Dynamic.class).eq(Dynamic::getUserId, userId));
        update.setDynamicCount(count);
        update.setReceiveGiftCount(receiveGiftCount);
        update.setGiveGiftCount(giveGiftCount);
        this.updateById(update);
    }

    @Override
    public void resetAsync(Long userId) {
        ExecutorConstant.SYNC_EXECUTOR.execute(() -> reset(userId));
    }

    @Override
    public UserCount getByUserId(Long userId) {
        return this.getOne(Wrappers.lambdaQuery(UserCount.class).eq(UserCount::getUserId,userId).last("limit 1"));
    }

    @Override
    public Page<UserCountAdminVo> pageAdmin(PageQuery pageQuery, UserCountAdminVo bo) {
        return baseMapper.pageAdmin(pageQuery.build(),bo);
    }
}
