package com.mingrisoft.action;

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

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.QuestionsForm;
import com.mingrisoft.dao.LessonDAO;
import com.mingrisoft.dao.QuestionsDAO;
import com.mingrisoft.dao.TaoTiDAO;

public class Questions extends Action {
	private QuestionsDAO questionsDAO = null;
	private LessonDAO lessonDAO=null;
	private TaoTiDAO taoTiDAO=null;

	public Questions() {
		this.questionsDAO = new QuestionsDAO();
		this.lessonDAO=new LessonDAO();
		this.taoTiDAO=new TaoTiDAO();
	}
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		String action = request.getParameter("action");	//获取action参数的值
		if ("questionsQuery".equals(action)) {
			return questionsQuery(mapping, form, request, response);	//考试题目查询
		}else if("questionsAddQuery".equals(action)){
				//添加考试题目时查询包括套题的课程列表
			return questionsAddQuery(mapping,form,request,response); 
		} else if ("questionsAdd".equals(action)) {
			return questionsAdd(mapping, form, request, response);		//添加考试题目
		} else if ("questionsDel".equals(action)) {
			return questionsDel(mapping, form, request, response);		//删除考试题目
		}else if("questionsModifyQuery".equals(action)){
					//修改考试题目时的查询
			return questionsModifyQuery(mapping,form,request,response); 
		}else if("questionsModify".equals(action)){
			return questionsModify(mapping,form,request,response);		//修改考试题目
		}else if("queryTaoTi".equals(action)){
			return queryTaoTi(mapping,form,request,response);		//根据课程查询套题
		}else{
			request.setAttribute("error", "操作失败！");//将错误提示信息保存到error参数中
			return mapping.findForward("error");		//转到显示错误信息的页面
		}
	}


	// 查询考试题目信息
	private ActionForward questionsQuery(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		request.setAttribute("questionsQuery", questionsDAO.query(0)); 	//将查询结果保存到questionsQuery参数中
		return mapping.findForward("questionsQuery");				//转到显示考试题目列表页面
	}

	
	// 添加考试题目
	private ActionForward questionsAdd(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		QuestionsForm questionsForm = (QuestionsForm) form;
		int ret = questionsDAO.insert(questionsForm);
		if (ret == 1) {
			return mapping.findForward("questionsAdd");
		} else if (ret == 2) {
			request.setAttribute("error", "该考试题目已经添加！");
			return mapping.findForward("error");
		} else {
			request.setAttribute("error", "添加考试题目失败！");
			return mapping.findForward("error");
		}
	}
	
	
	// 添加考试题目时查询
	private ActionForward questionsAddQuery(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		request.setAttribute("lessonList",lessonDAO.query(-1));	//全部包括套题的课程列表
		return mapping.findForward("questionsAddQuery");
	}
	
	
	//根据课程查询套题ajax
	private ActionForward queryTaoTi(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		//查询指定课程的套题列表
		request.setAttribute("taoTiList",taoTiDAO.queryTaoTi(Integer.parseInt(request.getParameter("id")))); 
		return mapping.findForward("queryTaoTi");				//转到生成套题下拉列表的页面
	}

	
	
	// 修改考试题目时的查询
	private ActionForward questionsModifyQuery(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		QuestionsForm questionsForm=(QuestionsForm)((questionsDAO.query(Integer.parseInt(request.getParameter("id")))).get(0));
		request.setAttribute("questionsModifyQuery", questionsForm);
		return mapping.findForward("questionsModifyQuery");
	}
	
	
	//修改考试题目
	private ActionForward questionsModify(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		QuestionsForm questionsForm = (QuestionsForm) form;
		int ret = questionsDAO.update(questionsForm);
		if (ret == 0) {
			request.setAttribute("error", "修改考试题目失败！");
			return mapping.findForward("error");
		} else {
			return mapping.findForward("questionsModify");
		}
	}
	
	
	// 删除考试题目
	private ActionForward questionsDel(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		QuestionsForm questionsForm = (QuestionsForm) form;	//将获取的表单信息强制转换为ActionForm类型
		int ret = questionsDAO.delete(questionsForm);				//调用删除考试题目的方法
		if (ret == 0) {
			request.setAttribute("error", "删除考试题目失败！");
			return mapping.findForward("error");
		} else {
			return mapping.findForward("questionsDel");
		}
	}
	
	
}