package idc.deposit.command;

import idc.model.IdcCmd;
import idc.model.IdcDAO;
import idc.model.IdcDepositDTO;

import java.sql.Connection;
import java.sql.Date;
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 IdcDepositListCmd implements IdcCmd 
{
	IdcDAO dao;
	private final int WRITING_PER_PAGE=25;
	public IdcDepositListCmd() 
	{
		dao = new IdcDAO();
	}
	//입금리스트
	@Override
	public void execute(HttpServletRequest request, HttpServletResponse response) 
	{
		ArrayList<IdcDepositDTO> list;
		String sc=request.getParameter("searchContent");
		if(!(sc!=null && sc!=""))
			sc="";
		int pageCnt = getPageCnt(sc);
		int curPage = getCurPage(request.getParameter("curPage"));
		int firstPage = getFirstPage(pageCnt, curPage);
		int endPage = getEndPage(firstPage, pageCnt);
		
		list = idcDepositList(curPage,sc);
		pageCnt = idcDepositPageCnt();
		
		request.setAttribute("IdcDepositList", list);
		request.setAttribute("IdcDepositListSize", list.size());
		request.setAttribute("pageCnt", pageCnt);
		request.setAttribute("curPage", curPage);
		request.setAttribute("firstPage", firstPage);
		request.setAttribute("endPage", endPage);

		request.setAttribute("searchContent", sc);
	}
	private int getCurPage(String strPage)
	{
		int curPage=1;
		
		if(strPage !=null && strPage!="")
			curPage=Integer.parseInt(strPage);
		
		return curPage;
	}
	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 depositdb WHERE client_name like '%"+sc+"%'";
			pstmt =conn.prepareStatement(sql);
			rs = pstmt.executeQuery();
			if(rs.next())
				pageCnt = rs.getInt("a")/WRITING_PER_PAGE+1;
		}
		catch (Exception e) 
		{
			System.out.println("IdcDepositListCmd - 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("IdcDepositListCmd - pageCnt  에서의 sqlexception");
				e.printStackTrace();
			}
		}
		
		return pageCnt;
	}
	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;
	}
	
	//입금리스트 가져오기
	public ArrayList<IdcDepositDTO> idcDepositList(int curPage, String sc)
	{
		ArrayList<IdcDepositDTO> list = new ArrayList<IdcDepositDTO>();
		
		Connection conn=null;
		PreparedStatement pstmt=null, pstmt2=null;
		ResultSet rs = null,rs2=null;
		int numbering=0;
		int numbering_end=IdcDAO.WRITING_PER_PAGE*(curPage-1);
		try
		{
			conn = dao.getConn();
			String sql ="SELECT COUNT(*) as a FROM depositdb WHERE client_name like '%"+sc+"%'";
			pstmt2=conn.prepareStatement(sql);
			rs2=pstmt2.executeQuery();
			if(rs2.next())
				numbering=rs2.getInt("a")-numbering_end;
 			
			sql = "SELECT deposit_num, client_code, client_name, deposit_date, deposit_amount, deposit_bank, deposit_name, deposit_note";
			sql+=" FROM depositdb WHERE client_name like '%"+sc+"%' ";
			sql+=" ORDER BY deposit_num desc LIMIT ?,? ";
	 		
			pstmt = conn.prepareStatement(sql);
	 		pstmt.setInt(1,numbering_end);
	 		pstmt.setInt(2,IdcDAO.WRITING_PER_PAGE);
	 	 		
	 		rs = pstmt.executeQuery();
			
	 		while(rs.next())
	 		{
	 			int num = numbering--;
	 			int deposit_num = rs.getInt("deposit_num");
	 			String code = rs.getString("client_code");
	 			String name = rs.getString("client_name");
	 			Date date = rs.getDate("deposit_date");
	 			int amount2 = rs.getInt("deposit_amount");
	 			//String amount2 = rs.getString("deposit_amount");
	 			//DecimalFormat df = new DecimalFormat("#,##0");
	 			//String amount = rs.getString(df.format("deposit_amount"));
	 			
	 			
	 			String amount = String.format("%,d", amount2);
	 			
	 			
	 			
	 			
	 			
	 			String bank = rs.getString("deposit_bank");
	 			String de_name = rs.getString("deposit_name");
	 			String note = rs.getString("deposit_note");

	 			IdcDepositDTO dto = new IdcDepositDTO();
	 			
	 			dto.setNum(num);
	 			dto.setDeposit_num(deposit_num);
	 			dto.setClient_code(code);
	 			dto.setClient_name(name);
	 			dto.setDeposit_date(date);
	 			
	 			dto.setDeposit_amount2(amount);
	 			
	 			dto.setDeposit_bank(bank);
	 			dto.setDeposit_name(de_name);
	 			dto.setDeposit_note(note);
	 			
	 			list.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(rs2 != null) rs2.close();
				if(pstmt2 != null) pstmt2.close();
			} 
			catch (Exception e)
			{
				e.printStackTrace();
			}
		}
		
		return list;
	}
	public int idcDepositPageCnt()
	 {
		 int pageCnt = 0;
		 
		 Connection conn = null;
		 PreparedStatement pstmt = null;
		 ResultSet rs = null;
		 
		 try
		 {
			 conn=dao.getConn();
			 String sql = "SELECT COUNT(*) AS deposit_num FROM ";
				 sql+="depositdb";
			 pstmt = conn.prepareStatement(sql);
			 rs = pstmt.executeQuery();
			 if(rs.next())
			 {
				 pageCnt = rs.getInt("deposit_num") / IdcDAO.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;
	 }
}
