package idc.json.command;

import idc.model.IDCserviceListDTO;
import idc.model.IdcCmd;
import idc.model.IdcDAO;
import idc.model.IdcDeleteSubDBDTO;

import java.io.IOException;
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 net.sf.json.JSONObject;

public class IdcGetDeleteServiceDetailJSONCmd implements IdcCmd
{
	private IdcDAO dao;
	public IdcGetDeleteServiceDetailJSONCmd()
	{
		dao = new IdcDAO();
	}
	
	//부가서비스 삭제.
	@Override
	public void execute(HttpServletRequest request, HttpServletResponse response)
	{
		String sc = request.getParameter("serviceCode");
		IDCserviceListDTO dto = new IDCserviceListDTO();
		ArrayList<IdcDeleteSubDBDTO> kcs= new ArrayList<IdcDeleteSubDBDTO>(),
				vpc = new ArrayList<IdcDeleteSubDBDTO>(), ip= new ArrayList<IdcDeleteSubDBDTO>(),
				cs = new ArrayList<IdcDeleteSubDBDTO>();
		
		getDetailData(dto, sc);
		getsub(ip, sc, dto.getSservice_cancel_date(), 1);
		getsub(cs, sc, dto.getSservice_cancel_date(), 2);
		getsub(kcs, sc, dto.getSservice_cancel_date(), 3);
		getsub(vpc, sc, dto.getSservice_cancel_date(), 4);
		
		response.setCharacterEncoding("utf-8");
		response.setContentType("application/json");
		JSONObject obj = new JSONObject();
		
		obj.put("a", dto);
		obj.put("ip", ip);
		obj.put("cs", cs);
		obj.put("kcs", kcs);
		obj.put("vpc", vpc);
		
		try
		{
			System.out.println(obj.toString());
			response.getWriter().write(obj.toString());
		}
		catch (IOException e)
		{
			System.out.println("IdcGetDeleteServiceDetailJSONCmd - send오류");
			e.printStackTrace();
		}
	}
	private void getsub(ArrayList<IdcDeleteSubDBDTO> list, String sc, String cancel, int num)
	{
		String scase="";
		switch(num)
		{
		case 1:
			scase="ip";
			break;
		case 2:
			scase="cs";
			break;
		case 3:
			scase="kcs-cs";
			break;
		case 4:
			scase="vpc-cs";
			break;
		}
		
		Connection conn=null;
		PreparedStatement pstmt=null;
		ResultSet rs=null;
		try	
		{
			conn=dao.getConn();
			String sql="SELECT * FROM deletesub WHERE service_code='"+sc+"' AND ";
			sql+="cancel_date=? AND adddb_case='"+scase+"' ";
			pstmt =conn.prepareStatement(sql);
			pstmt.setDate(1, Date.valueOf(cancel));
			rs=pstmt.executeQuery();
			while(rs.next())
			{
				IdcDeleteSubDBDTO dto = new IdcDeleteSubDBDTO();
				
				dto.setAdddb_code(rs.getString("adddb_code"));
				dto.setIp(rs.getString("ip"));
				list.add(dto);
			}
		}
		catch (Exception e) 
		{
			System.out.println("IdcGetDeleteServiceDetailJSONCmd - getkcs 에서의 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("IdcGetDeleteServiceDetailJSONCmd - getkcs 에서의 sqlexception");
				e.printStackTrace();
			}
		}
	}
	private void getDetailData(IDCserviceListDTO dto, String sc)
	{
		Connection conn=null;
		PreparedStatement pstmt=null;
		ResultSet rs=null;
		try	
		{
			conn=dao.getConn();
			String sql="SELECT * FROM deleteservicedb WHERE service_code='"+sc+"'";
			pstmt =conn.prepareStatement(sql);
			rs=pstmt.executeQuery();
			if(rs.next())
			{
				dto.setService_status(rs.getString("service_status"));
				dto.setService_code(rs.getString("service_code"));
				dto.setClient_code(rs.getString("client_code"));
				dto.setService_line(rs.getString("service_line"));
				dto.setServer_code(rs.getString("server_code"));
				dto.setService_sw(rs.getString("service_sw"));
				dto.setService_os(rs.getString("service_os"));
				dto.setService_ip(rs.getString("service_ip"));
				dto.setSservice_request_date(rs.getDate("service_request_date").toString());
				dto.setSservice_open_date(rs.getDate("service_open_date").toString());
				dto.setSservice_payment_date(rs.getDate("service_payment_date").toString());
				dto.setSservice_cancel_date(rs.getDate("service_cancel_date").toString());
				dto.setService_amount(rs.getString("service_amount"));
				dto.setService_nonpayment(rs.getString("service_nonpayment"));
				dto.setService_note(rs.getString("service_note"));
				dto.setService_manager(rs.getString("service_manager"));
				dto.setService_case(rs.getString("service_case"));
				dto.setService_defence(rs.getString("service_defence"));
				dto.setService_admin(rs.getString("service_admin"));
				dto.setServer_spec(rs.getString("server_spec"));
			}
		}
		catch (Exception e) 
		{
			System.out.println("IdcGetDeleteServiceDetailJSONCmd - getDetailData 에서의 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("IdcGetDeleteServiceDetailJSONCmd - getDetailData 에서의 sqlexception");
				e.printStackTrace();
			}
		}
	}
}
