package idc.model;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class IdcSetHistory 
{
	//히스토리 남기기
	public static void setHistory(String service_code, String server_code, String addDB_case, String addDB_ip, String addDB_code,String Note, String client_name)
	{
		IdcDAO dao = new IdcDAO();
		
		IdcHistoryDBDTO dto = new IdcHistoryDBDTO();
		dto.setService_code(service_code);
		dto.setServer_code(server_code);
		dto.setBehavior(addDB_case);
		dto.setBehavior_case(addDB_code + "<br>" + addDB_ip);
		dto.setNote(Note);
		dto.setClient_name(client_name);
		
		Connection conn=null;
		PreparedStatement pstmt=null,pstmt2=null;
		ResultSet rs=null,rs2=null;
		int pstmtIndex=1;
		
		try{
			conn=dao.getConn();
			String sql = "SELECT client_name FROM clientdb WHERE client_code=?";
			pstmt2=conn.prepareStatement(sql);
			pstmt2.setString(1, dto.getClient_name());
			rs2 = pstmt2.executeQuery();
			if(rs2.next())
				dto.setClient_name(rs2.getString("client_name"));
			
			sql="INSERT INTO historydb (service_code, server_code, behavior_case, behavior, behavior_date, note, client_name) ";
			sql+="values(?,?,?,?,curdate(),?,?)";
			pstmt =conn.prepareStatement(sql);
			pstmt.setString(pstmtIndex++, dto.getService_code());
			pstmt.setString(pstmtIndex++, dto.getServer_code());
			pstmt.setString(pstmtIndex++, dto.getBehavior_case());
			pstmt.setString(pstmtIndex++, dto.getBehavior());
			pstmt.setString(pstmtIndex++, dto.getNote());
			pstmt.setString(pstmtIndex++, dto.getClient_name());
			
			pstmt.executeUpdate();
		}
		catch (Exception e) 
		{
			System.out.println("setHistory 에서의 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("setHistory 에서의 sqlexception");
				e.printStackTrace();
			}
		}
		
	}
}
