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 IdcLineListCmd implements IdcCmd
{
	IdcDAO dao;
	private static final int WRITING_PER_PAGE = 10;
	public IdcLineListCmd() 
	{
		dao= new IdcDAO();
	}
	
	//등록되어있는 라인 리스트
	@Override
	public void execute(HttpServletRequest request, HttpServletResponse response) 
	{
		ArrayList<IdcLineDBDTO> list;
		String curPage = request.getParameter("curPage");
		
		list = idcLineList(curPage);
		int pageCnt = idcLineDBPageCnt();
		request.getSession().setAttribute("IdcLineDBList", list);
		request.setAttribute("pageCnt", pageCnt);
	}
	private ArrayList<IdcLineDBDTO> idcLineList(String curPage)
	{
		ArrayList<IdcLineDBDTO> list = new ArrayList<IdcLineDBDTO>();
		
		Connection conn=null;
		PreparedStatement pstmt=null;
		ResultSet rs=null;
		if(curPage==null || curPage=="")
			curPage="1";
		int curpage = Integer.parseInt(curPage);
		
		try	
		{
			conn=dao.getConn();
			
			String sql="SELECT * FROM linedb ORDER BY line_code ASC ";
					sql+="LIMIT ?,?";
			pstmt =conn.prepareStatement(sql);
			pstmt.setInt(1, WRITING_PER_PAGE *(curpage-1));
			pstmt.setInt(2, WRITING_PER_PAGE);
			rs = pstmt.executeQuery();
			
			while(rs.next())
			{
				IdcLineDBDTO dto = new IdcLineDBDTO();
				dto.setLine_code(rs.getString("line_code"));
				dto.setLine_case(rs.getString("line_case"));
				dto.setLine_IP(rs.getString("line_IP"));
				dto.setLine_able(rs.getString("line_able"));
				dto.setLine_block(rs.getString("line_block"));
				dto.setLine_client_name(rs.getString("line_client_name"));
				dto.setLine_user_allocatingday(rs.getDate("line_user_allocatingday"));
				dto.setLine_user_endday(rs.getDate("line_user_endday"));
				dto.setLine_mainip(rs.getString("line_mainip"));
				dto.setLine_subip1(rs.getString("line_subip1"));
				dto.setLine_subip2(rs.getString("line_subip2"));
				dto.setLine_subip3(rs.getString("line_subip3"));
				dto.setLine_before_user(rs.getString("line_before_user"));
				dto.setLine_before_allocatingday(rs.getDate("line_before_allocatingday"));
				dto.setLine_before_endday(rs.getDate("line_before_endday"));
				dto.setLine_before_mainip(rs.getString("line_before_mainip"));
				dto.setLine_before_subip1(rs.getString("line_before_subip1"));
				dto.setLine_before_subip2(rs.getString("line_before_subip2"));
				dto.setLine_before_subip3(rs.getString("line_before_subip3"));
				dto.setLine_note(rs.getString("line_note"));
				dto.setLine_reason(rs.getString("line_reason"));
				dto.setLine_block_note(rs.getString("line_block_note"));
				dto.setLine_block_endday(rs.getDate("line_block_endday"));
				
				list.add(dto);
			}
		}
		catch (Exception e) 
		{
			System.out.println("IDCLINEListCmd 에서의 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("IDCLINEListCmd 에서의 sqlexception");
				e.printStackTrace();
			}
		}
		
		return list;
	}
	 public int idcLineDBPageCnt()
	 {
		 int pageCnt = 0;
		 
		 Connection conn = null;
		 PreparedStatement pstmt = null;
		 ResultSet rs = null;
		 
		 try
		 {
			 conn=dao.getConn();
			 String sql = "SELECT COUNT(*) AS line_code FROM linedb";
			 pstmt = conn.prepareStatement(sql);
			 rs = pstmt.executeQuery();
			 if(rs.next())
			 {
				 pageCnt = rs.getInt(1) / WRITING_PER_PAGE+1;
			 }
		 }
		 catch(Exception e)
		 {
			 e.printStackTrace();
		 }
		 finally
		 {
			 try
		 		{
		 			if(rs!=null)rs.close();
		 			if(pstmt!=null)pstmt.close();
		 			if(conn!=null)conn.close();
		 		}
		 		catch(SQLException e)
		 		{
		 			e.printStackTrace();
		 		}
		 }
		 return pageCnt;
	 }
}
