package com.landaiqing.servlet.system;

import com.landaiqing.service.AdminUserService;
import com.mysql.cj.util.StringUtils;
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;
import java.io.PrintWriter;

@WebServlet("/openAdmin")
public class OpenAdmin extends HttpServlet {
    private AdminUserService adminUserService;

    {
        try {
            adminUserService = new AdminUserService();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            String idStr = req.getParameter("id");
            if (StringUtils.isNullOrEmpty(idStr)){
                req.setAttribute("errorMsg","id的值不能为空!!!");
                req.getRequestDispatcher("error.jsp").forward(req,resp);
                return;
            }
            int result = adminUserService.OpenAdmin(Integer.valueOf(idStr));
            if (result<=0){
                req.setAttribute("errorMsg","修改失败!!!");
                req.getRequestDispatcher("error.jsp").forward(req,resp);
                return;
            }
            PrintWriter writer=resp.getWriter();
            writer.println("修改成功!");
            writer.close();
            resp.sendRedirect("");
        } catch (Exception e) {
            req.setAttribute("errorMsg","系统异常!!!");
            req.getRequestDispatcher("error.jsp").forward(req,resp);
            throw new RuntimeException(e);
        }
    }
}
