package com.ruoyi.xq.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.common.core.domain.PageQuery;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.xq.domain.User;
import com.ruoyi.xq.domain.UserVisitor;
import com.ruoyi.xq.dto.app.userstar.vo.UserStarListVo;
import com.ruoyi.xq.dto.app.uservisitor.UserVisitorQuery;
import com.ruoyi.xq.dto.app.uservisitor.vo.UserVisitorListVo;
import com.ruoyi.xq.lock.LockKey;
import com.ruoyi.xq.manager.UserUnreadFlagManager;
import com.ruoyi.xq.mapper.UserVisitorMapper;
import com.ruoyi.xq.service.UserService;
import com.ruoyi.xq.service.UserVisitorService;
import org.apache.commons.lang3.BooleanUtils;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 用户来访记录Service业务层处理
 *
 * @author 77
 * @date 2024-03-18
 */
@Service
public class UserVisitorServiceImpl extends ServiceImpl<UserVisitorMapper,UserVisitor> implements UserVisitorService {
    @Autowired
    private RedissonClient redissonClient;
    @Autowired
    private UserService userService;
    @Autowired
    private UserUnreadFlagManager userUnreadFlagManager;
    @Override
    public void visitor(Long userId, Long tarUserId) {
        User user = userService.getById(userId);
        User tarUser = userService.getById(tarUserId);
        if(user == null || tarUser == null){
            return;
        }
        UserVisitor one = this.getOne(Wrappers.lambdaQuery(UserVisitor.class).eq(UserVisitor::getUserId, userId)
                .eq(UserVisitor::getTarId, tarUserId).last("limit 1"));
        if(one != null){
            baseMapper.incsVisitorCount(one.getId());
            return;
        }
        RLock lock = redissonClient.getLock(LockKey.getUserVisitorAddLockKey(userId, tarUserId));
        try {
            lock.lock();
            one = this.getOne(Wrappers.lambdaQuery(UserVisitor.class).eq(UserVisitor::getUserId, userId)
                    .eq(UserVisitor::getTarId, tarUserId).last("limit 1"));
            if(one != null){
                return;
            }
            UserVisitor userVisitor = new UserVisitor();
            userVisitor.setUserId(user.getId());
            userVisitor.setUsercode(user.getUsercode());
            userVisitor.setTarId(tarUser.getId());
            userVisitor.setTarUsercode(tarUser.getUsercode());
            userVisitor.setVisitorNum(1);
            this.save(userVisitor);
            userUnreadFlagManager.visible(userVisitor.getTarId());
        }finally {
            lock.unlock();
        }
    }

    @Override
    public Page<UserVisitorListVo> pageApp(PageQuery pageQuery, UserVisitorQuery query) {
        Page<UserVisitorListVo> page = baseMapper.pageApp(pageQuery.build(), query);
        Long userId = LoginHelper.getUserId();
        User user = userService.getById(userId);
        if(user == null || BooleanUtils.isNotTrue(user.getOpenVip())){
            List<UserVisitorListVo> records = page.getRecords();
            for (UserVisitorListVo record : records) {
                record.setUserId(null);
                record.setNickname("***");
            }
        }
        return page;
    }

}
