package com.cpop.oam.framework.config.wxOpen;

import com.cpop.core.utils.StringUtils;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * 微信开放平台统一配置文件
 * @author DB
 * @version 1.0.0
 * @since 2024-03-26 18:14
 */
@Data
@Configuration
@ConfigurationProperties(prefix = "wx.open.redis")
public class WxOpenRedisProperties {

    /**
     * 地址
     */
    private String host;
    /**
     * 端口
     */
    private Integer port;
    /**
     * 密码
     */
    private String password;
    /**
     * 数据库
     */
    private Integer database;

    @Autowired
    private RedisProperties redisProperties;


    @Bean(name = "wxOpenRedisConnectionFactory")
    public RedisConnectionFactory wxOpenRedisConnectionFactory() {
        RedisStandaloneConfiguration configuration = new RedisStandaloneConfiguration(host, port);
        configuration.setDatabase(database);
        if (StringUtils.isNotBlank(password)) {
            configuration.setPassword(password);
        }
        return new JedisConnectionFactory(configuration);
    }

    @Bean(name = "wxOpenRedisTemplate")
    public StringRedisTemplate wxOpenRedisTemplate(@Qualifier("wxOpenRedisConnectionFactory") RedisConnectionFactory redisConnectionFactory) {
        StringRedisTemplate redisTemplate = new StringRedisTemplate();
        redisTemplate.setConnectionFactory(redisConnectionFactory);
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(new StringRedisSerializer());
        return redisTemplate;
    }
}
