package idc.service.command;

import idc.model.IdcCmd;
import idc.model.IdcDAO;
import idc.model.IdcSetHistory;

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

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import utility.IdcGetClientNameCode;
import utility.IdcSetIP;

public class IdcIPChangeCmd implements IdcCmd
{
	private IdcDAO dao = new IdcDAO();
	
	@Override
	public void execute(HttpServletRequest request, HttpServletResponse response)
	{
		String mkworker = request.getParameter("mkworker");
		String service_code = request.getParameter("service_code");
		String category = request.getParameter("category");
		String ip_address = request.getParameter("ip");
		if(category.equals("main"))
			setMain(service_code, ip_address,mkworker);
		else if(category.equals("sub"))
			setSub(service_code, ip_address, request.getParameter("old_ip"),mkworker);
		
		request.setAttribute("service_code", service_code);
	}
	
	//보조 아이피 변경
	private void setSub(String service_code, String ip_address, String old_ip, String mkworker)
	{
		Connection conn=null;
		PreparedStatement pstmt=null, pstmt2=null;
		ResultSet rs=null;
		String sql="";
				
		try	
		{
			conn=dao.getConn();
			sql="SELECT service_ip, client_code, server_code FROM servicedb WHERE service_code=?";
			pstmt =conn.prepareStatement(sql);
			pstmt.setString(1, service_code);
			rs = pstmt.executeQuery();
			if(rs.next())
			{
				IdcSetIP.SetIP("일반", null, 1, null, service_code, old_ip);
				IdcSetIP.SetIP("ip_sub", IdcGetClientNameCode.getClientName(rs.getString("client_code")),
									0, null, service_code, ip_address);
				IdcSetHistory.setHistory(service_code, rs.getString("server_code"), "보조 IP 변경", "이전IP : " + old_ip, "변경IP : "+ip_address, mkworker,
													IdcGetClientNameCode.getClientName(rs.getString("client_code")));
			}
			
			sql="UPDATE adddb SET adddb_ip=? WHERE adddb_ip=?";
			pstmt2 = conn.prepareStatement(sql);
			pstmt2.setString(1, ip_address);
			pstmt2.setString(2, old_ip);
			pstmt2.executeUpdate();
		}
		catch (Exception e) 
		{
			System.out.println("IdcIPChangeCmd 에서의 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("IdcIPChangeCmd 에서의 sqlexception");
				e.printStackTrace();
			}
		}
	}
	
	//메인 아이피 변경
	private void setMain(String service_code, String ip_address, String mkworker)
	{
		Connection conn=null;
		PreparedStatement pstmt=null, pstmt2=null, pstmt3=null;
		ResultSet rs=null;
		String sql="";
				
		try	
		{
			conn=dao.getConn();
			sql="SELECT service_ip, client_code, server_code FROM servicedb WHERE service_code=?";
			pstmt =conn.prepareStatement(sql);
			pstmt.setString(1, service_code);
			rs = pstmt.executeQuery();
			
			if(rs.next())
			{
				IdcSetIP.SetIP("일반", null, 1, null, service_code, rs.getString("service_ip"));
				IdcSetIP.SetIP("ip_main", IdcGetClientNameCode.getClientName(rs.getString("client_code")),
									0, null, service_code, ip_address);
				IdcSetHistory.setHistory(service_code, rs.getString("server_code"), "IP 변경", "이전IP : " +rs.getString("service_ip"),"변경IP : "+ip_address,mkworker, 
													IdcGetClientNameCode.getClientName(rs.getString("client_code")));
			}
			
			sql="UPDATE servicedb SET service_ip=? WHERE service_code=?";
			pstmt2=conn.prepareStatement(sql);
			pstmt2.setString(1, ip_address);
			pstmt2.setString(2, service_code);
			pstmt2.executeUpdate();
			
			sql="UPDATE adddb SET adddb_ip=? WHERE service_code=? AND adddb_case='service' ";
			pstmt3=conn.prepareStatement(sql);
			pstmt3.setString(1, ip_address);
			pstmt3.setString(2, service_code);
			pstmt3.executeUpdate();
		}
		catch (Exception e) 
		{
			System.out.println("IdcIPChangeCmd 에서의 exception");
			e.printStackTrace();
		}
		finally
		{
			try 
			{
				if(conn!=null) conn.close();
				if(pstmt!=null)pstmt.close();
				if(rs!=null)rs.close();
			}
			catch (SQLException e) 
			{
				System.out.println("IdcIPChangeCmd 에서의 sqlexception");
				e.printStackTrace();
			}
		}
	}
}
