package com.landaiqing.servlet;

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.util.Date;

@WebServlet("/publish")
public class PublishServlet 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 {
        doGet(req,resp);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {

            String name = req.getParameter("name");
            if (StringUtils.isNullOrEmpty(name)){
                req.setAttribute("errorMsg","name 的值不能为空!!!");
                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(email)){
                req.setAttribute("errorMsg","arriveAirport 的值不能为空!!!");
                req.getRequestDispatcher("error.jsp").forward(req,resp);
                return;
            }
            String content = req.getParameter("content");
            if (StringUtils.isNullOrEmpty(content)){
                req.setAttribute("errorMsg","content 的值不能为空!!!");
                req.getRequestDispatcher("error.jsp").forward(req,resp);
                return;
            }
            Date time = new java.sql.Date(new Date().getTime());

            UserEntity userEntity=new UserEntity();
            userEntity.setNickName(name);
            userEntity.setQQ(qq);
            userEntity.setEmail(email);
            userEntity.setContent(content);
            userEntity.setDateTime((java.sql.Date) time);

            int result = userService.insert(userEntity);
            if (result<=0){
                req.setAttribute("errorMsg","插入失败!!!");
                req.getRequestDispatcher("error.jsp").forward(req,resp);
                return;
            }
            userService.list();
            resp.sendRedirect("index.jsp");
        } catch (Exception e) {
            req.setAttribute("errorMsg","系统异常!!!");
            req.getRequestDispatcher("error.jsp").forward(req,resp);
            throw new RuntimeException(e);
        }
    }
}
