package servlet.admin;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.sql.Connection;

import javabean.Base;
import javabean.Util;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * Servlet 实现类，用于处理管理员的添加操作
 *
 * 该 Servlet 接收管理员的姓名、账号、密码和邮箱，通过 SQL 查询验证账号是否重复，如果不重复则插入新管理员。
 */
@WebServlet("/admin/managerAdd")
public class ManagerAdd extends HttpServlet {

	/**
	 * 处理 POST 请求，用于添加新的管理员
	 *
	 * 该方法会接收请求中的管理员姓名、账号、密码和邮箱，首先验证账号是否已存在，
	 * 如果不存在，则将新的管理员信息插入到数据库中。返回 JSON 格式的响应结果。
	 *
	 * @param req 请求对象
	 * @param resp 响应对象
	 * @throws ServletException 如果发生 Servlet 错误
	 * @throws IOException 如果发生 I/O 错误
	 */
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// 设置响应内容类型为 JSON
		resp.setContentType("application/json; charset=utf8");

		// 接收前端传递的参数
		String name = req.getParameter("name");
		String account = req.getParameter("account");
		String password = req.getParameter("password");
		String email = req.getParameter("email");

		// SQL 语句及数据库连接准备
		String sql = "";
		Connection connection = null;
		PreparedStatement pstmt = null;
		ResultSet resultSet = null;
		int result = 0;
		int count = 0;

		// 返回的响应数据
		int code = 1;
		String msg = "";
		PrintWriter out = resp.getWriter();
		JSONArray jsonArray = new JSONArray();
		JSONObject jsonObject = new JSONObject();

		// 进行参数校验，检查不能为空的字段
		if (name == null || name.equals("") || account == null || account.equals("") ||
				password == null || password.equals("") || email == null || email.equals("")) {
			msg = "参数不能为空";
			out.print(Util.jsonResponse(code, msg, null));
		} else {
			try {
				// 获取数据库连接
				connection = (Connection) Base.getConnection();

				// 验证账号是否已存在
				sql = "select count(*) as count from manager where account=?";
				pstmt = connection.prepareStatement(sql);
				pstmt.setString(1, account);
				resultSet = pstmt.executeQuery();
				resultSet.next();
				count = resultSet.getInt("count");

				// 如果账号不存在，则插入新管理员
				if (count == 0) {
					sql = "insert into manager(name, account, password, email) values(?,?,?,?)";
					pstmt = connection.prepareStatement(sql);
					pstmt.setString(1, name);
					pstmt.setString(2, account);
					pstmt.setString(3, password);
					pstmt.setString(4, email);
					result = pstmt.executeUpdate();
				}

				// 根据执行结果返回响应信息
				if (result == 1 && count == 0) {
					code = 0;
					msg = "添加成功";
				} else if (count > 0) {
					msg = "账号重复";
				} else {
					msg = "添加失败";
				}
			} catch (ClassNotFoundException e) {
				msg = "class not found";
			} catch (SQLException e) {
				msg = "sql错误";
			} finally {
				try {
					// 关闭数据库资源
					Base.closeResource(connection, pstmt, resultSet);
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}

			// 返回响应结果
			out.print(Util.jsonResponse(code, msg, null));
		}
	}
}
