package cn.fateverse.query.factory.impl;

import cn.fateverse.common.core.utils.ObjectUtils;
import cn.fateverse.query.entity.DataSourceManage;
import cn.fateverse.query.mapper.DynamicDataSourceMapper;
import cn.fateverse.query.mapper.dynamic.OracleDynamicDataSourceMapper;
import com.alibaba.fastjson2.JSONObject;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

/**
 * @author Clay
 * @date 2023-07-24
 */
public class OracleDynamicDataSourceFactory extends AbstractDynamicDataSourceFactory {

    @Override
    public SqlSessionFactory getSqlSessionFactory(DataSourceManage dataSourceManage) {
        return super.getSqlSessionFactory(dataSourceManage, "classpath:mapper/dynamic/OracleDynamicDataSourceMapper.xml");
    }

    @Override
    public String getDataBaseUrl(DataSourceManage dataSourceManage) {
        JSONObject params = super.checkParam(dataSourceManage.getParams(), "服务名称不能为空", "serviceName");
        return "jdbc:oracle:thin:@" + dataSourceManage.getHost() + ":" + dataSourceManage.getPort() + ":" + params.getString("serviceName") + (ObjectUtils.isEmpty(dataSourceManage.getArgs()) ? "" : dataSourceManage.getArgs());
    }

    @Override
    public DynamicDataSourceMapper getTableMapper(SqlSession sqlSession) {
        return sqlSession.getMapper(OracleDynamicDataSourceMapper.class);
    }

}
