package com.energy.manage.service.service.cache.impl;


import com.energy.manage.service.service.cache.CacheService;
import lombok.extern.java.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Objects;

@Service
@Log
public class CacheServiceImpl implements CacheService {

    @Autowired
    protected RedisTemplate<String, Object> redisTemplate;


    @Override
    public boolean add(String key, String value) {


        return Boolean.TRUE.equals(redisTemplate.execute(
                (RedisCallback<Boolean>) connection -> connection.set(
                        redisTemplate.getStringSerializer().serialize(key), redisTemplate.getStringSerializer().serialize(value))));
    }

    @Override
    public boolean exists(String key) {


        return Boolean.TRUE.equals(redisTemplate.execute(
                (RedisCallback<Boolean>) connection -> connection.exists(Objects.requireNonNull(redisTemplate.getStringSerializer().serialize(key)))));
    }

    @Override
    public String get(String key) {

        String cacheValue = redisTemplate.execute(
                (RedisCallback<String>) connection -> {
                    byte[] keyByte = redisTemplate.getStringSerializer().serialize(key);
                    if (Boolean.TRUE.equals(connection.exists(keyByte))) {
                        byte[] value = connection.get(keyByte);
                        return redisTemplate.getStringSerializer().deserialize(value);
                    }
                    return null;
                });

        return cacheValue;
    }

    @Override
    public void setEx(String key, long expire, String value) {

        redisTemplate.execute(
                (RedisCallback<Object>) connection -> {
                    connection.setEx(Objects.requireNonNull(redisTemplate.getStringSerializer().serialize(key)), expire,
                            Objects.requireNonNull(redisTemplate.getStringSerializer().serialize(value)));
                    return null;
                });

    }

    @Override
    public void updateExpire(String key, long expire) {

        redisTemplate.execute(
                (RedisCallback<Object>) connection -> {
                    connection.expire(Objects.requireNonNull(redisTemplate.getStringSerializer().serialize(key)), expire);
                    return null;
                });
    }

    @Override
    public void delete(String key) {

        redisTemplate.execute(
                (RedisCallback<Object>) connection -> {
                    connection.del(redisTemplate.getStringSerializer().serialize(key));
                    return null;
                });

    }

    @Override
    public Long incr(String key) {

        return redisTemplate.execute(
                (RedisCallback<Long>) connection -> connection.incr(redisTemplate.getStringSerializer().serialize(key)));

    }

    @Override
    public Long incrBy(String key,long value) {

        return redisTemplate.execute(
                (RedisCallback<Long>) connection -> connection.incrBy(redisTemplate.getStringSerializer().serialize(key),value));

    }


}
