package idc.service.command;

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

import idc.model.*;

import java.sql.*;

public class IdcServiceWriteFormCmd implements IdcCmd{

	IdcDAO dao = new IdcDAO();
		
	//서비스 등록창 정보 불러오기
	public void execute(HttpServletRequest request, HttpServletResponse response) 
	{
		String client_code = request.getParameter("client_code");		
		request.setAttribute("client_code", client_code);
		
		String client_reseller = getClient_reseller(client_code);
		request.setAttribute("client_reseller", client_reseller);
		
		Date curdate = getCurdate();
		request.setAttribute("curdate", curdate);
	}
	public String getClient_reseller(String client_code){
		
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		String client_reseller = "";
		
		try {
			conn = dao.getConn();
			String sql = "SELECT client_reseller FROM clientDB WHERE client_code=?";
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, client_code);			
			rs = pstmt.executeQuery();
			
			if(rs.next()) client_reseller = rs.getString("client_reseller");						
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally{
			try {
				if(pstmt != null) pstmt.close();
				if(conn != null) conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}		
		return client_reseller;
	}

	private Date getCurdate(){
		Date curdate = null;
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;				
		
		try {
			conn = dao.getConn();
			String sql = "SELECT curdate() AS curdate FROM serverDB";
			pstmt = conn.prepareStatement(sql);
			rs = pstmt.executeQuery();
			
			if(rs.next()){
				curdate = rs.getDate("curdate");
			}			
						
		} 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 curdate;
	}
	
}
