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 IdcGearArrayCmd implements IdcCmd
{
	private IdcDAO dao;
	public IdcGearArrayCmd()
	{
		dao = new IdcDAO();
	}
	
	//재원 장비용 정렬기능
	@Override
	public void execute(HttpServletRequest request, HttpServletResponse response)
	{
		String arr = request.getParameter("arr");
		int old = Integer.parseInt(request.getParameter("old"));
		int num = Integer.parseInt(request.getParameter("num"));
		
		arrayDB(old, num);
	}
	private void arrayDB(int old, int num)
	{
		int temp = old*-1;
		Connection conn=null;
		PreparedStatement pstmt=null;
		ResultSet rs=null;
		try	
		{
			conn=dao.getConn();
			String sql="UPDATE gearlist SET firarr=? WHERE firarr=?";
			pstmt =conn.prepareStatement(sql);
			
//			기존값 임시 저장숫자로 바꿔버림.
			pstmt.setInt(1, temp);
			pstmt.setInt(2, old);
			pstmt.executeUpdate();
			
//			바뀔값을 이전값으로 바꾼다.
			pstmt.setInt(1, old);
			pstmt.setInt(2, num);
			pstmt.executeUpdate();
			
//			임시 주소로 보낸놈을 바꿔준다.
			pstmt.setInt(1, num);
			pstmt.setInt(2, temp);
			pstmt.executeUpdate();
		}
		catch (Exception e) 
		{
			System.out.println("IdcGearArrayCmd - arrayDB 에서의 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("IdcGearArrayCmd -arrayDB 에서의 sqlexception");
				e.printStackTrace();
			}
		}
		
	}
}
