package idc.client.command;

import idc.model.IdcCmd;
import idc.model.IdcDAO;
import idc.model.IdcHaveListDBDTO;

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;

public class IdcClientAllServerListCmd implements IdcCmd 
{
	private IdcDAO dao;
	public IdcClientAllServerListCmd() 
	{
		dao = new IdcDAO();
	}
	@Override
	public void execute(HttpServletRequest request, HttpServletResponse response)
	{
		ArrayList<IdcHaveListDBDTO> list=null;
		
		list = idcClientAllServerList();

		request.setAttribute("IdcHaveList", list);
		request.setAttribute("IdcHaveListSize", list.size());
	}
	
	//보유현황 페이지 읽어오기
	public ArrayList<IdcHaveListDBDTO> idcClientAllServerList()
	{
		ArrayList<IdcHaveListDBDTO> names=new ArrayList<IdcHaveListDBDTO>();
		
		Connection conn=null;
		PreparedStatement pstmt=null,pstmt2=null;
		ResultSet rs = null, rs2=null;
		
		try 
		{
			conn = dao.getConn();
			String sql = "SELECT * FROM havelist ";
			pstmt = conn.prepareStatement(sql);
			
			rs = pstmt.executeQuery();
			
			while(rs.next())
			{
				IdcHaveListDBDTO dto = new IdcHaveListDBDTO();
				dto.setClient_name(rs.getString("client_name"));
				dto.setDefence_c(rs.getInt("defence_c"));
				dto.setDefence_t(rs.getInt("defence_t"));
				dto.setNormal_c(rs.getInt("normal_c"));
				dto.setNormal_t(rs.getInt("normal_t"));
				dto.setSolo_c(rs.getInt("solo_c"));
				dto.setSolo_t(rs.getInt("solo_t"));
				dto.setSub_c(rs.getInt("substitution_c"));
				dto.setSub_t(rs.getInt("substitution_t"));
				dto.setTest_c(rs.getInt("test_c"));
				dto.setTest_t(rs.getInt("test_t"));
				dto.setTest_total(rs.getInt("test_total"));
				dto.setTotal_c(rs.getInt("total_c"));
				dto.setTotal_t(rs.getInt("total_t"));
				dto.setTotal_total(rs.getInt("total_total"));
				dto.setSoloLine_c(rs.getInt("soloLine_c"));
				dto.setSoloLine_t(rs.getInt("soloLine_t"));
				names.add(dto);
			}
			
			sql="SELECT  sum(normal_t) as normal_t, sum(normal_c) as normal_c, sum(defence_t) as defence_t, sum(defence_c) as defence_c, ";
			sql+="sum(solo_t) as solo_t, sum(solo_c) as solo_c, sum(substitution_t) as substitution_t, sum(substitution_c) as substitution_c, sum(total_t) as total_t, sum(total_c) as total_c, ";
			sql+="sum(total_total) as total_total, sum(test_t) as test_t, sum(test_c) as test_c, sum(test_total) as test_total, sum(sololine_t) as soloLine_t, sum(sololine_c) as soloLine_c  ";
			sql+=" FROM havelist";
			
			pstmt2 = conn.prepareStatement(sql);
			rs2 = pstmt2.executeQuery();
			
			if(rs2.next())
			{
				IdcHaveListDBDTO dto = new IdcHaveListDBDTO();
				dto.setClient_name("최종합계");
				dto.setDefence_c(rs2.getInt("defence_c"));
				dto.setDefence_t(rs2.getInt("defence_t"));
				dto.setNormal_c(rs2.getInt("normal_c"));
				dto.setNormal_t(rs2.getInt("normal_t"));
				dto.setSolo_c(rs2.getInt("solo_c"));
				dto.setSolo_t(rs2.getInt("solo_t"));
				dto.setSub_c(rs2.getInt("substitution_c"));
				dto.setSub_t(rs2.getInt("substitution_t"));
				dto.setTest_c(rs2.getInt("test_c"));
				dto.setTest_t(rs2.getInt("test_t"));
				dto.setTest_total(rs2.getInt("test_total"));
				dto.setTotal_c(rs2.getInt("total_c"));
				dto.setTotal_t(rs2.getInt("total_t"));
				dto.setTotal_total(rs2.getInt("total_total"));
				dto.setSoloLine_c(rs2.getInt("soloLine_c"));
				dto.setSoloLine_t(rs2.getInt("soloLine_t"));
				names.add(dto);
			}
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			try 
			{
				if(conn!=null) conn.close();
				if(pstmt!=null) pstmt.close();
				if(rs!=null) rs.close();
				if(pstmt2!=null) pstmt2.close();
				if(rs2!=null) rs2.close();
			} 
			catch (SQLException e) 
			{
				e.printStackTrace();
			}
		}
		return names;
	}	
}
