package idc.line.command;

import idc.model.IdcCmd;
import idc.model.IdcDAO;
import idc.model.IdcLineDBDTO;

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 IdcLineListFormCmd implements IdcCmd
{
	IdcDAO dao;
	private int WRITE_PER_PAGE = 10;
	public IdcLineListFormCmd() 
	{
		dao = new IdcDAO();
		
	}
	
	//등록되어있는 라인 리스트
	@Override
	public void execute(HttpServletRequest request, HttpServletResponse response) 
	{
		String strPage = request.getParameter("curPage");
		int curPage=1,pageCnt=1;
		if(strPage != null && strPage !="")
			curPage=Integer.parseInt(strPage);
		
		ArrayList<IdcLineDBDTO> list = new ArrayList<IdcLineDBDTO>();
		idcLineListForm(list,curPage);
		pageCnt = getPageCnt();
		request.setAttribute("IdcLineDB", list);
		request.setAttribute("pageCnt", pageCnt);
	}
	private int getPageCnt()
	{
		int pageCnt = 1;
		Connection conn=null;
		PreparedStatement pstmt=null;
		ResultSet rs=null;
		try	
		{
			conn=dao.getConn();
			String sql="SELECT COUNT(*) as c FROM linedb";
			pstmt =conn.prepareStatement(sql);
			rs = pstmt.executeQuery();
			if(rs.next())
				pageCnt = rs.getInt("c");
			
			pageCnt=pageCnt/WRITE_PER_PAGE+1;
		}
		catch (Exception e) 
		{
			System.out.println("getPageCnt 에서의 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("getPageCnt 에서의 sqlexception");
				e.printStackTrace();
			}
		}
		return pageCnt;
	}
	private void idcLineListForm(ArrayList<IdcLineDBDTO> list, int curPage)
	{
		Connection conn=null;
		PreparedStatement pstmt=null,pstmt2=null;
		ResultSet rs=null,rs2=null;
		int numbering=0;
		int numbering_end=WRITE_PER_PAGE*(curPage-1);
		try	
		{
			conn=dao.getConn();
			String sql="SELECT COUNT(*) as a FROM linedb";
			pstmt2=conn.prepareStatement(sql);
			rs2=pstmt2.executeQuery();
			if(rs2.next())
				numbering=rs2.getInt("a")-numbering_end;
			sql="SELECT line_num, line_code, line_name, line_case, line_client_name, line_haveip, line_user_allocatingday, ";
			sql+="line_user_endday, line_phone, line_id, line_pass ";
			sql+="FROM linedb ORDER BY line_num DESC ";
			sql+="LIMIT ?,?";
			pstmt =conn.prepareStatement(sql);
			pstmt.setInt(1, numbering_end);
			pstmt.setInt(2, WRITE_PER_PAGE);
			
			rs = pstmt.executeQuery();
			
			while(rs.next())
			{
				IdcLineDBDTO dto = new IdcLineDBDTO();
				dto.setNum(numbering--);
				dto.setLine_num(rs.getInt("line_num"));
				dto.setLine_code(rs.getString("line_code"));
				dto.setLine_name(rs.getString("line_name"));
				dto.setLine_case(rs.getString("line_case"));
				dto.setLine_client_name(rs.getString("line_client_name"));
				dto.setLine_haveip(rs.getString("line_haveip"));
				dto.setLine_user_allocatingday(rs.getDate("line_user_allocatingday"));
				dto.setLine_user_endday(rs.getDate("line_user_endday"));
				dto.setLine_phone(rs.getString("line_phone"));
				dto.setLine_id(rs.getString("line_id"));
				dto.setLine_pass(rs.getString("line_pass"));
				
				list.add(dto);
			}
		}
		catch (Exception e) 
		{
			System.out.println("IdcLineListFormCmd 에서의 exception");
			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) 
			{
				System.out.println("IdcLineListFormCmd 에서의 sqlexception");
				e.printStackTrace();
			}
		}
	}
}
















