package idc.deposit.command;

import idc.model.IdcBillPaperDTO;
import idc.model.IdcClientDBDTO;
import idc.model.IdcCmd;
import idc.model.IdcDAO;
import idc.model.IdcDepositNonPaymentDTO;
import idc.model.IdcNonpaymentDTO;

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 IdcDepositBillPaperCmd implements IdcCmd 
{
	private IdcDAO dao;
	private String path = this.getClass().getResource("").getPath()+"../../../../../ETCFolder/BillPaper.txt";
	public IdcDepositBillPaperCmd() 
	{
		path = path.replaceAll("%20", " ");
		dao = new IdcDAO();
	}
	//미납리스트 메일발송 폼
	@Override
	public void execute(HttpServletRequest request, HttpServletResponse response) 
	{
		ArrayList<IdcNonpaymentDTO> temp=null;
		ArrayList<IdcNonpaymentDTO> nonDto = new ArrayList<IdcNonpaymentDTO>();
		ArrayList<IdcBillPaperDTO> list = new ArrayList<IdcBillPaperDTO>();
		String[] listNum = null, listNum2=null;
		
		int[][] targetNum2 = null;
		int totalAmount = 0, listnumLen=0, listnum2Len=0;
		String strOut="",sc;
		
		temp = (ArrayList<IdcNonpaymentDTO>)request.getSession().getAttribute("IdcNonPayMentList");
		request.getSession().removeAttribute("IdcNonPayMentList");
		
		listNum = request.getParameterValues("Bill");
		listNum2=request.getParameterValues("nuBill");
		if(listNum!=null)
		{
			listnumLen = listNum.length;
		}
		if(listNum2!=null)
		{
			listnum2Len = listNum2.length;
			targetNum2 = new int[listnum2Len][2]; //분할과금 체크 결과
			for(int i=0;i<listnum2Len;i++)
			{
				String[] a = listNum2[i].split("-");
				targetNum2[i][0]=Integer.parseInt(a[0]);
				targetNum2[i][1]=Integer.parseInt(a[1]);
			}
		}
		
		//데이터 필요한것만 재설정.
		int ind=0;
		for(int i=0;i<listnumLen;i++)
		{
			IdcNonpaymentDTO dto = new IdcNonpaymentDTO();
			if(listNum.length>i)
			{
				dto.setDto(temp.get(Integer.parseInt(listNum[i])).getDto());
				//totalAmount +=dto.getDto().getService_amount();
			}
			ArrayList<IdcDepositNonPaymentDTO> nulist = new ArrayList<IdcDepositNonPaymentDTO>();
			while(ind<listnum2Len && i==targetNum2[ind][0])
			{
				IdcDepositNonPaymentDTO nudto = new IdcDepositNonPaymentDTO();
				nudto = temp.get(Integer.parseInt(listNum[i])).getNulist().get(targetNum2[ind++][1]);
				nulist.add(nudto);
				totalAmount+=nudto.getService_amount();
			}
			dto.setNulist(nulist);
			nonDto.add(dto);
		}
		sc = request.getParameter("searchContent");
		IdcClientDBDTO clientdto = null;
		list = setBill(nonDto);
		clientdto=getClient(sc);
		request.setAttribute("reseller", clientdto.getClient_reseller());
		request.setAttribute("ClientDto", clientdto);
		request.setAttribute("IdcBillPaperList", list);

		System.out.println("TotalAmount : "+ totalAmount);
		//request.setAttribute("TotalAmount",String.format("%,d",totalAmount));		
		request.setAttribute("TotalAmount",totalAmount);
		request.setAttribute("StrOut", strOut);
	}
	
	//해당 서비스에 대한 자료 가져오기.
	private ArrayList<IdcBillPaperDTO> setBill(ArrayList<IdcNonpaymentDTO> nonDto)
	{
		ArrayList<IdcBillPaperDTO> list = new ArrayList<IdcBillPaperDTO>();
		
		for(int i=0;i<nonDto.size();i++)
		{
			IdcNonpaymentDTO dto = nonDto.get(i);
			if(nonDto.get(i)!=null)
			{
				IdcBillPaperDTO bdto = new IdcBillPaperDTO();
				bdto.setService_code(dto.getDto().getService_code());
				bdto.setService_case(dto.getDto().getService_case());
				bdto.setService_mainip(dto.getDto().getService_ip());
				bdto.setService_amount(dto.getDto().getService_amount());
				bdto.setPayment_date(dto.getDto().getService_payment_date());
				list.add(bdto);
			}
			for(int j=0;j<dto.getNulist().size();j++)
			{
				IdcDepositNonPaymentDTO ndto = dto.getNulist().get(j);
				IdcBillPaperDTO bdto = new IdcBillPaperDTO();
				bdto.setService_code(ndto.getService_code());
				bdto.setService_case(ndto.getService_case());
				bdto.setService_mainip(ndto.getService_ip());
				bdto.setService_amount(ndto.getService_amount());
				bdto.setPayment_date(ndto.getService_payment_date());
				list.add(bdto);
			}
		}
		
		return list;
	}
	//고객명으로 해당 고객 정보 가져오기
	private IdcClientDBDTO getClient(String client_name)
	{
		Connection conn=null;
		PreparedStatement pstmt=null;
		ResultSet rs=null;
		
		IdcClientDBDTO dto = new IdcClientDBDTO();
		try	
		{
			conn=dao.getConn();
			String sql="SELECT client_code, client_reseller, client_name, client_email1, (curdate()) as onday ";
			sql+="FROM clientdb ";
			sql+="WHERE client_name=?";
			pstmt =conn.prepareStatement(sql);
			pstmt.setString(1, client_name);
			rs=pstmt.executeQuery();
			
			if(rs.next())
			{
				dto.setClient_reseller(rs.getString("client_reseller"));
				dto.setClient_code(rs.getString("client_code"));
				dto.setClient_name(rs.getString("client_name"));
				dto.setClient_email1(rs.getString("client_email1"));
				dto.setClient_receive_date(rs.getDate("onday"));
			}
		}
		catch (Exception e) 
		{
			System.out.println("idcDepositBillPaper 에서의 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("idcDepositBillPaper 에서의 sqlexception");
				e.printStackTrace();
			}
		}
		
		return dto;
	}
}




























