package com.ruoyi.xq.manager;

import com.ruoyi.xq.constant.RedisHttpConstant;
import lombok.Data;
import org.redisson.api.RAtomicLong;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 用户角标处理
 */
@Component
public class UserUnreadFlagManager {

    private final static String UNREAD_STAR_FLAG = "star";
    private final static String UNREAD_STAR_DYNAMIC_FLAG = "starDynamic";
    private final static String UNREAD_VISIBLE_FLAG = "visible";

    @Autowired
    private RedissonClient redissonClient;

    private String getStarKey(Long userId){
        return String.format(RedisHttpConstant.USER_UNREAD_REDIS,UNREAD_STAR_FLAG,userId);
    }
    private String getStarDynamicKey(Long userId){
        return String.format(RedisHttpConstant.USER_UNREAD_REDIS,UNREAD_STAR_DYNAMIC_FLAG,userId);
    }
    private String getVisibleKey(Long userId){
        return String.format(RedisHttpConstant.USER_UNREAD_REDIS,UNREAD_VISIBLE_FLAG,userId);
    }

    public void resetStarKey(Long userId){
        String key = getStarKey(userId);
        RAtomicLong atomicLong = redissonClient.getAtomicLong(key);
        atomicLong.set(0);
    }
    public void resetStarDynamicKey(Long userId){
        String key = getStarDynamicKey(userId);
        RAtomicLong atomicLong = redissonClient.getAtomicLong(key);
        atomicLong.set(0);
    }
    public void resetVisibleKey(Long userId){
        String key = getVisibleKey(userId);
        RAtomicLong atomicLong = redissonClient.getAtomicLong(key);
        atomicLong.set(0);
    }

    public Long getStarFlag(Long userId){
        String key = getStarKey(userId);
        RAtomicLong atomicLong = redissonClient.getAtomicLong(key);
        long nn = atomicLong.get();
        if(nn < 0){
            return 0L;
        }
        return nn;
    }

    public Long getStarDynamicFlag(Long userId){
        String key = getStarDynamicKey(userId);
        RAtomicLong atomicLong = redissonClient.getAtomicLong(key);
        long nn = atomicLong.get();
        if(nn < 0){
            return 0L;
        }
        return nn;
    }

    public Long getVisibleFlag(Long userId){
        String key = getVisibleKey(userId);
        RAtomicLong atomicLong = redissonClient.getAtomicLong(key);
        long nn = atomicLong.get();
        if(nn < 0){
            return 0L;
        }
        return nn;
    }

    public void star(Long userId){
        String key = getStarKey(userId);
        RAtomicLong atomicLong = redissonClient.getAtomicLong(key);
        atomicLong.incrementAndGet();
    }

    public void unstar(Long userId){
        String key = getStarKey(userId);
        RAtomicLong atomicLong = redissonClient.getAtomicLong(key);
        atomicLong.decrementAndGet();
    }

    public void starDynamic(Long userId){
        String key = getStarDynamicKey(userId);
        RAtomicLong atomicLong = redissonClient.getAtomicLong(key);
        atomicLong.incrementAndGet();
    }

    public void unStarDynamic(Long userId){
        String key = getStarDynamicKey(userId);
        RAtomicLong atomicLong = redissonClient.getAtomicLong(key);
        atomicLong.incrementAndGet();
    }


    public void visible(Long userId){
        String key = getVisibleKey(userId);
        RAtomicLong atomicLong = redissonClient.getAtomicLong(key);
        atomicLong.incrementAndGet();
    }

}
