package idc.server.command;

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

import utility.StringToDate;
import idc.model.*;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

public class IdcServerListExcelCmd implements IdcCmd{
	
	private int WRITING_PER_PAGE = 10;
	IdcDAO dao = new IdcDAO(); 
	
	public void execute(HttpServletRequest request, HttpServletResponse response) {
		
		ArrayList<IdcServerDTO> list; 
		
		String sc = "", so = "", sn = "";
		sc=request.getParameter("searchContent");
		if(!(sc!=null && sc!=""))
			sc="";
		so=request.getParameter("searchOption");						
		if(!(so!=null && so!=""))
			so="server_use_status";
		
		//포맷보류 필터링(전체(all)/포맷보류(format)/일반(nomal));
		String format_filter = request.getParameter("format_filter");
		if(format_filter == null || format_filter == ""){
			format_filter = "";
		}
		
		//포맷보류 날짜 선택시
		String str_date = request.getParameter("select_date");
		StringToDate st = new StringToDate();
		Date select_date; 						
		if(str_date == "" || str_date == null){
			select_date = null;
		}else{
			select_date = st.stringToDate(str_date);
		}
								
		int curPage = getCurPage(request.getParameter("curPage"));
		
		list = idcServerList(Integer.toString(curPage), sc, so, format_filter, select_date);
		
		request.setAttribute("idcServerList", list);;
		request.setAttribute("curPage", curPage);
		request.setAttribute("searchOption",so);
		request.setAttribute("searchContent", sc);
		request.setAttribute("format_filter", format_filter);
		request.setAttribute("select_date", select_date);
		
		switch(so)
		{
			case "server_use_status":sn="가용";break;
			case "server_code":sn="장비명";break;
			case "server_process":sn="cpu";break;
		}
		request.setAttribute("searchOptionName",sn);
	}
	
	//현재페이지
	private int getCurPage(String strPage){
		int curPage=1;
		
		if(strPage !=null && strPage!="")
			curPage=Integer.parseInt(strPage);
		
		return curPage;
	}
	
	//서버 리스트 록록 조회 기능 수행
	public ArrayList<IdcServerDTO> idcServerList(String curPage, String sc, String so, String filter, Date select_date){
		
		ArrayList<IdcServerDTO> list = new ArrayList<IdcServerDTO>();
		
		Connection conn = null;
		PreparedStatement pstmt = null,pstmt2=null;
		ResultSet rs = null,rs2=null
				;
		int numbering_end = WRITING_PER_PAGE * (Integer.parseInt(curPage) -1 );
		int numbering = 0;
		try {
			conn = dao.getConn();
			String sql = "SELECT COUNT(*) as a FROM serverdb WHERE "+so+" like '%"+sc+"%' ";
			if(filter.equals("format")){
				sql += " and server_fomat_date is not null and server_use_status ='n' ";
			}else if(filter.equals("normal")){
				sql += " and server_use_status ='y' ";
			}

			if(select_date != null){
				sql += " and server_fomat_date=? ";
				pstmt2 =conn.prepareStatement(sql);
				pstmt2.setDate(1, select_date);
			}else{
				pstmt2 =conn.prepareStatement(sql);
			}									
			rs2=pstmt2.executeQuery();
						
			if(rs2.next())
				numbering = rs2.getInt("a")-numbering_end;
			sql = "SELECT server_num, server_use_status, server_code, server_process, server_spec, server_cpuname, server_ram, server_hdd, server_brand, server_location, ";
			sql += "server_fomat_date, server_recieve_date, server_release_date, server_note, server_disuse_date ";
			sql += "FROM serverDB ";
			sql += "WHERE "+so+" like '%"+sc+"%' ";
			if(filter.equals("format")){
				sql += " and server_fomat_date is not null and server_use_status ='n'";
			}else if(filter.equals("normal")){
				sql += " and server_use_status ='y'";
			}
			
			if(select_date != null){
				sql += " and server_fomat_date=? ";				
				sql += " ORDER BY server_num desc ";			
				pstmt = conn.prepareStatement(sql);
				pstmt.setDate(1, select_date);			
				
			}else{
				sql += " ORDER BY server_num desc ";			
				pstmt = conn.prepareStatement(sql);				
			}
												
			rs = pstmt.executeQuery();
			
			while(rs.next()){
				int num = numbering--;
				int server_num = rs.getInt("server_num");
				String server_use_status = rs.getString("server_use_status");
				String server_code = rs.getString("server_code");
				String server_process = rs.getString("server_process");
				String server_spec = rs.getString("server_spec");
				String server_cpuname = rs.getString("server_cpuname");
				String server_ram = rs.getString("server_ram");
				String server_hdd = rs.getString("server_hdd");
				String server_brand = rs.getString("server_brand");
				String server_location = rs.getString("server_location");
				Date server_fomat_date = rs.getDate("server_fomat_date");
				Date server_recieve_date = rs.getDate("server_recieve_date");
				Date server_release_date = rs.getDate("server_release_date");
				String server_note = rs.getString("server_note");
				Date server_disuse_date = rs.getDate("server_disuse_date");				
				
				IdcServerDTO writing = new IdcServerDTO();
				writing.setNumber(num);
				writing.setServer_num(server_num);
				writing.setServer_use_status(server_use_status);
				writing.setServer_code(server_code);
				writing.setServer_process(server_process);
				writing.setServer_spec(server_spec);
				writing.setServer_cpuname(server_cpuname);
				writing.setServer_ram(server_ram);
				writing.setServer_hdd(server_hdd);
				writing.setServer_brand(server_brand);
				writing.setServer_location(server_location);
				writing.setServer_fomat_date(server_fomat_date);
				writing.setServer_recieve_date(server_recieve_date);
				writing.setServer_release_date(server_release_date);
				writing.setServer_note(server_note);
				writing.setServer_disuse_date(server_disuse_date);
				
				list.add(writing);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally{
			try {
				if(rs != null) rs.close();
				if(pstmt != null) pstmt.close();
				if(rs2 != null) rs2.close();
				if(pstmt2 != null) pstmt2.close();
				if(conn != null) conn.close();
			} catch (SQLException e) {
				e.printStackTrace();				
			}
		}		
		return list;
		
	}

}