Files
nuttyreading-java/src/main/java/com/peanut/modules/common/controller/ClassExamController.java
2025-05-20 13:43:23 +08:00

346 lines
14 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package com.peanut.modules.common.controller;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.peanut.common.excel.ExcelUtil;
import com.peanut.common.utils.DateUtils;
import com.peanut.common.utils.R;
import com.peanut.common.utils.ShiroUtils;
import com.peanut.modules.common.entity.*;
import com.peanut.modules.common.service.*;
import com.peanut.modules.master.service.CourseService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import java.io.InputStream;
import java.util.*;
@Slf4j
@RestController("commonClassExam")
@RequestMapping("common/classExam")
public class ClassExamController {
@Autowired
private ClassExamService classExamService;
@Autowired
private ClassExamUserService classExamUserService;
@Autowired
private ClassExamSubjectService classExamSubjectService;
@Autowired
private ClassExamOptionService classExamOptionService;
@Autowired
private CourseService courseService;
@Autowired
private ClassEntityService classEntityService;
@Autowired
private UserCertificateService userCertificateService;
@Autowired
private SysNoticeService sysNoticeService;
//导入考试题
@RequestMapping("/importSubject")
@Transactional
public R importSubject(@RequestParam("file") MultipartFile file, @RequestParam("courseId") int courseId) {
int num = 0;
try (InputStream fis = file.getInputStream()) {
//解析数据
ExcelUtil example = new ExcelUtil();
example.processOneSheet(fis);
LinkedHashMap<String, String> map = example.getRowContents();
Iterator<Map.Entry<String, String>> it = map.entrySet().iterator();
//处理数据
int count = 0;
while (it.hasNext()) {
Map.Entry<String, String> entry = it.next();
String pos = entry.getKey();
String rowNo = pos.replaceAll("[a-zA-Z]", "");
count = Integer.parseInt(rowNo);
// System.out.println(pos + ";" + entry.getValue());
}
List<ClassExamOption> optionList = new ArrayList<>();
//先校验数据
StringBuffer sb = new StringBuffer();
for (int i=2;i<=count;i++){
//类型
String type = map.containsKey("A"+i)?map.get("A"+i):"";
if (type.contains("单选")||type.contains("多选")){
}else {
sb.append(""+i+"行题目类型出错\n");
}
String option1 = map.containsKey("C"+i)?map.get("C"+i):"";
if (StringUtils.isBlank(option1)){
sb.append(""+i+"行无选项1\n");
}
String option2 = map.containsKey("D"+i)?map.get("D"+i):"";
if (StringUtils.isBlank(option2)){
sb.append(""+i+"行无选项2\n");
}
String option3 = map.containsKey("E"+i)?map.get("E"+i):"";
if (StringUtils.isBlank(option3)){
sb.append(""+i+"行无选项3\n");
}
String option4 = map.containsKey("F"+i)?map.get("F"+i):"";
if (StringUtils.isBlank(option4)){
sb.append(""+i+"行无选项4\n");
}
String answer = map.containsKey("I"+i)?map.get("I"+i):"";
if (StringUtils.isBlank(answer)){
sb.append(""+i+"行无答案\n");
}
//题目
String content = map.containsKey("B"+i)?map.get("B"+i):"";
if (StringUtils.isBlank(content)) {
sb.append(""+i+"行无题目\n");
}
}
if (sb.length() > 0){
throw new Exception(sb.toString());
}
for (int i=2;i<=count;i++){
ClassExamSubject subject = new ClassExamSubject();
subject.setCourseId(courseId);
if (map.get("A"+i).contains("单选")){
subject.setType(0);
}else if (map.get("A"+i).contains("多选")){
subject.setType(1);
}
subject.setContent(map.get("B"+i));
//所属章节
subject.setChapter(map.containsKey("N"+i)?map.get("N"+i):"");
//音视频序号
subject.setMedia(map.containsKey("O"+i)?map.get("O"+i):"");
//音视频时间
subject.setMediaTime(map.containsKey("P"+i)?map.get("P"+i):"");
//出题人
subject.setCreateUser(map.containsKey("Q"+i)?map.get("Q"+i):"");
classExamSubjectService.save(subject);
num++;
//插入选项
insertOption(optionList,subject.getId(),map.get("C"+i),map.get("I"+i));
insertOption(optionList,subject.getId(),map.get("D"+i),map.get("I"+i));
insertOption(optionList,subject.getId(),map.get("E"+i),map.get("I"+i));
insertOption(optionList,subject.getId(),map.get("F"+i),map.get("I"+i));
insertOption(optionList,subject.getId(),map.containsKey("G"+i)?map.get("G"+i):"",map.get("I"+i));
insertOption(optionList,subject.getId(),map.containsKey("H"+i)?map.get("H"+i):"",map.get("I"+i));
}
classExamOptionService.saveBatch(optionList);
} catch (Exception e) {
e.printStackTrace();
TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
return R.error(e.getMessage());
}
return R.ok("导入成功"+num+"");
}
public void insertOption(List<ClassExamOption> optionList,int subjectId,String content,String rightAnswer){
if (StringUtils.isNotEmpty(content)){
ClassExamOption option = new ClassExamOption();
option.setSubjectId(subjectId);
if (rightAnswer.contains(content.substring(0,1))){
option.setRightWrong(1);
}else {
option.setRightWrong(0);
}
char[] c = content.toCharArray();
if (c.length>1){
if (c[1]=='、'||c[1]==''||c[1]==''||c[1]==':'||c[1]==';'||c[1]==' '||c[1]=='.'){
content = content.substring(2);
}else {
content = content.substring(1);
}
}else {
content = "";
}
if (StringUtils.isNotBlank(content)){
option.setContent(content);
optionList.add(option);
}
}
}
//考试题目列表
@RequestMapping("/getClassExamSubjectList")
public R getClassExamSubjectList(@RequestBody Map<String,Object> params){
Page classExamSubjectList = classExamService.getClassExamSubjectList(params);
return R.ok().put("page",classExamSubjectList);
}
//获取服务器时间
@RequestMapping("/getServerTime")
public R getServerTime(){
return R.ok().put("serverTime",new Date().getTime());
}
//生成试卷
@RequestMapping("/generateExamPaper")
public R generateExamPaper(@RequestBody Map<String,Object> params){
return classExamService.generateExamPaper(params);
}
//考试中的试卷
@RequestMapping("/examingPaper")
public R examingPaper(){
return classExamService.examingPaper();
}
//提交选项
@RequestMapping("/submitOption")
public R submitOption(@RequestBody Map<String,Object> params){
classExamService.submitOption(params);
return R.ok();
}
//提交试卷
@RequestMapping("/submitExamPaper")
public R submitExamPaper(@RequestBody Map<String,Object> params){
ClassExamUser classExamUser = classExamService.submitExamPaper(params);
if ("1".equals(classExamUser.getType())){
SysNotice notice = new SysNotice();
notice.setUserId(classExamUser.getUserId());
notice.setContent("考试已完成,请等待结班获取证书获取情况。");
sysNoticeService.save(notice);
}
//自考的人成绩达标,发放证书
if ("2".equals(classExamUser.getType())&&classExamUser.getScore()>=60){
SysNotice notice = new SysNotice();
notice.setUserId(classExamUser.getUserId());
notice.setContent("恭喜您,您自考成绩合格,获得课程自考证书,在证书管理填写您的相关信息,即可出证啦!");
sysNoticeService.save(notice);
UserCertificate userCertificate = new UserCertificate();
userCertificate.setType("ZK");
userCertificate.setLabelId(8);
userCertificate.setUserId(ShiroUtils.getUId());
CourseEntity courseEntity = courseService.getById(classExamUser.getRelationId());
String courseTitle = courseEntity.getTitle();
if (courseTitle.contains("")){
courseTitle = courseEntity.getTitle().substring(0,courseEntity.getTitle().indexOf(""));
}
userCertificate.setTitle(courseTitle);
String certificateNo = classEntityService.getNextCertificateNo("ZK",courseEntity.getTitleAbbr());//证书编号
userCertificate.setCertificateNo(certificateNo);
if (StringUtils.isNotEmpty(ShiroUtils.getUser().getPhoto())&&StringUtils.isNotEmpty(ShiroUtils.getUser().getName())){
String endYear = DateUtil.year(classExamUser.getEndTime())+"";
String endMonth = DateUtil.month(classExamUser.getEndTime())+1+"";
String endDay = DateUtil.dayOfMonth(classExamUser.getEndTime())+"";
String[] description= {endYear,endMonth,endDay,courseTitle};
String[] edes = {courseEntity.getEtitle()};
String url = userCertificateService.generateCertificate("ZK",certificateNo,ShiroUtils.getUser().getPhoto(),
ShiroUtils.getUser().getName(), description, edes, endYear,endMonth,endDay);
userCertificate.setCertificateUrl(url);
}
userCertificate.setCourseId(courseEntity.getId());
userCertificateService.save(userCertificate);
}
return R.ok().put("examPaper",classExamUser);
}
//试卷列表
@RequestMapping("/getExamPaperList")
public R getExamPaperList(@RequestBody Map<String,Object> params){
Object examPaper = classExamService.getExamPaperList(params);
return R.ok().put("examPaper",examPaper);
}
//自考试卷列表
@RequestMapping("/getZKExamPaperList")
public R getZKExamPaperList(@RequestBody Map<String,Object> params){
List<ClassExamUser> ZKExamUserList = classExamUserService.list(new LambdaQueryWrapper<ClassExamUser>()
.eq(ClassExamUser::getType,"2")
.eq(ClassExamUser::getUserId,ShiroUtils.getUId())
.eq(ClassExamUser::getRelationId,params.get("courseId"))
.orderByAsc(ClassExamUser::getId));
return R.ok().put("ZKExamUserList",ZKExamUserList);
}
//自考试卷列表
@RequestMapping("/getNextZKTime")
public R getNextZKTime(@RequestBody Map<String,Object> params){
List<ClassExamUser> ZKExamUserList = classExamUserService.list(new LambdaQueryWrapper<ClassExamUser>()
.eq(ClassExamUser::getType,"2")
.eq(ClassExamUser::getUserId,ShiroUtils.getUId())
.eq(ClassExamUser::getRelationId,params.get("courseId"))
.orderByDesc(ClassExamUser::getId));
if (ZKExamUserList.size() == 0){
return R.ok().put("nextZKTime",null).put("nextLongTime",null);
}else {
Date nextTime = DateUtils.addDateDays(ZKExamUserList.get(0).getStartTime(),183);
return R.ok().put("nextZKTime",nextTime).put("nextLongTime",nextTime.getTime());
}
}
//试卷详情
@RequestMapping("/getExamPaperInfo")
public R getExamPaperInfo(@RequestBody Map<String,Object> params){
return classExamService.getExamPaperInfo(params);
}
//添加题目
@RequestMapping("/addClassExamSubject")
public R addClassExamSubject(@RequestBody ClassExamSubject classExamSubject){
classExamService.addClassExamSubject(classExamSubject);
return R.ok();
}
//添加选项
@RequestMapping("/addClassExamOption")
public R addClassExamOption(@RequestBody ClassExamOption classExamOption){
classExamService.addClassExamOption(classExamOption);
return R.ok();
}
//题目详情
@RequestMapping("/classExamSubjectInfo")
public R classExamSubjectInfo(@RequestBody Map<String,Object> params){
return R.ok().put("classExamSubjectInfo",classExamService.classExamSubjectInfo(params));
}
//修改题目
@RequestMapping("/updateClassExamSubject")
public R updateClassExamSubject(@RequestBody ClassExamSubject classExamSubject){
classExamService.updateClassExamSubject(classExamSubject);
return R.ok();
}
//修改选项
@RequestMapping("/updateClassExamOption")
public R updateClassExamOption(@RequestBody ClassExamOption classExamOption){
classExamService.updateClassExamOption(classExamOption);
return R.ok();
}
//删除题目
@RequestMapping("/delClassExamSubject")
public R delClassExamSubject(@RequestBody Map<String,Object> params){
classExamService.delClassExamSubject(params);
return R.ok();
}
//删除课程下全部题目
@RequestMapping("/delSubjectByCourseId")
public R delSubjectByCourseId(@RequestBody Map<String,Object> params){
classExamService.delSubjectByCourseId(params);
return R.ok();
}
//删除选项
@RequestMapping("/delClassExamOption")
public R delClassExamOption(@RequestBody Map<String,Object> params){
classExamService.delClassExamOption(params);
return R.ok();
}
}