package idc.server.command;

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

import utility.StringToDate;

import java.sql.*;

import idc.model.*;

public class IdcServerWriteCmd implements IdcCmd{
	
	IdcDAO dao = new IdcDAO();
	
	public void execute(HttpServletRequest request, HttpServletResponse response){
		//checkToken 값이 다르면 true반환.
		if(dao.checkToken(request)){
			request.setAttribute("reError", dao.reError);
			return;
		}else{
			//토큰이 같아서 등록성공이 될 경우 토큰을 삭제해 준다.
			request.getSession().removeAttribute("token");
		}
				
		//String server_use_status = request.getParameter("server_use_status");
		String server_use_status = request.getParameter("server_use_status");
		String server_code = request.getParameter("server_code");
		String server_process = request.getParameter("server_process");
		String server_spec = request.getParameter("server_spec");
		String server_cpuname = request.getParameter("server_cpuname");
		String server_cost = request.getParameter("server_cost");
		
		String server_ram1 = request.getParameter("server_ram1");
		String server_ram2 = request.getParameter("server_ram2");
		String server_ram3 = request.getParameter("server_ram3");
		String server_ram4 = request.getParameter("server_ram4");
		String server_ram5 = request.getParameter("server_ram5");
		
		String server_hdd1 = request.getParameter("server_hdd1");
		String server_hdd2 = request.getParameter("server_hdd2");
		String server_hdd3 = request.getParameter("server_hdd3");
		String server_hdd4 = request.getParameter("server_hdd4");
		String server_hdd5 = request.getParameter("server_hdd5");
				
		String server_ram = server_ram1 + " " + server_ram2 + " " + server_ram3 + " " + server_ram4 + " " + server_ram5;  
		String server_hdd = server_hdd1 + " " + server_hdd2 + " " + server_hdd3 + " " + server_hdd4 + " " + server_hdd5;				
		
		String server_brand = request.getParameter("server_brand");
		String server_location = request.getParameter("server_location");
		String server_fomat_date = request.getParameter("server_fomat_date");
		String server_recieve_date = request.getParameter("server_recieve_date");
		String server_release_date = request.getParameter("server_release_date");
		String server_note = request.getParameter("server_note");

		//String을 Date형으로 형변환	
		StringToDate st = new StringToDate();
		Date fomat; 						
		if(server_fomat_date == "" || server_fomat_date == null){
			fomat= null;
		}else{
			fomat = st.stringToDate(server_fomat_date);
			server_use_status = "n";
		}
		
		Date recieve; 						
		if(server_recieve_date == "" || server_recieve_date == null){
			recieve= null;			
		}else{
			recieve = st.stringToDate(server_recieve_date);
		}
		
		Date release; 						
		if(server_release_date == "" || server_release_date == null){
			release= null;			
		}else{
			release = st.stringToDate(server_release_date);
		}
																	
		idcServerWrite(server_use_status, server_code, server_process, server_spec, server_cpuname, server_ram, server_hdd,
				server_brand, server_location,fomat, recieve, release, server_note,
				server_ram1, server_ram2, server_ram3, server_ram4, server_ram5,
				server_hdd1, server_hdd2, server_hdd3, server_hdd4, server_ram5, server_cost);		
	}
	
	//서버 등록 기능 수행
	public void idcServerWrite(String server_use_status, String server_code,
			String server_process, String server_spec, String server_cpuname,String server_ram, 
			String server_hdd, String server_brand, String server_location, Date server_fomat_date, Date server_recieve_date,  
			Date server_release_date, String server_note,
			String server_ram1, String server_ram2, String server_ram3, String server_ram4, String server_ram5,
			String server_hdd1, String server_hdd2, String server_hdd3, String server_hdd4, String server_hdd5, String server_cost){
		
		Connection conn = null;
		PreparedStatement pstmt = null, pstmt1 = null;
		ResultSet rs = null;
		int server_num = 1;			
		
		try {
			conn = dao.getConn();
			String sql = "SELECT IFNULL(MAX(server_num), 0) + 1 AS server_num FROM serverDB";
			pstmt = conn.prepareStatement(sql);
			rs = pstmt.executeQuery();
			
			if(rs.next()){
				server_num = rs.getInt("server_num");
			}
			
			sql = "INSERT INTO serverDB (server_num, server_use_status, server_code, server_process, server_spec, server_cpuname, server_ram, server_hdd, ";
			sql += "server_brand, server_location, server_fomat_date, server_recieve_date, server_release_date, server_note";
			sql += " ,server_ram1, server_ram2, server_ram3, server_ram4, server_ram5, server_hdd1, server_hdd2, server_hdd3, server_hdd4, server_hdd5, server_cost) ";
			sql += " values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
			
			pstmt1 = conn.prepareStatement(sql);			
			
			pstmt1.setInt(1, server_num);
			pstmt1.setString(2, server_use_status);
			pstmt1.setString(3, server_code);
			pstmt1.setString(4, server_process);
			pstmt1.setString(5, server_spec);
			pstmt1.setString(6, server_cpuname);
			pstmt1.setString(7, server_ram);
			pstmt1.setString(8, server_hdd);
			pstmt1.setString(9, server_brand);
			pstmt1.setString(10, server_location);
			pstmt1.setDate(11, server_fomat_date);
			pstmt1.setDate(12, server_recieve_date);
			pstmt1.setDate(13, server_release_date);
			pstmt1.setString(14, server_note);
			pstmt1.setString(15, server_ram1);
			pstmt1.setString(16, server_ram2);
			pstmt1.setString(17, server_ram3);
			pstmt1.setString(18, server_ram4);
			pstmt1.setString(19, server_ram5);
			pstmt1.setString(20, server_hdd1);
			pstmt1.setString(21, server_hdd2);
			pstmt1.setString(22, server_hdd3);
			pstmt1.setString(23, server_hdd4);
			pstmt1.setString(24, server_hdd5);
			pstmt1.setString(25, server_cost);						
			pstmt1.executeUpdate();
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally{
			try {
				if(rs != null) rs.close();
				if(pstmt != null) pstmt.close();
				if(pstmt1 != null) pstmt1.close();
				if(conn != null) conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}	
	}

}
