package cn.reghao.tnb.sb.db.mysql;

import cn.reghao.tnb.sb.db.mysql.mapper.UserProfileMapper;
import cn.reghao.tnb.sb.db.mysql.model.UserProfile;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import org.apache.ibatis.mapping.Environment;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.ibatis.transaction.TransactionFactory;
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;

import javax.sql.DataSource;
import java.io.InputStream;
import java.util.List;

/**
 * @author reghao
 * @date 2025-05-15 15:16:17
 */
public class MyClient {
    public DataSource getDataSource() {
        String host = "127.0.0.1";
        int port = 3306;
        String username = "dev";
        String password = "Dev@123456";
        String database = "tnb_account_rdb";

        HikariConfig hikariConfig = new HikariConfig();
        hikariConfig.setMinimumIdle(5);
        hikariConfig.setMaximumPoolSize(10);
        hikariConfig.setAutoCommit(true);
        hikariConfig.setIdleTimeout(30000);
        hikariConfig.setPoolName("EvaluationHikariCP");
        hikariConfig.setMaxLifetime(1800000);
        hikariConfig.setConnectionTimeout(60000);
        hikariConfig.setConnectionTestQuery("SELECT 1");
        hikariConfig.setDriverClassName("com.mysql.cj.jdbc.Driver");

        String prefix = String.format("jdbc:mysql://%s:%s/%s?", host, port, database);
        String jdbcUrl = prefix + "useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2b8";
        hikariConfig.setJdbcUrl(jdbcUrl);
        hikariConfig.setUsername(username);
        hikariConfig.setPassword(password);
        return new HikariDataSource(hikariConfig);
    }

    public SqlSessionFactory getSqlSessionFactory(List<Class<?>> mapperList) {
        DataSource dataSource = getDataSource();
        String env = "dev";
        TransactionFactory transactionFactory = new JdbcTransactionFactory();
        Environment environment = new Environment(env, transactionFactory, dataSource);
        Configuration configuration = new Configuration(environment);
        mapperList.forEach(configuration::addMapper);
        return new SqlSessionFactoryBuilder().build(configuration);
    }

    public SqlSessionFactory getSqlSessionFactory() {
        String env = "dev";
        String resource = "mybatis-config.xml";
        InputStream is = this.getClass().getClassLoader().getResourceAsStream(resource);
        return new SqlSessionFactoryBuilder().build(is, env);
    }

    public static void main(String[] args) {
        List<Class<?>> mapperList = List.of(UserProfileMapper.class);

        MyClient myClient = new MyClient();
        SqlSessionFactory sqlSessionFactory = myClient.getSqlSessionFactory();
        try (SqlSession session = sqlSessionFactory.openSession()) {
            UserProfileMapper mapper = session.getMapper(UserProfileMapper.class);
            List<UserProfile> list = mapper.findAll();
            UserProfile userProfile = mapper.findByUserId(1);
            System.out.println(userProfile);
        }
    }
}
