package com.zhangmeng.admin.manager.utils;

import com.zhangmeng.admin.manager.config.verificationCode.VerificationCodeConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
public class RedisTemplateUtil {

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @Autowired
    private VerificationCodeConfig verificationCodeConfig;

    /**
     * 存储验证码集合
     * @param map 验证码集合
     */
    public void set_verification_code_map(Map<String, Object> map) {
        this.redisTemplate.opsForHash().putAll(this.verificationCodeConfig.getRedisKey(), map);
    }

    /**
     * 获取验证码集合
     * @return map
     */
    public Map<String, Object> get_verification_code_map() {
        HashOperations<String, String, Object> hashOperations = this.redisTemplate.opsForHash();
        return hashOperations.entries(this.verificationCodeConfig.getRedisKey());
    }

    public boolean verification_code_isTrue(String code){
        boolean flag = false;
        Map<String, Object> verification_code_map = get_verification_code_map();
        for (Map.Entry<String, Object> entry : verification_code_map.entrySet()) {
            String value = String.valueOf(entry.getValue());
            if (value.equalsIgnoreCase(code)){
                flag = true;
                break;
            }
        }
        return flag;
    }
}
