package com.landaiqing.servlet.system;

import com.landaiqing.service.UserService;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;

@WebServlet("/deleteUser")
public class DeleteUser extends HttpServlet {
    private UserService userService;

    {
        try {
            userService = new UserService();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String idStr = req.getParameter("id");
        if (idStr==null || idStr==""){
            req.setAttribute("errorMsg","ID的值不能为空!!!");
            req.getRequestDispatcher("./error.jsp").forward(req,resp);
            return;
        }

        try {
            Integer id = Integer.parseInt(idStr);
            int result = userService.deleteUser(id);
            if (result > 0) {
//                req.getRequestDispatcher("showFlight.jsp").forward(req,resp);
                resp.sendRedirect("./System/index.jsp");

            }else {
                req.setAttribute("errorMsg","删除失败!!!");
                req.getRequestDispatcher("./error.jsp").forward(req,resp);
            }
        } catch (NumberFormatException e) {
            req.setAttribute("errorMsg","类型转换异常,id 不能转换成Int类型!!!");
            req.getRequestDispatcher("./error.jsp").forward(req,resp);
            e.printStackTrace();
        }catch (Exception e){
            req.setAttribute("errorMsg","系统异常!!!");
            req.getRequestDispatcher("./error.jsp").forward(req,resp);
        }
    }
}
