package idc.main.command;

import idc.model.IDCserviceListDTO;
import idc.model.IdcClientDBDTO;
import idc.model.IdcCmd;
import idc.model.IdcDAO;
import idc.model.IdcIPDBDTO;
import idc.model.IdcServerDTO;

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

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

public class IdcTotalSearchFormCmd implements IdcCmd
{
	private IdcDAO dao=null;
	
	public IdcTotalSearchFormCmd()
	{
		dao = new IdcDAO();
	}
	
	//전체검색결과창
	@Override
	public void execute(HttpServletRequest request, HttpServletResponse response)
	{
		String sc = request.getParameter("totSearchContent");
		if(sc==null){
			return;
		}else{
			sc = sc.trim();
		}
		
		ArrayList<IdcClientDBDTO> clientList = new ArrayList<IdcClientDBDTO>();
		ArrayList<IdcServerDTO> serverList = new ArrayList<IdcServerDTO>();
		ArrayList<IDCserviceListDTO> serviceList = new ArrayList<IDCserviceListDTO>();
		ArrayList<IdcIPDBDTO> ipList = new ArrayList<IdcIPDBDTO>();
		
		getList(clientList, serverList, serviceList, ipList, sc);
		
		request.setAttribute("ClientList", clientList);
		request.setAttribute("ServerList", serverList);
		request.setAttribute("ServiceList", serviceList);
		request.setAttribute("IPList", ipList);
	}
	//ClientList 얻어오기
	private void getList(ArrayList<IdcClientDBDTO> clientList, ArrayList<IdcServerDTO> serverList,
								 ArrayList<IDCserviceListDTO> serviceList, ArrayList<IdcIPDBDTO> ipList, String sc)
	{
		Connection conn=null;
		PreparedStatement pstmt=null, pstmt2=null, pstmt3=null, pstmt4=null;
		ResultSet rs=null, rs2=null, rs3=null, rs4=null;
		
		try	
		{
			conn=dao.getConn();
			//클라이언트 리스트 얻기.
			String sql="SELECT * FROM clientdb WHERE client_name like '%"+sc+"%'";
			pstmt =conn.prepareStatement(sql);
			rs = pstmt.executeQuery();
			while(rs.next())
			{
				IdcClientDBDTO dto = new IdcClientDBDTO();
				
				dto.setClient_code(rs.getString("client_code"));
				dto.setClient_email1(rs.getString("client_email1"));
				dto.setClient_email2(rs.getString("client_email2"));
				dto.setClient_name(rs.getString("client_name"));
				dto.setClient_note(rs.getString("client_note"));
				dto.setClient_phone1(rs.getString("client_phone1"));
				dto.setClient_phone2(rs.getString("client_phone2"));
				dto.setClient_receive_date(rs.getDate("client_receive_date"));
				dto.setClient_renew_date(rs.getDate("client_renew_date"));
				dto.setClient_reseller(rs.getString("client_reseller"));
				
				clientList.add(dto);
				
				//해당 클라이언트의 사용중인 서비스 얻기.
				
				sql="SELECT * FROM servicedb WHERE client_code=?";
				pstmt2=conn.prepareStatement(sql);
				pstmt2.setString(1, dto.getClient_code());
				rs2 = pstmt2.executeQuery();
				while(rs2.next())
				{
					IDCserviceListDTO sdto = new IDCserviceListDTO();
					
					sdto.setService_code(rs2.getString("service_code"));
					sdto.setService_line(rs2.getString("service_line"));
					sdto.setClient_code(rs2.getString("client_code"));
					sdto.setService_status(rs2.getString("service_status"));
					sdto.setClient_name(dto.getClient_name());
					sdto.setService_case(rs2.getString("service_case"));
					sdto.setServer_code(rs2.getString("server_code"));
					sdto.setService_sw(rs2.getString("service_sw"));
					sdto.setService_os(rs2.getString("service_os"));
					sdto.setService_ip(rs2.getString("service_ip"));
					sdto.setService_request_date(rs2.getDate("service_request_date"));
					sdto.setService_payment_date(rs2.getDate("service_payment_date"));
					sdto.setService_amount(rs2.getString("service_amount"));
					sdto.setService_nonpayment(rs2.getString("service_nonpayment"));
					sdto.setService_manager(rs2.getString("service_manager"));
					sdto.setService_note(rs2.getString("service_note"));
					
					serviceList.add(sdto);
					
					sql="SELECT * FROM serverdb WHERE server_code=?";
					pstmt3 = conn.prepareStatement(sql);
					pstmt3.setString(1, sdto.getServer_code());
					rs3 = pstmt3.executeQuery();
					if(rs3.next())
					{
						IdcServerDTO vdto = new IdcServerDTO();
						vdto.setServer_num(rs3.getInt("server_num"));
						vdto.setServer_use_status(rs3.getString("server_use_status"));
						vdto.setServer_code(rs3.getString("server_code"));
						vdto.setServer_process(rs3.getString("server_process"));
						vdto.setServer_spec(rs3.getString("server_spec"));
						vdto.setServer_cpuname(rs3.getString("server_cpuname"));
						vdto.setServer_ram(rs3.getString("server_ram"));
						vdto.setServer_hdd(rs3.getString("server_hdd"));
						vdto.setServer_brand(rs3.getString("server_brand"));
						vdto.setServer_location(rs3.getString("server_location"));
						vdto.setServer_fomat_date(rs3.getDate("server_fomat_date"));
						vdto.setServer_recieve_date(rs3.getDate("server_recieve_date"));
						vdto.setServer_release_date(rs3.getDate("server_release_date"));
						vdto.setServer_note(rs3.getString("server_note"));
						vdto.setServer_disuse_date(rs3.getDate("server_disuse_date"));
						serverList.add(vdto);
					}
					sql="SELECT * FROM ipdb WHERE ip_address=?";
					pstmt4 = conn.prepareStatement(sql);
					pstmt4.setString(1, sdto.getService_ip());
					rs4 = pstmt4.executeQuery();
					if(rs4.next())
					{
						IdcIPDBDTO idto = new IdcIPDBDTO();
						
						idto.setLine_case(rs4.getString("line_name"));
						idto.setLine_code(rs4.getString("line_code"));
						idto.setIp_user(rs4.getString("ip_user"));
						idto.setIp_address(rs4.getString("ip_address"));
						idto.setIp_able(rs4.getInt("ip_able"));
						idto.setCancel_reason(rs4.getString("cancel_reason"));
						idto.setBlock(rs4.getString("block"));
						idto.setBlock_endday(rs4.getDate("block_endday"));
						idto.setIp_note(rs4.getString("ip_note"));
						
						ipList.add(idto);
					}
				}
			}
			
		}
		catch (Exception e) 
		{
			System.out.println("IdcTotalSearchFormCmd - getList 에서의 exception");
			e.printStackTrace();
		}
		finally
		{
			try 
			{
				if(conn!=null) conn.close();
				if(pstmt!=null)pstmt.close();
				if(rs!=null)rs.close();
				if(pstmt2!=null)pstmt2.close();
				if(rs2!=null)rs2.close();
				if(pstmt3!=null)pstmt3.close();
				if(rs3!=null)rs3.close();
				if(pstmt4!=null)pstmt4.close();
				if(rs4!=null)rs4.close();
			}
			catch (SQLException e) 
			{
				System.out.println("IdcTotalSearchFormCmd - getList 에서의 sqlexception");
				e.printStackTrace();
			}
		}
	}
	
	
	private ArrayList<IdcClientDBDTO> getClientLIst(String sc)
	{
		ArrayList<IdcClientDBDTO> list = new ArrayList<IdcClientDBDTO>();
		
		Connection conn=null;
		PreparedStatement pstmt=null;
		ResultSet rs=null;
		try	
		{
			conn=dao.getConn();
			String sql="SELECT * FROM clientdb ";
			sql+="WHERE client_name like '%"+sc+"%' ";
			pstmt =conn.prepareStatement(sql);
			rs=pstmt.executeQuery();
			while(rs.next())
			{
				IdcClientDBDTO dto = new IdcClientDBDTO();
				
				dto.setClient_code(rs.getString("client_code"));
				dto.setClient_email1(rs.getString("client_email1"));
				dto.setClient_email2(rs.getString("client_email2"));
				dto.setClient_name(rs.getString("client_name"));
				dto.setClient_note(rs.getString("client_note"));
				dto.setClient_phone1(rs.getString("client_phone1"));
				dto.setClient_phone2(rs.getString("client_phone2"));
				dto.setClient_receive_date(rs.getDate("client_receive_date"));
				dto.setClient_renew_date(rs.getDate("client_renew_date"));
				dto.setClient_reseller(rs.getString("client_reseller"));
				
				list.add(dto);
			}
		}
		catch (Exception e) 
		{
			System.out.println("IdcTotalSearchFormCmd - getClient 에서의 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("IdcTotalSearchFormCmd - getClient 에서의 sqlexception");
				e.printStackTrace();
			}
		}
		
		return list;
	}
	//ServerList 얻어오기
	private ArrayList<IdcServerDTO> getServerList(String sc)
	{
		ArrayList<IdcServerDTO> list = new ArrayList<IdcServerDTO>();
		
		Connection conn=null;
		PreparedStatement pstmt=null;
		ResultSet rs=null;
		try	
		{
			conn=dao.getConn();
			String sql="SELECT * FROM serverdb ";
			sql+="WHERE server_code like '%"+sc+"%' ";
			pstmt =conn.prepareStatement(sql);
			rs=pstmt.executeQuery();
			while(rs.next())
			{
				IdcServerDTO dto = new IdcServerDTO();
				dto.setServer_num(rs.getInt("server_num"));
				dto.setServer_use_status(rs.getString("server_use_status"));
				dto.setServer_code(rs.getString("server_code"));
				dto.setServer_process(rs.getString("server_process"));
				dto.setServer_spec(rs.getString("server_spec"));
				dto.setServer_cpuname(rs.getString("server_cpuname"));
				dto.setServer_ram(rs.getString("server_ram"));
				dto.setServer_hdd(rs.getString("server_hdd"));
				dto.setServer_brand(rs.getString("server_brand"));
				dto.setServer_location(rs.getString("server_location"));
				dto.setServer_fomat_date(rs.getDate("server_fomat_date"));
				dto.setServer_recieve_date(rs.getDate("server_recieve_date"));
				dto.setServer_release_date(rs.getDate("server_release_date"));
				dto.setServer_note(rs.getString("server_note"));
				dto.setServer_disuse_date(rs.getDate("server_disuse_date"));
				
				list.add(dto);
			}
		}
		catch (Exception e) 
		{
			System.out.println("IdcTotalSearchFormCmd - getServer 에서의 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("IdcTotalSearchFormCmd - getServer 에서의 sqlexception");
				e.printStackTrace();
			}
		}
		
		return list;
	}
	//ServiceList 얻어오기
	private ArrayList<IDCserviceListDTO> getServiceList(String sc)
	{
		ArrayList<IDCserviceListDTO> list = new ArrayList<IDCserviceListDTO>();
		Connection conn=null;
		PreparedStatement pstmt=null;
		ResultSet rs=null;
		try	
		{
			conn=dao.getConn();
			String sql="SELECT service_code, s.client_code,service_line, service_status, c.client_name, service_case, server_code, service_sw, ";
			sql+="service_os, service_ip, service_request_date, service_payment_date, service_amount, service_nonpayment, service_manager, service_note ";
			sql+="FROM servicedb as s, clientdb as c ";
			sql+="WHERE c.client_code = s.client_code AND service_code like '%"+sc+"%' ";
			System.out.println(sql);
			pstmt =conn.prepareStatement(sql);
			rs=pstmt.executeQuery();
			while(rs.next())
			{
				IDCserviceListDTO dto = new IDCserviceListDTO();
				
				dto.setService_code(rs.getString("service_code"));
				dto.setService_line(rs.getString("service_line"));
				dto.setClient_code(rs.getString("s.client_code"));
				dto.setService_status(rs.getString("service_status"));
				dto.setClient_name(rs.getString("c.client_name"));
				dto.setService_case(rs.getString("service_case"));
				dto.setServer_code(rs.getString("server_code"));
				dto.setService_sw(rs.getString("service_sw"));
				dto.setService_os(rs.getString("service_os"));
				dto.setService_ip(rs.getString("service_ip"));
				dto.setService_request_date(rs.getDate("service_request_date"));
				dto.setService_payment_date(rs.getDate("service_payment_date"));
				dto.setService_amount(rs.getString("service_amount"));
				dto.setService_nonpayment(rs.getString("service_nonpayment"));
				dto.setService_manager(rs.getString("service_manager"));
				dto.setService_note(rs.getString("service_note"));
				
				list.add(dto);
			}
		}
		catch (Exception e) 
		{
			System.out.println("IdcTotalSearchFormCmd - getServiceList 에서의 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("IdcTotalSearchFormCmd - getServiceList 에서의 sqlexception");
				e.printStackTrace();
			}
		}
		
		return list;
	}
	//IP 리스트 얻어오기.
	private ArrayList<IdcIPDBDTO> getIPList(String sc)
	{
		ArrayList<IdcIPDBDTO> list = new ArrayList<IdcIPDBDTO>();
		Connection conn=null;
		PreparedStatement pstmt=null;
		ResultSet rs=null;
		try	
		{
			conn=dao.getConn();
			String sql="SELECT * FROM ipdb ";
			sql+="WHERE ip_address like '%"+sc+"%' ";
			sql+="ORDER BY ip1 ASC, ip2 ASC, ip3 ASC, ip4 ASC ";
			pstmt =conn.prepareStatement(sql);
			rs=pstmt.executeQuery();
			while(rs.next())
			{
				IdcIPDBDTO dto = new IdcIPDBDTO();
				
				dto.setLine_case(rs.getString("line_name"));
				dto.setLine_code(rs.getString("line_code"));
				dto.setIp_user(rs.getString("ip_user"));
				dto.setIp_address(rs.getString("ip_address"));
				dto.setIp_able(rs.findColumn("ip_able"));
				dto.setCancel_reason(rs.getString("cancel_reason"));
				dto.setBlock(rs.getString("block"));
				dto.setBlock_endday(rs.getDate("block_endday"));
				dto.setIp_note(rs.getString("ip_note"));
				
				list.add(dto);
			}
		}
		catch (Exception e) 
		{
			System.out.println("IdcTotalSearchFormCmd - getServiceList 에서의 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("IdcTotalSearchFormCmd - getServiceList 에서의 sqlexception");
				e.printStackTrace();
			}
		}
		
		return list;
	}
}
