package idc.service.command;

import idc.model.IdcAddDBDTO;
import idc.model.IdcCmd;
import idc.model.IdcDAO;
import idc.model.IdcOnetimeDTO;
import idc.model.IdcServiceDepositDTO;
import idc.model.IdcSingleServiceDTO;

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 utility.IdcGetClientNameCode;

public class IdcServiceAccountingCmd implements IdcCmd{
	
	IdcDAO dao = new IdcDAO();
	
	//과금처리 정보 받아오기
	public void execute(HttpServletRequest request, HttpServletResponse response) {
		
		ArrayList<IdcServiceDepositDTO>deposit_list;
		ArrayList<IdcSingleServiceDTO>service_list;
		ArrayList<IdcOnetimeDTO>onetime_list;
		ArrayList<IdcAddDBDTO> adddb_unitedlist,adddb_notunitedlist;
		ArrayList<IdcAddDBDTO> total_list = new ArrayList<IdcAddDBDTO>();
		String client_code = request.getParameter("client_code");		
		String client_money = client_money(client_code);
		
		request.setAttribute("client_code", client_code);
		request.setAttribute("client_money", client_money);
		request.setAttribute("client_name",IdcGetClientNameCode.getClientName(client_code) );
		deposit_list = accountMonth(client_code);
		request.setAttribute("deposit_list", deposit_list);
		
		service_list = serviceFee(client_code);
		request.setAttribute("service_list", service_list);
		
		onetime_list = onetimeFee(client_code);
		request.setAttribute("onetime_list", onetime_list);			
		
		adddb_unitedlist = getUnitedList(client_code);
		request.setAttribute("adddb_unitedlist", adddb_unitedlist);
		
		adddb_notunitedlist = getNotUnitedList(client_code);
		request.setAttribute("adddb_notunitedlist", adddb_notunitedlist);
		
		getTotalList(client_code, total_list);
		request.setAttribute("total_list", total_list);
		
	}
	
	public void getTotalList(String client_code, ArrayList<IdcAddDBDTO> list)
	{
		Connection conn=null;
		PreparedStatement pstmt=null;
		ResultSet rs=null;
		try	
		{
			conn=dao.getConn();
			String sql="SELECT *, (curdate()) as a FROM adddb WHERE client_code=? AND adddb_oneTime!='o' AND ";
			sql+="adddb_accountStatus='incomplete' ";
			sql+=" ORDER BY service_code ASC, adddb_case DESC";
			pstmt =conn.prepareStatement(sql);
			pstmt.setString(1, client_code);
			rs = pstmt.executeQuery();
			while(rs.next())
			{
				IdcAddDBDTO dto = new IdcAddDBDTO();
				dto.setAddDB_num(rs.getInt("addDB_num"));
				dto.setAddDB_case(rs.getString("addDB_case"));
				dto.setAddDB_ip(rs.getString("addDB_ip"));
				dto.setAddDB_amount(rs.getString("addDB_amount"));
				dto.setAddDB_nonpayment(rs.getString("addDB_nonpayment"));
				dto.setAddDB_payment_date(rs.getDate("addDB_payment_date"));
				dto.setToday(rs.getDate("a"));
				dto.setAddDB_note(rs.getString("addDB_note"));
				dto.setService_code(rs.getString("service_code"));
				
				list.add(dto);
			}
		}
		catch (Exception e) 
		{
			System.out.println("IdcServiceAccountingCmd - getTotalList 에서의 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("IdcServiceAccountingCmd - getTotalList 에서의 sqlexception");
				e.printStackTrace();
			}
		}
	}
	
	public String client_money(String client_code){

		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		String client_money = "";
		
		try {
			conn = dao.getConn();
			String sql = "select client_money from clientDB where client_code=?";
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, client_code);
			rs = pstmt.executeQuery();
			
			if(rs.next()){
				client_money = rs.getString("client_money");
			}			
			
		} 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 client_money;
	}
	
	public ArrayList<IdcServiceDepositDTO> accountMonth(String client_code){
		ArrayList<IdcServiceDepositDTO> list = new ArrayList<IdcServiceDepositDTO>();
		
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		String sql;
		
		try {
			conn = dao.getConn();
			sql = "SELECT deposit_bank, deposit_name, deposit_amount, deposit_date, deposit_note From depositDB ";
			sql += "WHERE client_code=? ";
			
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, client_code);
			rs = pstmt.executeQuery();
			
			while(rs.next()){				
				IdcServiceDepositDTO writing = new IdcServiceDepositDTO();
				writing.setDeposit_bank(rs.getString("deposit_bank"));
				writing.setDeposit_name(rs.getString("deposit_name"));
				writing.setDeposit_amount(rs.getString("deposit_amount"));
				writing.setDeposit_date(rs.getDate("deposit_date"));
				writing.setDeposit_note(rs.getString("deposit_note"));
				list.add(writing);
			}

		} 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 list;
	}
	
	public ArrayList<IdcSingleServiceDTO> serviceFee(String client_code){
		ArrayList<IdcSingleServiceDTO> list = new ArrayList<IdcSingleServiceDTO>();
		
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		String sql;
		
		try {
			conn = dao.getConn();
			sql = "SELECT service_code, server_code, service_ip, service_amount, service_payment_date, service_note, service_nonpayment ";
			sql += "From serviceDB WHERE client_code=? and service_nonpayment >'0' ";
			
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, client_code);
			rs = pstmt.executeQuery();
			
			while(rs.next())
			{
				IdcSingleServiceDTO writing = new IdcSingleServiceDTO();
				
				writing.setService_code(rs.getString("service_code"));
				writing.setServer_code(rs.getString("server_code"));
				writing.setService_ip(rs.getString("service_ip"));
				writing.setService_amount(Integer.parseInt(rs.getString("service_amount")));
				writing.setService_payment_date(rs.getDate("service_payment_date"));
				writing.setService_note(rs.getString("service_note"));
				writing.setService_nonpayment(Integer.parseInt(rs.getString("service_nonpayment")));
				
				list.add(writing);
			}
						
		} 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 list;
	}
	
	public ArrayList<IdcOnetimeDTO> onetimeFee(String client_code){
		ArrayList<IdcOnetimeDTO> list = new ArrayList<IdcOnetimeDTO>();
		
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		
		try {
			conn = dao.getConn();
			String sql = "SELECT onetime_num, service_code, onetime_case, onetime_sub, onetime_amount, onetime_nonpayment, onetime_payment_date, onetime_note, curdate() as date, onetime_handle_date ";
			sql += "From onetimeDB WHERE client_code=? and onetime_nonpayment !='0'";
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, client_code);
			rs = pstmt.executeQuery();
			
			while(rs.next()){
				
				String onetime_num = rs.getString("onetime_num");
				String service_code = rs.getString("service_code");
				String onetime_case = rs.getString("onetime_case");
				String onetime_sub = rs.getString("onetime_sub");
				String onetime_amount = rs.getString("onetime_amount");
				String onetime_nonpayment = rs.getString("onetime_nonpayment");
				Date onetime_payment_date = rs.getDate("onetime_payment_date");
				String onetime_note = rs.getString("onetime_note");
				Date onetime_insert_date = rs.getDate("date");
				Date onetime_handle_date = rs.getDate("onetime_handle_date");
								
				IdcOnetimeDTO writing = new IdcOnetimeDTO();
				
				writing.setOnetime_num(onetime_num);
				writing.setService_code(service_code);
				writing.setOnetime_case(onetime_case);
				writing.setOnetime_sub(onetime_sub);
				writing.setOnetime_amount(onetime_amount);
				writing.setOnetime_nonpayment(onetime_nonpayment);
				writing.setOnetime_payment_date(onetime_payment_date);
				writing.setOnetime_note(onetime_note);
				writing.setOnetime_insert_date(onetime_insert_date);
				writing.setOnetime_handle_date(onetime_handle_date);
				
				list.add(writing);
								
			}		
			
		} 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 list;
	}
	
	private ArrayList<IdcAddDBDTO> getUnitedList(String client_code)
	{
		ArrayList<IdcAddDBDTO> list = new ArrayList<IdcAddDBDTO>();
		
		Connection conn=null;
		PreparedStatement pstmt=null;
		ResultSet rs=null;
		try	
		{
			conn=dao.getConn();
			String sql= "SELECT adddb_num,adddb_case, adddb_amount, adddb_nonpayment, adddb_united, adddb_ip, adddb_payment_date, adddb_note, service_code FROM adddb ";
			sql += "WHERE client_code=? and adddb_accountstatus='incomplete' and adddb_united = 'o' ";
			pstmt =conn.prepareStatement(sql);
			pstmt.setString(1, client_code);
			rs = pstmt.executeQuery();
			
			while(rs.next())
			{
				IdcAddDBDTO dto = new IdcAddDBDTO();
				dto.setService_code(rs.getString("service_code"));
				dto.setAddDB_note(rs.getString("addDB_note"));
				dto.setAddDB_num(rs.getInt("addDB_num"));
				dto.setAddDB_amount(rs.getString("addDB_amount"));
				dto.setAddDB_case(rs.getString("addDB_case"));
				dto.setAddDB_ip(rs.getString("addDB_ip"));
				dto.setAddDB_nonpayment(rs.getString("addDB_nonpayment"));
				dto.setAddDB_payment_date(rs.getDate("addDB_payment_date"));
				dto.setAddDB_united(rs.getString("addDB_united"));
				
				list.add(dto);
			}
		}
		catch (Exception e) 
		{
			System.out.println("IdcServiceAccountingCmd 에서의 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("IdcServiceAccountingCmd 에서의 sqlexception");
				e.printStackTrace();
			}
		}
		
		return list;
	}
	private ArrayList<IdcAddDBDTO> getNotUnitedList(String client_code)
	{
		ArrayList<IdcAddDBDTO> list = new ArrayList<IdcAddDBDTO>();

		Connection conn=null;
		PreparedStatement pstmt=null;
		ResultSet rs=null;
		try	
		{
			conn=dao.getConn();
			String sql= "SELECT adddb_num, adddb_case, adddb_amount, adddb_nonpayment, adddb_united, adddb_ip, adddb_payment_date, adddb_note FROM adddb ";
			sql += "WHERE client_code=? and adddb_accountStatus ='incomplete' and adddb_united = 'x' "; 
			pstmt =conn.prepareStatement(sql);
			pstmt.setString(1, client_code);
			
			rs = pstmt.executeQuery();
			while(rs.next())
			{
				IdcAddDBDTO dto = new IdcAddDBDTO();
				dto.setAddDB_note(rs.getString("addDB_note"));
				dto.setAddDB_num(rs.getInt("addDB_num"));
				dto.setAddDB_amount(rs.getString("addDB_amount"));
				dto.setAddDB_case(rs.getString("addDB_case"));
				dto.setAddDB_ip(rs.getString("addDB_ip"));
				dto.setAddDB_nonpayment(rs.getString("addDB_nonpayment"));
				dto.setAddDB_payment_date(rs.getDate("addDB_payment_date"));
				dto.setAddDB_united(rs.getString("addDB_united"));
				
				list.add(dto);
			}
		}
		catch (Exception e) 
		{
			System.out.println("IdcServiceAccountingCmd 에서의 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("IdcServiceAccountingCmd 에서의 sqlexception");
				e.printStackTrace();
			}
		}
		
		return list;
	}
}