package com.ruoyi.cai.manager;

import com.ruoyi.cai.constant.RedisConstant;
import com.ruoyi.cai.domain.UserForbid;
import com.ruoyi.cai.enums.ForbidTypeEnum;
import com.ruoyi.cai.ws.util.MapGetUtil;
import org.redisson.api.RMap;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class ForbidCache {

    @Autowired
    private RedissonClient redissonClient;

    private String getKey(Integer type){
        return String.format(RedisConstant.FORBID_CACHE_REDIS,type);
    }

    public void addForbid(UserForbid userForbid){
        RMap<Object, Object> redisMap = redissonClient.getMap(getKey(userForbid.getType()));
        redisMap.put(userForbid.getMember(),userForbid.getId());
    }

    public Long checkForbid(String member, ForbidTypeEnum forbidTypeEnum){
        String key = getKey(forbidTypeEnum.getCode());
        RMap<Object, Object> redisMap = redissonClient.getMap(key);
        if(!redisMap.containsKey(member)){
            return null;
        }
        Object o = redisMap.get(member);
        return MapGetUtil.getLong(o);

    }

    public void unForbid(Integer type, String member) {
        String key = this.getKey(type);
        RMap<Object, Object> redisMap = redissonClient.getMap(key);
        redisMap.remove(member);
    }
}
