|
@@ -104,26 +104,40 @@ public class PaperServiceImpl extends ServiceImpl<PaperMapper, Paper> implements
|
|
@Transactional(rollbackFor = Exception.class)
|
|
@Transactional(rollbackFor = Exception.class)
|
|
@Override
|
|
@Override
|
|
public List<PaperQuestion> generateQuestion(String paperId) {
|
|
public List<PaperQuestion> generateQuestion(String paperId) {
|
|
- //获取规则组
|
|
|
|
- List<PaperRuleGroup> ruleGroups = paperRuleGroupService.listByPaperId(paperId);
|
|
|
|
- //获取规则明细
|
|
|
|
- List<PaperRuleDetail> ruleDetails = paperRuleDetailService.listByPaperId(paperId);
|
|
|
|
|
|
+ long start = System.currentTimeMillis();
|
|
|
|
+ //获取规则组及规则明细
|
|
|
|
+ List<PaperRuleGroup> ruleGroups = paperRuleGroupService.listGroupAndDetail(paperId);
|
|
|
|
+ //从规则组中获取规则明细
|
|
|
|
+ List<PaperRuleDetail> ruleDetails = listRuleDetail(ruleGroups);
|
|
|
|
|
|
//生成题目查询条件
|
|
//生成题目查询条件
|
|
List<QuestionConditionDTO> dtoList = generateQuestionCondition(ruleDetails);
|
|
List<QuestionConditionDTO> dtoList = generateQuestionCondition(ruleDetails);
|
|
//根据查询条件一次性获取题目,防止每个规则明细都查询一次,降低数据库负载,提高性能
|
|
//根据查询条件一次性获取题目,防止每个规则明细都查询一次,降低数据库负载,提高性能
|
|
List<Question> ruleQuestions = questionService.listQuestionByCondition(dtoList);
|
|
List<Question> ruleQuestions = questionService.listQuestionByCondition(dtoList);
|
|
|
|
|
|
- //规则明细按组id分组
|
|
|
|
- Map<String, List<PaperRuleDetail>> datailMap = ruleDetails.stream().collect(Collectors.groupingBy(PaperRuleDetail::getGroupId, Collectors.toList()));
|
|
|
|
//按每组规则生成试题
|
|
//按每组规则生成试题
|
|
- Map<String, List<Question>> groupQuestionMap = generateGroupQuestionMap(ruleGroups, datailMap, ruleQuestions);
|
|
|
|
|
|
+ Map<String, List<Question>> groupQuestionMap = generateGroupQuestionMap(ruleGroups, ruleQuestions);
|
|
//生成试卷试题
|
|
//生成试卷试题
|
|
List<PaperQuestion> paperQuestions = generatePaperQuestions(paperId, ruleGroups, groupQuestionMap);
|
|
List<PaperQuestion> paperQuestions = generatePaperQuestions(paperId, ruleGroups, groupQuestionMap);
|
|
|
|
+ log.info("生成试卷试题完成,耗时:" + (System.currentTimeMillis() - start) / 1000 + "秒");
|
|
//试卷试题排序
|
|
//试卷试题排序
|
|
return paperQuestionService.sort(paperQuestions);
|
|
return paperQuestionService.sort(paperQuestions);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * 从规则组中获取规则明细
|
|
|
|
+ *
|
|
|
|
+ * @param ruleGroups
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ public List<PaperRuleDetail> listRuleDetail(List<PaperRuleGroup> ruleGroups) {
|
|
|
|
+ List<PaperRuleDetail> ruleDetails = new ArrayList<>();
|
|
|
|
+ if (!CollectionUtils.isEmpty(ruleGroups)) {
|
|
|
|
+ ruleGroups.stream().forEach(g -> ruleDetails.addAll(g.getRuleList()));
|
|
|
|
+ }
|
|
|
|
+ return ruleDetails;
|
|
|
|
+ }
|
|
|
|
+
|
|
/**
|
|
/**
|
|
* 生成题目查询条件
|
|
* 生成题目查询条件
|
|
*
|
|
*
|
|
@@ -148,17 +162,15 @@ public class PaperServiceImpl extends ServiceImpl<PaperMapper, Paper> implements
|
|
* 按每组规则生成试题
|
|
* 按每组规则生成试题
|
|
*
|
|
*
|
|
* @param ruleGroups
|
|
* @param ruleGroups
|
|
- * @param datailMap
|
|
|
|
* @return
|
|
* @return
|
|
*/
|
|
*/
|
|
public Map<String, List<Question>> generateGroupQuestionMap(List<PaperRuleGroup> ruleGroups,
|
|
public Map<String, List<Question>> generateGroupQuestionMap(List<PaperRuleGroup> ruleGroups,
|
|
- Map<String, List<PaperRuleDetail>> datailMap,
|
|
|
|
List<Question> ruleQuestions) {
|
|
List<Question> ruleQuestions) {
|
|
long start = System.currentTimeMillis();
|
|
long start = System.currentTimeMillis();
|
|
- Map<String, List<Question>> groupQuestionMap = new HashMap<>();
|
|
|
|
|
|
+ Map<String, List<Question>> groupQuestionMap = new LinkedHashMap<>();
|
|
if (!CollectionUtils.isEmpty(ruleGroups)) {
|
|
if (!CollectionUtils.isEmpty(ruleGroups)) {
|
|
//根据规则明细,多线程并发生成试题
|
|
//根据规则明细,多线程并发生成试题
|
|
- Map<String, FutureTask<List<Question>>> resultMap = generateQuestion(ruleGroups, datailMap, ruleQuestions);
|
|
|
|
|
|
+ Map<String, FutureTask<List<Question>>> resultMap = generateQuestion(ruleGroups, ruleQuestions);
|
|
|
|
|
|
//获取试题并按分组归类
|
|
//获取试题并按分组归类
|
|
for (Map.Entry<String, FutureTask<List<Question>>> entry : resultMap.entrySet()) {
|
|
for (Map.Entry<String, FutureTask<List<Question>>> entry : resultMap.entrySet()) {
|
|
@@ -187,16 +199,13 @@ public class PaperServiceImpl extends ServiceImpl<PaperMapper, Paper> implements
|
|
* 多线程并发生成试题
|
|
* 多线程并发生成试题
|
|
*
|
|
*
|
|
* @param ruleGroups
|
|
* @param ruleGroups
|
|
- * @param datailMap
|
|
|
|
* @return
|
|
* @return
|
|
*/
|
|
*/
|
|
- public Map<String, FutureTask<List<Question>>> generateQuestion(List<PaperRuleGroup> ruleGroups, Map<String,
|
|
|
|
- List<PaperRuleDetail>> datailMap, List<Question> ruleQuestions) {
|
|
|
|
|
|
+ public Map<String, FutureTask<List<Question>>> generateQuestion(List<PaperRuleGroup> ruleGroups, List<Question> ruleQuestions) {
|
|
Map<String, FutureTask<List<Question>>> resultMap = new LinkedHashMap<>();
|
|
Map<String, FutureTask<List<Question>>> resultMap = new LinkedHashMap<>();
|
|
for (PaperRuleGroup g : ruleGroups) {
|
|
for (PaperRuleGroup g : ruleGroups) {
|
|
- List<PaperRuleDetail> detaiTmps = datailMap.get(g.getId());
|
|
|
|
//组内规则明细排序
|
|
//组内规则明细排序
|
|
- List<PaperRuleDetail> details = detaiTmps.stream().sorted(Comparator.comparing(PaperRuleDetail::getSort)).collect(Collectors.toList());
|
|
|
|
|
|
+ List<PaperRuleDetail> details = g.getRuleList().stream().sorted(Comparator.comparing(PaperRuleDetail::getSort)).collect(Collectors.toList());
|
|
for (PaperRuleDetail d : details) {
|
|
for (PaperRuleDetail d : details) {
|
|
Callable<List<Question>> genCall = () -> questionService.generateQuestionsByRule(d.getRepositoryId(),
|
|
Callable<List<Question>> genCall = () -> questionService.generateQuestionsByRule(d.getRepositoryId(),
|
|
g.getQuestionType(), d.getLevel(), d.getNum(), ruleQuestions);
|
|
g.getQuestionType(), d.getLevel(), d.getNum(), ruleQuestions);
|
|
@@ -248,14 +257,14 @@ public class PaperServiceImpl extends ServiceImpl<PaperMapper, Paper> implements
|
|
if (paper == null) {
|
|
if (paper == null) {
|
|
return null;
|
|
return null;
|
|
}
|
|
}
|
|
- List<PaperRuleGroup> groups = paperRuleGroupService.listByPaperId(paperId);
|
|
|
|
|
|
+ List<PaperRuleGroup> groups = paperRuleGroupService.listGroupAndDetail(paperId);
|
|
PaperDTO paperDTO = new PaperDTO();
|
|
PaperDTO paperDTO = new PaperDTO();
|
|
BeanUtils.copyProperties(paper, paperDTO);
|
|
BeanUtils.copyProperties(paper, paperDTO);
|
|
if (!CollectionUtils.isEmpty(groups)) {
|
|
if (!CollectionUtils.isEmpty(groups)) {
|
|
if (paper.getJoinType().intValue() == JoinType.XT.getCode()) {
|
|
if (paper.getJoinType().intValue() == JoinType.XT.getCode()) {
|
|
joinTypeOfSelect(paperId, groups);
|
|
joinTypeOfSelect(paperId, groups);
|
|
} else if (paper.getJoinType().intValue() == JoinType.SJ.getCode()) {
|
|
} else if (paper.getJoinType().intValue() == JoinType.SJ.getCode()) {
|
|
- joinTypeOfRandom(paperId, groups);
|
|
|
|
|
|
+ joinTypeOfRandom(groups);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
paperDTO.setGroupList(groups);
|
|
paperDTO.setGroupList(groups);
|
|
@@ -292,17 +301,12 @@ public class PaperServiceImpl extends ServiceImpl<PaperMapper, Paper> implements
|
|
/**
|
|
/**
|
|
* 随机方式返回
|
|
* 随机方式返回
|
|
*
|
|
*
|
|
- * @param paperId
|
|
|
|
* @param groups
|
|
* @param groups
|
|
*/
|
|
*/
|
|
- public void joinTypeOfRandom(String paperId, List<PaperRuleGroup> groups) {
|
|
|
|
- List<PaperRuleDetail> ruleDetails = paperRuleDetailService.listByPaperId(paperId);
|
|
|
|
- Map<String, List<PaperRuleDetail>> datailMap = ruleDetails.stream().collect(Collectors
|
|
|
|
- .groupingBy(PaperRuleDetail::getGroupId, Collectors.toList()));
|
|
|
|
|
|
+ public void joinTypeOfRandom(List<PaperRuleGroup> groups) {
|
|
for (PaperRuleGroup g : groups) {
|
|
for (PaperRuleGroup g : groups) {
|
|
- List<PaperRuleDetail> temps = datailMap.get(g.getId());
|
|
|
|
//组内规则明细排序
|
|
//组内规则明细排序
|
|
- List<PaperRuleDetail> details = temps.stream().sorted(Comparator.comparing(PaperRuleDetail::getSort)).collect(Collectors.toList());
|
|
|
|
|
|
+ List<PaperRuleDetail> details = g.getRuleList().stream().sorted(Comparator.comparing(PaperRuleDetail::getSort)).collect(Collectors.toList());
|
|
details.stream().forEach(detail -> {
|
|
details.stream().forEach(detail -> {
|
|
detail.setRepositoryName(repositoryService.getById(detail.getRepositoryId()).getTitle());
|
|
detail.setRepositoryName(repositoryService.getById(detail.getRepositoryId()).getTitle());
|
|
detail.setLevel_dictText(LevelType.getByCode(detail.getLevel()).getValue());
|
|
detail.setLevel_dictText(LevelType.getByCode(detail.getLevel()).getValue());
|