package idc.cupon.command;

import idc.model.IdcCmd;
import idc.model.IdcCuponDBDTO;
import idc.model.IdcDAO;

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 IdcCuponListCmd implements IdcCmd 
{
	private IdcDAO dao;
	private final int WRITE_PER_PAGE = 10;
	
	public IdcCuponListCmd()
	{
		dao = new IdcDAO();
	}
	
	//쿠폰리스트 불러오기
	@Override

	public void execute(HttpServletRequest request, HttpServletResponse response) 
	{
		ArrayList<IdcCuponDBDTO> list = new ArrayList<IdcCuponDBDTO>();
		//검색결과값 
		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);
		//쿠폰리스트 얻어오기.
		idcCuponListCmd(list,curPage,sc);
		
		request.setAttribute("IdcCuponList", list);
		
		request.setAttribute("pageCnt", pageCnt);
		request.setAttribute("curPage", curPage);
		request.setAttribute("firstPage", firstPage);
		request.setAttribute("endPage", endPage);
		
		request.setAttribute("searchContent", sc);
   
		
	}


	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 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;
			sql="SELECT COUNT(*) as a ";
			sql+="FROM clientdb as cli, ( ";
			sql+="SELECT client_code, service_ip ";
			sql+="FROM servicedb as s, cupondb as cup ";
			sql+="	WHERE s.service_code = cup.service_code ";
			sql+="	) as t ";
			sql+="	WHERE cli.client_code = t.client_code";
			
			pstmt =conn.prepareStatement(sql);
			rs = pstmt.executeQuery();
			if(rs.next())
				pageCnt = rs.getInt("a")/WRITE_PER_PAGE+1;
		}
		catch (Exception e) 
		{
			System.out.println("IdcCuponListCmd - 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("IdcCuponListCmd - pageCnt  에서의 sqlexception");
				e.printStackTrace();
			}
		}
		
		return pageCnt;
	}


	
	//쿠폰 리스트 얻어오기.
	private void idcCuponListCmd(ArrayList<IdcCuponDBDTO> list, int curPage ,String sc)
	{
		Connection conn=null;
		PreparedStatement pstmt=null;
		ResultSet rs=null;
		try	
		{
			conn=dao.getConn();
			String sql="";
			sql+="SELECT cli.client_code, cli.client_name, cupon_current, cupon_end_date, cupon_note, cupon_num, cupon_request_date, ";
			sql+="cupon_result, cupon_use, service_ip, server_code, server_upgrade, service_code, service_status ";
			sql+="FROM clientdb as cli, ( ";
			sql+="SELECT client_code, cupon_current, cupon_end_date, cupon_note, cupon_num, cupon_request_date, ";
			sql+="cupon_result, cupon_use, service_ip, s.server_code, server_upgrade, s.service_code, service_status ";
			sql+="FROM servicedb as s, cupondb as cup ";
			sql+="WHERE s.service_code = cup.service_code ";
			sql+=") as t ";
			sql+="WHERE cli.client_code = t.client_code AND (client_name like '%"+sc+"%' OR service_ip like '%"+sc+"%') ";
			sql+="ORDER BY cupon_num DESC LIMIT ?,? ";
			
			pstmt =conn.prepareStatement(sql);
			pstmt.setInt(1, (curPage-1)*WRITE_PER_PAGE);
			pstmt.setInt(2, WRITE_PER_PAGE);
			rs = pstmt.executeQuery();
			while(rs.next())
			{
				IdcCuponDBDTO dto = new IdcCuponDBDTO();
				
				dto.setClient_code(rs.getString("cli.client_code"));
				dto.setClient_name(rs.getString("cli.client_name"));
				dto.setCupon_current(rs.getInt("cupon_current"));
				dto.setCupon_end_date(rs.getDate("cupon_end_date"));
				dto.setCupon_note(rs.getString("cupon_note"));
				dto.setCupon_request_date(rs.getDate("cupon_request_date"));
				dto.setCupon_result(rs.getInt("cupon_result"));
				dto.setCupon_use(rs.getInt("cupon_use"));
				dto.setService_ip(rs.getString("service_ip"));
				dto.setServer_code(rs.getString("server_code"));
				dto.setServer_upgrade(rs.getString("server_upgrade"));
				dto.setService_code(rs.getString("service_code"));
				dto.setService_status(rs.getString("service_status"));
				dto.setCupon_num(rs.getInt("cupon_num"));
				list.add(dto);
			}
			int t = list.size();
			for(int i=0;i<t;i++)
			{
				list.get(i).setNum((curPage-1)*WRITE_PER_PAGE+t-i);
			}
		}
		
		
		catch (Exception e) 
		{
			System.out.println("idcCuponListCmd 에서의 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("idcCuponListCmd 에서의 sqlexception");
				e.printStackTrace();
			}
		}
	}
}
