package com.muyu.cloud.common.many.datasource;

import com.alibaba.druid.pool.DruidDataSource;
import com.muyu.cloud.common.saas.domain.model.EntInfo;
import com.muyu.cloud.common.many.datasource.factory.DruidDataSourceFactory;
import com.muyu.cloud.common.many.datasource.domain.model.DataSourceInfo;
import com.muyu.cloud.common.many.datasource.role.DynamicDataSource;
import com.muyu.common.core.utils.SpringUtils;
import lombok.extern.log4j.Log4j2;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: DongZeLiang
 * @date: 2024/6/3
 * @Description: 多数据源
 * @Version: 1.0
 */
@Log4j2
@Component
public class ManyDataSource {

    @PostConstruct
    public void init(){
        new Thread(() -> {
            try {
                Thread.sleep(10000);
            } catch (InterruptedException ignored) {}
            DruidDataSourceFactory druidDataSourceFactory = SpringUtils.getBean(DruidDataSourceFactory.class);
            DynamicDataSource dynamicDataSource = SpringUtils.getBean(DynamicDataSource.class);
            EntInfo entInfo = EntInfo.builder()
                    .entCode("ent_4588")
                    .ip("192.168.40.132")
                    .port(3308)
                    .build();
            DataSourceInfo dataSourceInfo = DataSourceInfo.hostAndPortBuild(entInfo.getEntCode(), entInfo.getIp(), entInfo.getPort());
            DruidDataSource druidDataSource = druidDataSourceFactory.create(dataSourceInfo);
            dynamicDataSource.put(dataSourceInfo.getKey(), druidDataSource);
        }).start();
    }


    private List<EntInfo> dataSourceInfoList(){
        List<EntInfo> list = new ArrayList<>();
        list.add(
                EntInfo.builder()
                        .entCode("ent_4587")
                        .ip("192.168.40.132")
                        .port(3307)
                        .build()
        );
        return list;
    }

    @Bean
    @Primary
    public DynamicDataSource dynamicDataSource(DruidDataSourceFactory druidDataSourceFactory) {

        // 企业列表 企业CODE，端口，IP

        Map<Object, Object> dataSourceMap = new HashMap<>();
        dataSourceInfoList()
                .stream()
                .map(entInfo -> DataSourceInfo.hostAndPortBuild(entInfo.getEntCode(), entInfo.getIp(), entInfo.getPort()))
                .forEach(dataSourceInfo -> {
                    dataSourceMap.put(dataSourceInfo.getKey(), druidDataSourceFactory.create(dataSourceInfo));
                });
        //设置动态数据源
        DynamicDataSource dynamicDataSource = new DynamicDataSource();
//        dynamicDataSource.setDefaultTargetDataSource(masterDataSource());
        dynamicDataSource.setTargetDataSources(dataSourceMap);
        //将数据源信息备份在defineTargetDataSources中
        dynamicDataSource.setDefineTargetDataSources(dataSourceMap);
        return dynamicDataSource;
    }
}
