package idc.json.command;

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

import java.io.FileWriter;
import java.io.IOException;
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;

public class IdcSendMailSaveJSONCmd implements IdcCmd
{
	IdcDAO dao = new IdcDAO();
	
	
	//보낸 메일 저장하는 JSON
	@Override
	public void execute(HttpServletRequest request, HttpServletResponse response)
	{
		String client_name = request.getParameter("client_name");
		SaveMail(client_name, request.getParameter("filepath"), request.getParameter("service_code"));
	}
	
	private Date getDate()
	{
		Date result = null;
		Connection conn=null;
		PreparedStatement pstmt=null;
		ResultSet rs=null;
		try	
		{
			conn=dao.getConn();
			String sql="SELECT curdate() as a";
			pstmt =conn.prepareStatement(sql);
			rs = pstmt.executeQuery();
			if(rs.next())
			{
				result = rs.getDate("a");
			}
		}
		catch (Exception e) 
		{
			System.out.println("getDate 에서의 exception");
			e.printStackTrace();
		}
		finally
		{
			try 
			{
				if(conn!=null) conn.close();
				if(rs!=null)rs.close();
				if(pstmt!=null)pstmt.close();
			}
			catch (SQLException e) 
			{
				System.out.println("getDate 에서의 sqlexception");
				e.printStackTrace();
			}
		}
		
		return result;
	}
	
	private void SaveMail(String client_name, String filepath, String service_code)
	{
		Connection conn=null;
		PreparedStatement pstmt=null;
		ResultSet rs=null;
		try	
		{
			conn=dao.getConn();
			String sql="INSERT INTO mailSave(client_name, service_code, filepath, date, inTime) ";
			sql+="values(?,?,?,curdate(), sysdate())";
			pstmt =conn.prepareStatement(sql);
			pstmt.setString(1, client_name);
			pstmt.setString(2, service_code);
			pstmt.setString(3, filepath);
			
			pstmt.executeUpdate();
		}
		catch (Exception e) 
		{
			System.out.println("IdcSendMailSaveJSONCmd 에서의 exception");
			e.printStackTrace();
		}
		finally
		{
			try 
			{
				if(conn!=null) conn.close();
				if(rs!=null)rs.close();
				if(pstmt!=null)pstmt.close();
			}
			catch (SQLException e) 
			{
				System.out.println("IdcSendMailSaveJSONCmd 에서의 sqlexception");
				e.printStackTrace();
			}
		}
	}
}
