package idc.json.command;

import idc.model.IdcCmd;
import idc.model.IdcDAO;
import idc.model.IdcHistoryDBDTO;

import java.io.IOException;
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;

import utility.IdcGetClientNameCode;
import net.sf.json.JSONException;
import net.sf.json.JSONObject;

public class IdcHistoryListJSONCmd implements IdcCmd
{
	private IdcDAO dao;
	
	public IdcHistoryListJSONCmd()
	{
		dao = new IdcDAO();
	}
	
	//히스토리 내역 가져오기.
	@Override
	public void execute(HttpServletRequest request, HttpServletResponse response)
	{
		String client_name = request.getParameter("client_name");
		ArrayList<IdcHistoryDBDTO> list = new ArrayList<IdcHistoryDBDTO>();
		getList(list,client_name);
		
		response.setContentType("application/json");
		response.setCharacterEncoding("utf-8");

		JSONObject obj = new JSONObject();
		obj.put("a", list);
		
//		System.out.println(obj.toString());
		
		try
		{
			response.getWriter().write(obj.toString());
		}
		catch (IOException e)
		{
			System.out.println("IdcHistoryListJSONCmd - send오류 IOException");
			e.printStackTrace();
		}
		catch (JSONException e)
		{
			System.out.println("IdcHistoryListJSONCmd - send오류JSONException");
			e.printStackTrace();
		}
	}
	
	private void getList(ArrayList<IdcHistoryDBDTO> list, String client_name)
	{
		Connection conn=null;
		PreparedStatement pstmt=null, pstmt2=null;
		ResultSet rs=null, rs2=null;
		
		

		try	
		{
			conn=dao.getConn();
			
			/*
			 * 손꺼
			 * 
			 * 
			String sql="SELECT service_code FROM servicedb WHERE client_code=?";
			pstmt2=conn.prepareStatement(sql);
			pstmt2.setString(1, IdcGetClientNameCode.getClientCode(client_name));
			rs2 = pstmt2.executeQuery();
			sql="SELECT * FROM historydb WHERE service_code=? ORDER BY history_num DESC";
			pstmt = conn.prepareStatement(sql);
			
			while(rs2.next())
			{
				pstmt.setString(1, rs2.getString("service_code"));
				rs = pstmt.executeQuery();
				while(rs.next())
				{
					IdcHistoryDBDTO dto = new IdcHistoryDBDTO();
					dto.setBehavior(rs.getString("behavior"));
					dto.setBehavior_case(rs.getString("behavior_case"));
					dto.setClient_name(rs.getString("client_name"));
					dto.setNote(rs.getString("note"));
					dto.setServer_code(rs.getString("server_code"));
					dto.setService_code(rs.getString("service_code"));
					dto.setBehavior_date(rs.getDate("behavior_date"));
					dto.setStrbehavior_date(dto.getBehavior_date().toString());
					dto.setBehavior_date(null);
					
					list.add(dto);
				}
			}
			*/
			
			String sql="SELECT * FROM historydb WHERE client_name=? ORDER BY history_num DESC";
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, client_name);
			rs = pstmt.executeQuery();
			
			while(rs.next()){			
				IdcHistoryDBDTO dto = new IdcHistoryDBDTO();
				dto.setBehavior(rs.getString("behavior"));
				dto.setBehavior_case(rs.getString("behavior_case"));
				dto.setClient_name(rs.getString("client_name"));
				dto.setNote(rs.getString("note"));
				dto.setServer_code(rs.getString("server_code"));
				dto.setService_code(rs.getString("service_code"));
				dto.setBehavior_date(rs.getDate("behavior_date"));
				dto.setStrbehavior_date(dto.getBehavior_date().toString());
				dto.setBehavior_date(null);
				
				list.add(dto);				
			}
			
			
		}
		catch (Exception e) 
		{
			System.out.println("IdcHistoryListJSONCmd - getList 에서의 exception");
			e.printStackTrace();
		}
		finally
		{
			try 
			{
				if(conn!=null) conn.close();
				if(pstmt!=null)pstmt.close();
				if(rs!=null)rs.close();
				if(pstmt2!=null)pstmt2.close();
				if(rs2!=null)rs2.close();
			}
			catch (SQLException e) 
			{
				System.out.println("IdcHistoryListJSONCmd - getList 에서의 sqlexception");
				e.printStackTrace();
			}
		}
	}
}
