package com.ywt.mg.configs;

import com.ywt.mg.core.SqlHelper;
import com.ywt.mg.core.redis.RedisCacheManager;
import com.ywt.mg.core.redis.ShardedJedisProxy;
import com.ywt.rpc.core.RestCaller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
import redis.clients.jedis.BinaryJedisCommands;
import redis.clients.jedis.JedisCommands;

import java.lang.reflect.Proxy;

@Component
public class Beans {
    @Value("${service.agent.rest.addr}")
    private String restServiceAgent;

    @Autowired
    private RedisCacheManager redisCacheManager;

    /**
     * 引用 ywt_center 库的 JdbcTemplate
     */
    @Autowired
    @Qualifier("ywtCenterJdbcTemplate")
    private JdbcTemplate ywtCenterJdbcTemplate;

    /**
     * 引用 ywt_log 库的 JdbcTemplate
     */
    @Autowired
    @Qualifier("ywtDrugJdbcTemplate")
    private JdbcTemplate ywtDrugJdbcTemplate;


    @Autowired
    @Qualifier("nutrimealJdbcTemplate")
    private JdbcTemplate nutrimealJdbcTemplate;


    @Bean(name = "jedisCommandsClient")
    public JedisCommands getjedisCommands() {
        ShardedJedisProxy shardedJedisProxy = new ShardedJedisProxy(redisCacheManager);
        JedisCommands jedisProxyInstance = (JedisCommands) Proxy.newProxyInstance(shardedJedisProxy.getClass().getClassLoader(), new Class[]{JedisCommands.class, BinaryJedisCommands.class}, shardedJedisProxy);
        return jedisProxyInstance;
    }

    @Primary
    @Bean(name = "sqlHelperYwtCenter")
    public SqlHelper getSqlHelperYwtCenter() {
        return new SqlHelper(ywtCenterJdbcTemplate);
    }

    @Bean(name = "sqlHelperywtDrug")
    public SqlHelper getSqlHelperywtDrug() {
        return new SqlHelper(ywtDrugJdbcTemplate);
    }
    @Bean(name = "sqlHelperNutrimeal")
    public SqlHelper getSqlHelperNutrimeal() {
        return new SqlHelper(nutrimealJdbcTemplate);
    }


    @Bean(name = "restCaller")
    public RestCaller restCaller() {
        return new RestCaller(restServiceAgent);
    }




}