package idc.service.command;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import idc.model.*;
import java.sql.*;
import java.util.ArrayList;

public class IdcDeleteServiceHandleCmd implements IdcCmd{

	IdcDAO dao = new IdcDAO();
	
	public void execute(HttpServletRequest request, HttpServletResponse response) {
		
		String service_code = request.getParameter("service_code");
		String client_code = request.getParameter("client_code");
		
		request.setAttribute("service_code", service_code);
		request.setAttribute("client_code", client_code);
		
		ArrayList<IdcDeleteServiceDTO>list;
		list = getDeleteServiceInfo(service_code);
		request.setAttribute("list", list);
		
		IdcDeleteServiceDTO dto;
		dto = getServiceDTO(service_code);
		
		request.setAttribute("dto", dto);
	}
	
	//삭제 리스트 불러오기
	public ArrayList<IdcDeleteServiceDTO> getDeleteServiceInfo(String service_code){
		ArrayList<IdcDeleteServiceDTO>list = new ArrayList<IdcDeleteServiceDTO>();
		
		Connection conn = null;
		PreparedStatement pstmt = null, pstmt2=null, pstmt3 = null, pstmt4 = null;
		ResultSet rs = null, rs2=null, rs3=null, rs4=null;
		String sql;
		
		try {
			conn = dao.getConn();
			
			//서비스 방어라인 부분
			sql = "SELECT adddb_num, adddb_request_date, adddb_case, adddb_code ";
			sql += " FROM adddb WHERE service_code=? AND adddb_case='defence' ";
			
			pstmt2 = conn.prepareStatement(sql);
			pstmt2.setString(1, service_code);
			
			rs2 = pstmt2.executeQuery();
			if(rs2.next())
			{
				IdcDeleteServiceDTO dto = new IdcDeleteServiceDTO();
				
				dto.setContent("방어 서비스");
				dto.setContent_code(rs2.getString("adddb_num"));
				dto.setContent_db("addDB");
				dto.setContent_request_date(rs2.getDate("adddb_request_date"));
				dto.setAdddb_code(rs2.getString("adddb_code"));
				dto.setSub_content(rs2.getString("adddb_case"));
				list.add(dto);
			}
			
			//addDB 부분
			sql = "SELECT addDB_num, addDB_request_date, addDB_case, addDB_code ";
			sql +=" FROM addDB WHERE service_code=? AND (adddb_case='ip' OR adddb_case='etc') ";			
			
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, service_code);
			
			rs = pstmt.executeQuery();
			
			while(rs.next()){
				String content="기타 부가서비스";
				if(rs.getString("adddb_case").equals("IP"))
					content = "IP";
				String content_code = rs.getString("addDB_num");
				String content_db = "addDB";
				Date content_request_date = rs.getDate("addDB_request_date");
				String sub_content = rs.getString("addDB_case");
				String adddb_code = rs.getString("adddb_code");
				IdcDeleteServiceDTO writing = new IdcDeleteServiceDTO();
				
				writing.setContent(content);
				writing.setContent_code(content_code);
				writing.setContent_db(content_db);
				writing.setContent_request_date(content_request_date);
				writing.setSub_content(sub_content);
				writing.setAdddb_code(adddb_code);
				list.add(writing);					
			}
			
			//vpcDB 작업
			sql = "SELECT vpc_num, vpc_request_date, vpc_code, vpc_csip ";
			sql +=" FROM vpcDB WHERE service_code=?";			
			
			pstmt3 = conn.prepareStatement(sql);
			pstmt3.setString(1, service_code);
			
			rs3 = pstmt3.executeQuery();
			
			
			while(rs3.next()){								
				
				String content = "VPC ";
				String content_code = rs3.getString("vpc_num");
				String content_db = "vpcDB";
				Date content_request_date = rs3.getDate("vpc_request_date");
				String sub_content = rs3.getString("vpc_code")+" " + rs3.getString("vpc_csip");
				
				IdcDeleteServiceDTO writing = new IdcDeleteServiceDTO();
				writing.setContent(content);
				writing.setContent_code(content_code);
				writing.setContent_db(content_db);
				writing.setContent_request_date(content_request_date);
				writing.setSub_content(sub_content);
				list.add(writing);					
			}
			
			//kcsDB 작업
			sql = "SELECT kcs_num, kcs_request_date, kcs_code, kcs_ip ";
			sql +=" FROM kcsDB WHERE service_code=?";			
			
			pstmt4 = conn.prepareStatement(sql);
			pstmt4.setString(1, service_code);
			
			rs4 = pstmt4.executeQuery();
						
			while(rs4.next()){								
				
				String content = "KCS ";
				String content_code = rs4.getString("kcs_num");
				String content_db = "kcsDB";
				Date content_request_date = rs4.getDate("kcs_request_date");
				String sub_content = rs4.getString("kcs_code")+" " + rs4.getString("kcs_ip");
				
				IdcDeleteServiceDTO writing = new IdcDeleteServiceDTO();
				writing.setContent(content);
				writing.setContent_code(content_code);
				writing.setContent_db(content_db);
				writing.setContent_request_date(content_request_date);
				writing.setSub_content(sub_content);
				list.add(writing);					
			}
			

		} catch (Exception e) {
			e.printStackTrace();
		} finally{
			try {
				if(rs != null) rs.close();
				if(pstmt != null) pstmt.close();
				if(rs2 != null) rs2.close();
				if(pstmt2 != null) pstmt2.close();
				if(rs3 != null) rs3.close();
				if(pstmt3 != null) pstmt3.close();
				if(rs4 != null) rs4.close();
				if(pstmt4 != null) pstmt4.close();
				if(conn != null) conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}		
		
		return list;		
	}
		
	//삭제할 서비스 가져오기
	public IdcDeleteServiceDTO getServiceDTO(String service_code){
		
		IdcDeleteServiceDTO writing = new IdcDeleteServiceDTO();
		
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		String sql = "";
		
		try {
			conn = dao.getConn();			
			
			sql = "SELECT service_request_date, service_line, service_ip FROM serviceDB WHERE service_code=?";
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1,service_code);
			rs = pstmt.executeQuery();
			
			if(rs.next()){
				String content = "전체 서비스";				
				Date service_request_date = rs.getDate("service_request_date");
				String content_code = service_code;
				String sub_content = rs.getString("service_line") +" " + rs.getString("service_ip");
								
				writing.setContent(content);
				writing.setContent_request_date(service_request_date);
				writing.setContent_code(content_code);
				writing.setSub_content(sub_content);				
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally{
			try {
				if(rs != null) rs.close();
				if(pstmt != null) pstmt.close();
				if(conn != null) conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return writing;
	}
	
}