浏览代码

Merge remote-tracking branch 'origin/dev' into dev

wangliang 4 年之前
父节点
当前提交
7c6b4b5cb2

+ 12 - 10
themis-business/src/main/java/com/qmth/themis/business/cache/bean/ExamStudentAnswerCacheBean.java

@@ -17,13 +17,13 @@ public class ExamStudentAnswerCacheBean implements Serializable {
 	private Integer subIndex;
 
 	// 作答内容
-	private String content;
+	private String answer;
 
 	// 版本时间戳
 	private Long version;
 
 	// 作答时长 秒
-	private Integer duration;
+	private Integer durationSeconds;
 	
 	//得分
 	private Double score;
@@ -31,12 +31,13 @@ public class ExamStudentAnswerCacheBean implements Serializable {
 	//是否客观题
 	private Boolean objective;
 
-	public String getContent() {
-		return content;
+
+	public String getAnswer() {
+		return answer;
 	}
 
-	public void setContent(String content) {
-		this.content = content;
+	public void setAnswer(String answer) {
+		this.answer = answer;
 	}
 
 	public Long getVersion() {
@@ -47,12 +48,13 @@ public class ExamStudentAnswerCacheBean implements Serializable {
 		this.version = version;
 	}
 
-	public Integer getDuration() {
-		return duration;
+
+	public Integer getDurationSeconds() {
+		return durationSeconds;
 	}
 
-	public void setDuration(Integer duration) {
-		this.duration = duration;
+	public void setDurationSeconds(Integer durationSeconds) {
+		this.durationSeconds = durationSeconds;
 	}
 
 	public Integer getMainNumber() {

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

@@ -317,16 +317,16 @@ public class TEExamServiceImpl extends ServiceImpl<TEExamMapper, TEExam> impleme
             answerCache.setMainNumber(mainNumber);
             answerCache.setSubNumber(subNumber);
             answerCache.setSubIndex(subIndex);
-            answerCache.setContent(answer);
+            answerCache.setAnswer(answer);
             answerCache.setVersion(version);
-            answerCache.setDuration(durationSeconds);
+            answerCache.setDurationSeconds(durationSeconds);
         } else {
             if (version.longValue() < answerCache.getVersion().longValue()) {
                 throw new BusinessException("版本时间戳过期");
             }
-            answerCache.setContent(answer);
+            answerCache.setAnswer(answer);
             answerCache.setVersion(version);
-            answerCache.setDuration(durationSeconds);
+            answerCache.setDurationSeconds(durationSeconds);
         }
         // 每次提交,清空得分
         answerCache.setScore(null);

+ 59 - 5
themis-business/src/main/java/com/qmth/themis/business/service/impl/TOeExamRecordServiceImpl.java

@@ -1,15 +1,19 @@
 package com.qmth.themis.business.service.impl;
 
+import java.io.File;
 import java.math.BigDecimal;
+import java.text.SimpleDateFormat;
 import java.util.Date;
 import java.util.HashMap;
 import java.util.Map;
 import java.util.Objects;
+import java.util.UUID;
 
 import javax.annotation.Resource;
 
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+import org.springframework.beans.BeanUtils;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 
@@ -19,18 +23,25 @@ import com.qmth.themis.business.cache.ExamRecordCacheUtil;
 import com.qmth.themis.business.cache.RedisKeyHelper;
 import com.qmth.themis.business.cache.bean.ExamCacheBean;
 import com.qmth.themis.business.cache.bean.ExamStudentAnswerCacheBean;
+import com.qmth.themis.business.cache.bean.ExamStudentPaperStructCacheBean;
 import com.qmth.themis.business.cache.bean.ObjectiveAnswerCacheBean;
+import com.qmth.themis.business.config.SystemConfig;
+import com.qmth.themis.business.constant.SpringContextHolder;
 import com.qmth.themis.business.constant.SystemConstant;
 import com.qmth.themis.business.dao.TOeExamRecordMapper;
 import com.qmth.themis.business.dto.response.TEExamUnFinishDto;
+import com.qmth.themis.business.entity.TOeExamAnswer;
 import com.qmth.themis.business.entity.TOeExamRecord;
 import com.qmth.themis.business.enums.ExamRecordStatusEnum;
 import com.qmth.themis.business.enums.ObjectiveScorePolicyEnum;
 import com.qmth.themis.business.service.TEExamPaperService;
 import com.qmth.themis.business.service.TEExamService;
+import com.qmth.themis.business.service.TOeExamAnswerService;
 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.contanst.Constants;
+import com.qmth.themis.common.util.FileUtil;
 import com.qmth.themis.common.util.SimpleBeanUtil;
 
 /**
@@ -44,7 +55,7 @@ import com.qmth.themis.common.util.SimpleBeanUtil;
 public class TOeExamRecordServiceImpl extends ServiceImpl<TOeExamRecordMapper, TOeExamRecord> implements TOeExamRecordService {
 	private final static Logger log = LoggerFactory.getLogger(TOeExamRecordServiceImpl.class);
 	
-	
+	private SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
     @Resource
     TOeExamRecordMapper tOeExamRecordMapper;
 
@@ -59,6 +70,12 @@ public class TOeExamRecordServiceImpl extends ServiceImpl<TOeExamRecordMapper, T
 
     @Resource
     TEExamService teExamService;
+    
+    @Resource
+    SystemConfig systemConfig;
+    
+    @Resource
+    TOeExamAnswerService examAnswerService;
 
     /**
      * 获取考试未完列表
@@ -160,7 +177,7 @@ public class TOeExamRecordServiceImpl extends ServiceImpl<TOeExamRecordMapper, T
         }
 
         if (cb.getStructType().intValue() == 1) {
-            if (checkSingleChoice(answer.getContent(), cb.getChoiceAnswer())) {
+            if (checkSingleChoice(answer.getAnswer(), cb.getChoiceAnswer())) {
                 answer.setScore(cb.getScore());
             } else {
                 answer.setScore(0.0);
@@ -168,7 +185,7 @@ public class TOeExamRecordServiceImpl extends ServiceImpl<TOeExamRecordMapper, T
         }
         if (cb.getStructType().intValue() == 2) {
             ExamCacheBean ec = examService.getExamCacheBean(ExamRecordCacheUtil.getExamId(recordId));
-            int r = checkMultipleChoice(answer.getContent(), cb.getChoiceAnswer());
+            int r = checkMultipleChoice(answer.getAnswer(), cb.getChoiceAnswer());
             if (ec.getObjectiveScorePolicy().equals(ObjectiveScorePolicyEnum.EQUAL)) {// 全对给分
                 if (r == 1) {
                     answer.setScore(cb.getScore());
@@ -188,7 +205,7 @@ public class TOeExamRecordServiceImpl extends ServiceImpl<TOeExamRecordMapper, T
             }
         }
         if (cb.getStructType().intValue() == 3) {
-            if (Boolean.parseBoolean(answer.getContent()) == cb.getBoolAnswer().booleanValue()) {
+            if (Boolean.parseBoolean(answer.getAnswer()) == cb.getBoolAnswer().booleanValue()) {
                 answer.setScore(cb.getScore());
             } else {
                 answer.setScore(0.0);
@@ -272,6 +289,7 @@ public class TOeExamRecordServiceImpl extends ServiceImpl<TOeExamRecordMapper, T
 	/**
 	 *考试记录数据持久化
 	 */
+	@SuppressWarnings("unchecked")
 	@Override
 	public void persisted(Long recordId) {
 		String lockKey = SystemConstant.REDIS_LOCK_EXAM_RECORD_PERSISTED_PREFIX + recordId;
@@ -279,10 +297,46 @@ public class TOeExamRecordServiceImpl extends ServiceImpl<TOeExamRecordMapper, T
         if (!lock) {
         	log.debug("persisted doing,request ignore");
         }
+        String tempDir = systemConfig.getProperty("sys.config.tempDataDir");
+        String dir = tempDir + "/" + uuid() + "/";
+        File dfile = new File(dir);
         try {
-        	//TODO
+            dfile.mkdirs();
+            String structFilePath = sdf.format(new Date()) + "/" + uuid() + ".json";
+	        
+        	//保存考试记录
+        	Map<String,Object> record=redisUtil.getHashEntries(RedisKeyHelper.examRecordCacheKey(recordId));
+        	TOeExamRecord er=SimpleBeanUtil.mapToObject(record, TOeExamRecord.class);
+        	er.setStatus(ExamRecordStatusEnum.PERSISTED);
+        	er.setPaperStruct(structFilePath);
+        	er.setPaperStructUpload(1);
+        	saveOrUpdate(er);
+        	//保存作答
+        	Map<String,ExamStudentAnswerCacheBean> answerMap=redisUtil.getHashEntries(RedisKeyHelper.examAnswerKey(recordId));
+        	for(ExamStudentAnswerCacheBean answerCache:answerMap.values()) {
+        		TOeExamAnswer answer=new TOeExamAnswer();
+        		BeanUtils.copyProperties(answerCache, answer);
+        		answer.setId(Constants.idGen.next());
+        		answer.setExamRecordId(recordId);
+        		examAnswerService.saveOrUpdate(answer);
+        	}
+        	//上传个人试卷结构
+            ExamStudentPaperStructCacheBean struct=(ExamStudentPaperStructCacheBean)redisUtil.get(RedisKeyHelper.studentPaperStructKey(recordId));
+            File structFile = new File(dir+"struct.json");
+            FileUtil.saveAsFile(structFile.getAbsolutePath(), struct.getContent());
+            SystemConfig systemConfig = SpringContextHolder.getBean(SystemConfig.class);
+            OssUtil.ossUpload(systemConfig.getOssEnv(3), structFilePath, structFile);
+            
+            //清除缓存
+            redisUtil.delete(RedisKeyHelper.examRecordCacheKey(recordId));
+            redisUtil.delete(RedisKeyHelper.examAnswerKey(recordId));
+            redisUtil.delete(RedisKeyHelper.audioLeftPlayCountKey(recordId));
         } finally {
             redisUtil.releaseLock(lockKey);
+            FileUtil.deleteFolder(dir);
         }
 	}
+	private String uuid() {
+        return UUID.randomUUID().toString().replaceAll("-", "");
+    }
 }

+ 23 - 0
themis-common/src/main/java/com/qmth/themis/common/util/SimpleBeanUtil.java

@@ -44,4 +44,27 @@ public class SimpleBeanUtil {
 
 		return map;
 	}
+
+	public static  <T> T mapToObject(Map<String, Object> map, Class<T> beanClass) {
+		if (map == null)
+			return null;
+
+		try {
+			T obj = beanClass.newInstance();
+
+			Field[] fields = obj.getClass().getDeclaredFields();
+			for (Field field : fields) {
+				int mod = field.getModifiers();
+				if (Modifier.isStatic(mod) || Modifier.isFinal(mod)) {
+					continue;
+				}
+
+				field.setAccessible(true);
+				field.set(obj, map.get(field.getName()));
+			}
+			return obj;
+		} catch (Exception e) {
+			throw new RuntimeException(e);
+		}
+	}
 }