xiatian 2 жил өмнө
parent
commit
feb0be9c6e

+ 1 - 1
examcloud-core-questions-api-provider/src/main/java/cn/com/qmth/examcloud/core/questions/api/controller/PaperStructController.java

@@ -138,7 +138,7 @@ public class PaperStructController extends ControllerSupport {
     @DeleteMapping(value = "/paperStruct/{ids}")
     public ResponseEntity<Object> removePaperStruct(@PathVariable String ids) {
         List<String> paperList = Stream.of(ids.split(",")).collect(Collectors.toList());
-        paperStructRepo.deleteAll(paperStructRepo.findAllById(paperList));
+        paperStructService.removePaperStruct(paperList);
         return new ResponseEntity<>(HttpStatus.OK);
     }
 

+ 4 - 0
examcloud-core-questions-dao/src/main/java/cn/com/qmth/examcloud/core/questions/dao/RandomPaperQuestionRepo.java

@@ -1,5 +1,7 @@
 package cn.com.qmth.examcloud.core.questions.dao;
 
+import java.util.List;
+
 import org.springframework.data.mongodb.repository.MongoRepository;
 import org.springframework.data.repository.query.QueryByExampleExecutor;
 
@@ -10,4 +12,6 @@ public interface RandomPaperQuestionRepo
 
 	void deleteByRandomPaperId(String id);
 
+	List<RandomPaperQuestion> findByRandomPaperId(String id);
+
 }

+ 4 - 10
examcloud-core-questions-dao/src/main/java/cn/com/qmth/examcloud/core/questions/dao/entity/RandomPaperQuestion.java

@@ -16,9 +16,10 @@ public class RandomPaperQuestion extends MongoBaseEntity {
 	private String randomPaperId;
 
 	private Long courseId;
-
-	private Integer detailNumber;
-
+	/**
+	 * 精确结构:大题号-结构序号(1开始)-公开/非公开-难度  |  示例:1-1-true-难
+	 * 蓝图结构:大题号-一级属性id-二级属性id-公开/非公开-难度  |  示例:1-1-60efd3e85d030a52bb08b1e8-60efd3e85d030a52bb08b1e9-true-易
+	 */
 	private String key;
 
 	private List<String> questionIds;
@@ -63,12 +64,5 @@ public class RandomPaperQuestion extends MongoBaseEntity {
 		this.courseId = courseId;
 	}
 
-	public Integer getDetailNumber() {
-		return detailNumber;
-	}
-
-	public void setDetailNumber(Integer detailNumber) {
-		this.detailNumber = detailNumber;
-	}
 
 }

+ 33 - 1
examcloud-core-questions-service/src/main/java/cn/com/qmth/examcloud/core/questions/service/PaperStructService.java

@@ -16,6 +16,9 @@ import cn.com.qmth.examcloud.core.questions.dao.entity.*;
 import cn.com.qmth.examcloud.core.questions.dao.entity.dto.CoursePropertyNumberDto;
 import cn.com.qmth.examcloud.core.questions.dao.entity.dto.PaperDetailUnitStructDto;
 import cn.com.qmth.examcloud.core.questions.service.bean.dto.QuesNameDto;
+import cn.com.qmth.examcloud.support.CacheConstants;
+import cn.com.qmth.examcloud.web.redis.RedisClient;
+
 import org.apache.commons.collections.CollectionUtils;
 import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -37,6 +40,8 @@ import java.util.stream.Collectors;
  */
 @Service
 public class PaperStructService {
+	
+	private static int cacheTimeOut=2*60*60;
 
     @Autowired
     PaperStructRepo paperStructRepo;
@@ -49,6 +54,9 @@ public class PaperStructService {
 
     @Autowired
     CourseCloudService courseCloudService;
+    
+    @Autowired
+    private RedisClient redisClient;
 
     /**
      * 获取所有试卷结构(分页)
@@ -389,7 +397,10 @@ public class PaperStructService {
         paperStruct.setCreator(user.getDisplayName());
         paperStruct.setCreateTime(CommonUtils.getCurDateTime());
         paperStruct.setDetailCount(paperDetailStructs.size());
-        return paperStructRepo.save(paperStruct);
+        PaperStruct ret=paperStructRepo.save(paperStruct);
+        String key=CacheConstants.CACHE_Q_PAPER_STRUCT+paperStruct.getId();
+    	redisClient.delete(key);
+        return ret;
     }
 
     /**
@@ -463,5 +474,26 @@ public class PaperStructService {
     private QuesNameDto getQuesName(String name) {
         return new QuesNameDto(name, name);
     }
+    
+    public PaperStruct getByCache(String id) {
+    	String key=CacheConstants.CACHE_Q_PAPER_STRUCT+id;
+    	PaperStruct ps=redisClient.get(key, PaperStruct.class, cacheTimeOut);
+    	if(ps==null){
+    		ps=Model.of(paperStructRepo.findById(id));
+    		if(ps==null){
+    			throw new StatusException("未找到试卷结构:"+id);
+    		}
+    		redisClient.set(key, ps, cacheTimeOut);
+    	}
+    	return ps;
+    }
+
+	public void removePaperStruct(List<String> ids) {
+		for(String id:ids) {
+			paperStructRepo.deleteById(id);
+			String key=CacheConstants.CACHE_Q_PAPER_STRUCT+id;
+	    	redisClient.delete(key);
+		}
+	}
 
 }

+ 29 - 0
examcloud-core-questions-service/src/main/java/cn/com/qmth/examcloud/core/questions/service/cache/RandomPaperCache.java

@@ -0,0 +1,29 @@
+package cn.com.qmth.examcloud.core.questions.service.cache;
+
+import java.util.Map;
+
+import cn.com.qmth.examcloud.core.questions.dao.entity.RandomPaperQuestion;
+
+public class RandomPaperCache {
+
+	private String paperStructId;
+
+	private Map<String, RandomPaperQuestion> rpqs;
+
+	public String getPaperStructId() {
+		return paperStructId;
+	}
+
+	public void setPaperStructId(String paperStructId) {
+		this.paperStructId = paperStructId;
+	}
+
+	public Map<String, RandomPaperQuestion> getRpqs() {
+		return rpqs;
+	}
+
+	public void setRpqs(Map<String, RandomPaperQuestion> rpqs) {
+		this.rpqs = rpqs;
+	}
+
+}

+ 38 - 5
examcloud-core-questions-service/src/main/java/cn/com/qmth/examcloud/core/questions/service/impl/RandomPaperServiceImpl.java

@@ -4,6 +4,8 @@ import java.math.BigDecimal;
 import java.util.ArrayList;
 import java.util.Iterator;
 import java.util.List;
+import java.util.Map;
+import java.util.stream.Collectors;
 
 import org.apache.commons.collections4.CollectionUtils;
 import org.apache.commons.lang3.StringUtils;
@@ -43,6 +45,7 @@ import cn.com.qmth.examcloud.core.questions.dao.entity.RandomPaper;
 import cn.com.qmth.examcloud.core.questions.dao.entity.RandomPaperQuestion;
 import cn.com.qmth.examcloud.core.questions.dao.entity.dto.CoursePropertyNumberDto;
 import cn.com.qmth.examcloud.core.questions.dao.entity.dto.PaperDetailUnitStructDto;
+import cn.com.qmth.examcloud.core.questions.service.PaperStructService;
 import cn.com.qmth.examcloud.core.questions.service.RandomPaperService;
 import cn.com.qmth.examcloud.core.questions.service.bean.randompaper.PaperDetailUnitDto;
 import cn.com.qmth.examcloud.core.questions.service.bean.randompaper.QuestionDto;
@@ -54,18 +57,24 @@ import cn.com.qmth.examcloud.core.questions.service.bean.randompaper.StructInfo;
 import cn.com.qmth.examcloud.core.questions.service.bean.randompaper.StructQuestionCheckDto;
 import cn.com.qmth.examcloud.core.questions.service.bean.randompaper.StructQuestionCountInfo;
 import cn.com.qmth.examcloud.core.questions.service.bean.randompaper.StructQuestionInfo;
+import cn.com.qmth.examcloud.core.questions.service.cache.RandomPaperCache;
 import cn.com.qmth.examcloud.question.commons.core.paper.DefaultPaper;
+import cn.com.qmth.examcloud.support.CacheConstants;
 import cn.com.qmth.examcloud.support.cache.CacheHelper;
 import cn.com.qmth.examcloud.support.cache.bean.CourseCacheBean;
+import cn.com.qmth.examcloud.web.redis.RedisClient;
 
 @Service
 public class RandomPaperServiceImpl implements RandomPaperService {
-
+	private static int cacheTimeOut = 2 * 60 * 60;
 	@Autowired
 	private MongoTemplate mongoTemplate;
 	@Autowired
+	private PaperStructService paperStructService;
+	@Autowired
 	private PaperStructRepo paperStructRepo;
-
+	@Autowired
+	private RedisClient redisClient;
 	@Autowired
 	private PropertyRepo propertyRepo;
 	@Autowired
@@ -675,6 +684,8 @@ public class RandomPaperServiceImpl implements RandomPaperService {
 				}
 			}
 			randomPaperQuestionRepo.saveAll(rqs);
+			String key = CacheConstants.CACHE_Q_RANDOM_PAPER + e.getId();
+			redisClient.delete(key);
 		}
 		clearQuestionIds(ret);
 		return ret;
@@ -685,8 +696,7 @@ public class RandomPaperServiceImpl implements RandomPaperService {
 			RandomPaperQuestion rq = new RandomPaperQuestion();
 			rqs.add(rq);
 			rq.setCourseId(e.getCourseId());
-			rq.setDetailNumber(dto.getDetailNumber());
-			rq.setKey(dto.getKey());
+			rq.setKey(dto.getDetailNumber() + "-" + dto.getKey());
 			rq.setQuestionIds(dto.getQuestionIds());
 			rq.setRandomPaperId(e.getId());
 			rq.setRootOrgId(e.getRootOrgId());
@@ -712,8 +722,31 @@ public class RandomPaperServiceImpl implements RandomPaperService {
 
 	@Override
 	public DefaultPaper getRandomPaper(String randomPaperId) {
-		// TODO Auto-generated method stub
+		RandomPaperCache rp=getByCache(randomPaperId);
+		PaperStruct ps=paperStructService.getByCache(rp.getPaperStructId());
 		return null;
 	}
 
+	private RandomPaperCache getByCache(String id) {
+		String key = CacheConstants.CACHE_Q_RANDOM_PAPER + id;
+		RandomPaperCache rp = redisClient.get(key, RandomPaperCache.class, cacheTimeOut);
+		if (rp == null) {
+			rp = new RandomPaperCache();
+			RandomPaper e = Model.of(randomPaperRepo.findById(id));
+			if (e == null) {
+				throw new StatusException("未找到随机模板:" + id);
+			}
+			rp.setPaperStructId(e.getPaperStructId());
+			List<RandomPaperQuestion> rpqs = randomPaperQuestionRepo.findByRandomPaperId(id);
+			if (CollectionUtils.isEmpty(rpqs)) {
+				throw new StatusException("随机模板试题库为空:" + id);
+			}
+			Map<String, RandomPaperQuestion> map = rpqs.stream()
+					.collect(Collectors.toMap(RandomPaperQuestion::getKey, account -> account, (key1, key2) -> key2));
+			rp.setRpqs(map);
+			redisClient.set(key, rp, cacheTimeOut);
+		}
+		return rp;
+	}
+
 }