package com.mingrisoft.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.mingrisoft.actionForm.StudentForm;
import com.mingrisoft.dao.StudentDAO;

public class Student extends Action {

	private StudentDAO studentDAO = null;

	public Student() {
		this.studentDAO = new StudentDAO();
	}

	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		String action = request.getParameter("action"); // 获取action参数的值
		if ("studentQuery".equals(action)) {
			return studentQuery(mapping, form, request, response); // 查询考生信息的方法
		} else if ("login".equals(action)) {
			return studentLogin(mapping, form, request, response); // 考生身份验证的方法
		} else if ("studentAdd".equals(action)) {
			return studentAdd(mapping, form, request, response); // 考生注册的方法
		} else if ("studentDel".equals(action)) {
			return studentDel(mapping, form, request, response); // 删除考生的方法
		} else if ("modifyQuery".equals(action)) {
			return modifyQuery(mapping, form, request, response); // 修改个人资料时的查询方法
		} else if ("studentModify".equals(action)) {
			return studentModify(mapping, form, request, response); // 修改个人资料的方法
		} else if ("seekPwd1".equals(action)) {
			return seekPwd1(mapping, form, request, response); // 找回密码第一步时应用的方法
		} else if ("seekPwd2".equals(action)) {
			return seekPwd2(mapping, form, request, response); // 找回密码第二步时应用的方法
		} else {
			request.setAttribute("error", "操作失败！"); // 将错误信息保存到error中
			return mapping.findForward("error"); // 转到显示错误信息的页面
		}
	}

	// 考生身份验证
	public ActionForward studentLogin(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		StudentForm studentForm = (StudentForm) form;
		int ret = studentDAO.checkStudent(studentForm);
		System.out.print("验证结果ret的值:" + ret);
		if (ret == 2) {
			request.setAttribute("error", "您输入的考生准考证号码或密码错误！");
			return mapping.findForward("error");
		} else {

			studentForm = studentDAO.getStudentObj(studentForm.getID());
			HttpSession session = request.getSession();
			session.setAttribute("student", studentForm.getID());
			session.setAttribute("studentName", studentForm.getName());
			return mapping.findForward("studentLoginok");
		}
	}

	// 查询考生信息
	private ActionForward studentQuery(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		request.setAttribute("studentQuery", studentDAO.query(null));
		return mapping.findForward("studentQuery");
	}

	// 添加考生注册信息
	private ActionForward studentAdd(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		StudentForm studentForm = (StudentForm) form; 		//将接收的表单信息强制转换成ActionForm类型
		String ret = studentDAO.insert(studentForm); 			//调用添加客户信息的方法insert()
		if (ret.equals("re")) {
			request.setAttribute("error", "您已经注册，直接登录即可！");	//保存错误提示信息到error中
			return mapping.findForward("error");			//转到错误提示页面
		} else if(ret.equals("miss")){
			request.setAttribute("error", "注册失败！");		//保存错误提示信息到error中
			return mapping.findForward("error");			//转到错误提示页面
		}else{
			request.setAttribute("ret",ret);						//将生成的准考证号保存到ret中
			return mapping.findForward("studentAdd");		//转到考生信息添加成功页面
		}
	}

	// 修改考生信息时查询
	private ActionForward modifyQuery(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		System.out.println("获取的ID：" + request.getParameter("ID"));
		StudentForm studentForm = (StudentForm) (studentDAO.query(request
				.getParameter("ID")).get(0));
		System.out.println("从Bean中获取的ID：" + studentForm.getID());
		request.setAttribute("modifyQuery", studentForm);
		return mapping.findForward("modifyQuery");
	}

	// 找回密码（第一步）
	private ActionForward seekPwd1(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		StudentForm studentForm = (StudentForm) form; // 将接收的表单信息强制转换成ActionForm类型
		StudentForm s = studentDAO.seekPwd1(studentForm); // 调用seekPwd1()方法验证输入的准考证号是否存在
		request.setAttribute("seekPwd2", s); // 将返回的s对象保存到seekPwd2中
		if (s.getID().equals("")) { // 如果不存在
			request.setAttribute("error", "您输入的准考证号不存在！"); // 将错误提示信息保存到error中
			return mapping.findForward("error"); // 转到错误提示页面
		} else {
			return mapping.findForward("seekPwd1"); // 转到找回密码第二步
		}
	}

	// 找回密码（第二步）
	private ActionForward seekPwd2(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		StudentForm studentForm = (StudentForm) form;
		StudentForm s = studentDAO.seekPwd2(studentForm);
		request.setAttribute("seekPwd3", s);
		if (s.getID().equals("")) {
			request.setAttribute("error", "您输入的密码提示问题的答案不正确！");
			return mapping.findForward("error");
		} else {
			return mapping.findForward("seekPwd2");
		}
	}

	// 修改考生信息
	private ActionForward studentModify(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		StudentForm studentForm = (StudentForm) form;
		int ret = studentDAO.update(studentForm);
		if (ret == 0) {
			request.setAttribute("error", "修改考生信息失败！");
			return mapping.findForward("error");
		} else {
			return mapping.findForward("studentModify");
		}
	}

	// 删除考生信息
	private ActionForward studentDel(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		StudentForm studentForm = (StudentForm) form;
		int ret = studentDAO.delete(studentForm);
		if (ret == 0) {
			request.setAttribute("error", "删除考生信息失败！");
			return mapping.findForward("error");
		} else {
			return mapping.findForward("studentDel");
		}
	}
}