package com.njcn.utils;

import com.njcn.shiro.realm.DataRealm;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class TestProcedures{
	private static final Logger logger = LoggerFactory.getLogger(TestProcedures.class);
	static Connection conn = null;
	static CallableStatement cstmt = null;
	PreparedStatement pstmt = null;
	private static String url ="jdbc:oracle:thin:@192.168.1.101:1521:pqsevent";
	private static String driver = "oracle.jdbc.driver.OracleDriver";
	private static String user = "eventdata";
	private static String password = "eventdata";

	/**
	 * 新建【稳态系统】或者【两个系统】的监测点时候需要调用存储过程
	 * @param tableName
	 */
	public static void creatNodesTable(String tableName){
		try{
			Class.forName(driver);
			conn = DriverManager.getConnection(url,user,password);
			cstmt = conn.prepareCall("{call CREATENODESTATTABLE(?)}");
			cstmt.setString(1, tableName);
			cstmt.execute();
		}catch(Exception e){
			logger.error(e.getMessage());
		}finally{
			try {
				cstmt.close();
			} catch (SQLException e) {
				logger.error("关闭cstmt连接发生异常，异常是"+e.getMessage());
			}
			try {
				conn.close();
			} catch (SQLException e) {
				logger.error("关闭conn连接发生异常，异常是"+e.getMessage());
			}
		}
	}
	
	/**
	 * 删除【稳态系统】或者【两个系统】的监测点时候需要调用存储过程
	 * @param tableName
	 */
	public static void delNodesTable(String tableName){
		try{
			Class.forName(driver);
			conn = DriverManager.getConnection(url,user,password);
			cstmt = conn.prepareCall("{call DELETENODESTATTABLE(?)}");
			cstmt.setString(1, tableName);
			cstmt.execute();
		}catch(Exception e){
			logger.error(e.getMessage());
		}finally{
			try {
				cstmt.close();
			} catch (SQLException e) {
				logger.error(e.getMessage());
			}
			try {
				conn.close();
			} catch (SQLException e) {
				logger.error(e.getMessage());
			}
		}
	}
	
	/**
	 * 新建【变电站】、【终端】、【母线】需要调用存储过程
	 * @param tableName
	 * @param kind
	 */
	public static void creatTable(String tableName,Long kind){
		try{
			Class.forName(driver);
			conn = DriverManager.getConnection(url,user,password);
			cstmt = conn.prepareCall("{call CREATESUBTABLE(?,?)}");
			cstmt.setString(1, tableName);
			cstmt.setLong(2, kind);
			cstmt.execute();
		}catch(Exception e){
			logger.error(e.getMessage());
		}finally{
			try {
				cstmt.close();
			} catch (SQLException e) {
				logger.error("关闭cstmt");
			}
			try {
				conn.close();
			} catch (SQLException e) {
				logger.error(e.getMessage());
			}
		}
	}

	/**
	 * 删除【变电站】、【终端】、【母线】需要调用存储过程
	 * @param tableName
	 * @param kind
	 */
	public static void delTable(String tableName,Long kind){
		try{
			Class.forName(driver);
			conn = DriverManager.getConnection(url,user,password);
			cstmt = conn.prepareCall("{call DELETESUBTABLE(?,?)}");
			cstmt.setString(1, tableName);
			cstmt.setLong(2, kind);
			cstmt.execute();
		}catch(Exception e){
			logger.error(e.getMessage());
		}finally{
			try {
				cstmt.close();
			} catch (SQLException e) {
				logger.error("关闭cstmt");
			}
			try {
				conn.close();
			} catch (SQLException e) {
				logger.error(e.getMessage());
			}
		}
	}
}