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

import cn.reghao.tnb.sb.util.RuntimeClass;
import cn.reghao.tnb.sb.db.autobatis.mapper.MapperManager;
import cn.reghao.tnb.sb.db.autobatis.table.Table;
import cn.reghao.tnb.sb.db.autobatis.table.TableManager;
import cn.reghao.tnb.sb.db.autobatis.table.ddl.DdlMapper;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author reghao
 * @date 2020-04-02 17:57:50
 */
public class AutoBatis {
    private final SqlSessionFactory sessionFactory;
    private final TableManager tableManager;
    private MapperManager mapperManager;
    private RuntimeClass rtClass;

    public AutoBatis(SqlSessionFactory sqlSessionFactory) {
        this.sessionFactory = sqlSessionFactory;
        this.tableManager = new TableManager();
        sessionFactory.getConfiguration().addMapper(DdlMapper.class);
    }

    public AutoBatis(SqlSessionFactory sqlSessionFactory, String configPath, String mapperDir) {
        this.sessionFactory = sqlSessionFactory;
        this.tableManager = new TableManager();
        this.mapperManager = new MapperManager();
        this.rtClass = new RuntimeClass();
        sessionFactory.getConfiguration().addMapper(DdlMapper.class);
    }

    private DdlMapper ddlMapper() {
        sessionFactory.getConfiguration().addMapper(DdlMapper.class);
        SqlSession sqlSession = sessionFactory.openSession();
        return sqlSession.getMapper(DdlMapper.class);
    }

    /**
     * 创建 POJO 对应的表
     *
     * @param
     * @return
     * @date 2020-04-09 下午3:10
     */
    public void createTable(Class pojo) {
        Table table = tableManager.classToTable(pojo);
        try (SqlSession sqlSession = sessionFactory.openSession()) {
            DdlMapper ddlMapper = sqlSession.getMapper(DdlMapper.class);
            ddlMapper.create(table);
        }
    }

    public void createTables(String pojoPkg) {
        List<Class> classes = rtClass.classesInPackage(pojoPkg);
        List<Table> tables = classes.stream()
                .map(tableManager::classToTable)
                .collect(Collectors.toList());

        try (SqlSession sqlSession = sessionFactory.openSession()) {
            DdlMapper ddlMapper = sqlSession.getMapper(DdlMapper.class);
            tables.forEach(ddlMapper::create);
        }
    }

    /**
     * 创建 mapper 接口对应的 xml 文件，并将 mapper 注册到 mybatis 配置文件
     *
     * @param
     * @return
     * @date 2020-04-09 下午5:29
     */
    public void createMapper(Class mapper) {
        mapperManager.mapperProcessor(mapper);
    }

    public void createMappers(String mapperPkg) {
        List<Class> mappers = rtClass.classesInPackage(mapperPkg);
        mappers.forEach(mapperManager::mapperProcessor);
    }

    public static void main(String[] args) throws Exception {
        MapperManager mapperManager = new MapperManager();

        String mapperPkg = "cn.reghao.jtool.autobatis.test.mapper";
        String mapperDir = "/home/reghao/code/aha/autobatis/src/main/resources/mapper";
        mapperManager.generateMapperFile(mapperPkg,mapperDir);
    }
}
