package com.java1234.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
import java.util.Set;

import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;

import com.java1234.dao.PaperDao;
import com.java1234.dao.QuestionDao;
import com.java1234.model.Paper;
import com.java1234.model.Question;
import com.java1234.util.ResponseUtil;
import com.java1234.util.StringUtil;
import com.opensymphony.xwork2.ActionSupport;

/**
 * 试卷Action类
 * @author Administrator
 *
 */
public class PaperAction extends ActionSupport{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private PaperDao paperDao=new PaperDao();
	private QuestionDao questionDao=new QuestionDao();
	
	private String mainPage;
	private String paperId;
	
	private List<Paper> paperList=new ArrayList<Paper>();
	private List<Question> squestionList=new ArrayList<Question>();
	private List<Question> mquestionList=new ArrayList<Question>();
	
	private String title; // 标题
	

	private Paper paper;
	
	public List<Paper> getPaperList() {
		return paperList;
	}

	public void setPaperList(List<Paper> paperList) {
		this.paperList = paperList;
	}
	
	

	public List<Question> getSquestionList() {
		return squestionList;
	}

	public void setSquestionList(List<Question> squestionList) {
		this.squestionList = squestionList;
	}

	public List<Question> getMquestionList() {
		return mquestionList;
	}

	public void setMquestionList(List<Question> mquestionList) {
		this.mquestionList = mquestionList;
	}

	public void setPaper(Paper paper) {
		this.paper = paper;
	}
	

	public Paper getPaper() {
		return paper;
	}

	

	public String getPaperId() {
		return paperId;
	}

	public void setPaperId(String paperId) {
		this.paperId = paperId;
	}

	public String getMainPage() {
		return mainPage;
	}

	public void setMainPage(String mainPage) {
		this.mainPage = mainPage;
	}

	
	
	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	/**
	 * 获取所有试卷
	 * @return
	 * @throws Exception
	 */
	public String list()throws Exception{
		paperList=paperDao.getPapers();
		mainPage="exam/selectPaper.jsp";
		return SUCCESS;
	}
	
	/**
	 * 获取所有试卷(管理)
	 * @return
	 * @throws Exception
	 */
	public String paperList()throws Exception{
		paperList=paperDao.getPapers();
		mainPage="paper/paperList.jsp";
		return SUCCESS;
	}
	
	/**
	 * 通过id获取试卷实体
	 * @return
	 * @throws Exception
	 */
	public String getPaperById()throws Exception{
		paper=paperDao.getPaper(paperId);
		mainPage="paper/paperSave.jsp";
		return SUCCESS;
	}
	
	/**
	 * 保存预操作
	 * @return
	 * @throws Exception
	 */
	public String preSave()throws Exception{
		if(StringUtil.isNotEmpty(paperId)){
			paper=paperDao.getPaper(paperId);
			title="修改试卷";
		}else{
			title="添加试卷";
		}
		mainPage="paper/paperSave.jsp";
		return SUCCESS;
	}
	
	/**
	 * 保存试卷
	 * @return
	 * @throws Exception
	 */
	public String savePaper()throws Exception{
		if(StringUtil.isNotEmpty(paperId)){
			paper.setId(Integer.parseInt(paperId));
		}else{
			paper.setJoinDate(new Date());			
		}
		paperDao.savePaper(paper);
		return "save";
	}
	
	/**
	 * 删除试卷
	 * @return
	 * @throws Exception
	 */
	public String deletePaper()throws Exception{
		paper=paperDao.getPaper(paperId);
		JSONObject resultJson=new JSONObject();
		if(questionDao.existQuestionByPaperId(paperId)){
			resultJson.put("error","试卷下面有题目,不能删除");
		}else{
			paperDao.paperDelete(paper);
			resultJson.put("success",true);
		}
		ResponseUtil.write(resultJson,ServletActionContext.getResponse());
		return null;
	}
	
	/**
	 * 获取指定试卷
	 * @return
	 * @throws Exception
	 */
	public String getDetailPaper()throws Exception{
		paper=paperDao.getPaper(paperId);
		Set<Question> questionList=paper.getQuestions();
		Iterator<Question> it=questionList.iterator();
		while(it.hasNext()){
			Question q=it.next();
			if("1".equals(q.getType())){
				squestionList.add(q);
			}else{
				mquestionList.add(q);
			}
		}
		squestionList=this.getRandomQuestion(squestionList, 3);
		mquestionList=this.getRandomQuestion(mquestionList, 2);
		mainPage="exam/paper.jsp";
		return SUCCESS;
	}
	
	/**
	 * 获取随机试题
	 * @param questionList
	 * @param num
	 * @return
	 */
	private List<Question> getRandomQuestion(List<Question> questionList,int num){
		List<Question> resultList=new ArrayList<Question>();
		Random random=new Random();
		if(num>0){
			for(int i=1;i<=num;i++){
				int n=random.nextInt(questionList.size());
				Question q=questionList.get(n);
				if(resultList.contains(q)){
					i--;
				}else{
					resultList.add(questionList.get(n));					
				}
			}
		}
		return resultList;
	}

}
最近下载更多
weilaizhisheng  LV21 2023年9月16日
似鹤不是鹤似鹤不是鹤  LV1 2023年5月5日
sswert  LV2 2023年5月4日
amour1  LV11 2023年5月3日
tujf13763823976  LV3 2023年3月28日
fu8867  LV2 2023年2月20日
wubinbin  LV11 2023年2月11日
shamexidan  LV9 2022年5月20日
shibinf  LV3 2022年4月8日
and123456  LV11 2022年2月7日
最近浏览更多
pokerf  LV5 4月7日
taoshen95  LV14 2月29日
gjw19884260466  LV3 1月8日
WBelong  LV7 2023年12月11日
cpla1998  LV8 2023年11月28日
zhxhcj  LV8 2023年11月26日
hhhh别卷了 2023年10月20日
暂无贡献等级
186092  LV4 2023年9月20日
weilaizhisheng  LV21 2023年9月16日
暂无贡献等级
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友