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


import com.muyu.cloud.common.saas.domain.Datasource;
import com.muyu.common.core.domain.Result;
import com.muyu.common.system.domain.Firm;
import lombok.extern.log4j.Log4j2;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

@Component
@Log4j2
public class InitDataSource {

    public static final String USER="root";
    public static final String PASSWORD="Lw030106";

    @Bean
    @Primary
    public Result<List<Datasource>> initDatasource(){
        ArrayList<Datasource> list = new ArrayList<>();

        try {
            DriverManager.registerDriver(new com.mysql.cj.jdbc.Driver());
            Connection connection= DriverManager.getConnection("jdbc:mysql://47.101.53.251:3306/datasource?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT&useSSL=false",USER,PASSWORD);
            String sql="select * from `datasource` ";

            Statement stmt = connection.createStatement();
            ResultSet rs = stmt.executeQuery(sql);

            while (rs.next()){
                Datasource datasource = new Datasource();
                datasource.setId(rs.getInt("id"));
                datasource.setFirmName(rs.getString("firm_name"));
                datasource.setDatabaseName(rs.getString("database_name"));
                list.add(datasource);
            }

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return Result.success(list);
    };










}
