package idc.deposit.command;

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

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

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


public class IdcDepositFormCmd implements IdcCmd 
{
	private IdcDAO dao;
	private IdcClientDBDTO dto;
	public IdcDepositFormCmd() 
	{
		dao =new IdcDAO();
		dto =new IdcClientDBDTO();
	}
	//입금폼
	@Override
	public void execute(HttpServletRequest request, HttpServletResponse response) 
	{
		String client_code = request.getParameter("client_code"); 
		idcDepositForm(client_code);
		request.setAttribute("IdcClientDBTarget", dto);
		request.setAttribute("client_code", client_code);
		
	}
	//날짜와 고객정보 얻어오기
	private void idcDepositForm(String targetName)
	{		
		Connection conn=null;
		PreparedStatement pstmt =null;
		ResultSet rs = null;
		
		try 
		{
			String sql="";
			conn = dao.getConn();
			
			sql= "SELECT client_code,client_name,curDate() as today ";
			sql+="FROM clientdb WHERE client_code=?";
			pstmt = conn.prepareStatement(sql);
			
			pstmt.setString(1,targetName);
			rs =pstmt.executeQuery();
			if(rs.next())
			{
				dto.setClient_code(rs.getString("client_code"));
				System.out.println(dto.getClient_code());
				dto.setClient_name(rs.getString("client_name"));
				System.out.println(dto.getClient_name());
				dto.setToday(rs.getDate("today"));
			}
		} 
		catch (Exception e) 
		{
			e.printStackTrace();
		}
		finally
		{
			try 
			{
				if(conn!=null) conn.close();
				if(pstmt!=null) pstmt.close();
				if(rs!=null) rs.close();
			}
			catch (SQLException e) 
			{
				e.printStackTrace();
			}
		}
	}
}
