package idc.service.command;

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;

import idc.model.*;

public class IdcEtcListCmd implements IdcCmd{

	IdcDAO dao = new IdcDAO();
	int WRITING_PER_PAGE = 10;
		
	public void execute(HttpServletRequest request, HttpServletResponse response){

		//서비스 전체 리스트
		ArrayList<IdcOnetimeDTO>list;		
		int pageCnt = 0;
		String curPage = request.getParameter("curPage");		
		if(curPage == null) curPage = "1";		
		pageCnt = getPageCnt();
		request.setAttribute("pageCnt", pageCnt);
		
		list = getEtcList(curPage);		
		request.setAttribute("list", list);		
	}

	//1회성 리스트 받아오기
	private ArrayList<IdcOnetimeDTO> getEtcList(String curPage){
		ArrayList<IdcOnetimeDTO>list = new ArrayList<IdcOnetimeDTO>();
		
		Connection conn = null;
		PreparedStatement pstmt = null, pstmt2 = null;
		ResultSet rs = null, rs2 = null;
		int numbering_end = WRITING_PER_PAGE * (Integer.parseInt(curPage) - 1);
		int numbering = 0;
		
		try {
			conn = dao.getConn();
			String sql = "SELECT COUNT(*) as a FROM onetimeDB";
			pstmt2 = conn.prepareStatement(sql);
			rs2 = pstmt2.executeQuery();
			
			if(rs2.next())
				numbering = rs2.getInt("a")-numbering_end;
									
			sql = "SELECT onetime_num, onetime_case, onetime_sub, onetime_amount, onetime_payment, client_name, server_code, ";
			sql += "onetime_nonpayment, onetime_accountStatus, onetime_request_date, onetime_payment_date, onetime_note ";
			sql +=" FROM onetimeDB ";
			sql += "ORDER BY onetime_num desc ";
			sql += " LIMIT ?, ?";
			
			pstmt = conn.prepareStatement(sql);
			pstmt.setInt(1, numbering_end);
			pstmt.setInt(2, WRITING_PER_PAGE);
			
			rs = pstmt.executeQuery();
			
			while(rs.next()){
				int onetime_num =numbering--;
				String client_name = rs.getString("client_name");
				String server_code = rs.getString("server_code");
				String onetime_case = rs.getString("onetime_case");
				String onetime_sub = rs.getString("onetime_sub");
				String onetime_amount = rs.getString("onetime_amount");
				String onetime_payment = rs.getString("onetime_payment");
				String onetime_nonpayment = rs.getString("onetime_nonpayment");
				String onetime_accountStatus = rs.getString("onetime_accountStatus");
				Date onetime_request_date = rs.getDate("onetime_request_date");
				Date onetime_payment_date = rs.getDate("onetime_payment_date");
				String onetime_note = rs.getString("onetime_note");
				
				String temp = "";
				switch (onetime_case){
				case "domain":
					temp = "도메인 구매";
					break;
				case "hdd":
					temp = "HDD 추가";
					break;
				case "ram":
					temp = "RAM 추가";
					break;
				case "ip":
					temp = "IP 변경";
					break;
				case "etc":
					temp = "기타 서비스";
					break;
				}
																
				IdcOnetimeDTO writing = new IdcOnetimeDTO();
				
				writing.setOnetime_num(onetime_num+"");
				writing.setClient_name(client_name);
				writing.setServer_code(server_code);
				writing.setOnetime_case(onetime_case);
				writing.setOnetime_sub(onetime_sub);
				writing.setOnetime_amount(onetime_amount);
				writing.setOnetime_payment(onetime_payment);
				writing.setOnetime_nonpayment(onetime_nonpayment);
				writing.setOnetime_accountStatus(onetime_accountStatus);
				writing.setOnetime_request_date(onetime_request_date);
				writing.setOnetime_payment_date(onetime_payment_date);
				writing.setOnetime_note(onetime_note);
				writing.setTemp(temp);
				
				//System.out.println("onetime_accountStatus " + onetime_accountStatus);
				if(onetime_accountStatus.equals("complete")){
					writing.setStatus("완납");
				}else{
					writing.setStatus("미납");
				}
				
				list.add(writing);					
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally{
			try {
				
				if(rs != null) rs.close();
				if(pstmt != null) rs.close();
				if(rs2 != null) rs2.close();
				if(pstmt2 != null) pstmt2.close();
				if(conn != null) conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}		
		
		return list;
	}
	
	//서비스 리스트 페이징 처리를 위한 기능 수행
	public int getPageCnt(){
		int pageCnt = 0;
		
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		
		try {
			conn = dao.getConn();
			String sql = "SELECT COUNT(*) AS num FROM onetimeDB";
			pstmt = conn.prepareStatement(sql);
			rs = pstmt.executeQuery();
			
			if(rs.next()){
				pageCnt = rs.getInt("num")/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;
	}
}
