package idc.line.command;

import idc.model.IdcCmd;
import idc.model.IdcDAO;
import idc.model.IdcIPDBDTO;
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 IdcLineIPListFormExcelCmd implements IdcCmd 
{
	private IdcDAO dao;
	private int WRITE_PER_PAGE=10;
	public IdcLineIPListFormExcelCmd() 
	{
		dao = new IdcDAO();
	}
	
	
	//ip리스트 엑셀 페이지
	@Override
	public void execute(HttpServletRequest request, HttpServletResponse response) 
	{
		String sc = request.getParameter("searchContent");
		if(sc==null)
			sc="";

		int pageCnt = getPageCnt(sc);
		int curPage = getCurPage(request.getParameter("curPage"));
		int firstPage = getFirstPage(pageCnt, curPage);
		int endPage = getEndPage(firstPage, pageCnt);
		String line_num = request.getParameter("lineNum");
		ArrayList<IdcIPDBDTO> list = new ArrayList<IdcIPDBDTO>();
		IdcLineDBDTO dto=null;
		
		//라인 코드 넘어오면 실행된다.
		if(line_num!=null && line_num!="")
		{
			dto = idcLineIPListFormCmd(list,Integer.parseInt(line_num));
			request.setAttribute("IdcLineDB", dto);
		}
		else	//라인 코드 없으면 전체 뿌려준다.
		{
			idcLineIPListFormCmd(list,curPage, sc);
			request.setAttribute("pageCnt",pageCnt);
			request.setAttribute("firstPage", firstPage);
			request.setAttribute("curPage",curPage);
			request.setAttribute("endPage", endPage);
			request.setAttribute("sc", sc);
			request.setAttribute("WRITE_PER_PAGE", WRITE_PER_PAGE);
		}
		request.setAttribute("IPList", list);
	}
	private int getCurPage(String strPage)
	{
		int curPage=1;
		
		if(strPage !=null && strPage!="")
			curPage=Integer.parseInt(strPage);
		
		return curPage;
	}
	private int getFirstPage(int pageCnt, int curPage)
	{
		int firstPage = 1;
		if(pageCnt-10>1)
			firstPage=pageCnt-10;
		
		int div =0;
		if(firstPage>=curPage)
			div = firstPage-curPage;
		firstPage-=div;
		
		return firstPage;
	}
	private int getEndPage(int firstPage, int pageCnt)
	{
		int endPage = pageCnt;
		
		int div = pageCnt-firstPage;
		if(div>10)
			endPage-=div-10;
		return endPage;
	}
	private int getPageCnt(String sc)
	{
		int pageCnt=1;
		
		Connection conn=null;
		PreparedStatement pstmt=null;
		ResultSet rs=null;
		
		try	
		{
			conn=dao.getConn();
			String sql="SELECT COUNT(*) as a FROM ipdb WHERE ip_user like '%"+sc+"%' OR ip_address like '%"+sc+"%'";
			pstmt =conn.prepareStatement(sql);
			rs = pstmt.executeQuery();

			if(rs.next())
			{
				pageCnt = rs.getInt("a")/WRITE_PER_PAGE+1;
			}
		}
		catch (Exception e) 
		{
			System.out.println("IdcLineIPListFormCmd - pageCnt 에서의 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("IdcLineIPListFormCmd - pageCnt  에서의 sqlexception");
				e.printStackTrace();
			}
		}
		
		return pageCnt;
	}
	
	private void idcLineIPListFormCmd(ArrayList<IdcIPDBDTO> list, int curPage, String sc)
	{
		Connection conn=null;
		PreparedStatement pstmt=null;
		ResultSet rs=null;
		int index=1;
		try	
		{
			conn=dao.getConn();
			
			String sql="SELECT i.line_code, i.ip_case, i.ip_case2, i.ip_user, i.ip_before_user, i.ip_able, i.ip_address, ";
			sql+="i.ip1, i.ip2, i.ip3, i.ip4, i.ip5, i.ip6, i.ip_digit, i.ip_note, i.service_code, i.block, i.block_endday, i.cancel_reason, l.line_case";
			sql+= " FROM ipdb as i, linedb as l ";
			sql+= " WHERE i.line_num=l.line_num AND (ip_user like '%"+sc+"%' OR ip_address like '%"+sc+"%')";
			sql+= " ORDER BY ip1 ASC, ip2 ASC, ip3 ASC, ip4 ASC, ip5 ASC, ip6 ASC ";
//			System.out.println(sql);
			pstmt =conn.prepareStatement(sql);
			rs = pstmt.executeQuery();
			
			while(rs.next())
			{
				IdcIPDBDTO dto = new IdcIPDBDTO();
				
				dto.setBlock(rs.getString("i.block"));
				dto.setBlock_endday(rs.getDate("i.block_endday"));
				dto.setCancel_reason(rs.getString("i.cancel_reason"));
				dto.setIp1(rs.getInt("i.ip1"));
				dto.setIp2(rs.getInt("i.ip2"));
				dto.setIp3(rs.getInt("i.ip3"));
				dto.setIp4(rs.getInt("i.ip4"));
				dto.setIp5(rs.getInt("i.ip5"));
				dto.setIp6(rs.getInt("i.ip6"));
				dto.setIp_able(rs.getInt("i.ip_able"));
				dto.setIp_address(rs.getString("i.ip_address"));
				dto.setIp_before_user(rs.getString("i.ip_before_user"));
				dto.setIp_case(rs.getInt("i.ip_case"));
				dto.setIp_case2(rs.getString("i.ip_case2"));
				dto.setIp_digit(rs.getInt("i.ip_digit"));
				dto.setIp_note(rs.getString("i.ip_note"));
				dto.setIp_user(rs.getString("i.ip_user"));
				dto.setLine_code(rs.getString("i.line_code"));
				dto.setLine_case(rs.getString("l.line_case"));
				list.add(dto);
			}
		}
		catch (Exception e) 
		{
			System.out.println("IdcLineIPListFormCmd- 에서의 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("IdcLineIPListFormCmd 에서의 sqlexception");
				e.printStackTrace();
			}
		}
	}
	
	
	//검색 OR 대상 클릭하여 들어올 경우
	private IdcLineDBDTO idcLineIPListFormCmd(ArrayList<IdcIPDBDTO> list, int line_num)
	{
		Connection conn=null;
		PreparedStatement pstmt=null,pstmt2=null;
		ResultSet rs=null,rs2=null;
		
		IdcLineDBDTO dto = new IdcLineDBDTO();
		
		try	
		{
			conn=dao.getConn();
			String sql="SELECT * FROM linedb WHERE line_num = ?";
			pstmt =conn.prepareStatement(sql);
			pstmt.setInt(1, line_num);
			rs = pstmt.executeQuery();
			
			if(rs.next())
			{
				dto.setLine_num(line_num);
				dto.setLine_able(rs.getString("line_able"));
				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_before_user(rs.getString("line_before_user"));
				dto.setLine_block(rs.getString("line_block"));
				dto.setLine_block_endday(rs.getDate("line_block_endday"));
				dto.setLine_block_note(rs.getString("line_block_note"));
				dto.setLine_case(rs.getString("line_case"));
				dto.setLine_client_name(rs.getString("line_client_name"));
				dto.setLine_code(rs.getString("line_code"));
				dto.setLine_gateway(rs.getString("line_gateway"));
				dto.setLine_haveip(rs.getString("line_haveip"));
				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_user_allocatingday(rs.getDate("line_user_allocatingday"));
				dto.setLine_user_endday(rs.getDate("line_user_endday"));
				
			}
			
			sql="SELECT * FROM ipdb WHERE line_num = ? ";
			sql+="ORDER BY ip1 ASC, ip2 ASC, ip3 ASC, ip4 ASC ";
			pstmt2=conn.prepareStatement(sql);
			pstmt2.setInt(1, line_num);			
			rs2 = pstmt2.executeQuery();
			
			while(rs2.next())
			{
				IdcIPDBDTO temp = new IdcIPDBDTO();
				temp.setLine_code(rs.getString("line_code"));
				temp.setIp_case2(rs2.getString("ip_case2"));
				temp.setIp_address(rs2.getString("ip_address"));
				temp.setIp_user(rs2.getString("ip_user"));
				temp.setIp_able(rs2.getInt("ip_able"));
				temp.setBlock(rs2.getString("block"));
				temp.setBlock_endday(rs2.getDate("block_endday"));
				temp.setCancel_reason(rs2.getString("cancel_reason"));
				temp.setIp_note(rs2.getString("ip_note"));
				list.add(temp);
			}
		}
		catch (Exception e) 
		{
			System.out.println("IdcLineIPListFormCmd 에서의 exception");
			e.printStackTrace();
		}
		finally
		{
			try 
			{
				if(conn!=null) conn.close();
				if(pstmt!=null)pstmt.close();
				if(pstmt2!=null)pstmt2.close();
				if(rs!=null)rs.close();
				if(rs2!=null)rs2.close();
			}
			catch (SQLException e) 
			{
				System.out.println("IdcLineIPListFormCmd 에서의 sqlexception");
				e.printStackTrace();
			}
		}
		return dto;
	}
}
