xiatian 4 лет назад
Родитель
Сommit
ada1a20dd5

+ 8 - 0
themis-business/src/main/java/com/qmth/themis/business/service/TEExamService.java

@@ -54,4 +54,12 @@ public interface TEExamService extends IService<TEExam> {
 	 * @return
 	 */
 	public ExamStartBean start(Long studentId, Long recordId);
+
+	/**上传个人试卷结构
+	 * @param studentId
+	 * @param recordId
+	 * @param content
+	 * @return
+	 */
+	public Long studentPaperStruct(Long studentId, Long recordId,String content);
 }

+ 201 - 159
themis-business/src/main/java/com/qmth/themis/business/service/impl/TEExamServiceImpl.java

@@ -1,9 +1,12 @@
 package com.qmth.themis.business.service.impl;
 
+import java.io.File;
+import java.text.SimpleDateFormat;
 import java.util.Date;
 import java.util.List;
 import java.util.Map;
 import java.util.Objects;
+import java.util.UUID;
 
 import javax.annotation.Resource;
 
@@ -33,6 +36,7 @@ import com.qmth.themis.business.service.TOeExamRecordService;
 import com.qmth.themis.business.util.OssUtil;
 import com.qmth.themis.business.util.RedisUtil;
 import com.qmth.themis.common.exception.BusinessException;
+import com.qmth.themis.common.util.FileUtil;
 
 /**
  * @Description: 考试批次 服务实现类
@@ -43,174 +47,174 @@ import com.qmth.themis.common.exception.BusinessException;
  */
 @Service
 public class TEExamServiceImpl extends ServiceImpl<TEExamMapper, TEExam> implements TEExamService {
+    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
+	@Resource
+	TEExamMapper teExamMapper;
 
-    @Resource
-    TEExamMapper teExamMapper;
+	@Resource
+	TEExamActivityService teExamActivityService;
 
-    @Resource
-    TEExamActivityService teExamActivityService;
+	@Resource
+	TEExamStudentService teExamStudentService;
 
-    @Resource
-    TEExamStudentService teExamStudentService;
+	@Resource
+	TEExamCourseService teExamCourseService;
 
-    @Resource
-    TEExamCourseService teExamCourseService;
+	@Resource
+	TEExamPaperService teExamPaperService;
 
-    @Resource
-    TEExamPaperService teExamPaperService;
+	@Resource
+	TOeExamRecordService toeExamRecordService;
 
-    @Resource
-    TOeExamRecordService toeExamRecordService;
+	@Resource
+	RedisUtil redisUtil;
 
-    @Resource
-    RedisUtil redisUtil;
+	@Resource
+	SystemConfig systemConfig;
 
-    @Resource
-    SystemConfig systemConfig;
+	/**
+	 * 查询考试批次
+	 *
+	 * @param iPage
+	 * @param id
+	 * @param code
+	 * @param name
+	 * @param mode
+	 * @param enable
+	 * @return
+	 */
+	@Override
+	public IPage<TEExam> examQuery(IPage<Map> iPage, Long id, String code, String name, Integer mode, Integer enable) {
+		return teExamMapper.examQuery(iPage, id, code, name, mode, enable);
+	}
 
-    /**
-     * 查询考试批次
-     *
-     * @param iPage
-     * @param id
-     * @param code
-     * @param name
-     * @param mode
-     * @param enable
-     * @return
-     */
-    @Override
-    public IPage<TEExam> examQuery(IPage<Map> iPage, Long id, String code, String name, Integer mode, Integer enable) {
-        return teExamMapper.examQuery(iPage, id, code, name, mode, enable);
-    }
+	/**
+	 * 获取考试待考列表
+	 *
+	 * @param studentId
+	 * @param examId
+	 * @param orgId
+	 * @return
+	 */
+	@Override
+	public List<Map> getWaitingExam(Long studentId, Long examId, Long orgId) {
+		List<Map> list = teExamMapper.getWaitingExam(studentId, examId, orgId);
+		if (Objects.nonNull(list) && list.size() > 0) {
+			for (int i = 0; i < list.size(); i++) {
+				Map m = list.get(i);
+				List<Map> teExamActivityList = teExamActivityService.getWaitingExam(studentId,
+						Long.parseLong(String.valueOf(m.get("id"))),
+						Long.parseLong(String.valueOf(m.get("examActivityId"))));
+				m.put("activities", teExamActivityList);
+			}
+		}
+		return list;
+	}
 
-    /**
-     * 获取考试待考列表
-     *
-     * @param studentId
-     * @param examId
-     * @param orgId
-     * @return
-     */
-    @Override
-    public List<Map> getWaitingExam(Long studentId, Long examId, Long orgId) {
-        List<Map> list = teExamMapper.getWaitingExam(studentId, examId, orgId);
-        if (Objects.nonNull(list) && list.size() > 0) {
-            for (int i = 0; i < list.size(); i++) {
-                Map m = list.get(i);
-                List<Map> teExamActivityList = teExamActivityService.getWaitingExam(studentId,
-                        Long.parseLong(String.valueOf(m.get("id"))),
-                        Long.parseLong(String.valueOf(m.get("examActivityId"))));
-                m.put("activities", teExamActivityList);
-            }
-        }
-        return list;
-    }
+	@Transactional
+	@Override
+	public ExamPrepareBean prepare(Long studentId, Long examStudentId) {
+		ExamPrepareBean ret = null;
+		ExamStudentCacheBean es = null;
+		es = (ExamStudentCacheBean) redisUtil.get(RedisKeyHelper.examStudentCacheKey(examStudentId));
+		if (es == null) {
+			es = teExamStudentService.getExamStudnetCacheBean(examStudentId);
+		}
+		if (es == null) {
+			throw new BusinessException("未找到考试");
+		}
 
-    @Transactional
-    @Override
-    public ExamPrepareBean prepare(Long studentId, Long examStudentId) {
-        ExamPrepareBean ret = null;
-        ExamStudentCacheBean es = null;
-        es = (ExamStudentCacheBean) redisUtil.get(RedisKeyHelper.examStudentCacheKey(examStudentId));
-        if (es == null) {
-            es = teExamStudentService.getExamStudnetCacheBean(examStudentId);
-        }
-        if (es == null) {
-            throw new BusinessException("未找到考试");
-        }
-
-        if (studentId.equals(es.getStudentId())) {
-            throw new BusinessException("考生Id和当前登录用户不一致");
-        }
-        if (es.getLeftExamCount() == 0) {
-            throw new BusinessException("没有剩余考试次数");
-        }
-        Long activityId = es.getExamActivityId();
-        ExamActivityCacheBean ac = teExamActivityService.getExamActivityCacheBean(activityId);
-        if (ac == null) {
-            throw new BusinessException("未找到场次");
-        }
-        Date now = new Date();
-        Long start = ac.getStartTime().getTime() - (ac.getPrepareSeconds() * 1000);
-        Long end = ac.getStartTime().getTime() + (ac.getOpeningSeconds() * 1000);
-        if (now.getTime() < start) {
-            throw new BusinessException("没有到允许开考的时间");
-        }
-        if (now.getTime() > end) {
-            throw new BusinessException("允许开考的时间已结束");
-        }
-        ExamCourseCacheBean ec = teExamCourseService.getExamCourseCacheBean(es.getExamId(), es.getCourseCode());
-        if (ec == null) {
-            throw new BusinessException("未找到考试科目");
-        }
-        if (ec.getPaperIds() == null) {
-            throw new BusinessException("考试科目未绑定试卷");
-        }
-        if (ec.getHasAnswer() == null || ec.getHasAnswer().intValue() == 0) {
-            throw new BusinessException("考试科目答案未补全");
-        }
-        // 根据权重选中试卷
-        Long paperId = ec.getPaperIds().get(getPaperByWeight(ec.getPaperWeight()));
-        ExamPaperCacheBean ep = teExamPaperService.getExamPaperCacheBean(paperId);
-        if (ep == null) {
-            throw new BusinessException("未找到试卷:" + paperId);
-        }
-        if (StringUtils.isBlank(ep.getAnswerPath())) {
-            throw new BusinessException("试卷答案未上传:" + paperId);
-        }
-
-        // 写入次数
-        Integer serialNumber = es.getCurrentSerialNumber();
-        if (serialNumber == null) {
-            serialNumber = 0;
-        }
-        es.setCurrentSerialNumber(serialNumber + 1);
-
-        Long recordId = toeExamRecordService.saveByPrepare(es.getExamId(), es.getExamActivityId(), examStudentId,
-                paperId, es.getCurrentSerialNumber());
-
-        Integer leftExamCount = es.getLeftExamCount();
-        es.setLeftExamCount(leftExamCount - 1);
-
-        es.setCurrentRecordId(recordId);
-
-        ExamPrepareBean prepare = new ExamPrepareBean();
-        prepare.setRecordId(recordId);
-        prepare.setAudioPlayTime(ep.getAudioPlayTime());
-        prepare.setHasAudio((ep.getHasAudio() == null || ep.getHasAudio().intValue() == 0 ? false : true));
-        prepare.setObjectiveShuffle(
-                (ec.getObjectiveShuffle() == null || ec.getObjectiveShuffle().intValue() == 0 ? false : true));
-        prepare.setOptionShuffle(
-                (ec.getOptionShuffle() == null || ec.getOptionShuffle().intValue() == 0 ? false : true));
-        String paperurl = OssUtil.getUrlForPrivateBucket(systemConfig.getOssEnv(3), ep.getPaperPath());
-        String structurl = OssUtil.getUrlForPrivateBucket(systemConfig.getOssEnv(3), ep.getStructPath());
-        prepare.setPaperUrl(paperurl);
-        prepare.setStructUrl(structurl);
-
-        // 更新考生缓存
-        redisUtil.set(RedisKeyHelper.examStudentCacheKey(examStudentId), es);
-
-        return ret;
-    }
+		if (studentId.equals(es.getStudentId())) {
+			throw new BusinessException("考生Id和当前登录用户不一致");
+		}
+		if (es.getLeftExamCount() == 0) {
+			throw new BusinessException("没有剩余考试次数");
+		}
+		Long activityId = es.getExamActivityId();
+		ExamActivityCacheBean ac = teExamActivityService.getExamActivityCacheBean(activityId);
+		if (ac == null) {
+			throw new BusinessException("未找到场次");
+		}
+		Date now = new Date();
+		Long start = ac.getStartTime().getTime() - (ac.getPrepareSeconds() * 1000);
+		Long end = ac.getStartTime().getTime() + (ac.getOpeningSeconds() * 1000);
+		if (now.getTime() < start) {
+			throw new BusinessException("没有到允许开考的时间");
+		}
+		if (now.getTime() > end) {
+			throw new BusinessException("允许开考的时间已结束");
+		}
+		ExamCourseCacheBean ec = teExamCourseService.getExamCourseCacheBean(es.getExamId(), es.getCourseCode());
+		if (ec == null) {
+			throw new BusinessException("未找到考试科目");
+		}
+		if (ec.getPaperIds() == null) {
+			throw new BusinessException("考试科目未绑定试卷");
+		}
+		if (ec.getHasAnswer() == null || ec.getHasAnswer().intValue() == 0) {
+			throw new BusinessException("考试科目答案未补全");
+		}
+		// 根据权重选中试卷
+		Long paperId = ec.getPaperIds().get(getPaperByWeight(ec.getPaperWeight()));
+		ExamPaperCacheBean ep = teExamPaperService.getExamPaperCacheBean(paperId);
+		if (ep == null) {
+			throw new BusinessException("未找到试卷:" + paperId);
+		}
+		if (StringUtils.isBlank(ep.getAnswerPath())) {
+			throw new BusinessException("试卷答案未上传:" + paperId);
+		}
 
-    /**
-     * 根据设定几率取出一套试卷
-     */
-    private int getPaperByWeight(List<Double> paperWeight) {
-        // 从1开始
-        double r = Math.random();
-        for (int i = 0; i < paperWeight.size(); i++) {
-            r -= paperWeight.get(i);
-            if (r <= 0.0d) {
-                return i;// 选中
-            }
-        }
-
-        return -1;
-    }
-    
-    @Transactional
+		// 写入次数
+		Integer serialNumber = es.getCurrentSerialNumber();
+		if (serialNumber == null) {
+			serialNumber = 0;
+		}
+		es.setCurrentSerialNumber(serialNumber + 1);
+
+		Long recordId = toeExamRecordService.saveByPrepare(es.getExamId(), es.getExamActivityId(), examStudentId,
+				paperId, es.getCurrentSerialNumber());
+
+		Integer leftExamCount = es.getLeftExamCount();
+		es.setLeftExamCount(leftExamCount - 1);
+
+		es.setCurrentRecordId(recordId);
+
+		ExamPrepareBean prepare = new ExamPrepareBean();
+		prepare.setRecordId(recordId);
+		prepare.setAudioPlayTime(ep.getAudioPlayTime());
+		prepare.setHasAudio((ep.getHasAudio() == null || ep.getHasAudio().intValue() == 0 ? false : true));
+		prepare.setObjectiveShuffle(
+				(ec.getObjectiveShuffle() == null || ec.getObjectiveShuffle().intValue() == 0 ? false : true));
+		prepare.setOptionShuffle(
+				(ec.getOptionShuffle() == null || ec.getOptionShuffle().intValue() == 0 ? false : true));
+		String paperurl = OssUtil.getUrlForPrivateBucket(systemConfig.getOssEnv(3), ep.getPaperPath());
+		String structurl = OssUtil.getUrlForPrivateBucket(systemConfig.getOssEnv(3), ep.getStructPath());
+		prepare.setPaperUrl(paperurl);
+		prepare.setStructUrl(structurl);
+
+		// 更新考生缓存
+		redisUtil.set(RedisKeyHelper.examStudentCacheKey(examStudentId), es);
+
+		return ret;
+	}
+
+	/**
+	 * 根据设定几率取出一套试卷
+	 */
+	private int getPaperByWeight(List<Double> paperWeight) {
+		// 从1开始
+		double r = Math.random();
+		for (int i = 0; i < paperWeight.size(); i++) {
+			r -= paperWeight.get(i);
+			if (r <= 0.0d) {
+				return i;// 选中
+			}
+		}
+
+		return -1;
+	}
+
+	@Transactional
 	@Override
 	public ExamStartBean start(Long studentId, Long recordId) {
 		ExamStartBean ret = null;
@@ -218,7 +222,8 @@ public class TEExamServiceImpl extends ServiceImpl<TEExamMapper, TEExam> impleme
 		if (er == null) {
 			throw new BusinessException("未找到考试记录");
 		}
-		ExamStudentCacheBean es = (ExamStudentCacheBean) redisUtil.get(RedisKeyHelper.examStudentCacheKey(er.getExamStudentId()));
+		ExamStudentCacheBean es = (ExamStudentCacheBean) redisUtil
+				.get(RedisKeyHelper.examStudentCacheKey(er.getExamStudentId()));
 		if (es == null) {
 			throw new BusinessException("未找到考生");
 		}
@@ -240,10 +245,10 @@ public class TEExamServiceImpl extends ServiceImpl<TEExamMapper, TEExam> impleme
 			throw new BusinessException("允许开考的时间已结束");
 		}
 
-		//修改考试记录数据
+		// 修改考试记录数据
 		er.setFirstStartTime(new Date());
 		er.setStatus(2);
-		
+
 		ExamPaperCacheBean ep = teExamPaperService.getExamPaperCacheBean(er.getPaperId());
 		if (ep == null) {
 			throw new BusinessException("未找到试卷");
@@ -258,4 +263,41 @@ public class TEExamServiceImpl extends ServiceImpl<TEExamMapper, TEExam> impleme
 
 		return ret;
 	}
+
+	
+	@Transactional
+	@Override
+	public Long studentPaperStruct(Long studentId, Long recordId,String content) {
+		ExamRecordCacheBean er = (ExamRecordCacheBean) redisUtil.get(RedisKeyHelper.examRecordCacheKey(recordId));
+		if (er == null) {
+			throw new BusinessException("未找到考试记录");
+		}
+		ExamStudentCacheBean es = (ExamStudentCacheBean) redisUtil
+				.get(RedisKeyHelper.examStudentCacheKey(er.getExamStudentId()));
+		if (es == null) {
+			throw new BusinessException("未找到考生");
+		}
+		if (studentId.equals(es.getStudentId())) {
+			throw new BusinessException("考试记录的学生Id和当前登录用户不一致");
+		}
+
+		String tempDir = systemConfig.getProperty("sys.config.tempDataDir");
+		File file=new File(tempDir+"/"+ uuid() + ".json");
+		try {
+			FileUtil.saveAsFile(file.getAbsolutePath(), content);
+			String filePath = "upload/" + sdf.format(new Date()) + "/" + uuid() + ".json";
+			er.setPaperStruct(filePath);
+			er.setPaperStructUpload(1);
+			OssUtil.ossUpload(systemConfig.getOssEnv(3), filePath, file);
+			// 更新考试记录缓存
+			redisUtil.set(RedisKeyHelper.examRecordCacheKey(recordId), er);
+			return new Date().getTime();
+		} finally {
+			FileUtil.deleteFile(file);
+		}
+	}
+	
+    private String uuid() {
+        return UUID.randomUUID().toString().replaceAll("-", "");
+    }
 }

+ 13 - 8
themis-common/src/main/java/com/qmth/themis/common/util/FileUtil.java

@@ -381,16 +381,14 @@ public class FileUtil {
 
 	public static void deleteFile(String path) {
 		File file = new File(path);
+		deleteFile(file);
+	}
+	public static void deleteFile(File file) {
 		if (file.isFile() && file.exists()) {
 			file.delete();
 		}
 	}
-
-	public static void deleteDirectory(String path) {
-		if (!path.endsWith(File.separator)) {
-			path = path + File.separator;
-		}
-		File dirFile = new File(path);
+	public static void deleteDirectory(File dirFile) {
 		if (!dirFile.exists() || !dirFile.isDirectory()) {
 			return;
 		}
@@ -398,15 +396,22 @@ public class FileUtil {
 		if (files != null) {
 			for (int i = 0; i < files.length; i++) {
 				if (files[i].isFile()) {
-					deleteFile(files[i].getAbsolutePath());
+					deleteFile(files[i]);
 				} else {
-					deleteDirectory(files[i].getAbsolutePath());
+					deleteDirectory(files[i]);
 				}
 			}
 		}
 
 		dirFile.delete();
 	}
+	public static void deleteDirectory(String path) {
+		if (!path.endsWith(File.separator)) {
+			path = path + File.separator;
+		}
+		File dirFile = new File(path);
+		deleteDirectory(dirFile);
+	}
 
 	private static Cipher initAESCipher(String sKey, String vector, int cipherMode) throws Exception {
 		byte[] raw;

+ 20 - 0
themis-exam/src/main/java/com/qmth/themis/exam/api/TEExamController.java

@@ -94,4 +94,24 @@ public class TEExamController {
 			redisUtil.releaseLock(lockKey);
 		}
 	}
+	
+	@ApiOperation(value = "上传个人试卷结构")
+	@RequestMapping(value = "/student_paper_struct", method = RequestMethod.POST)
+	@ApiResponses({ @ApiResponse(code = 200, message = "试卷信息") })
+	public Long studentPaperStruct(@ApiParam(value = "考生ID", required = true) @RequestParam Long recordId,
+			@ApiParam(value = "试卷结构json字符串", required = true) @RequestParam String content) {
+		TEStudent teStudent = (TEStudent) ServletUtil.getRequestStudentAccount();
+		String lockKey = SystemConstant.REDIS_LOCK_STUDENT_PREFIX + teStudent.getId();
+		Boolean lock = redisUtil.lock(lockKey, SystemConstant.REDIS_CACHE_TIME_OUT);
+		if (!lock) {
+			throw new BusinessException(ExceptionResultEnum.REQUEST_AWAIT);
+		}
+		try {
+			return teExamService.studentPaperStruct(teStudent.getId(), recordId,content);
+		} finally {
+			redisUtil.releaseLock(lockKey);
+		}
+	}
+	
+	
 }