package com.platform.service.impl;

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

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.platform.dao.GatherOracleDao;
import com.platform.entities.GatherOracleInfo;
import com.platform.service.IGatherOracleService;

/** oracle数据总库 管理 
 * @author chen
 *
 */
@Service(value = "mySqlService")
public class GatherOracleServiceImpl implements IGatherOracleService{
	
	public final static Logger log = Logger.getLogger(GatherOracleServiceImpl.class);
	
	/**
	 * oracle数据库 对象记录
	 */
	@Resource(name = "gatherOracleDao")
	private GatherOracleDao gatherOracleDao;

	@Override
	public List<GatherOracleInfo> findAllOracle() throws Exception {
		List<GatherOracleInfo> result = gatherOracleDao.selectAllOracle();
		return result;
	}

	@Override
	public int deleteOracle(int id) throws Exception {
		
		int result = 0;
		if (id > 0) {
			//删除汇总的库--逻辑删除
			GatherOracleInfo oracle = new GatherOracleInfo();
			oracle.setId(id);
			oracle.setRemove("1");
			result = gatherOracleDao.updateOracleById(oracle );
		}
		return result;
	}
	
	public int insertOracle(GatherOracleInfo oracle) throws Exception {
		// 连接oracle，
		int result = 0;
		//1 代表 是标准库抽取
		if ("1".equals(oracle.getType())) {
			GatherOracleInfo ora = new GatherOracleInfo();
			ora.setType("0");
			gatherOracleDao.updateAllOracle(ora);
		}
		try{
			result = gatherOracleDao.insertOracle(oracle);
		}catch(Exception e){
			log.error(e);
		}
		return result;
	}

	@Override
	public int updateOracle(GatherOracleInfo oracle) throws Exception {
		int status = 1;
		int result = 0;
		Connection conn = null /*OracleConnector.ConnectionBuilder("jdbc:oracle:thin:@" + oracle.getIp() + ":" + oracle.getPort() + ":"
				+ oracle.getDatabaseName(), oracle.getUser(), oracle.getPassword())*/;	
		if (null == conn) {
			status = 0;
		}
		oracle.setStatus(status);
		if (oracle.getId() > 0) {
			if ("1".equals(oracle.getType())) {
				GatherOracleInfo ora = new GatherOracleInfo();
				ora.setType("0");
				gatherOracleDao.updateAllOracleExceptId(ora);
			}
			result = gatherOracleDao.updateOracleById(oracle);
		}
		else {
			result = insertOracle(oracle);
		}
		return result;
	}

}
