/**
 * 
 */
package org.springblade.etl.service;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.extern.slf4j.Slf4j;
import org.springblade.etl.entity.bi.ReportDatasource;
import org.springblade.etl.mapper.DynamicTableMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @author wookvn
 *
 *	动态创建表和插入数据
 */
@Slf4j
@Service
public class DynamicTableService {

	@Autowired
	private DynamicTableMapper dynamicTableMapper;

	/**
	 * 执行SQL
	 * @param ds
	 * @param sqls
	 * @throws Exception
	 */
	@DS("#ds.poolName")
	public void executeSql(ReportDatasource ds, String... sqls) {
		for(String sql : sqls) {
			dynamicTableMapper.executeSql(sql);
		}
	}

	/**
	 *
	 * @param ds
	 * @param sql
	 * @return
	 */
	@DS("#ds.poolName")
	public List<Map<String, Object>> selectList(ReportDatasource ds, String sql) {
		return dynamicTableMapper.selectList(sql);
	}

	/**
	 *
	 * @param ds
	 * @param sql
	 * @return
	 */
	@DS("#ds.poolName")
	public Map<String, Object> selectOne(ReportDatasource ds, String sql) {
		return dynamicTableMapper.selectOne(sql);
	}

	/**
	 *
	 * @param page
	 * @param sql
	 * @return
	 */
	@DS("#ds.poolName")
	public List<Map<String, Object>> selectPageList(IPage<?> page, ReportDatasource ds, String sql) {
		return dynamicTableMapper.selectPageList(page, sql);
	}

	/**
	 *
	 * @param page
	 * @param sql
	 * @return
	 */
	@DS("#ds.poolName")
	public IPage<Map<String, Object>> selectPage(IPage<?> page, ReportDatasource ds, String sql) {
		return dynamicTableMapper.selectPage(page, sql);
	}

	/*** ====================兼容老版本对外暴露的接口[后续在某个版本删除掉]=================**/
	/**
	 * 
	 * @param ds
	 * @param sql
	 * @return
	 */
	@DS("#ds.poolName")
	@Deprecated
	public List<JSONObject> selectListForJSONObject(ReportDatasource ds, String sql) {
		return dynamicTableMapper.selectListForJSONObject(sql);
	}
	
	/**
	 * 
	 * @param ds
	 * @param sql
	 * @return
	 */
	@DS("#ds.poolName")
	@Deprecated
	public JSONObject selectOneForJSONObject(ReportDatasource ds, String sql) {
		return dynamicTableMapper.selectOneForJSONObject(sql);
	}
	
	/**
	 * 
	 * @param page
	 * @param sql
	 * @return
	 */
	@DS("#ds.poolName")
	@Deprecated
	public IPage<JSONObject> selectPageForJSONObject(IPage<?> page, ReportDatasource ds, String sql) {
		return dynamicTableMapper.selectPageForJSONObject(page, sql);
	}
}
