package com.gci.boot.common.util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Component
public class Lock {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    public int getLock(String lockKey, String lockValue){
        boolean n = stringRedisTemplate.opsForValue().setIfAbsent(lockKey, lockValue);
        if(n){
            stringRedisTemplate.expire(lockKey, 180, TimeUnit.SECONDS);
            return 1;
        }else{
            return 0;
        }
    }

    public void delLock(String lockKey, String lockValue){
        String value = stringRedisTemplate.opsForValue().get(lockKey);
        if(lockValue.equals(value)){
            stringRedisTemplate.delete(lockKey);
        }
    }

    public String getUniqueNum(){
        String uuid = UUID.randomUUID().toString();
        String timestamp = System.currentTimeMillis()+"";
        return uuid+"-"+timestamp;
    }



}
