|
@@ -3,17 +3,18 @@ package com.gis.service.impl;
|
|
import com.gis.common.util.Result;
|
|
import com.gis.common.util.Result;
|
|
import com.gis.domain.po.AnswerEntity;
|
|
import com.gis.domain.po.AnswerEntity;
|
|
import com.gis.domain.po.QuestionEntity;
|
|
import com.gis.domain.po.QuestionEntity;
|
|
|
|
+import com.gis.mapper.AnswerMapper;
|
|
import com.gis.mapper.IBaseMapper;
|
|
import com.gis.mapper.IBaseMapper;
|
|
import com.gis.mapper.QuestionMapper;
|
|
import com.gis.mapper.QuestionMapper;
|
|
import com.gis.service.AnswerService;
|
|
import com.gis.service.AnswerService;
|
|
import com.gis.service.QuestionService;
|
|
import com.gis.service.QuestionService;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
import java.util.List;
|
|
-import java.util.Random;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -30,45 +31,82 @@ public class QuestionServiceImpl extends IBaseServiceImpl<QuestionEntity, Long>
|
|
@Autowired
|
|
@Autowired
|
|
AnswerService answerService;
|
|
AnswerService answerService;
|
|
|
|
|
|
|
|
+ @Autowired
|
|
|
|
+ AnswerMapper answerMapper;
|
|
|
|
+
|
|
@Override
|
|
@Override
|
|
public IBaseMapper<QuestionEntity, Long> getBaseMapper() {
|
|
public IBaseMapper<QuestionEntity, Long> getBaseMapper() {
|
|
return this.entityMapper;
|
|
return this.entityMapper;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
- @Override
|
|
|
|
- public Result<List<QuestionEntity>> getQuestion() {
|
|
|
|
|
|
+// @Override
|
|
|
|
+// public Result<List<QuestionEntity>> getQuestion() {
|
|
|
|
+//
|
|
|
|
+// // 获取所有题目id
|
|
|
|
+// List<Integer> allIds = entityMapper.getIds();
|
|
|
|
+//
|
|
|
|
+// //要随机取的元素个数
|
|
|
|
+// Random random = new Random();
|
|
|
|
+// int size = 10;
|
|
|
|
+//
|
|
|
|
+// // 定义一个跟all一样长的数字,默认为false, 用来防重的
|
|
|
|
+// boolean r[]=new boolean[allIds.size()];
|
|
|
|
+//
|
|
|
|
+// int n = 0;
|
|
|
|
+//
|
|
|
|
+// List<QuestionEntity> result = new ArrayList<>();
|
|
|
|
+//
|
|
|
|
+// while (true) {
|
|
|
|
+// int temp = random.nextInt(allIds.size());
|
|
|
|
+// if(!r[temp]){
|
|
|
|
+// if (n == size) {
|
|
|
|
+// break;
|
|
|
|
+// }
|
|
|
|
+// QuestionEntity entity = this.findById(Long.valueOf(allIds.get(temp)));
|
|
|
|
+// List<AnswerEntity> answers = answerService.findByQuestionId(entity.getId());
|
|
|
|
+// entity.setAnswers(answers);
|
|
|
|
+// result.add(entity);
|
|
|
|
+//// System.out.println("得到的第" + n +"个随du机数为:" + temp + " rtemp:"+r[temp] );
|
|
|
|
+// n ++;
|
|
|
|
+// // 修改下标值
|
|
|
|
+// r[temp] = true;
|
|
|
|
+// }
|
|
|
|
+// }
|
|
|
|
+// log.info("返回问题数量: {}", result.size());
|
|
|
|
+// return Result.success(result);
|
|
|
|
+//
|
|
|
|
+//
|
|
|
|
+// }
|
|
|
|
|
|
- // 获取所有题目id
|
|
|
|
- List<Integer> allIds = entityMapper.getIds();
|
|
|
|
|
|
+ @Override
|
|
|
|
+ public Result<List<QuestionEntity>> getQuestion(Integer type, Integer pcs) {
|
|
|
|
|
|
- //要随机取的元素个数
|
|
|
|
- Random random = new Random();
|
|
|
|
- int size = 10;
|
|
|
|
|
|
+ // 随机获取10条问题
|
|
|
|
+ List<Integer> randomIds = entityMapper.getRandomIds(type, pcs);
|
|
|
|
|
|
- // 定义一个跟all一样长的数字,默认为false, 用来防重的
|
|
|
|
- boolean r[]=new boolean[allIds.size()];
|
|
|
|
|
|
+ // list to sting 以逗号分隔
|
|
|
|
+ String join = StringUtils.join(randomIds.toArray(),",");
|
|
|
|
|
|
- int n = 0;
|
|
|
|
|
|
+ // 获取问题、答案
|
|
|
|
+ List<QuestionEntity> questions = this.findByIds(join);
|
|
|
|
+ List<AnswerEntity> answers = answerService.findByQuestionIds(randomIds);
|
|
|
|
|
|
|
|
+ // 整合问题、答案
|
|
List<QuestionEntity> result = new ArrayList<>();
|
|
List<QuestionEntity> result = new ArrayList<>();
|
|
-
|
|
|
|
- while (true) {
|
|
|
|
- int temp = random.nextInt(allIds.size());
|
|
|
|
- if(!r[temp]){
|
|
|
|
- if (n == size) {
|
|
|
|
- break;
|
|
|
|
|
|
+ for (QuestionEntity question : questions) {
|
|
|
|
+ List<AnswerEntity> ans = new ArrayList<>();
|
|
|
|
+ for (AnswerEntity answer : answers) {
|
|
|
|
+ // 答案分组
|
|
|
|
+ if (question.getId() == answer.getQuestionId()) {
|
|
|
|
+ ans.add(answer);
|
|
}
|
|
}
|
|
- QuestionEntity entity = this.findById(Long.valueOf(allIds.get(temp)));
|
|
|
|
- List<AnswerEntity> answers = answerService.findByQuestionId(entity.getId());
|
|
|
|
- entity.setAnswers(answers);
|
|
|
|
- result.add(entity);
|
|
|
|
-// System.out.println("得到的第" + n +"个随du机数为:" + temp + " rtemp:"+r[temp] );
|
|
|
|
- n ++;
|
|
|
|
- // 修改下标值
|
|
|
|
- r[temp] = true;
|
|
|
|
}
|
|
}
|
|
|
|
+ question.setAnswers(ans);
|
|
|
|
+ result.add(question);
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+
|
|
log.info("返回问题数量: {}", result.size());
|
|
log.info("返回问题数量: {}", result.size());
|
|
return Result.success(result);
|
|
return Result.success(result);
|
|
|
|
|