package idc.business.command;

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

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import idc.model.*;

public class IdcBusinessHandoverWriteHandleCmd implements IdcCmd{

	IdcDAO dao = new IdcDAO();
	
	public void execute(HttpServletRequest request, HttpServletResponse response){
		
		//checkToken 값이 다르면 true반환.
		if(dao.checkToken(request)){
			request.setAttribute("reError", dao.reError);
			return;
		}else{
			//토큰이 같아서 등록성공이 될 경우 토큰을 삭제해 준다.
			request.getSession().removeAttribute("token");
		}
		
		IdcMemberDBDTO dto1 = (IdcMemberDBDTO)request.getSession().getAttribute("member");
		String consult_manager = dto1.getId();
		
		String consult_name = request.getParameter("consult_name");
		String consult_contact = request.getParameter("consult_contact");
		String consult_question = request.getParameter("consult_question");
		String consult_answer = request.getParameter("consult_answer");
		String consult_add = request.getParameter("consult_add");
		
		//DB입력
		insertConsult( consult_manager,  consult_name,  consult_contact,  consult_question, consult_answer, consult_add);
	}
	
	public void insertConsult(String consult_manager, String consult_name, String consult_contact, String consult_question,
			 String consult_answer, String consult_add){
		
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		
		try {
			conn = dao.getConn();
			String sql = "INSERT INTO consultDB (consult_manager, consult_name, consult_contact, consult_question, consult_answer, consult_add, consult_date) ";
			sql += " values(?, ?, ?, ?, ?, ?, curdate())"; 
			pstmt = conn.prepareStatement(sql);						
			
			pstmt.setString(1, consult_manager);						
			pstmt.setString(2, consult_name);
			pstmt.setString(3, consult_contact);
			pstmt.setString(4, consult_question);
			pstmt.setString(5, consult_answer);
			pstmt.setString(6, consult_add);						
			pstmt.executeUpdate();
			
		} 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();
			}
		}
	}

}
