package idc.main.command;

import idc.model.IdcCmd;
import idc.model.IdcDAO;
import idc.model.IdcMemberDBDTO;

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;

public class IdcMemberQuickModifyCmd implements IdcCmd
{
	private IdcDAO dao;
	
	public IdcMemberQuickModifyCmd()
	{
		dao = new IdcDAO();
	}
	
	//퀵메뉴 수정
	@Override
	public void execute(HttpServletRequest request, HttpServletResponse response)
	{
		String quick = request.getParameter("quick");
		String num = request.getParameter("num");
		IdcMemberDBDTO member = (IdcMemberDBDTO)request.getSession().getAttribute("member");
		setQuick(quick, num, member.getId(), member);
		request.getSession().setAttribute("member", member);
	}
	
	private void setQuick(String quick, String num, String id, IdcMemberDBDTO dto)
	{
		Connection conn=null;
		PreparedStatement pstmt=null, pstmt2=null;
		ResultSet rs=null;
		try	
		{
			conn=dao.getConn();
			String sql="UPDATE memberdb SET quick"+num+"=? WHERE id=?";
			pstmt =conn.prepareStatement(sql);
			if(quick!=null)
				pstmt.setString(1, quick);
			else
				pstmt.setString(1, null);
			pstmt.setString(2, id);
			pstmt.executeUpdate();
			
			sql="SELECT * FROM memberdb WHERE id=?";
			pstmt2=conn.prepareStatement(sql);
			pstmt2.setString(1, id);
			rs = pstmt2.executeQuery();
			
			while(rs.next())
			{
				dto.setQuick1(rs.getString("quick1"));
				dto.setQuick2(rs.getString("quick2"));
				dto.setQuick3(rs.getString("quick3"));
				dto.setQuick4(rs.getString("quick4"));
				dto.setQuick5(rs.getString("quick5"));
				dto.setQuick6(rs.getString("quick6"));
				dto.setQuick7(rs.getString("quick7"));
				dto.setQuick8(rs.getString("quick8"));
				dto.setQuick9(rs.getString("quick9"));
				dto.setQuick10(rs.getString("quick10"));
				dto.setQuick11(rs.getString("quick11"));
				dto.setQuick12(rs.getString("quick12"));
				dto.setQuick13(rs.getString("quick13"));
				dto.setQuick14(rs.getString("quick14"));
				dto.setQuick15(rs.getString("quick15"));
				dto.setQuick16(rs.getString("quick16"));
				dto.setQuick17(rs.getString("quick17"));
				dto.setQuick18(rs.getString("quick18"));
				dto.setQuick19(rs.getString("quick19"));
				dto.setQuick20(rs.getString("quick20"));
				dto.setQuick21(rs.getString("quick21"));
				dto.setQuick22(rs.getString("quick22"));
				dto.setQuick23(rs.getString("quick23"));
				dto.setQuick24(rs.getString("quick24"));
				dto.setQuick25(rs.getString("quick25"));
				
			}
		}
		catch (Exception e) 
		{
			System.out.println("IdcMemberQuickModifyCmd 에서의 exception");
			e.printStackTrace();
		}
		finally
		{
			try 
			{
				if(conn!=null) conn.close();
				if(rs!=null)rs.close();
				if(pstmt!=null)pstmt.close();
			}
			catch (SQLException e) 
			{
				System.out.println("IdcMemberQuickModifyCmd 에서의 sqlexception");
				e.printStackTrace();
			}
		}
	}
}
