package idc.service.command;

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

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

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

import utility.StringToDate;

public class IdcVpnWriteHandleCmd implements IdcCmd{

	IdcDAO dao = new IdcDAO();
	
	public void execute(HttpServletRequest request, HttpServletResponse response)
	{
		//checkToken 값이 다르면 true반환.
		if(dao.checkToken(request))
		{
			request.setAttribute("reError", dao.reError);
			return;
		}
		else
		{
			//토큰이 같아서 등록성공이 될 경우 토큰을 삭제해 준다.
			request.getSession().removeAttribute("token");
		}
				
		String service_code = generatorVPN_code();
		String client_code = request.getParameter("client_code");
		String service_status = request.getParameter("service_status");
		String service_line = request.getParameter("service_line");
		String server_code = request.getParameter("server_code");
		String service_ip = request.getParameter("service_ip");
		String request_date = request.getParameter("service_request_date");
		String payment_date = request.getParameter("service_payment_date");
		
		StringToDate st = new StringToDate();
		Date service_request_date;		
		if(request_date == "" || request_date == null){
			service_request_date = null;
		}else{
			service_request_date = st.stringToDate(request_date);			
		}		
		Date service_payment_date;		
		if(payment_date == "" || payment_date == null){
			service_payment_date = null;
		}else{
			service_payment_date = st.stringToDate(payment_date);			
		}
		
		String service_amount= request.getParameter("service_amount");
		String service_note = request.getParameter("service_note");
				
		idcServiceWrite( service_status,  service_code,  client_code,  service_line,  server_code, 
				 service_ip,  service_request_date,  service_payment_date,  service_amount,  service_note, 
				 service_amount);
		if(request.getParameter("selCS").equals("vpc"))
			setVPC(request, client_code, service_ip, service_code);
		else
			setKT(request, client_code, service_ip, service_code);
	}
	
	//kcs 등록 및 addDB추가
	private void setKT(HttpServletRequest r, String client_code, String service_ip, String service_code)
	{
		Connection conn=null;
		PreparedStatement pstmt=null, pstmt2=null, pstmt3=null;
		ResultSet rs=null, rs3=null;
		String sql="";
		
		Date d = Date.valueOf(r.getParameter("service_payment_date")), atk=null;
		String ad = r.getParameter("k_attackDay");
		if(ad!=null && ad!="")
			atk=Date.valueOf(ad);
		String csip = r.getParameter("k_ip");
		int adddb_num=0;
		try	
		{
			conn=dao.getConn();
//			adddb에 넣기.
			sql="INSERT INTO adddb (adddb_case, service_code, adddb_request_date, adddb_amount, adddb_code, client_code, ";
			sql+="adddb_united, adddb_accountStatus, adddb_handle_date, adddb_nonpayment, adddb_payment,adddb_payment_date, ";
			sql+="adddb_original_date) ";
			sql+=" values(?,?,curdate(),?,?,?,'o','incomplete',curdate(),?,'0',?,?)";
			
			String addDB_case = "cs"; 
			String addDB_code = r.getParameter("k_server_code");
			pstmt2=conn.prepareStatement(sql);
			pstmt2.setString(1, addDB_case);
			pstmt2.setString(2, service_code);
			pstmt2.setString(3, "0");
			pstmt2.setString(4, addDB_code);
			pstmt2.setString(5, client_code);
			pstmt2.setString(6, "0");
			pstmt2.setDate(7, d);
			pstmt2.setDate(8, d);
			pstmt2.executeUpdate();
			
//			adddb 넘버링
			sql="SELECT adddb_num FROM adddb WHERE adddb_code=?";
			pstmt3=conn.prepareStatement(sql);
			pstmt3.setString(1, addDB_code);
			rs3=pstmt3.executeQuery();
			if(rs3.next())
				adddb_num=rs3.getInt("adddb_num");			
			
			sql = "INSERT INTO kcsDB (kcs_code, kcs_ip, service_code, kcs_use_domain, kcs_connect_domain, kcs_last_date, kcs_note, kcs_request_date, adddb_num) ";			
			sql += "values(?, ?, ?, ?, ?, ?, ?, curdate(),?)";
			
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, addDB_code);
			pstmt.setString(2, csip);
			pstmt.setString(3, service_code);
			pstmt.setString(4, r.getParameter("k_linkDomain"));
			pstmt.setString(5, r.getParameter("k_inDomain"));
			pstmt.setDate(6, atk);
			pstmt.setString(7, r.getParameter("k_note"));		
			pstmt.setInt(8, adddb_num);
			
			pstmt.executeUpdate();
		}
		catch (Exception e) 
		{
			System.out.println("IdcVpnWriteHandleCmd - setkt 에서의 exception");
			e.printStackTrace();
		}
		finally
		{
			try 
			{
				if(conn!=null) conn.close();
				if(pstmt!=null)pstmt.close();
				if(pstmt2!=null)pstmt2.close();
				if(pstmt3!=null)pstmt3.close();
				if(rs!=null)rs.close();
				if(rs3!=null)rs3.close();
				
			}
			catch (SQLException e) 
			{
				System.out.println("IdcVpnWriteHandleCmd - setkt 에서의 sqlexception");
				e.printStackTrace();
			}
		}
	}
	
	//vpc등록 및 addDB 추가
	private void setVPC(HttpServletRequest r, String client_code, String service_ip, String service_code)
	{
		Connection conn=null;
		PreparedStatement pstmt=null, pstmt2=null, pstmt3=null;
		ResultSet rs=null, rs3=null;
		String sql="";
		int adddb_num=0;
		Date d = Date.valueOf(r.getParameter("service_payment_date")), atk=null;
		String ad = r.getParameter("v_attackDay");
		if(ad!=null && ad!="")
			atk=Date.valueOf(ad);
		String csip = r.getParameter("v_ip");
		try	
		{
			conn=dao.getConn();
			
			//adddb 설정
			sql="INSERT INTO adddb (adddb_case, service_code, adddb_request_date, adddb_amount, adddb_code, client_code, ";
			sql+="adddb_united, adddb_accountStatus, adddb_handle_date, adddb_nonpayment, adddb_payment,adddb_payment_date, ";
			sql+="adddb_ip, ";
			sql+="adddb_original_date) ";
			sql+=" values(?,?,curdate(),?,?,?,'o','incomplete',curdate(),?,'0',?,?,?)";
			

			String addDB_case = "cs"; 
			String addDB_code = r.getParameter("v_server_code");
			pstmt2=conn.prepareStatement(sql);
			pstmt2.setString(1, addDB_case);
			pstmt2.setString(2, service_code);
			pstmt2.setString(3, "0");
			pstmt2.setString(4, addDB_code);
			pstmt2.setString(5, client_code);
			pstmt2.setString(6, "0");
			pstmt2.setDate(7, d);
			pstmt2.setString(8, csip);
			pstmt2.setDate(9, d);
			
			pstmt2.executeUpdate();
			
			//adddb_num 가져오기
			sql="SELECT adddb_num FROM adddb WHERE adddb_code = ?";
			pstmt3=conn.prepareStatement(sql);
			pstmt3.setString(1, addDB_code);
			rs3=pstmt3.executeQuery();
			if(rs3.next())
				adddb_num=rs3.getInt("adddb_num");
			
			
			//vpc 등록하기.
			sql = "INSERT INTO vpcDB (vpc_code, vpc_csip, service_code, vpc_connect_domain, vpc_fireWall, vpc_last_date, vpc_note, vpc_request_date, adddb_num) ";			
			sql += "values(?, ?, ?, ?, ?, ?, ?, curdate(),?)";
			
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, addDB_code);
			pstmt.setString(2, csip);
			pstmt.setString(3, service_code);
			pstmt.setString(4, r.getParameter("v_linkDomain"));
			pstmt.setString(5, r.getParameter("v_fireWall"));
			pstmt.setDate(6, atk);
			pstmt.setString(7, r.getParameter("v_note"));			
			pstmt.setInt(8, adddb_num);
			
			pstmt.executeUpdate();
			
		}
		catch (Exception e) 
		{
			System.out.println("IdcVpnWriteHandleCmd - setVPC 에서의 exception");
			e.printStackTrace();
		}
		finally
		{
			try 
			{
				if(conn!=null) conn.close();
				if(pstmt!=null)pstmt.close();
				if(pstmt2!=null)pstmt2.close();
				if(pstmt3!=null)pstmt3.close();
				if(rs!=null)rs.close();
				if(rs3!=null)rs3.close();

			}
			catch (SQLException e) 
			{
				System.out.println("IdcVpnWriteHandleCmd - setVPC 에서의 sqlexception");
				e.printStackTrace();
			}
		}
	}
	private String generatorVPN_code()
	{
		String server_code ="";
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		int num = 1;				
		
		try {
			conn = dao.getConn();
			String sql = "SELECT IFNULL(MAX(service_num), 0) + 1 AS NUM FROM serviceDB WHERE service_line='vpn' ";
			pstmt = conn.prepareStatement(sql);
			rs = pstmt.executeQuery();
			
			if(rs.next()){
				num = rs.getInt("num");
				server_code = "VPN-" + String.format("%03d", num);
			}			
			
		} 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 server_code;
	}

	
	//서비스 등록 기능 수행
	public void idcServiceWrite(String service_status, String service_code, String client_code, String service_line, String server_code, 
			String service_ip, Date service_request_date, Date service_payment_date, String service_amount, String service_note, 
			String service_cost){
		
		Connection conn = null;
		PreparedStatement pstmt = null, pstmt1= null, pstmt2 = null, pstmt3=null;
		ResultSet rs = null;
		int service_num = 1;
		
		try 
		{
			conn = dao.getConn();
			String sql = "SELECT IFNULL(MAX(service_num), 0) + 1 AS service_num FROM serviceDB";
			pstmt = conn.prepareStatement(sql);
			rs = pstmt.executeQuery();
			
			if(rs.next())
			{
				service_num = rs.getInt("service_num");
			}
			 			
			sql = "INSERT INTO serviceDB (service_num, service_status, service_code, client_code, service_line, server_code,service_ip, ";
			sql += " service_request_date, service_payment_date, service_amount, service_note, ";
			sql += " service_nonpayment, service_cost, service_unitedAmount, service_original_date ) values";
			sql += "(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
			
			pstmt1 = conn.prepareStatement(sql);
			
			pstmt1.setInt(1, service_num);
			pstmt1.setString(2, service_status);
			pstmt1.setString(3, service_code);
			pstmt1.setString(4, client_code);
			pstmt1.setString(5, service_line);
			pstmt1.setString(6, server_code);			
			pstmt1.setString(7, service_ip);					
			pstmt1.setDate(8, service_request_date);			
			pstmt1.setDate(9, service_payment_date);
			pstmt1.setString(10, service_amount);
			pstmt1.setString(11, service_note);
			pstmt1.setString(12, service_amount);
			pstmt1.setString(13, service_cost);
			pstmt1.setString(14, service_amount);
			pstmt1.setDate(15, service_payment_date);			
			pstmt1.executeUpdate();

//			adddb에 셋팅해준다.
			int i=1;
			sql = "INSERT INTO adddb (adddb_case, service_code, adddb_amount, adddb_nonpayment, adddb_payment, "; 
			sql += "adddb_accountStatus, adddb_united, adddb_payment_date, adddb_ip, client_code, "; 
			sql +=	"adddb_handle_date, adddb_oneTime, adddb_request_date,  adddb_note, isNext, ";
			sql +=	"adddb_original_date) ";
			sql += "values(?,?,?,?,?,    ?,?,?,?,?,  curdate(),?,?,?,?,     ?) "; 
			pstmt2=conn.prepareStatement(sql);
			pstmt2.setString(i++, "VPN");
			pstmt2.setString(i++, service_code);
			pstmt2.setString(i++, service_amount);
			pstmt2.setString(i++, service_amount);
			pstmt2.setString(i++, "0");
			
			pstmt2.setString(i++, "incomplete");
			pstmt2.setString(i++, "o");
			pstmt2.setDate(i++, service_payment_date);
			pstmt2.setString(i++, service_ip);
			pstmt2.setString(i++, client_code);
			
			pstmt2.setString(i++, "x");
			pstmt2.setDate(i++, service_payment_date);
			pstmt2.setString(i++, service_note);
			pstmt2.setString(i++, "o");
			
			pstmt2.setDate(i++, service_payment_date);
			pstmt2.executeUpdate();
			
		} 
		catch (Exception e)
		{
			e.printStackTrace();
		} 
		finally
		{
			try 
			{
				if(rs != null) rs.close();
				if(pstmt != null) pstmt.close();
				if(pstmt1 != null) pstmt1.close();
				if(pstmt2 != null) pstmt2.close();
				if(pstmt3 != null) pstmt3.close();
				if(conn != null) conn.close();
			}
			catch (SQLException e)
			{
				e.printStackTrace();
			}
		}
		
	}
}
