package idc.main.command;

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

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 IdcMemberPowerModifyCmd implements IdcCmd
{
	IdcDAO dao;
	
	public IdcMemberPowerModifyCmd()
	{
		dao = new IdcDAO();
	}
	
	
	//멤버 권한설정
	@Override
	public void execute(HttpServletRequest request, HttpServletResponse response)
	{
		int power = Integer.parseInt(request.getParameter("power"));
		String[] power2 = request.getParameterValues("power2");
		String id = request.getParameter("id");
		System.out.println(id);
		idcMemberPowerModify(power, id, power2);
	}
	
	private void idcMemberPowerModify(int power, String id, String[] p)
	{
		Connection conn=null;
		PreparedStatement pstmt=null;
		ResultSet rs=null;
		try	
		{
			conn=dao.getConn();
			String sql="UPDATE memberdb SET power=? , power1=?, power2=?, power3=?, power4=?, power5=?";
			sql+=", power6=?, power7=?, power8=?, power9=?, power10=? ";
			sql+="WHERE id=?";
			pstmt =conn.prepareStatement(sql);
			pstmt.setInt(1, power);
			for(int i=0;i<p.length;i++)
				pstmt.setString(i+2, p[i]);
			pstmt.setString(12, id);
			
			pstmt.executeUpdate();
		}
		catch (Exception e) 
		{
			System.out.println("idcMemberPowerModify 에서의 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("idcMemberPowerModify 에서의 sqlexception");
				e.printStackTrace();
			}
		}
	}
}
