package idc.service.command;

import idc.model.IdcAdditionalServiceDTO;
import idc.model.IdcAdditionalStartDTO;
import idc.model.IdcAmountDTO;
import idc.model.IdcCmd;
import idc.model.IdcDAO;
import idc.model.IdcServiceETCManageDTO;
import idc.model.IpAddDTO;

import java.sql.Connection;
import java.sql.Date;
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 IdcServiceAdditionalVPNCmd implements IdcCmd
{	
	
	IdcDAO dao = new IdcDAO();

	public void execute(HttpServletRequest request, HttpServletResponse response) {
		//System.out.println("나 serviceAdditional");
		String service_code = request.getParameter("service_code");
		IdcAdditionalServiceDTO iasd;
		iasd = getClient_info(service_code);
		
		//부가서비스추가부분
		request.setAttribute("iasd", iasd);
		
		//부가서비스 진행 내역
		ArrayList<IpAddDTO> add_list;
		add_list = getAddList(service_code);
		request.setAttribute("add_list", add_list);
		
		//부가서비스 시작일
		IdcAdditionalStartDTO start_dto;
		start_dto = getStartList(service_code);		
		request.setAttribute("start_dto", start_dto);
		
		//1회성&기타 서비스 리스트
		ArrayList<IdcServiceETCManageDTO> etc_list;
		etc_list = getEtcList(service_code);
		request.setAttribute("etc_list", etc_list);
		
		String flag = request.getParameter("flag");
		if(flag == null || flag == "") flag = "now";
		
		request.setAttribute("flag", flag);
		
		//서비스 가격정보
		IdcAmountDTO amount_dto;
		amount_dto = getAmountDTO(service_code);
		request.setAttribute("amount_dto", amount_dto);
		
	}
	
	public IdcAdditionalServiceDTO getClient_info(String service_code){

		Connection conn = null;
		PreparedStatement pstmt = null, pstmt1 = null, pstmt2 = null;
		ResultSet rs = null, rs1 = null, rs2 = null;

		IdcAdditionalServiceDTO iasd = new IdcAdditionalServiceDTO();
		
		String client_name = null;
		String client_code = null;
		String server_code = null;
		String server_process = null;
		String server_spec = null;
		String server_cpuname = null;
		String server_ram = null;
		String server_hdd = null;
		String service_amount = null;
		String client_money = null;
				
		try {
			conn = dao.getConn();
			String sql = "SELECT client_code, server_code, service_amount, service_ip FROM serviceDB WHERE service_code=?";			
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, service_code);
			
			rs = pstmt.executeQuery();
			
			if(rs.next()){
				client_code = rs.getString("client_code");
				server_code = rs.getString("server_code");
				service_amount = rs.getString("service_amount");
		
				iasd.setClient_code(client_code);
				iasd.setServer_code(server_code);
				iasd.setService_amount(service_amount);
				iasd.setService_code(service_code);
				iasd.setService_ip(rs.getString("service_ip"));
			}
			
			if(client_code != null){
				sql = "SELECT client_name, client_money FROM clientDB WHERE client_code=?";
				pstmt1 = conn.prepareStatement(sql);
				pstmt1.setString(1, client_code);
				
				rs1 = pstmt1.executeQuery();
				
				if(rs1.next()){
					client_name = rs1.getString("client_name");
					client_money = rs1.getString("client_money");
					
					iasd.setClient_name(client_name);
					iasd.setClient_money(client_money);
				}
			}
			
			if(server_code != null){
				sql = "SELECT server_process, server_spec, server_cpuname, server_ram, server_hdd FROM serverDB WHERE server_code=?";
				pstmt2 = conn.prepareStatement(sql);
				pstmt2.setString(1, server_code);
				
				rs2 = pstmt2.executeQuery();
				
				if(rs2.next()){
					server_process = rs2.getString("server_process");
					server_spec = rs2.getString("server_spec");
					server_cpuname = rs2.getString("server_cpuname");
					server_ram = rs2.getString("server_ram");
					server_hdd = rs2.getString("server_hdd");
										
					iasd.setServer_process(server_process);
					iasd.setServer_spec(server_spec);
					iasd.setServer_cpuname(server_cpuname);
					iasd.setServer_ram(server_ram);
					iasd.setServer_hdd(server_hdd);					
				}								
			}
						
		} catch (Exception e) {			
			e.printStackTrace();						
		} finally{
			try {
				if(rs != null) rs.close();
				if(pstmt != null) pstmt.close();
				if(rs1 != null) rs1.close();
				if(pstmt1 != null) pstmt1.close();
				if(rs2 != null) rs2.close();
				if(pstmt2 != null) pstmt2.close();
				if(conn != null) conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return iasd;
	}
	
	private ArrayList<IpAddDTO> getAddList(String service_code){
		
		ArrayList<IpAddDTO> list = new ArrayList<IpAddDTO>();
		
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		
		try {
			conn = dao.getConn();
			String sql = "SELECT addDB_handle_date, addDB_case, addDB_ip, addDB_manager ";
			sql +=" FROM addDB ";
			sql +=" WHERE service_code=? ";
			sql +=" ORDER BY  addDB_num asc ";

			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, service_code);
			rs = pstmt.executeQuery();
			
			while(rs.next()){
				Date addDB_handle_date = rs.getDate("addDB_handle_date");
				String addDB_case = rs.getString("addDB_case");
				String addDB_ip = rs.getString("addDB_ip");
				String addDB_manager = rs.getString("addDB_manager");
																
				IpAddDTO writing = new IpAddDTO();
								
				writing.setAddDB_handle_date(addDB_handle_date);
				writing.setAddDB_case(addDB_case);
				writing.setAddDB_ip(addDB_ip);
				writing.setAddDB_manager(addDB_manager);
				
				list.add(writing);									
			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally{
			try {
				if(rs != null) rs.close();
				if(pstmt != null) rs.close();
				if(conn != null) conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}				
		
		return list;
	}
	
	private IdcAdditionalStartDTO getStartList(String service_code){
		
		IdcAdditionalStartDTO dto = new IdcAdditionalStartDTO();
		
		Connection conn = null;
		PreparedStatement pstmt = null, pstmt1 = null, pstmt2 = null, pstmt3 = null;
		ResultSet rs = null, rs1 = null, rs2 = null, rs3 = null;
		String sql;
		
		try {
			conn = dao.getConn();						
			sql = "SELECT addDB_handle_date, addDB_ip FROM addDB WHERE service_code=? and addDB_case='ip' order by addDB_handle_date desc, addDB_num desc limit 1";

			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, service_code);
			rs = pstmt.executeQuery();									
			
			if(rs.next()){
				Date addDB_handle_date = rs.getDate("addDB_handle_date");
				String addDB_ip = rs.getString("addDB_ip");
				dto.setIp(addDB_ip);
				dto.setIp_date(addDB_handle_date);
			}
									
			sql = "SELECT addDB_handle_date, addDB_ip "; 
			sql += " FROM addDB ";
			sql += " WHERE service_code=? and addDB_case='cs' ";
			sql += " or service_code=? and addDB_case='kcs' ";
			sql += " order by addDB_handle_date desc , addDB_num desc ";
			sql += " limit 1";
			
			pstmt1 = conn.prepareStatement(sql);
			pstmt1.setString(1, service_code);
			pstmt1.setString(2, service_code);
			rs1 = pstmt1.executeQuery();									
			
			if(rs1.next()){
				Date addDB_handle_date = rs1.getDate("addDB_handle_date");
				String addDB_ip = rs1.getString("addDB_ip");
				dto.setCs(addDB_ip);
				dto.setCs_date(addDB_handle_date);
			}
			
			sql = "SELECT behavior_date, behavior FROM historyDB WHERE service_code=? and behavior='defence 수정' ";
			sql += "ORDER BY behavior_date desc, history_num desc limit 0, 1";

			pstmt2 = conn.prepareStatement(sql);
			pstmt2.setString(1, service_code);
			rs2 = pstmt2.executeQuery();									
			
			if(rs2.next()){
				Date addDB_handle_date = rs2.getDate("behavior_date");
				String addDB_ip = rs2.getString("behavior");
				dto.setDefence(addDB_ip);
				dto.setDefence_date(addDB_handle_date);
			}
									
			sql = "SELECT onetime_handle_date, onetime_case FROM onetimeDB WHERE service_code=? order by onetime_handle_date desc, onetime_num desc limit 0, 1";
			pstmt3 = conn.prepareStatement(sql);
			pstmt3.setString(1, service_code);
			rs3 = pstmt3.executeQuery();									
			
			if(rs3.next()){
				Date addDB_handle_date = rs3.getDate("onetime_handle_date");
				String addDB_ip = rs3.getString("onetime_case");
				dto.setOnetime(addDB_ip);
				dto.setOnetime_date(addDB_handle_date);
			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally{
			try {
				if(rs != null) rs.close();
				if(pstmt != null) pstmt.close();
				if(rs1 != null) rs1.close();
				if(pstmt1 != null) pstmt1.close();
				if(rs2 != null) rs2.close();
				if(pstmt2 != null) pstmt2.close();
				if(rs3 != null) rs3.close();
				if(pstmt3 != null) pstmt3.close();
				if(conn != null) conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}						
		return dto;
	}
	
	private ArrayList<IdcServiceETCManageDTO> getEtcList(String service_code){
		
		ArrayList<IdcServiceETCManageDTO> list = new ArrayList<IdcServiceETCManageDTO>();
		
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		
		try {
			conn = dao.getConn();
			String sql = "select onetime_handle_date, onetime_case, onetime_sub, onetime_manager from onetimeDB where service_code=?";
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, service_code);
			rs = pstmt.executeQuery();
			
			while(rs.next()){
				Date onetime_handle_date = rs.getDate("onetime_handle_date");
				String onetime_case = rs.getString("onetime_case");
				String onetime_sub = rs.getString("onetime_sub");
				String onetime_manager = rs.getString("onetime_manager");
																
				IdcServiceETCManageDTO writing = new IdcServiceETCManageDTO();
								
				writing.setOnetime_handle_date(onetime_handle_date);
				writing.setOnetime_case(onetime_case);
				writing.setOnetime_sub(onetime_sub);
				writing.setOnetime_manager(onetime_manager);
				
				list.add(writing);									
			}

		} 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 list;
	}
	
	private IdcAmountDTO getAmountDTO(String service_code){
		
		IdcAmountDTO writing = new IdcAmountDTO();
		
		Connection conn = null;
		PreparedStatement pstmt = null, pstmt1 = null, pstmt2 = null, pstmt3 = null;
		ResultSet rs = null, rs1 = null, rs2 = null, rs3 = null;
		String sql;
		
		try{
			conn = dao.getConn();
			sql = "select amount_rack, amount_line, amount_cpu, amount_defence from amountDB where service_code=?";
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, service_code);
			rs = pstmt.executeQuery();
			
			if(rs.next()){				
				
				String amount_rack = rs.getString("amount_rack");
				String amount_line = rs.getString("amount_line");
				String amount_cpu = rs.getString("amount_cpu");
				String amount_defence = rs.getString("amount_defence");
								
				writing.setAmount_rack(amount_rack);
				writing.setAmount_line(amount_line);
				writing.setAmount_cpu(amount_cpu);
				writing.setAmount_defence(amount_defence);				
			}
			
			writing.setAmount_ip("0");
			writing.setAmount_cs("0");
			writing.setAmount_vpn("0");			
			
			sql = "select addDB_amount from addDB where addDB_case='ip' and service_code=?";
			pstmt1 = conn.prepareStatement(sql);
			pstmt1.setString(1, service_code);
			rs1 = pstmt1.executeQuery();
			
			int ip_total = 0;
			while(rs1.next()){
				String addDB_amount = rs1.getString("addDB_amount");
				if(addDB_amount != null && addDB_amount != ""){					
					ip_total += Integer.parseInt(addDB_amount);
				}				
			}
			writing.setAmount_ip(ip_total+"");
			
			sql = "select addDB_amount from addDB where addDB_case='cs' and service_code=?";
			pstmt2 = conn.prepareStatement(sql);
			pstmt2.setString(1, service_code);
			rs2 = pstmt2.executeQuery();
			
			int cs_total = 0;
			while(rs2.next()){
				String addDB_amount = rs2.getString("addDB_amount");								
				if(addDB_amount != null && addDB_amount != ""){
					cs_total += Integer.parseInt(addDB_amount);
				}
			}
			writing.setAmount_cs(cs_total+"");
			
			sql = "select addDB_amount from addDB where addDB_case='kcs' and service_code=?";
			pstmt3 = conn.prepareStatement(sql);
			pstmt3.setString(1, service_code);
			rs3 = pstmt3.executeQuery();
			
			int vpn_total = 0;
			if(rs3.next()){
				String addDB_amount = rs3.getString("addDB_amount");
				if(addDB_amount != null && addDB_amount != ""){
					vpn_total += Integer.parseInt(addDB_amount);
				}				
			}	
			writing.setAmount_vpn(vpn_total+"");

		} catch (Exception e) {
			e.printStackTrace();
		} finally{
			try {
				if(rs != null) rs.close();
				if(pstmt != null) pstmt.close();
				if(rs1 != null) rs1.close();
				if(pstmt1 != null) pstmt1.close();
				if(rs2 != null) rs2.close();
				if(pstmt2 != null) pstmt2.close();
				if(rs3 != null) rs3.close();
				if(pstmt3 != null) pstmt3.close();
				if(conn != null) conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		
		return writing;
	}
}