package com.ruoyi.redis;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.cache.RedisOperation;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import static com.ruoyi.common.exception.enums.ServerExceptionEnum.COMMON_EXCEPTION;


/**
 * @author liwenlong
 * @date 2023/1/4 9:13
 */

@Component
public class RedisUtil {

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private RedisOperation redisOperation;


    public <T> T getRedisObject(String key, Class<T> clazz) {

        T catchObject = getCatchObject(key, clazz);

        if (catchObject != null) {
            return catchObject;
        }

        if (redisTemplate.hasKey(key)) {
            try {
                return clazz.newInstance();
            } catch (Exception e) {
                e.printStackTrace();
                throw new ServiceException(COMMON_EXCEPTION);
            }
        }

        if (!redisOperation.lock(key + "lock", 10 * 1000)) {
            int loopTimes = 0;
            while (loopTimes++ < 10) {
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                catchObject = getCatchObject(key, clazz);
                if (catchObject != null) {
                    return catchObject;
                }
            }
            try {
                return clazz.newInstance();
            }  catch (Exception e) {
                e.printStackTrace();
                throw new ServiceException(COMMON_EXCEPTION);
            }
        }

        return null;
    }

    public <T> List<T> getRedisArray(String key, Class<T> clazz) {

        List<T> catchArray = getCatchArray(key, clazz);

        if (catchArray != null) {
            return catchArray;
        }
        if (redisTemplate.hasKey(key)) {
            return new ArrayList<>();
        }

        if (!redisOperation.lock(key + "lock", 10 * 1000)) {
            int loopTimes = 0;
            while (loopTimes++ < 10) {
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                catchArray = getCatchArray(key, clazz);
                if (catchArray != null) {
                    return catchArray;
                }
            }
            return new ArrayList<>();
        }

        return null;
    }


    private <T> T getCatchObject(String key, Class<T> clazz) {
        if (redisTemplate.hasKey(key)) {
            String value = redisTemplate.opsForValue().get(key);

            if (StringUtils.isNotBlank(value)) {
                return JSONObject.parseObject(value, clazz);
            }
        }
        return null;
    }

    private <T> List<T> getCatchArray(String key, Class<T> clazz) {
        if (redisTemplate.hasKey(key)) {
            String value = redisTemplate.opsForValue().get(key);

            if (StringUtils.isNotBlank(value)) {
                return JSONObject.parseArray(value, clazz);
            }
        }
        return null;
    }

    public void setCatchObj(String key, Object object, long timeout, TimeUnit unit) {

        redisTemplate.opsForValue().set(key, JSONObject.toJSONString(object), timeout, unit);
        redisOperation.unlock(key + "lock");
    }
}
