package com.liangjian.dataplatform.member.service.impl;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectReader;
import com.liangjian.dataplatform.member.safe.Tuple3;
import com.liangjian.dataplatform.member.service.IPhoneSMSService;
import com.liangjian.dataplatform.member.util.http.SendUtils;
import com.liangjian.dataplatform.member.util.http.SimpleHttpClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.time.Duration;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;

/**
 * @Author: 黄子奇
 * @Date: 2019/11/15
 */
@Service
public class PhoneSMService implements IPhoneSMSService {
    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Override
    public void sendSms(String phone, String code) throws Exception {
        int i = SendUtils.sendSms(phone);
        if (i != 0) {
            Logger.getLogger("请求成功");
        } else {
            throw new Exception("请求失败");
        }
        redisTemplate.opsForValue().set(phone, String.valueOf(i), Duration.ofSeconds(60));
    }

    @Override
    public boolean verifyPhoneCode(String phone, String code) {
        String codeFromRedis = redisTemplate.opsForValue().get(phone);
        return codeFromRedis != null && codeFromRedis.equals(code);
    }
}
