package com.td.Dawa.util;

import cn.hutool.crypto.asymmetric.RSA;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.td.Dawa.sys.syssetting.service.SysSettingService;
import com.td.Dawa.sys.syssetting.vo.SysSettingVo;
import com.td.Dawa.sys.syssettinglimit.service.SysSettingLimitService;
import com.td.Dawa.sys.syssettinglimit.vo.SysSettingLimitVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

/**
 *
 * 存储对应的配置
 * @author:slambb
 * @date:2022/4/19
 */

@Component
public class RedisSettingMap {
    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private SysSettingService sysSettingService;

    @Autowired
    private SysSettingLimitService sysSettingLimitService;
    

    //动态设置一个RSA key保存
    private String rsaPublicKey = "data:rsa:public";
    private String rsaPrivateKey = "data:rsa:private";
    //update 一次rsa数据
    public void updateRSAMap(){
        RSA rsa = new RSA();
        String privateKeyBase64 = rsa.getPrivateKeyBase64();
        String publicKeyBase64 = rsa.getPublicKeyBase64();
        redisTemplate.opsForValue().set(rsaPublicKey, JSONUtil.toJsonStr(publicKeyBase64));
        redisTemplate.opsForValue().set(rsaPrivateKey, JSONUtil.toJsonStr(privateKeyBase64));
    }
    //从redis 获取系统设置 rsa 设置
    public String getRSAPublicKeyBase64(){
        if (redisTemplate.hasKey(rsaPublicKey)) {
            return  redisTemplate.opsForValue().get(rsaPublicKey);
        } else {
            RSA rsa = new RSA();
            String privateKeyBase64 = rsa.getPrivateKeyBase64();
            String publicKeyBase64 = rsa.getPublicKeyBase64();
            redisTemplate.opsForValue().set(rsaPublicKey, JSONUtil.toJsonStr(publicKeyBase64));
            redisTemplate.opsForValue().set(rsaPrivateKey, JSONUtil.toJsonStr(privateKeyBase64));
            return publicKeyBase64;
        }
    }
    public String getRSAPrivateKeyBase64(){
        if (redisTemplate.hasKey(rsaPrivateKey)) {
            return  redisTemplate.opsForValue().get(rsaPrivateKey);
        } else {
            RSA rsa = new RSA();
            String privateKeyBase64 = rsa.getPrivateKeyBase64();
            String publicKeyBase64 = rsa.getPublicKeyBase64();
            redisTemplate.opsForValue().set(rsaPublicKey, JSONUtil.toJsonStr(publicKeyBase64));
            redisTemplate.opsForValue().set(rsaPrivateKey, JSONUtil.toJsonStr(privateKeyBase64));
            return privateKeyBase64;
        }
    }

    //系统设置key
    private String sysSettingKey = "data:sys:setting";
    //update 一次redis数据
    public void updateSysSettingMap(){
        SysSettingVo sysSettingVo = sysSettingService.get("1").getData();
        redisTemplate.opsForValue().set(sysSettingKey, JSONUtil.toJsonStr(sysSettingVo));
    }
    //从redis 获取系统设置 sysSettingMap
    public SysSettingVo getSysSettingMap(){
        SysSettingVo sysSettingVo;
        if (redisTemplate.hasKey(sysSettingKey)) {
            sysSettingVo = JSON.parseObject(redisTemplate.opsForValue().get(sysSettingKey),new TypeReference<SysSettingVo>(){});
        } else {
            sysSettingVo = sysSettingService.get("1").getData();
            redisTemplate.opsForValue().set(sysSettingKey, JSONUtil.toJsonStr(sysSettingVo));
        }
        return sysSettingVo;
    }
    //redis 设置 sysSettingMap
    public void setSysSettingMap(SysSettingVo sysSettingVo){
        redisTemplate.opsForValue().set(sysSettingKey, JSONUtil.toJsonStr(sysSettingVo));
    }

    //限制登录ip key
    private String sysSettingLimitKey = "data:sys:settingLimit";
    //update 一次redis数据
    public void updateSysSettingLimitMap(){
        SysSettingLimitVo sysSettingLimitVo = sysSettingLimitService.get("1").getData();
        redisTemplate.opsForValue().set(sysSettingLimitKey, JSONUtil.toJsonStr(sysSettingLimitVo));;
    }
    //从redis 获取系统设置 sysSettingLimitMap
    public SysSettingLimitVo getSysSettingLimitMap(){
        SysSettingLimitVo sysSettingLimitVo;
        if (redisTemplate.hasKey(sysSettingLimitKey)) {
            sysSettingLimitVo = JSON.parseObject(redisTemplate.opsForValue().get(sysSettingLimitKey),new TypeReference<SysSettingLimitVo>(){});
        } else {
            sysSettingLimitVo = sysSettingLimitService.get("1").getData();
            redisTemplate.opsForValue().set(sysSettingLimitKey, JSONUtil.toJsonStr(sysSettingLimitVo));
        }
        return sysSettingLimitVo;
    }
    //设置SysSettingTask到Map
    public void setSysSettingLimitMap(SysSettingLimitVo sysSettingLimitVo){
        redisTemplate.opsForValue().set(sysSettingLimitKey, JSONUtil.toJsonStr(sysSettingLimitVo));
    }


}
