package com.cretin.webcore.redis

import com.cretin.webredis.RedisUtils
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service
import java.util.concurrent.TimeUnit

@Service
class RedisHelper {

    @Autowired
    private var redisUtils: RedisUtils? = null

    /**
     * 获取图片验证码的值
     */
    fun getPicCode(ip: String): String {
        return redisUtils?.get("translate_pro_cache_pic_code_$ip") ?: ""
    }

    /**
     * 设置图片验证码
     */
    fun setPicCode(ip: String, code: String) {
        redisUtils?.setEx("translate_pro_cache_pic_code_$ip", code, 10, TimeUnit.MINUTES)
    }

    /**
     * 清理图片验证码
     */
    fun clearPicCode(ip: String) {
        redisUtils?.delete("translate_pro_cache_pic_code_$ip")
    }

    /**
     * 设置手机验证码
     */
    fun setPhoneCode(phone: String, phoneCode: String) {
        redisUtils?.setEx("translate_pro_cache_phone_$phone", phoneCode + "#" + System.currentTimeMillis(), 10, TimeUnit.MINUTES)
    }

    /**
     * 获取手机验证码
     */
    fun getPhoneCode(phone: String): String {
        return redisUtils?.get("translate_pro_cache_phone_$phone") ?: ""
    }

    /**
     * 清理手机验证码
     */
    fun clearPhoneCode(phone: String) {
        redisUtils?.delete("translate_pro_cache_phone_$phone")
    }
}