package com.crunii.micro.service.sysmgr.service.impl;

import com.crunii.micro.common.constants.RedisKeys;
import org.redisson.api.RMapCache;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

/**
 * @author wangchen create 2023/7/3 17:16
 */
@Service
public class LoginCacheService implements InitializingBean {

	@Autowired
	private RedissonClient redissonClient;

	private RMapCache<String, String> smsCodeCache = null;
	private RMapCache<String, String> phoneRegisterCache = null;
	private RMapCache<String, String> phoneRegisterSessionCache = null;

	@Override
	public void afterPropertiesSet() throws Exception {
		smsCodeCache = redissonClient.getMapCache(RedisKeys.PHONE_SHORT_CODE);
		phoneRegisterCache = redissonClient.getMapCache(RedisKeys.PHONE_REGISTER);
		phoneRegisterSessionCache = redissonClient.getMapCache(RedisKeys.PHONE_REGISTER_SESSION);
	}

	public void putSmsCode(String key,String value){
		smsCodeCache.put(key,value,5, TimeUnit.MINUTES);
	}

	public String getSmsCode(String key){
		return smsCodeCache.get(key);
	}

	public String removeSmsCode(String key){
		return smsCodeCache.remove(key);
	}

	public void putPhoneRegister(String key,String value){
		phoneRegisterCache.put(key,value,10, TimeUnit.MINUTES);
	}

	public String getPhoneRegister(String key){
		return phoneRegisterCache.get(key);
	}

	public String removePhoneRegister(String key){
		return phoneRegisterCache.remove(key);
	}

	public void putPhoneRegisterSession(String key,String value){
		phoneRegisterSessionCache.put(key,value,5, TimeUnit.MINUTES);
	}

	public String getPhoneRegisterSession(String key){
		return phoneRegisterSessionCache.get(key);
	}

	public String removePhoneRegisterSession(String key){
		return phoneRegisterSessionCache.remove(key);
	}


}
