package com.platform.service.impl;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.platform.entities.DataInfoEntity;
import com.platform.entities.GatherOracleInfo;
import com.platform.entities.OracleConnectorParams;
import com.platform.oracle.OracleConnector;
import com.platform.service.IOracleExtractService;
import com.platform.service.OracleExtractHelper;
import com.platform.utils.Bean2MapUtils;

@Service(value = "OracleExtract")
public class OracleExtractServiceImpl implements IOracleExtractService {
	
	/**
	 * 抽取
	 */
	private OracleExtractHelper oracleExtract = new OracleExtractHelper(); 
	
	/**
	 * 数据库连接实现类
	 */
	private OracleConnector connect = new OracleConnector();
	
	@Override
	public boolean extractOracle(String name, List<Map<String, String>> dataInfoMap,
			Map<String, String> oracleConnect) throws Exception {
		boolean isSuccess = false;
		try{
			//map转  bean(汇总库信息-带tableName的)
			GatherOracleInfo oracleModel = (GatherOracleInfo) Bean2MapUtils.convertMap(GatherOracleInfo.class, oracleConnect);
			
			//采集库连接参数
			List<OracleConnectorParams> datainfos = new ArrayList<OracleConnectorParams>();
			for (Map<String, String> map : dataInfoMap) {
				OracleConnectorParams dataInfoEntity = (OracleConnectorParams) Bean2MapUtils.convertMap(OracleConnectorParams.class, oracleConnect);
				datainfos.add(dataInfoEntity);
			}
			
			Connection conn = OracleConnector.ConnectionBuilder("jdbc:oracle:thin:@" + oracleModel.getIp() + ":" + oracleModel.getPort() + "/"
					+ oracleModel.getDatabaseName(), oracleModel.getUser(), oracleModel.getPassword());
			
			for (OracleConnectorParams collectOracle : datainfos) {
				
				oracleExtract.createDBLink(conn, collectOracle);
				oracleExtract.createTableSpace(conn, oracleModel);
				oracleExtract.createUser(conn, oracleModel);
				oracleExtract.extractColleDB(conn, collectOracle);
			}
			isSuccess = true;
		}catch(Exception e){
			
		}
		return false;
	}

//	@Override
//	public boolean extractOracle(String name, List<OracleConnectorParams> dataInfos, GatherOracleInfo oracleConnect) throws Exception {
//		boolean isSuccess = false;
//		try{
//			//map转  bean(汇总库信息-带tableName的)
////			GatherOracleInfo oracleModel = (GatherOracleInfo) Bean2MapUtils.convertMap(GatherOracleInfo.class, oracleConnect);
//			
//			//采集库连接参数
////			List<OracleConnectorParams> datainfos = new ArrayList<OracleConnectorParams>();
////			for (Map<String, String> map : dataInfoMap) {
////				OracleConnectorParams dataInfoEntity = (OracleConnectorParams) Bean2MapUtils.convertMap(OracleConnectorParams.class, oracleConnect);
////				datainfos.add(dataInfoEntity);
////			}
//			
//			Connection conn = OracleConnector.ConnectionBuilder("jdbc:oracle:thin:@" + oracleConnect.getIp() + ":" + oracleConnect.getPort() + "/"
//					+ oracleConnect.getDatabaseName(), oracleConnect.getUser(), oracleConnect.getPassword());
//			
//			for (OracleConnectorParams collectOracle : dataInfos) {
//				
//				oracleExtract.createDBLink(conn, collectOracle);
//				oracleExtract.createTableSpace(conn, oracleConnect);
//				oracleExtract.createUser(conn, oracleConnect);
//				oracleExtract.extractColleDB(conn, collectOracle);
//			}
//			isSuccess = true;
//		}catch(Exception e){
//			
//		}
//		return false;
//	}

}
