/*
 * @Author: Kane
 * @Date: 2023-07-27 17:22:55
 * @LastEditors: Kane
 * @FilePath: /desktop_archievement_backend/src/main/java/com/cpic/xim/mybatis/utils/MybatisUtils.java
 * @Description: mybatis的工具集合
 * 
 * Copyright (c) ${2022} by Kane, All Rights Reserved. 
 */
package com.cpic.xim.mybatis.utils;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

public class MybatisUtils
{
    private static HashMap<String, SqlSession> SessionMap = new HashMap<>( 5 );
    private static HashMap<String, SqlSession> SessionMapBatch = new HashMap<>( 5 );
    private static HashMap<String, SqlSessionFactory> SessionFactoryMap = new HashMap<>( 5 );
    private static HashMap<String, SqlSessionFactory> SessionFactoryMapBatch = new HashMap<>( 5 );
    private static String DEFAULT_CONFIGFILE = "mybatis/mybatis-config.xml";

    /**
     * 
     * @param configFile
     * @return
     * @throws IOException
     */
    public static SqlSession getSqlSession( String configFile ) throws IOException
    {
        // 根据配置文件的路径，查找是否已经创建了对应的session
        SqlSessionFactory factory = SessionFactoryMap.get( configFile );

        // 找到就直接返回
        if ( factory != null )
        {
            return factory.openSession();
        }

        // 没找到就创建一个
        InputStream configIS = Resources.getResourceAsStream( configFile );
        factory = new SqlSessionFactoryBuilder().build( configIS );
        SqlSession session = factory.openSession();

        // 保存
        SessionFactoryMap.put( configFile, factory );

        return session;
    }

    public static SqlSession getSqlSessionBatch( String configFile ) throws IOException
    {
        // 根据配置文件的路径，查找是否已经创建了对应的session
        SqlSessionFactory factory = SessionFactoryMap.get( configFile );

        // 找到就直接返回
        if ( factory != null )
        {
            return factory.openSession();
        }

        // 没找到就创建一个
        InputStream configIS = Resources.getResourceAsStream( configFile );
        factory = new SqlSessionFactoryBuilder().build( configIS );
        SqlSession session = factory.openSession( ExecutorType.BATCH, false );

        // 保存
        SessionFactoryMap.put( configFile, factory );

        return session;
    }

    /**
     * 
     * @return
     * @throws IOException
     */
    public static SqlSession getSqlSession() throws IOException
    {
        // 根据配置文件的路径，查找是否已经创建了对应的session
        SqlSessionFactory factory = SessionFactoryMap.get( DEFAULT_CONFIGFILE );

        // 找到就直接返回
        if ( factory != null )
        {
            return factory.openSession();
        }

        // 没找到就创建一个
        InputStream configIS = Resources.getResourceAsStream( DEFAULT_CONFIGFILE );
        factory = new SqlSessionFactoryBuilder().build( configIS );
        SqlSession session = factory.openSession();

        // 保存
        SessionFactoryMap.put( DEFAULT_CONFIGFILE, factory );

        return session;
    }

    /**
     * 获取一个Batch模式的sqlsession，使用默认路径的mybatis-config.xml文件。
     * 先判断SessionMapBatch中是否有和配置文件对应的缓存的sqlsession对象，有就直接使用。
     * 没有就创建一个新的，然后放入SessionMapBatch中。
     * @return ExecutorType.BATCH模式的sqlsession实例对象
     * @throws IOException 读取配置文件错误时抛出IOException异常
     */
    public static SqlSession getSqlSessionBatch() throws IOException
    {
        // 根据配置文件的路径，查找是否已经创建了对应的session
        SqlSessionFactory factory = SessionFactoryMap.get( DEFAULT_CONFIGFILE );

        // 找到就直接返回
        if ( factory != null )
        {
            return factory.openSession();
        }

        // 没找到就创建一个
        InputStream configIS = Resources.getResourceAsStream( DEFAULT_CONFIGFILE );
        factory = new SqlSessionFactoryBuilder().build( configIS );
        SqlSession session = factory.openSession( ExecutorType.BATCH, false );

        // 保存
        SessionFactoryMap.put( DEFAULT_CONFIGFILE, factory );

        return session;
    }
}
