package xin.examinationSystem.servlet;
import com.alibaba.fastjson.JSON;
import xin.examinationSystem.account.Student;
import xin.examinationSystem.account.Teacher;
import xin.examinationSystem.account.testPaper.Location_table;
import xin.examinationSystem.account.testPaper.TP_Title;
import xin.examinationSystem.account.testPaper.TestPaper;
import xin.examinationSystem.account.testPaper.TestPaperSet;
import xin.examinationSystem.account.testPaper.topic.*;
import xin.examinationSystem.dao.IStudentMapper;
import xin.examinationSystem.dao.topic.ITestPaperMapper;
import xin.examinationSystem.imp.TestPaperMapperImp;
import xin.examinationSystem.utils.Tools;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Date;
@WebServlet("/queryTestPaperServlet")
public class queryTestPaperServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
boolean studentStatus = false;
boolean teacherStatus = false;
Student curStudent = new Student();
Cookie[] cookies = request.getCookies();
for (Cookie tCookie : cookies) {
if (tCookie.getName().equals("accountMessageStudent")) {
HttpSession session = request.getSession();
curStudent = (Student) session.getAttribute(tCookie.getValue());
if (curStudent != null) {
studentStatus = true;
}
}
}
for (Cookie tCookie : cookies) {
if (tCookie.getName().equals("accountMessageTeacher")) {
HttpSession session = request.getSession();
Teacher attribute = (Teacher) session.getAttribute(tCookie.getValue());
if (attribute != null) {
teacherStatus = true;
}
}
}
if (!studentStatus && !teacherStatus) {
response.sendRedirect("/index.html");
return;
}
ITestPaperMapper iTestPaperMapper = (ITestPaperMapper) Tools.getImp("ITestPaperMapper");
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
//添加单选题请求
String add_single_choice = request.getParameter("query_all_SingleChoice");
if (add_single_choice != null) {
SingleChoice[] singleChoices = iTestPaperMapper.queryAllSingleChoice();
out.write(JSON.toJSONString(singleChoices));
out.flush();
out.close();
return;
}
//添加单选题请求
String del_single_choice = request.getParameter("del_single_choice");
if (del_single_choice != null) {
iTestPaperMapper.delSingleChoice(Integer.parseInt(del_single_choice));
out.write("succeed");
out.flush();
out.close();
return;
}
//添加多选题请求
String query_all_multiple_choice_question = request.getParameter("query_all_multiple_choice_question");
if (query_all_multiple_choice_question != null) {
MultipleChoiceQuestion[] mcq = iTestPaperMapper.queryAllMultipleChoiceQuestion();
out.write(JSON.toJSONString(mcq));
out.flush();
out.close();
return;
}
//添加多选题请求
String del_multiple_choice_question = request.getParameter("del_multiple_choice_question");
if (del_multiple_choice_question != null) {
iTestPaperMapper.delMultipleChoiceQuestion(Integer.parseInt(del_multiple_choice_question));
out.write("succeed");
out.flush();
out.close();
return;
}
//添加判断选题请求
String query_all_judge_question = request.getParameter("query_all_judge_question");
if (query_all_judge_question != null) {
JudgeQuestion[] jq = iTestPaperMapper.queryAllJudgeQuestion();
out.write(JSON.toJSONString(jq));
out.flush();
out.close();
return;
}
//添加判断选题请求
String del_judge_question = request.getParameter("del_judge_question");
if (del_judge_question != null) {
iTestPaperMapper.delJudgeQuestion(Integer.parseInt(del_judge_question));
out.write("succeed");
out.flush();
out.close();
return;
}
//添加填空题请求
String query_all_gap_filling = request.getParameter("query_all_gap_filling");
if (query_all_gap_filling != null) {
GapFilling[] gf = iTestPaperMapper.queryAllGapFilling();
out.write(JSON.toJSONString(gf));
out.flush();
out.close();
return;
}
//添加填空题请求
String del_gap_filling = request.getParameter("del_gap_filling");
if (del_gap_filling != null) {
iTestPaperMapper.delGapFilling(Integer.parseInt(del_gap_filling));
out.write("succeed");
out.flush();
out.close();
return;
}
//添加简答题请求
String query_all_essay_question = request.getParameter("query_all_essay_question");
if (query_all_essay_question != null) {
EssayQuestion[] eq = iTestPaperMapper.queryAllEssayQuestion();
out.write(JSON.toJSONString(eq));
out.flush();
out.close();
return;
}
//添加简答请求
String del_essay_question = request.getParameter("del_essay_question");
if (del_essay_question != null) {
iTestPaperMapper.delEssayQuestion(Integer.parseInt(del_essay_question));
out.write("succeed");
out.flush();
out.close();
return;
}
//返回所有题目数量
String query_all_topic = request.getParameter("query_all_topic");
if (query_all_topic != null) {
out.write(TestPaperMapperImp.queryAllTopic());
out.flush();
out.close();
return;
}
//返回所有题目数量
String query_all_Student_test_paper = request.getParameter("query_all_Student_test_paper");
if (query_all_Student_test_paper != null) {
Student_test_paper[] stp = iTestPaperMapper.query_all_Student_test_paper(Integer.parseInt(query_all_Student_test_paper));
if (stp.length == 0) {
out.write("null");
out.flush();
out.close();
}
String[] resultSet = new String[stp.length];
IStudentMapper iStudentMapper = (IStudentMapper) Tools.getImp("IStudentMapper");
for (int index = 0, len = stp.length; index < len; index++) {
resultSet[index] = "" + stp[index].getTpId() + "###" + stp[index].getStuId()
+ "###" + iStudentMapper.findStuId(stp[index].getStuId()).getStuName() + "###" + stp[index].getGrade();
}
out.write(JSON.toJSONString(resultSet));
out.flush();
out.close();
return;
}
//返回当前时间
String query_current_time = request.getParameter("query_current_time");
if (query_current_time != null) {
//发送当前
out.write("" + new Date().getTime());
out.flush();
out.close();
return;
}
//返回考试时间
System.out.println("路过-----------------------------------------------