package com.ruoyi.ibs.app.util;


import com.ruoyi.common.exception.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 * @author 604343
 * @description: redis工具类
 * @date 2023/10/30 8:56
 */
@Component
public final class RedisUtil {
    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;



    /**
     * @return true成功
     *
     * @description: 普通缓存放入并设置时间
     * @author dz
     * @param: time 时间
     */
    public boolean set(String key, Object value, long time) {
        try {

            if (time > 0) {
                redisTemplate.opsForValue().set( key, value, time, TimeUnit.HOURS );
            } else {
                redisTemplate.opsForValue().set( key, value );
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * @return true成功
     *
     * @description: 普通缓存放入并设置时间
     * @author dz
     * @param: time 时间
     */
    public boolean setStr(String key, String value, long time) {
        try {

            if (time > 0) {
                stringRedisTemplate.opsForValue().set( key, value, time, TimeUnit.SECONDS );
            } else {
                stringRedisTemplate.opsForValue().set( key, value );
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * @return true成功
     *
     * @description: 普通缓存放入并设置时间
     * @author dz
     * @param: time 时间
     */
    public boolean setStr(String key, String value, long time, TimeUnit timeUnit) {
        try {

            if (time > 0) {
                stringRedisTemplate.opsForValue().set( key, value, time, timeUnit );
            } else {
                stringRedisTemplate.opsForValue().set( key, value );
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**'
     * 删除key
     * @param key
     * @return
     */
    public boolean removeKey(String key){
        if (key==null||key.isEmpty()){
            throw new ServiceException( "key不能为空" );
        }
        return stringRedisTemplate.delete(key);
    }

    /**
     * 设置失效时间
     * @param key
     * @param time
     * @param timeUnit
     * @return
     */
    public boolean setExpire(String key, long time, TimeUnit timeUnit){
        if (key==null||key.isEmpty()){
            throw new ServiceException( "key不能为空" );
        }
        if (timeUnit==null){
            throw new ServiceException( "时间单位不能为空" );
        }
        return stringRedisTemplate.expire(key, time, timeUnit);
    }
    /**
     * @description: 递增
     * @param: key: 键 delta: 间距(要增加几）
     * @return
     */
    public Long incr(String key,long time) {

        //没有这个键就初始化这个键并设置过期时间
        stringRedisTemplate.expire( key, 24, TimeUnit.HOURS );
//        String a =redisTemplate.opsForValue().get(key).toString();
//        if(redisTemplate.opsForValue().get(key)==null){
//            redisTemplate.opsForValue().set( key,1,time,TimeUnit.HOURS );
//            return Long.parseLong( String.valueOf( redisTemplate.opsForValue().get( key ) ) );
//        }
        return stringRedisTemplate.opsForValue().increment( key, 1 );
    }
    /**
     * @description: 获取键值对
     * @param:  key键
     * @return 值
     */
    public String get(String key) {
        return stringRedisTemplate.opsForValue().get( key );
    }
}

