package idc.line.command;

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

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

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

public class IdcLineModifyCmd implements IdcCmd 
{
	IdcDAO dao;
	
	public IdcLineModifyCmd() 
	{
		dao = new IdcDAO();
	}
	
	
	//라인 수정 
	@Override
	public void execute(HttpServletRequest request, HttpServletResponse response) 
	{
		//checkToken 값이 다르면 true반환.
		if(dao.checkToken(request))
		{
			request.setAttribute("reError", dao.reError);
			return;
		}
		else
		{
			//토큰이 같아서 등록성공이 될 경우 토큰을 삭제해 준다.
			request.getSession().removeAttribute("token");
		}
		
		
		IdcLineDBDTO dto = new IdcLineDBDTO();
		dtoSet(dto,request);
		idcLineModify(dto);
		request.setAttribute("curPage", "1");
	}
	private void idcLineModify(IdcLineDBDTO dto)
	{
		Connection conn=null;
		PreparedStatement pstmt=null;
		ResultSet rs=null;
		try	
		{
			conn=dao.getConn();
			String sql="UPDATE linedb SET line_code=?, line_case=?, line_IP=?, line_able=?, line_block=?, line_block_note=?, ";
			sql+="line_block_endday=?, line_user_allocatingday=?, line_user_endday=?, line_client_name=?, line_note=?, line_mainip=?, ";
			sql+="line_subip1=?, line_subip2=?, line_subip3=?, line_before_user=?, ";
			sql+="line_before_allocatingday=?, line_before_endday=?, line_reason=?, ";
			sql+="line_id=?, line_pass=?, line_name=?, line_phone=?, line_subnet=?, line_gateway=?, line_haveip=? ";
			sql+="WHERE line_num=?";
			pstmt =conn.prepareStatement(sql);
			
			pstmt.setString(1, dto.getLine_code());
			pstmt.setString(2, dto.getLine_case());
			pstmt.setString(3, dto.getLine_IP());
			pstmt.setString(4, dto.getLine_able());
			pstmt.setString(5, dto.getLine_block());
			pstmt.setString(6, dto.getLine_block_note());
			pstmt.setDate(7, dto.getLine_block_endday());
			pstmt.setDate(8,dto.getLine_user_allocatingday());
			pstmt.setDate(9, dto.getLine_user_endday());
			pstmt.setString(10, dto.getLine_client_name());
			pstmt.setString(11, dto.getLine_note());
			pstmt.setString(12, dto.getLine_mainip());
			pstmt.setString(13, dto.getLine_subip1());
			pstmt.setString(14, dto.getLine_subip2());
			pstmt.setString(15, dto.getLine_subip3());
			pstmt.setString(16, dto.getLine_before_user());
			pstmt.setDate(17,dto.getLine_before_allocatingday());
			pstmt.setDate(18, dto.getLine_before_endday());
			pstmt.setString(19,dto.getLine_reason());
			
			pstmt.setString(20, dto.getLine_id());
			pstmt.setString(21,  dto.getLine_pass());
			pstmt.setString(22, dto.getLine_name());
			pstmt.setString(23, dto.getLine_phone());
			pstmt.setString(24, dto.getLine_subnet());
			pstmt.setString(25, dto.getLine_gateway());
			pstmt.setString(26, dto.getLine_haveip());
			
			pstmt.setInt(27, dto.getLine_num());
			pstmt.executeUpdate();
			
			setAble(dto.getLine_able(), dto.getLine_num());
		}
		catch (Exception e) 
		{
			System.out.println("IdcLineModifyCmd 에서의 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("IdcLineModifyCmd 에서의 sqlexception");
				e.printStackTrace();
			}
		}
	}
	
	
	private void dtoSet(IdcLineDBDTO dto, HttpServletRequest r)
	{
		dto.setLine_num(Integer.parseInt(r.getParameter("line_num")));
		dto.setLine_code(r.getParameter("line_code"));
		dto.setLine_case(r.getParameter("line_case"));
		dto.setLine_IP(r.getParameter("line_ip"));
		dto.setLine_able(r.getParameter("line_able"));
		dto.setLine_block(r.getParameter("line_block"));
		dto.setLine_block_note(r.getParameter("line_block_note"));
		if(r.getParameter("line_block_endday")!="" && r.getParameter("line_block_endday")!=null)	
			dto.setLine_block_endday(Date.valueOf(r.getParameter("line_block_endday")));
		if(r.getParameter("line_user_allocatingday")!="" && r.getParameter("line_user_allocatingday")!=null)
			dto.setLine_user_allocatingday(r.getParameter("line_user_allocatingday"));
		if(r.getParameter("line_user_endday")!="" && r.getParameter("line_user_endday")!=null)
			dto.setLine_user_endday(r.getParameter("line_user_endday"));
		dto.setLine_client_name(r.getParameter("line_client_name"));
		dto.setLine_note(r.getParameter("line_note"));
		dto.setLine_mainip(r.getParameter("line_mainip"));
		dto.setLine_subip1(r.getParameter("line_subip1"));
		dto.setLine_subip2(r.getParameter("line_subip2"));
		dto.setLine_subip3(r.getParameter("line_subip3"));
		if(r.getParameter("line_user_allocatingday")!="" && r.getParameter("line_user_allocatingday")!=null)
			dto.setLine_user_allocatingday(r.getParameter("line_user_allocatingday"));
		if(r.getParameter("line_user_endday")!="" && r.getParameter("line_user_endday")!=null)	
			dto.setLine_user_endday(r.getParameter("line_user_endday"));
		dto.setLine_before_user(r.getParameter("line_before_user"));
		if(r.getParameter("line_before_allocatingday")!="" && r.getParameter("line_before_allocatingday")!=null)
			dto.setLine_before_allocatingday(r.getParameter("line_before_allocatingday"));
		if(r.getParameter("line_before_endday")!="" && r.getParameter("line_before_endday")!=null)
			dto.setLine_before_endday(r.getParameter("line_before_endday"));
		dto.setLine_reason(r.getParameter("line_reason"));
		
		dto.setLine_name(r.getParameter("line_name"));
		dto.setLine_subnet(r.getParameter("line_subnet"));
		dto.setLine_gateway(r.getParameter("line_gateway"));
		dto.setLine_id(r.getParameter("line_id"));
		dto.setLine_pass(r.getParameter("line_pass"));
		dto.setLine_phone(r.getParameter("line_phone"));
		dto.setLine_haveip(r.getParameter("line_haveip"));
		
		
	}
	
	private void setAble(String able, int line_num)
	{
		Connection conn=null;
		PreparedStatement pstmt=null;
		ResultSet rs=null;
		try	
		{
			conn=dao.getConn();
			String sql="update ipdb SET ip_able=? WHERE line_num = ?";
			pstmt =conn.prepareStatement(sql);
			pstmt.setString(1, able);
			pstmt.setInt(2, line_num);
			pstmt.executeUpdate();
			
		}
		catch (Exception e) 
		{
			System.out.println("IdcLineModifyCmd 에서의 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("IdcLineModifyCmd 에서의 sqlexception");
				e.printStackTrace();
			}
		}
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	

}
