package com.qmth.jkserver.common;

import com.qmth.jkserver.service.CommonCacheService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
public class LockService {

//    @Autowired
//    private RedisLock redisLock;

    @Autowired
    private MemoryLock memoryLock;

    @Resource
    CommonCacheService commonCacheService;

    /**
     * 加锁
     *
     * @param key     缓存key
     * @param value   缓存value
     * @param timeout 缓存过期时间 单位毫秒
     */
    public boolean lock(String key, String value, int timeout) {
//        SysConfig jkserverSingle = commonCacheService.addSysConfigCache(SystemConstant.JKSERVER_SINGLE);
//        Optional.ofNullable(jkserverSingle).orElseThrow(() -> new JkServerException("未配置是否单例部署"));
//        boolean single = Boolean.valueOf(jkserverSingle.getConfigValue());
//        if (single) {
        return memoryLock.lock(key, value, timeout);
//        } else {
//            return redisLock.lock(key, value, timeout);
//        }
    }

    public void unlock(String key, String value) {
//        SysConfig jkserverSingle = commonCacheService.addSysConfigCache(SystemConstant.JKSERVER_SINGLE);
//        Optional.ofNullable(jkserverSingle).orElseThrow(() -> new JkServerException("未配置是否单例部署"));
//        boolean single = Boolean.valueOf(jkserverSingle.getConfigValue());
//        if (single) {
        memoryLock.unlock(key, value);
//        } else {
//            redisLock.unlock(key, value);
//        }
    }
}
