package com.ozs.mdm.service.impl;

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

import com.ozs.mdm.mapper.KpiManageMapper;
import com.ozs.mdm.mapper.StorageManageMapper;
import com.ozs.mdm.service.KpiManageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ozs.mdm.dto.ColumnInfoDTO;
import com.ozs.mdm.dto.KpiListColumnDTO;
import com.ozs.mdm.dto.MdmInfoColumn;
import com.ozs.mdm.dto.MdmInfoDatabase;
import com.ozs.mdm.dto.MdmInfoTable;
import com.ozs.mdm.util.JDBCUtil;
import com.ozs.mdm.util.R;

@Service
public class KpiManageServiceImpl implements KpiManageService {

	@Autowired
	private KpiManageMapper kpiManageMapper;
	@Autowired
	private StorageManageMapper storageManageMapper;

	/**
	 * 获取已建数据库列表选项-非原始库
	 */
	@Override
	public List<MdmInfoDatabase> listDatabase(String resourceName) {
		return kpiManageMapper.listDatabase(resourceName);
	}

	@Override
	public List<MdmInfoTable> listTableByDatabaseId(String databaseId) {
		return kpiManageMapper.listTableByDatabaseId(databaseId);

	}

	@Override
	public List<MdmInfoColumn> listColumnByTableId(String tableId) {
		return kpiManageMapper.listColumnByTableId(tableId);

	}

	@Override
	public R<List<KpiListColumnDTO>> listColumn(String databaseId, String tableId, String sort, String order,
			Integer page, Integer pageSize) {
		R<KpiListColumnDTO> r = new R<>();
		r.pageStart(sort, order, page, pageSize);
		List<KpiListColumnDTO> resultList = kpiManageMapper.listColumn(databaseId, tableId);
		return r.page(resultList);
	}

	@Override
	public List<KpiListColumnDTO> listColumnByIds(List<String> ids) {
		return kpiManageMapper.listColumnByIds(ids);
	}

	@Override
	public R<ColumnInfoDTO> getColumnInfoByIds(String id) {
		return new R<ColumnInfoDTO>(kpiManageMapper.getColumnInfoByIds(id));
	}

	/**
	 * 获取已建数据库列表选项-原始库
	 */
	@Override
	public List<MdmInfoDatabase> listOriginal(String resourceName) {
		return kpiManageMapper.listOriginal(resourceName);
	}

	@Override
	public MdmInfoDatabase getDatabase(String id) {
		return kpiManageMapper.getDatabase(id);
	}

	/**
	 * 根据数据库ID 和 表ID(可选)获取 所有表和所有字段
	 */
	@Override
	public List<KpiListColumnDTO> listColumnByDatabaseId(String databaseId, String tableId) {
		return kpiManageMapper.listColumn(databaseId, tableId);
	}

	@Override
	public List<Map<String, Object>> listData(String databaseId, String sql) throws Exception {
		MdmInfoDatabase database = storageManageMapper.getDatabaseById(databaseId);
		JDBCUtil jdbcUtil = new JDBCUtil();
		jdbcUtil.init(database.getDriverName(), database.getUrl(), database.getIp(), database.getDatabaseName(),
				database.getResourcePort(), database.getUsername(), database.getPassword());
		List<Map<String, Object>> data = jdbcUtil.getData(sql);
		jdbcUtil.closeJDBC();
		return data;
	}

	@Override
	public Integer executeSql(String databaseId, String sql) throws Exception {
		MdmInfoDatabase database = storageManageMapper.getDatabaseById(databaseId);
		JDBCUtil jdbcUtil = new JDBCUtil();
		jdbcUtil.init(database.getDriverName(), database.getUrl(), database.getIp(), database.getDatabaseName(),
				database.getResourcePort(), database.getUsername(), database.getPassword());
		Integer result = jdbcUtil.update(sql);
		jdbcUtil.commit();
		jdbcUtil.closeJDBC();
		return result;
	}

}
