package com.landaiqing.servlet.system;

import com.landaiqing.entity.UserEntity;
import com.landaiqing.service.UserService;
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.sql.Date;

@WebServlet("/update")
public class UpdateServlet extends HttpServlet {
    private UserService userService;

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

    @Override
    protected void doPost(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;
            }
            Integer id = Integer.parseInt(idStr);

            String nickName = req.getParameter("nickName");
            if (StringUtils.isNullOrEmpty(nickName)){
                req.setAttribute("errorMsg","nickName 的值不能为空!!!");
                req.getRequestDispatcher("error.jsp").forward(req,resp);
                return;
            }
            String qq = req.getParameter("qq");
            if (StringUtils.isNullOrEmpty(qq)){
                req.setAttribute("errorMsg","qq 的值不能为空!!!");
                req.getRequestDispatcher("error.jsp").forward(req,resp);
                return;
            }
            String email = req.getParameter("email");
            if (StringUtils.isNullOrEmpty(idStr)){
                req.setAttribute("errorMsg","email 的值不能为空!!!");
                req.getRequestDispatcher("error.jsp").forward(req,resp);
                return;
            }

            String content = req.getParameter("content");
            if (StringUtils.isNullOrEmpty(idStr)){
                req.setAttribute("errorMsg","content 的值不能为空!!!");
                req.getRequestDispatcher("error.jsp").forward(req,resp);
                return;
            }

            String DateTime = req.getParameter("dateTime");
            if (StringUtils.isNullOrEmpty(idStr)){
                req.setAttribute("errorMsg","DateTime 的值不能为空!!!");
                req.getRequestDispatcher("error.jsp").forward(req,resp);
                return;
            }
            String replyContent = req.getParameter("replyContent");
            if (StringUtils.isNullOrEmpty(idStr)){
                req.setAttribute("errorMsg","replyContent 的值不能为空!!!");
                req.getRequestDispatcher("error.jsp").forward(req,resp);
                return;
            }
            String replyDateTime = req.getParameter("replyDateTime");
            if (StringUtils.isNullOrEmpty(idStr)){
                req.setAttribute("errorMsg","replyDateTime 的值不能为空!!!");
                req.getRequestDispatcher("error.jsp").forward(req,resp);
                return;
            }


            UserEntity userEntity=new UserEntity();
            userEntity.setUserId(id);
            userEntity.setNickName(nickName);
            userEntity.setQQ(qq);
            userEntity.setEmail(email);
            userEntity.setContent(content);
            userEntity.setDateTime(Date.valueOf(DateTime));
            userEntity.setReplyContent(replyContent);
            userEntity.setReplyDateTime(Date.valueOf(replyDateTime));

            int result = userService.update(userEntity);
            if (result<=0){
                req.setAttribute("errorMsg","修改失败!!!");
                req.getRequestDispatcher("error.jsp").forward(req,resp);
                return;
            }
            resp.sendRedirect("./System/messageManage.jsp");
        } catch (Exception e) {
            req.setAttribute("errorMsg","系统异常!!!");
            req.getRequestDispatcher("error.jsp").forward(req,resp);
            throw new RuntimeException(e);
        }
    }
}
