package idc.business.command;

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

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

import idc.model.*;

public class IdcBusinessHandoverUpdateCmd 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");
		}
				
		String name = request.getParameter("name");
		String name1 = request.getParameter("name1");
		String name2 = request.getParameter("name2");
		String name3 = request.getParameter("name3");
		String name4 = request.getParameter("name4");
		String name5 = request.getParameter("name5");
		String name6 = request.getParameter("name6");
		String name7 = request.getParameter("name7");
		String name8 = request.getParameter("name8");
		String part = request.getParameter("part");
		
		//DB업데이트
		updateHandover(name, name1, name2, name3, name4, name5, name6, name7, name8, part);
	}
	
	public void updateHandover(String name, String name1, String name2, String name3, String name4, 
			String name5, String name6, String name7, String name8, String part){
		
		Connection conn = null;
		PreparedStatement pstmt = null, pstmt2 = null;
		
		try {
			conn = dao.getConn();
			String sql = "UPDATE handoverDB SET name1=?, name2=?, name3=?, name4=?, name5=?, name6=?, name7=?, name8=? WHERE name=?";
			pstmt = conn.prepareStatement(sql);
			
			pstmt.setString(1, name1);
			pstmt.setString(2, name2);
			pstmt.setString(3, name3);
			pstmt.setString(4, name4);
			pstmt.setString(5, name5);
			pstmt.setString(6, name6);
			pstmt.setString(7, name7);
			pstmt.setString(8, name8);
			pstmt.setString(9, name);
			pstmt.executeUpdate();
						
			sql = "UPDATE memberDB SET part=? WHERE name=?";
			pstmt2 = conn.prepareStatement(sql);
			pstmt2.setString(1, part);
			pstmt2.setString(2, name);			
			pstmt2.executeUpdate();
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally{
			try {
				if(pstmt != null) pstmt.close();
				if(pstmt2 != null) pstmt2.close();
				if(conn != null) conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

}
