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 IdcCuponWriteFormCmd implements IdcCmd 
{
	IdcDAO dao;
	private final int WRITE_PER_PAGE=10;

	public IdcCuponWriteFormCmd() 
	{
		dao = new IdcDAO();
	}
	//쿠폰 등록창(1)
	@Override
	public void execute(HttpServletRequest request, HttpServletResponse response) 
	{
		String strPage;
		int pageCnt,curPage;
		
		ArrayList<IdcCuponDBDTO> list = new ArrayList<IdcCuponDBDTO>();
		
		strPage = request.getParameter("CurPage");
		if(strPage=="" || strPage==null)
			curPage=1;
		else
			curPage=Integer.parseInt(strPage);
		
		pageCnt = idcServiceListPageCnt();
		idcCuponWriteFormCmd(list,curPage);
		
		request.getSession().setAttribute("IdcServiceList", list);
		request.setAttribute("pageCnt", pageCnt);
		
	}
	private int idcServiceListPageCnt()
	{
		int pageCnt=0;
		
		Connection conn=null;
		PreparedStatement pstmt=null;
		ResultSet rs=null;
		try	
		{
			conn=dao.getConn();
			String sql = "SELECT COUNT(*) AS service_num FROM serviceDB";
			pstmt = conn.prepareStatement(sql);
			rs = pstmt.executeQuery();
			
			if(rs.next())
				pageCnt = rs.getInt("service_num")/WRITE_PER_PAGE + 1;
		}
		catch (Exception e) 
		{
			System.out.println("IdcCuponWriteFormCmd idcServiceListPageCnt 에서의 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("IdcCuponWriteFormCmd idcServiceListPageCnt 에서의 sqlexception");
				e.printStackTrace();
			}
		}
		
		return pageCnt;
	}
	private void idcCuponWriteFormCmd(ArrayList<IdcCuponDBDTO> list, int curPage)
	{
		Connection conn=null;
		PreparedStatement pstmt=null;
		ResultSet rs=null;
		try	
		{
			conn=dao.getConn();
			String sql="SELECT  s.service_status, s.client_code, s.service_code, s.server_code, c.client_name, s.service_num  ";
			sql+="FROM servicedb as s INNER JOIN clientdb as c ";
			sql+="WHERE s.client_code = c.client_code AND client_reseller=0 AND service_line!='test' AND service_line!='substitution' ";
			sql+="Limit ?,?";
//			System.out.println(sql);
			pstmt =conn.prepareStatement(sql);
			pstmt.setInt(1, WRITE_PER_PAGE*(curPage-1));
			pstmt.setInt(2, WRITE_PER_PAGE);
			
			rs = pstmt.executeQuery();
			
			while(rs.next())
			{
				IdcCuponDBDTO dto = new IdcCuponDBDTO();
				dto.setCupon_num(rs.getInt("service_num"));
				dto.setService_code(rs.getString("service_code"));
				dto.setService_status(rs.getString("service_status"));
				dto.setClient_code(rs.getString("client_code"));
				dto.setClient_name(rs.getString("client_name"));
				dto.setServer_code(rs.getString("server_code"));
				String a = dto.getServer_code().substring(0,1);
				if(!isCo(a))	
					list.add(dto);
			}
		}
		catch (Exception e) 
		{
			System.out.println("IdcCuponWriteFormCmd 에서의 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("IdcCuponWriteFormCmd 에서의 sqlexception");
				e.printStackTrace();
			}
		}
	}
	
	private boolean isCo(String a)
	{
		byte[] b = a.getBytes();
		System.out.println(b[0]);
		if(b[0]>=48 && b[0]<=57)
			return true;
		
		return false;
	}
}
