package cn.ey.biz.module.system.dal.redis.switchuser;

import cn.ey.biz.module.system.dal.redis.RedisKeyConstants;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.time.Duration;

@Repository
public class SwitchUserRedisDAO {

    @Resource
    private RedisTemplate redisTemplate;

    public void set(String app, String userId, Long value) {
        redisTemplate.opsForValue().set(formatKey(app, userId), value, Duration.ofMinutes(5));
    }

    public Long get(String app, String userId) {
        Number number = (Number)redisTemplate.opsForValue().get(formatKey(app, userId));
        if(number != null){
            return number.longValue();
        }
        return null;
    }

    public void delete(String app, String userId) {
        redisTemplate.delete(formatKey(app, userId));
    }

    private static String formatKey(String app, String userId) {
        return String.format(RedisKeyConstants.SWITCH_USER.getKeyTemplate(), app, userId);
    }

}
