package idc.business.command;

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

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 IdcBusinessModifyCmd implements IdcCmd{
	private IdcDAO dao;
	public IdcBusinessModifyCmd(){
		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");
		}
		
		int num = Integer.parseInt(request.getParameter("modiNum"));

		String[] d = request.getParameterValues("data");
		setUpdate(num, d[5]);
	}
	
	private void setUpdate(int num, String status){
		
		IdcDeliveryDTO dto = new IdcDeliveryDTO();		
		Connection conn=null;
		PreparedStatement pstmt=null, pstmt2=null, pstmt3=null;
		ResultSet rs=null;
		
		try{
			conn=dao.getConn();
			String sql="SELECT * FROM deliverydb WHERE delivery_num=?";
			pstmt =conn.prepareStatement(sql);
			pstmt.setInt(1, num);
			rs = pstmt.executeQuery();
			if(rs.next()){
				dto.setClient_name(rs.getString("client_name"));
				dto.setDelivery_check_date(rs.getDate("delivery_check_date"));
				dto.setDelivery_content(rs.getString("delivery_content"));
				dto.setDelivery_end_date(rs.getDate("delivery_end_date"));
				dto.setDelivery_manager(rs.getString("delivery_manager"));
				dto.setDelivery_sort(rs.getString("delivery_sort"));
				dto.setDelivery_status(status);
				dto.setDelivery_temp(rs.getString("delivery_note"));
			}
			
			sql = "DELETE FROM deliverydb WHERE delivery_num=?";
			pstmt2 = conn.prepareStatement(sql);
			pstmt2.setInt(1, num);
			pstmt2.executeUpdate();
			
			sql = "INSERT INTO deliverydb (delivery_sort, client_name, delivery_content, delivery_check_date, ";
			sql+="delivery_end_date, delivery_status, delivery_manager, delivery_note, delivery_finish_date) ";
			sql+="values(?,?,?,?,?, ?,?,?, curdate())";
			
			pstmt3=conn.prepareStatement(sql);
			pstmt3.setString(1, dto.getDelivery_sort());
			pstmt3.setString(2, dto.getClient_name());
			pstmt3.setString(3, dto.getDelivery_content());
			pstmt3.setDate(4, dto.getDelivery_check_date());
			pstmt3.setDate(5, dto.getDelivery_end_date());
			pstmt3.setString(6, dto.getDelivery_status());
			pstmt3.setString(7, dto.getDelivery_manager());
			pstmt3.setString(8, dto.getDelivery_temp());
			
			pstmt3.executeUpdate();
		}catch (Exception e){
			System.out.println("IdcBusinessModifyCmd - setUpdate 에서의 exception");
			e.printStackTrace();
		}finally{
			try{
				if(conn!=null) conn.close();
				if(rs!=null)rs.close();
				if(pstmt!=null)pstmt.close();
				if(pstmt2!=null)pstmt2.close();
				if(pstmt3!=null)pstmt3.close();
			}catch (SQLException e){
				System.out.println("IdcBusinessModifyCmd - setUpdate 에서의 sqlexception");
				e.printStackTrace();
			}
		}
	}
}
