package idc.service.command;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import idc.model.*;

import java.sql.*;
import java.util.ArrayList;

public class IdcServiceDetailCmd implements IdcCmd{

	private int WRITING_PER_PAGE = 10;
	IdcDAO dao = new IdcDAO();	
	IdcUseServiceClientDTO iusc = new IdcUseServiceClientDTO();
	ArrayList<AddIpDTO>aip = new ArrayList<AddIpDTO>();
	ArrayList<AddEtcDTO>aet = new ArrayList<AddEtcDTO>();
	
	
	public void execute(HttpServletRequest request, HttpServletResponse response){
		
		//고객별 서비스 뿌려주기
		String client_code = request.getParameter("client_code");		
		ArrayList<IdcSingleServiceDTO>detail;
		detail = serviceListSelected(client_code);
										
		request.setAttribute("detail", detail);
		request.setAttribute("iusc", iusc);
		request.setAttribute("aip", aip);
		request.setAttribute("aet", aet);
		request.setAttribute("client_code", client_code);
		
		//미지급된 1회성 리스트 불러오기
		ArrayList<IdcOnetimeDTO> onetime;
		onetime = getOnetimeList(client_code);
		request.setAttribute("onetime", onetime);
				
		//히스토리 부분 페이징
		ArrayList<IdcHistoryDBDTO>hlist;
		
		String d = getdate();
		String client_name = getClient_name(client_code);		
		int pageCnt = getPageCnt("historyDB", client_name);
		int curPage = getCurPage(request.getParameter("curPage"));
		int firstPage = getFirstPage(pageCnt,curPage);
		int endPage = getEndPage(firstPage, pageCnt);
		
		if(curPage > endPage){
			curPage = endPage;
		}

		hlist = getHistoryList(Integer.toString(curPage), client_name);

		request.setAttribute("pageCnt", pageCnt);
		request.setAttribute("curPage", curPage);
		request.setAttribute("firstPage", firstPage);
		request.setAttribute("endPage", endPage);						
		request.setAttribute("hlist", hlist);
		request.setAttribute("today", d);
	}
	
	private String getdate()
	{
		Connection conn=null;
		PreparedStatement pstmt=null;
		ResultSet rs=null;
		String d="";
		try	
		{
			conn=dao.getConn();
			String sql="SELECT curdate() as a";
			pstmt =conn.prepareStatement(sql);
			rs = pstmt.executeQuery();
			if(rs.next())
				d=rs.getDate("a").toString();
		}
		catch (Exception e) 
		{
			System.out.println("getdate 에서의 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("getdate 에서의 sqlexception");
				e.printStackTrace();
			}
		}
		
		return d;
	}
	
	//서비스 정보 불러오기
	private ArrayList<IdcSingleServiceDTO> serviceListSelected(String client_code){
		
		Connection conn = null;
		PreparedStatement pstmt = null, pstmt5=null, pstmt6 = null, pstmt7 = null, 
				pstmt8=null, pstmt9=null, pstmt10=null, pstmt11=null, pstmt12=null, pstmt13=null, 
				pstmt14=null, pstmt15=null;
		
		ResultSet rs=null, rs2=null, rs3=null,rs4 = null, rs5=null, rs6 = null, rs7 = null, 
				rs8 = null, rs9 = null, rs10 = null, rs11 = null, rs12 = null, rs13 = null, rs14 = null, rs15 = null;

		String sql;		
		
		//고객 단일 서비스 정보		
		ArrayList<IdcSingleServiceDTO> service_list = new ArrayList<IdcSingleServiceDTO>();
		
		String client_name = "";
		String client_money = "";
		
		int total_payment = 0;
		int total_nonpayment = 0;
		
		int tokyo_server_sum = 0;
		int chiba_server_sum = 0;
		
		int normal_server_sum = 0;
		int defence_server_sum = 0;
		int solo_server_sum = 0;
		int soloLine_server_sum = 0;
		int test_server_sum = 0;
		int substitution_server_sum = 0;
		
		String service_code="";
		String service_line = "";
		String service_ip;
		String server_code = "";
		String server_process;
		String server_spec;
		String server_cpuname;
		String server_ram, server_ram1, server_ram2, server_ram3, server_ram4, server_ram5;
		String server_hdd, server_hdd1, server_hdd2, server_hdd3, server_hdd4, server_hdd5;
		String server_location;
		int service_amount;
		Date service_payment_date;
		int service_nonpayment;
		String service_note;
		String service_status;
				
		try {
			conn = dao.getConn();
							
			sql = "SELECT server_code, service_code FROM serviceDB WHERE client_code=? ORDER BY service_num DESC";			
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, client_code);
									
			rs = pstmt.executeQuery();
						
			while(rs.next())
			{
				
				IdcSingleServiceDTO issd = new IdcSingleServiceDTO();				
				sql = "SELECT service_status, service_code, service_line, service_ip, service_os, service_sw, service_amount, service_payment_date, service_nonpayment, service_note, ";
				sql += " service_request_date, service_open_date, service_defence, service_cost, service_deduction, service_unitedAmount, service_nonAmount ";
				sql += " FROM serviceDB WHERE server_code=?";				
				pstmt6 = conn.prepareStatement(sql);				
				pstmt6.setString(1, rs.getString("server_code"));
				
				server_code = rs.getString("server_code");
				service_code = rs.getString("service_code");								
				rs6 = pstmt6.executeQuery();
				if(rs6.next()){										
					
					issd.setService_status(rs6.getString("service_status"));
					String singleService_code = rs6.getString("service_code"); 
					issd.setService_code(singleService_code);
					issd.setService_cost(rs6.getString("service_cost"));
					issd.setServer_code(rs.getString("server_code"));
					issd.setService_deduction(rs6.getString("service_deduction"));
					issd.setService_united(rs6.getString("service_unitedAmount"));
					issd.setService_nonAmount(rs6.getString("service_nonAmount"));
					
					int cs_count = setCsCount(singleService_code, "vpcdb");
					int kcs_count = setCsCount(singleService_code, "kcsdb");
					
					//서비스별 추가 아이피및/추가cs, kcs추가
					sql = "SELECT addDB_ip, addDB_code, addDB_amount FROM addDB ";
					sql += "WHERE service_code=? and addDB_case = 'ip'";
					sql += "order by addDB_ip ASC ";
					pstmt7 = conn.prepareStatement(sql);					
					pstmt7.setString(1, singleService_code);
					int ip_total = 0;
					
					rs7 = pstmt7.executeQuery();
					while(rs7.next()){
						AddIpDTO writing = new AddIpDTO();
						writing.setAddDB_ip(rs7.getString("addDB_ip"));
						writing.setAddDB_code(rs7.getString("addDB_code"));
						writing.setService_code(singleService_code);
						writing.setAddDB_case("ip");						
						aip.add(writing);
					}
					issd.setIp_total(ip_total);
					
					int cs_total = 0;
					sql = "SELECT addDB_ip, addDB_code, addDB_amount FROM addDB ";
					sql += "WHERE service_code=? and addDB_case = 'cs'";
					sql += "order by addDB_code asc ";
					pstmt8 = conn.prepareStatement(sql);					
					pstmt8.setString(1, singleService_code);
					
					rs8 = pstmt8.executeQuery();
					
					while(rs8.next()){
						AddIpDTO writing = new AddIpDTO();
						writing.setAddDB_ip(rs8.getString("addDB_ip"));
						writing.setAddDB_code(rs8.getString("addDB_code"));
						writing.setService_code(singleService_code);
						writing.setAddDB_case("cs");						
						aip.add(writing);
					}
					issd.setCs_total(cs_total);
					
					//서비스별 추가 아이피및/추가cs, kcs추가
					sql = "SELECT addDB_ip, addDB_code, addDB_amount FROM addDB ";
					sql += "WHERE service_code=? and addDB_case = 'kcs'";
					sql += "order by addDB_code asc ";
					pstmt9 = conn.prepareStatement(sql);					
					pstmt9.setString(1, singleService_code);
					
					rs9 = pstmt9.executeQuery();
					
					while(rs9.next()){
						AddIpDTO writing = new AddIpDTO();
						writing.setAddDB_ip(rs9.getString("addDB_ip"));
						writing.setAddDB_code(rs9.getString("addDB_code"));
						writing.setService_code(singleService_code);
						writing.setAddDB_case("kcs");												
						aip.add(writing);
					}
					
					//기타 서비스 명칭 불러오기
					sql = "SELECT addDB_ip, addDB_code FROM addDB ";
					sql += "WHERE service_code=? and addDB_case = 'etc'";
					sql += "order by addDB_code asc ";
					pstmt10 = conn.prepareStatement(sql);					
					pstmt10.setString(1, singleService_code);
					
					rs10 = pstmt10.executeQuery();
					
					while(rs10.next()){						
						AddEtcDTO aDTO = new AddEtcDTO();						
						aDTO.setAddDB_code(rs10.getString("addDB_code"));
						aDTO.setService_code(singleService_code);
						aDTO.setAddDB_case("etc");												
						aet.add(aDTO);						
					}
																			
					issd.setCs_count(cs_count);
					issd.setKcs_count(kcs_count);
					
					issd.setService_line(rs6.getString("service_line"));
					service_line = rs6.getString("service_line");
					String temp = rs6.getString("service_line");
					
					if(temp.equals("normal")){
						normal_server_sum++;
						issd.setTemp("일반");
					}else if(temp.equals("defence")){
						defence_server_sum++;
						issd.setTemp("방어");
					}else if(temp.equals("solo")){
						solo_server_sum++;
						issd.setTemp("전용");
					}else if(temp.equals("test")){
						test_server_sum++;
						issd.setTemp("테스트");
					}else if(temp.equals("soloLine")){
						soloLine_server_sum++;
						issd.setTemp("전용 라인");
					}else if(temp.equals("substitution")){
						substitution_server_sum++;
						issd.setTemp("대체");
					}else if(temp.equals("vpn")){
						issd.setTemp("VPN");
					}else if(temp.equals("event")){
						issd.setTemp("이벤트");
					}else if(temp.equals("office")){
						issd.setTemp("자사서버");
					}else if(temp.equals("onetime")){
						issd.setTemp("일회성");						
					}
					
					issd.setService_ip(rs6.getString("service_ip"));
					issd.setService_os(rs6.getString("service_os"));
					issd.setService_sw(rs6.getString("service_sw"));
										
					if(rs6.getString("service_amount") != null){
						issd.setService_amount(Integer.parseInt(rs6.getString("service_amount")));
						
						total_payment += Integer.parseInt(rs6.getString("service_amount"));
					}					
					issd.setService_payment_date(rs6.getDate("service_payment_date"));
					issd.setService_request_date(rs6.getDate("service_request_date"));
					issd.setService_open_date(rs6.getDate("service_open_date"));
					issd.setService_defence(rs6.getString("service_defence"));
					
					if(rs6.getString("service_nonpayment") != null){						
						issd.setService_nonpayment(Integer.parseInt(rs6.getString("service_nonpayment")));
						
						total_nonpayment += Integer.parseInt(rs6.getString("service_nonpayment"));
					}
					
					issd.setService_note(rs6.getString("service_note"));
					
					//과금부분 amountDB에 입력된 부분 issd에 입력
					sql = "SELECT amount_num, amount_rack, amount_line, amount_cpu, amount_ip, amount_defence, amount_reseller, amount_long, ";
					sql+= "solo_amount_long, amount_pre, amount_authority, amount_contact, amount_total_discount, service_amount, amount_cost, ";
					sql += "ip_count, last_long, last_pre, amount_check, amount_request_date, amount_note, amount_cs, amount_kcs, amount_sub_discount, ";					
					sql += "amount_etc ";
					sql += " FROM amountDB WHERE service_code=?";
					sql += " ORDER BY amount_request_date desc, amount_num desc LIMIT 1";
					pstmt11 = conn.prepareStatement(sql);					
					pstmt11.setString(1, singleService_code);
					rs11 = pstmt11.executeQuery();
										
					if(rs11.next()){
						issd.setAmount_num(rs11.getInt("amount_num"));
						issd.setAmount_rack(rs11.getString("amount_rack"));
						issd.setAmount_line(rs11.getString("amount_line"));
						issd.setAmount_cpu(rs11.getString("amount_cpu"));
						issd.setAmount_ip(rs11.getString("amount_ip"));
						issd.setIp_total(Integer.parseInt(rs11.getString("amount_ip")));
						issd.setCs_total(Integer.parseInt(rs11.getString("amount_cs")));
						issd.setKcs_total(Integer.parseInt(rs11.getString("amount_kcs")));
						issd.setAmount_defence(rs11.getString("amount_defence"));
						issd.setAmount_reseller(rs11.getString("amount_reseller"));
						issd.setAmount_long(rs11.getString("amount_long"));
						issd.setSolo_amount_long(rs11.getString("solo_amount_long"));
						issd.setAmount_pre(rs11.getString("amount_pre"));
						issd.setAmount_authority(rs11.getString("amount_authority"));
						issd.setAmount_contact(rs11.getString("amount_contact"));
						issd.setAmount_total_discount(rs11.getString("amount_total_discount"));
						issd.setAmount_cost(rs11.getString("amount_cost"));
						issd.setIp_count(rs11.getString("ip_count"));
						issd.setLast_long(rs11.getString("last_long"));
						issd.setLast_pre(rs11.getString("last_pre"));
						issd.setAmount_check(rs11.getString("amount_check"));
						issd.setAmount_request_date(rs11.getDate("amount_request_date"));
						issd.setAmount_note(rs11.getString("amount_note"));						
						issd.setAmount_service_amount(rs11.getString("service_amount"));
						issd.setAmount_sub_discount(rs11.getString("amount_sub_discount"));
						issd.setAmount_etc(rs11.getString("amount_etc"));
					}					
					service_list.add(issd);
					
				}
				
				sql = "SELECT server_process, server_spec, server_cpuname, server_ram, server_ram1, server_ram2, server_ram3, server_ram4, server_ram5,";
				sql += " server_hdd, server_hdd1, server_hdd2, server_hdd3, server_hdd4, server_hdd5,";
				sql += " server_location FROM serverDB WHERE server_code=?";
				pstmt12 = conn.prepareStatement(sql);
				pstmt12.setString(1, server_code);
				rs12 = pstmt12.executeQuery();
				
				if(rs12.next()){
					
					issd.setServer_code(rs.getString("server_code"));
					issd.setServer_process(rs12.getString("server_process"));
					issd.setServer_spec(rs12.getString("server_spec"));
					issd.setServer_cpuname(rs12.getString("server_cpuname"));					
					issd.setServer_ram(rs12.getString("server_ram"));					
					issd.setServer_ram1(rs12.getString("server_ram1"));
					issd.setServer_ram2(rs12.getString("server_ram2"));
					issd.setServer_ram3(rs12.getString("server_ram3"));
					issd.setServer_ram4(rs12.getString("server_ram4"));
					issd.setServer_ram5(rs12.getString("server_ram5"));					
					issd.setServer_hdd(rs12.getString("server_hdd"));
					issd.setServer_hdd1(rs12.getString("server_hdd1"));
					issd.setServer_hdd2(rs12.getString("server_hdd2"));
					issd.setServer_hdd3(rs12.getString("server_hdd3"));
					issd.setServer_hdd4(rs12.getString("server_hdd4"));
					issd.setServer_hdd5(rs12.getString("server_hdd5"));
					issd.setServer_location(rs12.getString("server_location"));
																				
				}
				sql="SELECT onetime_amount FROM onetimedb WHERE service_code=? AND onetime_accountstatus='incomplete' ";
				pstmt5 = conn.prepareStatement(sql);
				pstmt5.setString(1, service_code);
				rs5 = pstmt5.executeQuery();
				int tot = 0;
				while(rs5.next())
				{
					tot+=rs5.getInt("onetime_amount");
				}
				issd.setAmount_one(Integer.toString(tot));
				sql = "SELECT COUNT(*) AS server_use_status FROM serverDB WHERE server_code =? and server_location='tokyo'";
				pstmt13 = conn.prepareStatement(sql);
				pstmt13.setString(1, server_code);
				rs13 = pstmt13.executeQuery();
				
				if(rs13.next()){					
					if(!service_line.equals("soloLine"))
						tokyo_server_sum += rs13.getInt(1); 
				}
				
				sql = "SELECT COUNT(*) AS server_use_status FROM serverDB WHERE server_code =? and server_location='chiba'";
				pstmt14 = conn.prepareStatement(sql);
				pstmt14.setString(1, server_code);
				rs14 = pstmt14.executeQuery();
				
				if(rs14.next()){					
					if(!service_line.equals("soloLine"))
						chiba_server_sum += rs14.getInt(1); 
				}
			}
						
			
			//고객명
			sql = "SELECT client_name, client_money FROM clientDB WHERE client_code=?";			
			pstmt15 = conn.prepareStatement(sql);				
			pstmt15.setString(1, client_code);
			rs15 = pstmt15.executeQuery();
			
			if(rs15.next()){					
				client_name = rs15.getString("client_name");
				client_money = rs15.getString("client_money");
			}
			
			//서버리스트 고객정보 idcUseServiceClientDTO에 저장
			iusc.clearDTO();
			iusc.setClient_name(client_name);
			iusc.setClient_tokyo_sum(tokyo_server_sum);
			iusc.setClient_chiba_sum(chiba_server_sum);
			iusc.setClient_normal_sum(normal_server_sum);
			iusc.setClient_defence_sum(defence_server_sum);
			iusc.setClient_solo_sum(solo_server_sum);
			iusc.setClient_test_sum(test_server_sum);
			iusc.setClient_substitution(substitution_server_sum);
			if(client_money != null && client_money != ""){
				iusc.setClient_money(Integer.parseInt(client_money));
			}else{
				iusc.setClient_money(0);
			}
						
			//전체요금
			iusc.setClient_total_amount(total_payment);
			
			//미납요금
			iusc.setClient_unpaid_amount(total_nonpayment);
			
			//개개의 서비스 정보
					
			//서비스 정보 request에 넣기
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally{
			try {
				if(rs != null) rs.close();
				if(rs2 != null) rs2.close();
				if(rs3 != null) rs3.close();
				if(rs4 != null) rs4.close();
				if(rs5 != null) rs5.close();
				if(rs6 != null) rs6.close();
				if(rs7 != null) rs7.close();
				if(rs8 != null) rs8.close();
				if(rs9 != null) rs9.close();
				if(rs10 != null) rs10.close();
				if(rs11 != null) rs11.close();
				if(rs12 != null) rs12.close();
				if(rs13 != null) rs13.close();
				if(rs14 != null) rs14.close();
				if(rs15 != null) rs15.close();
				
				if(pstmt != null) pstmt.close();
				if(pstmt5 != null) pstmt5.close();
				if(pstmt6 != null) pstmt6.close();
				if(pstmt7 != null) pstmt7.close();
				if(pstmt8 != null) pstmt8.close();
				if(pstmt9 != null) pstmt9.close();
				if(pstmt10 != null) pstmt10.close();
				if(pstmt11 != null) pstmt11.close();
				if(pstmt12 != null) pstmt12.close();
				if(pstmt13 != null) pstmt13.close();
				if(pstmt14 != null) pstmt14.close();
				if(pstmt15 != null) pstmt15.close();
				
				if(conn != null) conn.close();
				
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
				
		return service_list;
	}
	
	private int setCsCount(String service_code, String db){
		int count = 0;		
		
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		
		try {
			conn = dao.getConn();
			String sql = "SELECT COUNT(*) AS cs_count FROM "+db+" WHERE service_code=?";
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, service_code);
			rs = pstmt.executeQuery();
			
			if(rs.next()){
				count = rs.getInt("cs_count");
			}
			
		} 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 count;
	}
		
	private ArrayList<IdcHistoryDBDTO> getHistoryList(String curPage, String client_name){
		ArrayList<IdcHistoryDBDTO>list = new ArrayList<IdcHistoryDBDTO>();
		
		Connection conn = null;
		PreparedStatement pstmt = null, pstmt1 = null;
		ResultSet rs = null, rs1 = null;
		String sql;
		
		try {
			conn = dao.getConn();
			
			sql = "SELECT count(*) AS num FROM historyDB WHERE client_name=?";
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, client_name);
			
			rs = pstmt.executeQuery();
			int numbering = 0;
			int numbering_end = WRITING_PER_PAGE * (Integer.parseInt(curPage) - 1);			
			
			if(rs.next()){
				numbering = rs.getInt("num") - numbering_end;
			}												

			sql = "select * from historyDB ";			
			sql += "WHERE client_name=? order by history_num desc limit ?, ?";
			
			pstmt1 = conn.prepareStatement(sql);
			pstmt1.setString(1, client_name);
			pstmt1.setInt(2, WRITING_PER_PAGE * (Integer.parseInt(curPage) -1));
			pstmt1.setInt(3, WRITING_PER_PAGE);
			
			rs1 = pstmt1.executeQuery();					
									
			while(rs1.next()){					
				
				IdcHistoryDBDTO dto = new IdcHistoryDBDTO();
				dto.setBehavior(rs1.getString("behavior"));
				dto.setBehavior_case(rs1.getString("behavior_case"));
				dto.setClient_name(rs1.getString("client_name"));
				dto.setNote(rs1.getString("note"));
				dto.setServer_code(rs1.getString("server_code"));
				dto.setService_code(rs1.getString("service_code"));
				dto.setBehavior_date(rs1.getDate("behavior_date"));
				
				dto.setStrbehavior_date(dto.getBehavior_date().toString());
				dto.setBehavior_date(null);				
				list.add(dto);				
			}
			
						
		} catch (Exception e) {
			e.printStackTrace();
		} finally{
			try {
				if(rs != null) rs.close();
				if(rs1 != null) rs1.close();
				if(pstmt != null) pstmt.close();
				if(pstmt1 != null) pstmt1.close();
				if(conn != null) conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}		
		return list;
	}

	private int getCurPage(String strPage)
	{
		int curPage=1;
						
		if(strPage !=null && strPage!=""){
			curPage=Integer.parseInt(strPage);
			if(curPage < 1){
				curPage = 1;
			}
		}			
		return curPage;
	}
	private int getPageCnt(String db, String client_name)
	{
		int pageCnt=1;
		
		Connection conn=null;
		PreparedStatement pstmt=null;
		ResultSet rs=null;
		try	
		{
			conn=dao.getConn();
			String sql="SELECT COUNT(*) as a FROM "+db + " WHERE client_name='"+client_name+"' ";
			pstmt =conn.prepareStatement(sql);
			rs = pstmt.executeQuery();

			if(rs.next()){
				int num = rs.getInt("a");
				if(num%WRITING_PER_PAGE == 0){
					pageCnt = num/WRITING_PER_PAGE;
				}else{
					pageCnt = num/WRITING_PER_PAGE + 1;
				}				
			}
				
		}
		catch (Exception e) 
		{		
			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("IdcServiceDetail - 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;
		
		if(endPage < 1 ){
			endPage = 1;
		}
		return endPage;
	}
	
	private String getClient_name(String client_code){
		String client_name = "";
		
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		String sql;
		
		try	
		{
			conn=dao.getConn();
			sql="SELECT client_name FROM clientDB WHERE client_code=?";
			pstmt =conn.prepareStatement(sql);
			pstmt.setString(1, client_code);
			rs = pstmt.executeQuery();
			if(rs.next()){
				client_name = rs.getString("client_name");
			}
		}
		catch (Exception e){		
			e.printStackTrace();
		}finally{
			
			try{
				if(conn!=null) conn.close();
				if(pstmt!=null)pstmt.close();
				if(rs!=null)rs.close();
				
			}catch (SQLException e){
			
				e.printStackTrace();
			}
		}				
		return client_name;
	}
	
	private ArrayList<IdcOnetimeDTO> getOnetimeList(String client_code){
		ArrayList<IdcOnetimeDTO> list = new ArrayList<IdcOnetimeDTO>();
		
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		String sql;

		//각 누적 금액 및 갯수
		int domain_count = 0;
		int domain_total = 0;
		int hdd_count = 0;
		int hdd_total = 0;
		int ram_count = 0;
		int ram_total = 0;
		int ip_count = 0;
		int ip_total = 0;
		int etc_count = 0;
		int etc_total = 0;
				
		try {
			conn = dao.getConn();			
			sql = "SELECT onetime_case, onetime_sub, onetime_amount, onetime_payment, onetime_nonpayment, onetime_request_date, onetime_payment_date, onetime_note, onetime_manager ";
			sql +=" FROM onetimeDB WHERE onetime_accountStatus='incomplete' and client_code=? ORDER BY onetime_request_date asc";
			//2017년 5월 1일 수정 [최준흠]
			//sql +=" FROM onetimeDB WHERE client_code=? ORDER BY onetime_request_date asc";
			//sql +=" FROM onetimeDB WHERE onetime_accountStatus='incomplete' and client_code=? ORDER BY onetime_case asc";
			
			pstmt=conn.prepareStatement(sql);
			pstmt.setString(1, client_code);
			rs=pstmt.executeQuery();
			while(rs.next()){							
				
				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");
				Date onetime_request_date = rs.getDate("onetime_request_date");
				Date onetime_payment_date = rs.getDate("onetime_payment_date");
				String onetime_manager = rs.getString("onetime_manager");
				String onetime_note = rs.getString("onetime_note");
								
				IdcOnetimeDTO writing = new IdcOnetimeDTO();
				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_request_date(onetime_request_date);
				writing.setOnetime_payment_date(onetime_payment_date);
				writing.setOnetime_manager(onetime_manager);
				writing.setOnetime_note(onetime_note);
				
				switch (onetime_case)
				{
				case "domain":
					domain_count++;
					domain_total += Integer.parseInt(rs.getString("onetime_amount"));
					writing.setOnetime_count(domain_count+"");
					writing.setOnetime_total(domain_total+"");			
					writing.setTemp("도메인 구매");
					break;
				case "hdd":
					hdd_count++;
					hdd_total += Integer.parseInt(rs.getString("onetime_amount"));
					writing.setOnetime_count(hdd_count+"");
					writing.setOnetime_total(hdd_total+"");
					writing.setTemp("HDD 추가");
					break;
				case "ram":
					ram_count++;
					ram_total += Integer.parseInt(rs.getString("onetime_amount"));
					writing.setOnetime_count(ram_count+"");
					writing.setOnetime_total(ram_total+"");
					writing.setTemp("RAM 추가");
					break;
				case "ip":
					ip_count++;
					ip_total += Integer.parseInt(rs.getString("onetime_amount"));
					writing.setOnetime_count(ip_count+"");
					writing.setOnetime_total(ip_total+"");
					writing.setTemp("IP 변경");
					break;
				case "etc":
					etc_count++;
					etc_total += Integer.parseInt(rs.getString("onetime_amount"));
					writing.setOnetime_count(etc_count+"");
					writing.setOnetime_total(etc_total+"");
					writing.setTemp("기타 변경");
					break;
					
				default:
					break;
				}				
				list.add(writing);
				
			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally{
			try {
				if(rs != null) rs.close();
				if(pstmt != null) rs.close();
				if(conn != null) conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}		
		
		return list;
	}
	
}
