package com.example.service;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.json.JSONObject;
import com.example.dao.NxQuestionInfoDao;
import com.example.dao.NxTestpaperInfoDao;
import com.example.entity.NxTestpaperInfo;
import com.example.exception.CustomException;
import com.example.vo.NxQuestionInfoVo;
import com.example.vo.NxTestpaperInfoVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
public class NxTestpaperInfoService {
@Resource
private NxQuestionInfoDao nxQuestionInfoDao;
@Resource
private NxTestpaperInfoDao nxTestpaperInfoDao;
public NxTestpaperInfo add(NxTestpaperInfo nxTestpaperInfo) {
nxTestpaperInfoDao.insertSelective(nxTestpaperInfo);
return nxTestpaperInfo;
}
/**
* 生成试卷
*
* @param nxTestpaperInfoVo
* @return
*/
public NxTestpaperInfo generatePaper(NxTestpaperInfoVo nxTestpaperInfoVo) {
// 试卷的type
String type = nxTestpaperInfoVo.getType();
List<NxQuestionInfoVo> finalSelectQuestions = CollUtil.newArrayList();
if (nxTestpaperInfoVo.getSelectQuestion() != null) {
List<NxQuestionInfoVo> selectQuestions = nxQuestionInfoDao.findByQuestionType("单选题", type);
if (nxTestpaperInfoVo.getSelectQuestion() <= 0) {
throw new CustomException("-1", "单选题设置需大于0");
}
if (nxTestpaperInfoVo.getSelectQuestion() > selectQuestions.size()) {
throw new CustomException("-1", "单选题库数量不足");
}
// 选择题题目集合
List<Integer> selectIndexList = getIndexList(selectQuestions.size(), nxTestpaperInfoVo.getSelectQuestion());
selectIndexList.forEach(i -> finalSelectQuestions.add(selectQuestions.get(i)));
}
List<NxQuestionInfoVo> finalJudgeQuestions = CollUtil.newArrayList();
if (nxTestpaperInfoVo.getJudgeQuestion() != null) {
List<NxQuestionInfoVo> judgeQuestions = nxQuestionInfoDao.findByQuestionType("判断题", type);
if (nxTestpaperInfoVo.getJudgeQuestion() <= 0) {
throw new CustomException("-1", "判断题设置需大于0");
}
if (nxTestpaperInfoVo.getJudgeQuestion() > judgeQuestions.size()) {
throw new CustomException("-1", "判断题库数量不足");
}
// 判断题题目集合
List<Integer> judgeIndexList = getIndexList(judgeQuestions.size(), nxTestpaperInfoVo.getJudgeQuestion());
judgeIndexList.forEach(i -> finalJudgeQuestions.add(judgeQuestions.get(i)));
}
List<NxQuestionInfoVo> finalAnswerQuestions = CollUtil.newArrayList();
if (nxTestpaperInfoVo.getAnswerQuestion() != null) {
List<NxQuestionInfoVo> answerQuestions = nxQuestionInfoDao.findByQuestionType("简答题", type);
if (nxTestpaperInfoVo.getAnswerQuestion() <= 0) {
throw new CustomException("-1", "简答题设置需大于0");
}
if (nxTestpaperInfoVo.getAnswerQuestion() > answerQuestions.size()) {
throw new CustomException("-1", "简答题库数量不足");
}
// 简答题题题目集合
List<Integer> answerIndexList = getIndexList(answerQuestions.size(), nxTestpaperInfoVo.getAnswerQuestion());
answerIndexList.forEach(i -> finalAnswerQuestions.add(answerQuestions.get(i)));
}
// 多选题
List<NxQuestionInfoVo> finalMulSelectQuestions = CollUtil.newArrayList();
if (nxTestpaperInfoVo.getMulSelectQuestion() != null) {
List<NxQuestionInfoVo> mulSelectQuestions = nxQuestionInfoDao.findByQuestionType("多选题", type);
if (nxTestpaperInfoVo.getMulSelectQuestion() <= 0) {
throw new CustomException("-1", "多选题设置需大于0");
}
if (nxTestpaperInfoVo.getMulSelectQuestion() > mulSelectQuestions.size()) {
throw new CustomException("-1", "多选题库数量不足");
}
// 多选题题题目集合
List<Integer> answerIndexList = getIndexList(mulSelectQuestions.size(), nxTestpaperInfoVo.getMulSelectQuestion());
answerIndexList.forEach(i -> finalMulSelectQuestions.add(mulSelectQuestions.get(i)));
}
// 填空题
List<NxQuestionInfoVo> finalFillQuestions = CollUtil.newArrayList();
if (nxTestpaperInfoVo.getFillQuestion() != null) {
List<NxQuestionInfoVo> fillQuestions = nxQuestionInfoDao.findByQuestionType("填空题", type);
if (nxTestpaperInfoVo.getFillQuestion() <= 0) {
throw new CustomException("-1", "填空题设置需大于0");
}
if (nxTestpaperInfoVo.getFillQuestion() > fillQuestions.size()) {
throw new CustomException("-1", "填空题库数量不足");
}
// 填空题题题目集合
List<Integer> answerIndexList = getIndexList(fillQuestions.size(), nxTestpaperInfoVo.getFillQuestion());
answerIndexList.forEach(i -> finalFillQuestions.add(fillQuestions.get(i)));
}
// 校验,不能全部为空
if (CollectionUtil.isEmpty(finalSelectQuestions) && CollectionUtil.isEmpty(finalJudgeQuestions) && CollectionUtil.isEmpty(finalAnswerQuestions)
&& CollectionUtil.isEmpty(finalMulSelectQuestions) && CollectionUtil.isEmpty(finalFillQuestions)) {
throw new CustomException("-1", "请至少确定一种题型数量");
}
// 生成试卷html
Map<String, String> map = getPaperHtml(nxTestpaperInfoVo, finalSelectQuestions, finalJudgeQuestions, finalAnswerQuestions,
finalMulSelectQuestions, finalFillQuestions);
nxTestpaperInfoVo.setContent(map.get("page"));
nxTestpaperInfoVo.setAnswer(map.get("answer"));
//插入表
add(nxTestpaperInfoVo);
return nxTestpaperInfoVo;
}
/**
* 生成试卷html
*
* @param selects
* @param judges
* @param answers
* @return
*/
private Map<String, String> getPaperHtml(NxTestpaperInfoVo nxTestpaperInfoVo, List<NxQuestionInfoVo> selects,
List<NxQuestionInfoVo> judges, List<NxQuestionInfoVo> answers,
List<NxQuestionInfoVo> mulSelect, List<NxQuestionInfoVo> fill) {
Map<String, String> map = new HashMap<>();
StringBuilder pageBuilder = new StringBuilder();
JSONObject answerBuilder = new JSONObject();
pageBuilder.append("<div class=\"panel-heading\">\n")
.append("<span style=\"font-size: 14px\">试卷名称:</span><span style=\"color: orangered\">" + nxTestpaperInfoVo.getName() + "</span>\n")
.append("<span style=\"font-size: 14px; margin-left: 20px\">试卷编号:</span><span style=\"color: orangered\">" + nxTestpaperInfoVo.getCode() + "</span>\n")
.append("<span style=\"font-size: 14px; margin-left: 20px\">考试剩余时间:</span><span id=\"time\" style=\"color: orangered\">" + nxTestpaperInfoVo.getTime() + "分</span>\n")
.append("</div>\n")
.append("<div class=\"panel-body\">\n")
.append("<div style=\"border: 1px solid #1E9FFF;\">
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
基于Java的答题系统毕业设计(源码+演示视频+数据库).rar 【项目技术】 开发语言:Java 架构:B/S 数据库:mysql 【实现功能】 主要使用者分为管理员、用户;管理员:首页、管理员信息、用户信息、新闻资讯、公告信息、试题类别、题型信息、试题信息、试卷信息、成绩信息、个人信息、修改密码、退出登录;用户:首页、新闻资讯、公告信息、试题类别、题型信息、试题信息、试卷信息、成绩信息、个人信息、修改密码、退出登录等功能。
资源推荐
资源详情
资源评论
收起资源包目录
基于Java的答题系统毕业设计(源码+演示视频+数据库).rar (575个子文件)
$PRODUCT_WORKSPACE_FILE$ 489B
NxTestpaperInfoService.class 14KB
NxTestpaperInfoService.class 14KB
AccountController.class 12KB
AccountController.class 12KB
AdminInfoController.class 8KB
AdminInfoController.class 8KB
UserInfoController.class 8KB
UserInfoController.class 8KB
EchartsController.class 8KB
EchartsController.class 8KB
NxTestpaperInfoController.class 8KB
NxTestpaperInfoController.class 8KB
NxSystemFileController.class 8KB
NxSystemFileController.class 8KB
AdvertiserInfoController.class 8KB
AdvertiserInfoController.class 8KB
NxQuestionTypeInfoController.class 7KB
NxQuestionTypeInfoController.class 7KB
NxQuestionInfoController.class 7KB
NxQuestionInfoController.class 7KB
NxTypeInfoController.class 7KB
NxTypeInfoController.class 7KB
MenuController.class 5KB
MenuController.class 5KB
NxScoreInfoController.class 4KB
NxScoreInfoController.class 4KB
ZixunInfoCommentService.class 4KB
ZixunInfoCommentService.class 4KB
AdminInfoService.class 4KB
AdminInfoService.class 4KB
UserInfoService.class 4KB
UserInfoService.class 4KB
ZixunInfoCommentController.class 4KB
ZixunInfoCommentController.class 4KB
NxScoreInfoService.class 4KB
NxScoreInfoService.class 4KB
MessageInfoService.class 4KB
MessageInfoService.class 4KB
NxQuestionInfoService.class 3KB
NxQuestionInfoService.class 3KB
MessageInfoController.class 3KB
MessageInfoController.class 3KB
ZixunInfoController.class 3KB
ZixunInfoController.class 3KB
AdminInfo.class 3KB
AdminInfo.class 3KB
AdvertiserInfoService.class 3KB
AdvertiserInfoService.class 3KB
UserInfo.class 3KB
UserInfo.class 3KB
NxQuestionTypeInfoService.class 3KB
NxQuestionTypeInfoService.class 3KB
NxScoreInfo.class 3KB
NxScoreInfo.class 3KB
NxTypeInfoService.class 3KB
NxTypeInfoService.class 3KB
ZixunInfoService.class 3KB
ZixunInfoService.class 3KB
Account.class 3KB
Account.class 3KB
NxSystemFileInfoService.class 3KB
NxSystemFileInfoService.class 3KB
Result.class 2KB
Result.class 2KB
NxQuestionInfo.class 2KB
NxQuestionInfo.class 2KB
NxTestpaperInfo.class 2KB
NxTestpaperInfo.class 2KB
EchartsData.class 2KB
EchartsData.class 2KB
ResultCode.class 2KB
ResultCode.class 2KB
ZixunInfoComment.class 2KB
ZixunInfoComment.class 2KB
MyInterceptor.class 2KB
MyInterceptor.class 2KB
GlobalExceptionHandler.class 2KB
GlobalExceptionHandler.class 2KB
MessageInfo.class 2KB
MessageInfo.class 2KB
LoginFilter.class 2KB
LoginFilter.class 2KB
WebMvcConfig.class 2KB
WebMvcConfig.class 2KB
EchartsData$Data.class 1KB
EchartsData$Data.class 1KB
AdvertiserInfo.class 1KB
AdvertiserInfo.class 1KB
NxTestpaperInfoVo.class 1KB
NxTestpaperInfoVo.class 1KB
EchartsData$Series.class 1KB
EchartsData$Series.class 1KB
AuthorityInfo.class 1KB
AuthorityInfo.class 1KB
NxQuestionTypeInfo.class 1KB
NxQuestionTypeInfo.class 1KB
NxSystemFileInfo.class 1KB
NxSystemFileInfo.class 1KB
NxTypeInfo.class 1KB
共 575 条
- 1
- 2
- 3
- 4
- 5
- 6
资源评论
职场程序猿
- 粉丝: 2810
- 资源: 3683
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功