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 IdcServiceUpdateVpnCmd implements IdcCmd
{
	private IdcDAO dao;
	 
	public IdcServiceUpdateVpnCmd()
	{
		dao = new IdcDAO();
	}
	
	//vpn서비스 정보 수정하기
	@Override
	public void execute(HttpServletRequest request, HttpServletResponse response)
	{
		String service_status = request.getParameter("service_status");
		String service_code = request.getParameter("service_code");
		String service_ip = request.getParameter("service_ip");
		String server_code = request.getParameter("server_code");
		String client_code = request.getParameter("client_code");
		String service_amount = request.getParameter("service_amount");
		String str_date = request.getParameter("service_payment_date");
		
		StringToDate st = new StringToDate();
		Date service_payment_date;		
		if(str_date == "" || str_date == null){
			service_payment_date = null;
		}else{
			service_payment_date = st.stringToDate(str_date);			
		}					
		setVpn(service_status, service_code, service_ip, server_code, client_code, service_amount, service_payment_date);
				
		request.setAttribute("client_code", client_code);
	}
	
	private void setVpn(String service_status, String service_code, String service_ip, String server_code, 
			String client_code, String service_amount, Date service_payment_date){
		Connection conn=null;
		PreparedStatement pstmt=null, pstmt2=null;
		ResultSet rs=null;
		try	
		{
			conn=dao.getConn();
			String sql="UPDATE servicedb SET service_status=?, service_amount=?, service_nonpayment=?, service_cost=?,  server_code=?, ";
			sql += " service_payment_date=?, service_original_date=? ";
			sql+=" WHERE service_code=?";
			pstmt =conn.prepareStatement(sql);
			pstmt.setString(1, service_status);
			pstmt.setString(2, service_amount);
			pstmt.setString(3, service_amount);
			pstmt.setString(4, service_amount);
			pstmt.setString(5, server_code);
			pstmt.setDate(6, service_payment_date);
			pstmt.setDate(7,  service_payment_date);
			pstmt.setString(8, service_code);
			pstmt.executeUpdate();
			
			sql = "UPDATE adddb SET adddb_amount=?, adddb_nonpayment=?, adddb_payment='0', adddb_accountStatus='incomplete', ";
			sql += " addDB_payment_date=?, addDB_original_date=? ";
			sql += " WHERE service_code=?";
			pstmt2 = conn.prepareStatement(sql);
			pstmt2.setString(1, service_amount);
			pstmt2.setString(2, service_amount);
			pstmt2.setDate(3, service_payment_date);
			pstmt2.setDate(4, service_payment_date);
			pstmt2.setString(5, service_code);
			pstmt2.executeUpdate();
		}
		catch (Exception e) 
		{
			System.out.println("IdcServiceUpdateVpnCmd 에서의 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("IdcServiceUpdateVpnCmd 에서의 sqlexception");
				e.printStackTrace();
			}
		}
	}
}