package idc.service.command;

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

import idc.model.*;
import utility.*;

import java.sql.*;

public class IdcVPCWriteHandleCmd implements IdcCmd{
	
	IdcDAO dao = new IdcDAO();
	
	//vpc등록 처리
	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 vpc_code = request.getParameter("vpc_code");
		String vpc_csip = request.getParameter("vpc_csip");
		String service_code = request.getParameter("service_code");
		String vpc_connect_domain = request.getParameter("vpc_connect_domain");
		String vpc_fireWall = request.getParameter("vpc_fireWall");
		String last_date = request.getParameter("vpc_last_date");
		String vpc_note = request.getParameter("vpc_note");
		
		StringToDate st = new StringToDate();
		Date vpc_last_date;
		
		if(last_date == "" || last_date == null){
			vpc_last_date = null;
		}else{			
			vpc_last_date = st.stringToDate(last_date);
		}
		
		insertVPCDB(vpc_code, vpc_csip, service_code, vpc_connect_domain, vpc_fireWall, vpc_last_date, vpc_note);
	}
	
	private void insertVPCDB(String vpc_code, String vpc_csip, String service_code, String vpc_connect_domain, String vpc_fireWall,
			Date vpc_last_date, String vpc_note){
		
		Connection conn = null;
		PreparedStatement pstmt = null,pstmt2=null, pstmt3=null,pstmt4=null,pstmt5=null, pstmt6=null;
		ResultSet rs = null,rs3=null,rs4=null,rs5=null, rs6=null;
		
		try {
			conn = dao.getConn();
			String sql="";
			//내소스
			int adddb_num=0;
			String client_code="";
			String server_code = "";
			String client_name="";
			Date adddb_payment_date=null;
			String amount="50000";
			int csCount=0;
			//cs갯수 얻어와서 금액 바꾸기
			sql="SELECT COUNT(adddb_case) as a FROM adddb WHERE service_code=? AND adddb_case='cs' ";
			pstmt6 = conn.prepareStatement(sql);
			pstmt6.setString(1, service_code);
			rs6 = pstmt6.executeQuery();
			if(rs6.next())
				csCount=rs6.getInt("a");
			if(csCount==0)
				amount="0";
			
//			client_code, server_code, cient_name 얻어오기
			sql="SELECT client_name, server_code, s.client_code FROM servicedb as s, clientdb as c WHERE service_code=? AND s.client_code = c.client_code";
			pstmt3=conn.prepareStatement(sql);
			pstmt3.setString(1, service_code);
			rs3 = pstmt3.executeQuery();
			if(rs3.next())
			{
				client_code = rs3.getString("client_code");
				server_code = rs3.getString("server_code");
				client_name = rs3.getString("client_name");
			}
			
//			adddb 통합일 경우 결제일 같이넣기.
			sql="SELECT adddb_payment_date FROM adddb WHERE service_code=? AND adddb_case='service'";
			pstmt4=conn.prepareStatement(sql);
			pstmt4.setString(1, service_code);
			rs4=pstmt4.executeQuery();
			if(rs4.next())
				adddb_payment_date = rs4.getDate("adddb_payment_date");


			//			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 = vpc_code;
			
			pstmt2=conn.prepareStatement(sql);
			pstmt2.setString(1, addDB_case);
			pstmt2.setString(2, service_code);
			pstmt2.setString(3, amount);
			pstmt2.setString(4, addDB_code);
			pstmt2.setString(5, client_code);
			pstmt2.setString(6, amount);
			pstmt2.setDate(7, adddb_payment_date);
			pstmt2.setString(8, vpc_csip);
			pstmt2.setDate(9, adddb_payment_date);
			
			pstmt2.executeUpdate();
			
//			히스토리 등록
			IdcSetHistory.setHistory(service_code, server_code, "vpc-"+addDB_case+" 추가", vpc_csip, addDB_code, "",client_name);
			
//			서비스에 해당 금액 업데이트.
			IdcUpdateServiceAmount.UpdateAmount(amount, service_code, true, adddb_num,null);
			
//			amountdb에 데이터 업데이트
			IdcUpdateServiceAmount.amountdbUpdate(amount, service_code, "cs");
			
//			IP업뎃
//			IdcSetIP.SetIP("vpc", client_name, 0, null, service_code, vpc_csip);
			
//			csdb 수정하기
			IdcSetCS.modifyCS(vpc_code, "vpc");

//			adddb 넘버링
			sql="SELECT adddb_num FROM adddb WHERE adddb_code = ?";
			pstmt5=conn.prepareStatement(sql);
			pstmt5.setString(1, vpc_code);
			rs5=pstmt5.executeQuery();
			if(rs5.next())
				adddb_num=rs5.getInt("adddb_num");
			//내소스 끝
			
			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, vpc_code);
			pstmt.setString(2, vpc_csip);
			pstmt.setString(3, service_code);
			pstmt.setString(4, vpc_connect_domain);
			pstmt.setString(5, vpc_fireWall);
			pstmt.setDate(6, vpc_last_date);
			pstmt.setString(7, vpc_note);			
			pstmt.setInt(8, adddb_num);
			
			pstmt.executeUpdate();
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally{
			try {
				if(rs != null) rs.close();
				if(pstmt != null) pstmt.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(rs5 != null) rs5.close();
				if(pstmt5 != null) pstmt5.close();
				if(conn != null) conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		
	}

}
