package idc.server.command;

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

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

import idc.model.*;

public class IdcServerUsableCmd implements IdcCmd{
	
	IdcDAO dao = new IdcDAO();
	
	public void execute(HttpServletRequest request, HttpServletResponse response) {
		
		ArrayList<IdcUsableServerDTO>list;
		
		IdcUsableServerTotalDTO dto = new IdcUsableServerTotalDTO();
		list = idcUsableServerList(dto);
		request.setAttribute("usableServerList", list);
		request.setAttribute("usableServerTotal",dto);
	}

	//가용장비 현황 리스트
	public ArrayList<IdcUsableServerDTO> idcUsableServerList(IdcUsableServerTotalDTO totdto){
		ArrayList<IdcUsableServerDTO> list = new ArrayList<IdcUsableServerDTO>();
		
		Connection conn = null;
		PreparedStatement pstmt=null,pstmt2=null,pstmt3=null,pstmt4=null,pstmt5=null,pstmt6=null,
				pstmt7=null,pstmt8=null,pstmt9=null;
		ResultSet rs=null,rs2=null,rs3=null,rs4=null,rs5=null,rs6=null,rs7=null,rs8=null,rs9=null;
		
		ArrayList<String[]> sList = new ArrayList<String[]>();
		
		try 
		{
			conn = dao.getConn();
			int cnt = 0;
			int total = 0;
			
			String sql = "";
			
			sql = "";//sql = "SELECT process, spec, cpuname, price  FROM gearlist ORDER BY firarr ASC, secArr ASC";
			
			pstmt5 = conn.prepareStatement(sql);
			rs5 = pstmt5.executeQuery();
			
			while(rs5.next())
			{
				String[] str = new String[4];
				
				str[0]=rs5.getString("process");
				str[1]=rs5.getString("spec");
				str[2]=rs5.getString("cpuname");
				str[3]=rs5.getString("price");
				
				sList.add(str);
			}
			
			for(int i=0;i<sList.size();i++ ){								
				total = 0;				
				IdcUsableServerDTO writing = new IdcUsableServerDTO();
				
				writing.setServer_process(sList.get(i)[0]);
				writing.setServer_spec(sList.get(i)[1]);
				writing.setServer_cpuname(sList.get(i)[2]);
				writing.setServer_amount(Integer.parseInt(sList.get(i)[3]));
				
				//도쿄 가용장비 카운트
				sql = "";//sql = "SELECT COUNT(*) AS server_use_status FROM serverDB WHERE server_use_status='y' and server_spec=? ";
				//sql += "and server_location='tokyo'";
				
				pstmt = conn.prepareStatement(sql);
				pstmt.setString(1, writing.getServer_spec());
	
				rs = pstmt.executeQuery();
				
				if(rs.next()){
					cnt = rs.getInt("server_use_status");
				}
				
				writing.setServer_tokyo_usable(cnt);
				total += cnt;
				
				//도쿄 포맷대기
				sql = "";//sql = "SELECT COUNT(*) AS server_use_status FROM serverDB WHERE server_use_status='n' and server_spec=? ";
				//sql += "and server_location='tokyo'";
				
				pstmt2 = conn.prepareStatement(sql);
				pstmt2.setString(1, writing.getServer_spec());
	
				rs2 = pstmt2.executeQuery();
				
				cnt = 0;
				if(rs2.next()){
					cnt = rs2.getInt("server_use_status");
				}
				
				writing.setServer_tokyo_fomat(cnt);
				total += cnt;
				
				//치바 가용
				sql = "";//sql = "SELECT COUNT(*) AS server_use_status FROM serverDB WHERE server_use_status='y' and server_spec=? ";
				//sql += "and server_location='chiba'";
				
				pstmt3 = conn.prepareStatement(sql);
				pstmt3.setString(1, writing.getServer_spec());
	
				rs3 = pstmt3.executeQuery();
				
				cnt = 0;
				if(rs3.next()){
					cnt = rs3.getInt("server_use_status");
				}
				
				writing.setServer_chiba_usable(cnt);
				total += cnt;
				
				//치바 포맷
				sql = "";//sql = "SELECT COUNT(*) AS server_use_status FROM serverDB WHERE server_use_status='n' and server_spec=? ";
				//sql += "and server_location='chiba'";
				
				pstmt4 = conn.prepareStatement(sql);
				pstmt4.setString(1, writing.getServer_spec());
				
				rs4 = pstmt4.executeQuery();
				cnt = 0;
				
				if(rs4.next()){
					cnt = rs4.getInt("server_use_status");
				}
				
				writing.setServer_chiba_fomat(cnt);
				total += cnt;
				
				writing.setServer_total(total);

				list.add(writing);			
			}
			
			sql = "";//sql="SELECT COUNT(*) as a FROM serverdb WHERE server_use_status='n' AND server_location='chiba' ";
			pstmt6=conn.prepareStatement(sql);
			rs6 = pstmt6.executeQuery();
			if(rs6.next())
			{
				totdto.setChiba_disable(rs6.getInt("a"));
			}
			
			sql = "";//sql="SELECT COUNT(*) as a FROM serverdb WHERE server_use_status='y' AND server_location='chiba' ";
			pstmt7=conn.prepareStatement(sql);
			rs7 = pstmt7.executeQuery();
			if(rs7.next())
			{
				totdto.setChiba_able(rs7.getInt("a"));
			}
			
			sql = "";//sql="SELECT COUNT(*) as a FROM serverdb WHERE server_use_status='n' AND server_location='tokyo' ";
			pstmt8=conn.prepareStatement(sql);
			rs8 = pstmt8.executeQuery();
			if(rs8.next())
			{
				totdto.setTokyo_disable(rs8.getInt("a"));
			}
			
			sql = "";//sql="SELECT COUNT(*) as a FROM serverdb WHERE server_use_status='y' AND server_location='tokyo' ";
			pstmt9=conn.prepareStatement(sql);
			rs9 = pstmt9.executeQuery();
			if(rs9.next())
			{
				totdto.setTokyo_able(rs9.getInt("a"));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		finally
		{
			try 
			{
				if(conn != null) conn.close();
				
				if(pstmt != null) pstmt.close();
				if(pstmt2 != null) pstmt2.close();
				if(pstmt3 != null) pstmt3.close();
				if(pstmt4 != null) pstmt4.close();
				if(pstmt5 != null) pstmt5.close();

				if(rs != null) rs.close();
				if(rs2 != null) rs2.close();
				if(rs3 != null) rs3.close();
				if(rs4 != null) rs4.close();
				if(rs5 != null) rs5.close();
				
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return list;
	}
}
