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.GatherOracleInfo;
import com.platform.entities.OracleConnectorParams;
import com.platform.kubernetes.SimpleKubeClient;
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 {
	/**
	 * kubernetes client
	 */
	private SimpleKubeClient client = new SimpleKubeClient();
	/**
	 * 抽取
	 */
	private OracleExtractHelper oracleExtract = new OracleExtractHelper(); 
	
	/**
	 * 数据库连接实现类
	 */
	private OracleConnector connect = new OracleConnector();
	
	@Override
	public boolean extractOracle(String name, List<OracleConnectorParams> dataInfolist,
			GatherOracleInfo oracleConnect) throws Exception {
		boolean isSuccess = false;
		try{
			//map转  bean(汇总库信息-带tableName的)
			GatherOracleInfo oracleModel =  oracleConnect;
			//采集库连接参数
			List<OracleConnectorParams> datainfos = dataInfolist;
			
			Connection conn = OracleConnector.ConnectionBuilder("jdbc:oracle:thin:@" + oracleModel.getIp() + ":" + oracleModel.getPort() + "/"
					+ oracleModel.getDatabaseName(), oracleModel.getUser(), oracleModel.getPassword());			
			for (OracleConnectorParams collectOracle : datainfos) {	
				client.updateOrAddReplicasLabelById(collectOracle.getName(), "isExtract", "1"); //更新oracle汇总状态，0标示为未汇总，1标示汇总中，2标示汇总完成
				oracleExtract.createDBLink(conn, collectOracle); //创建dblink
				oracleExtract.createTableSpace(conn, oracleModel); //创建表空间
				oracleExtract.createUser(conn, oracleModel);//创建用户并授权
				oracleExtract.extractColleDB(conn, collectOracle);//执行抽取
				client.updateOrAddReplicasLabelById(collectOracle.getName(), "isExtract", "2"); //更新oracle汇总状态，0标示为未汇总，1标示汇总中，2标示汇总完成
			}
			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;
//	}

}
