package utility;

import idc.model.IdcDAO;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class IdcSetIP
{
	//IPDB 업데이트
	public static void SetIP(String ipcase, String user, int ip_able, String note, String service_code, String address)
	{
		IdcDAO dao = new IdcDAO();
		
		Connection conn=null;
		PreparedStatement pstmt=null, pstmt2=null;
		ResultSet rs=null;
		String oldUser = "";
		try	
		{
			conn=dao.getConn();
			//이전 유저 가져오기
			String sql="SELECT ip_user FROM ipdb WHERE ip_address=?";
			pstmt =conn.prepareStatement(sql);
			pstmt.setString(1, address);
			rs = pstmt.executeQuery();
			if(rs.next())
				oldUser=rs.getString("ip_user");
			
			sql="UPDATE ipdb SET ip_case2=?, ip_user=?, ip_before_user=?, ip_able=?, ip_note=?, service_code=? ";
			sql+="WHERE ip_address=? ";
			pstmt2=conn.prepareStatement(sql);
			pstmt2.setString(1, ipcase);
			pstmt2.setString(2, user);
			pstmt2.setString(3, oldUser);
			pstmt2.setInt(4, ip_able);
			pstmt2.setString(5, note);
			pstmt2.setString(6, service_code);
			pstmt2.setString(7, address);		
			pstmt2.executeUpdate();
			
		}
		catch (Exception e) 
		{
			System.out.println("IdcSetIP-SetIP 에서의 exception");
			e.printStackTrace();
		}
		finally
		{
			try 
			{
				if(conn!=null) conn.close();
				if(rs!=null)rs.close();
				if(pstmt!=null)pstmt.close();
				if(pstmt2!=null)pstmt2.close();
			}
			catch (SQLException e) 
			{
				System.out.println("IdcSetIP-SetIP 에서의 sqlexception");
				e.printStackTrace();
			}
		}
	}
	
	//IP DB업데이트
	public static void SetIP(String ipcase, String user, int ip_able, String note, String service_code, String address,
										String block, Date endday, String reason)
	{
		IdcDAO dao = new IdcDAO();
		
		Connection conn=null;
		PreparedStatement pstmt=null, pstmt2=null;
		ResultSet rs=null;
		String oldUser = "";
		try	
		{
			conn=dao.getConn();
//			이전 유저 가져오기
			String sql="SELECT ip_user FROM ipdb WHERE ip_address=?";
			pstmt =conn.prepareStatement(sql);
			pstmt.setString(1, address);
			rs = pstmt.executeQuery();
			if(rs.next())
				oldUser=rs.getString("ip_user");
			
			sql="UPDATE ipdb SET ip_case2=?, ip_user=?, ip_before_user=?, ip_able=?, ip_note=?, service_code=?, ";
			sql+="block=?, block_endday=?, cancel_reason=? ";
			sql+="WHERE ip_address=? ";
			pstmt2=conn.prepareStatement(sql);
			pstmt2.setString(1, ipcase);
			pstmt2.setString(2, user);
			pstmt2.setString(3, oldUser);
			pstmt2.setInt(4, ip_able);
			pstmt2.setString(5, note);
			pstmt2.setString(6, service_code);
			
			pstmt2.setString(7, block);
			pstmt2.setDate(8,endday);
			pstmt2.setString(9, reason);
			
			pstmt2.setString(10, address);
			pstmt2.executeUpdate();
			
		}
		catch (Exception e) 
		{
			System.out.println("IdcSetIP-SetIP 에서의 exception");
			e.printStackTrace();
		}
		finally
		{
			try 
			{
				if(conn!=null) conn.close();
				if(rs!=null)rs.close();
				if(pstmt!=null)pstmt.close();
				if(pstmt2!=null)pstmt2.close();
			}
			catch (SQLException e) 
			{
				System.out.println("IdcSetIP-SetIP 에서의 sqlexception");
				e.printStackTrace();
			}
		}
	}
}
