package idc.service.command;

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;

import utility.IdcGetClientNameCode;

public class IdcETCAddFormCmd implements IdcCmd
{
	private IdcDAO dao = new IdcDAO();
	
	@Override
	public void execute(HttpServletRequest request, HttpServletResponse response)
	{
		// TODO Auto-generated method stub
		getAtt(request);
	}
	
	//서비스 정보 받아오기
	private void getAtt(HttpServletRequest request)
	{
		Connection conn=null;
		PreparedStatement pstmt=null;
		ResultSet rs=null;
		try	
		{
			conn=dao.getConn();
			String sql="SELECT *, curdate() as a FROM servicedb WHERE service_code=?";
			pstmt =conn.prepareStatement(sql);
			pstmt.setString(1, request.getParameter("service_code"));
			rs = pstmt.executeQuery();
			if(rs.next())
			{
				request.setAttribute("client_name", IdcGetClientNameCode.getClientName(rs.getString("client_code")));
				request.setAttribute("service_ip", rs.getString("service_ip"));
				request.setAttribute("server_code", rs.getString("server_code"));
				request.setAttribute("service_code", rs.getString("service_code"));
				request.setAttribute("payment_date", rs.getDate("service_payment_date"));
				request.setAttribute("curdate", rs.getDate("a"));
				String line = rs.getString("service_line");
				switch(line)
				{
				case "normal" :
					line="일반";
					break;
				case "defence" :
					line="방어";
					break;
				case "solo" : 
					line="전용";
					break;
				case "soloLine" :
					line="전용라인";
					break;
				case "substitution" : 
					line="대체서버";
					break;
				case "test" : 
					line="테스트";
					break;
				}
				request.setAttribute("service_line", line);
			}
		}
		catch (Exception e) 
		{
			System.out.println("IdcETCAddFormCmd - getAtt 에서의 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("IdcETCAddFormCmd - getAtt 에서의 sqlexception");
				e.printStackTrace();
			}
		}
	}
}
