//设置返回答题配置和派奖配置
AnswerInfoDTO answerDTO = new AnswerInfoDTO();
McCampaignAnswerModel answerModel = answerRepository.findBySchemaId(schemaId).orElseThrow(ErrorCode.NO_RECORD::toException);
BeanUtils.copyProperties(answerModel, answerDTO);
//设置返回题目
List<McCampaignQuestionModel> questions = new ArrayList<>();
//解析题库id集合
if (answerModel.getQuestionForm().equals("2")) {
Arrays.stream(StringUtils.split(answerModel.getQuestionIds(), "\\|")).mapToInt(Integer::valueOf).forEach(id -> {
//如果存在该题目,就添加到返回列表中
questionRepository.findById(id).ifPresent(questions::add);
});
}
else
{
String[] strarr = answerModel.getQuestionIds().split("\\|");//拆分成字符串数组
Random random = new Random();//随机对象
List<String> list = new ArrayList<>();
int i = 0;
while (i < answerModel.getTitleNum()) {
int num = random.nextInt(strarr.length);
if (!list.contains(strarr[num])) {
list.add(strarr[num]);
i++;
}
}
for (int j = 0; j < list.size(); j++) {
int inputquestionid =Integer.parseInt(list.get(j));
McCampaignQuestionModel mcquestion= questionRepository.findById(inputquestionid).orElseThrow(ErrorCode.NO_RECORD::toException);
questions.add(mcquestion);
}
int n = random.nextInt(strarr.length);//随机一个整数
}
List<AnswerQuestionDTO> answerPacks = new ArrayList<>();
questions.forEach(question -> {
String[] str={question.getCorrectAnswer(),question.getWrongAnswer1(),question.getWrongAnswer2(),question.getWrongAnswer3(),question.getWrongAnswer4(),question.getWrongAnswer5()};
int k = 0;
Random randomques = new Random();//随机对象
List<String> listRandom = new ArrayList<String>();
List<Integer> intRandom = new ArrayList<Integer>();
while (k < str.length) {
int numques = randomques.nextInt(str.length);
if (str[numques] !=null) {
if (!listRandom.contains(str[numques])) {
listRandom.add(str[numques]);
k++;
}
}
else
{
if (!intRandom.contains(numques)) {
intRandom.add(numques);
listRandom.add(str[numques]);
k++;
}
}
}
AnswerPackDTO answerPackDTO = new AnswerPackDTO();
AnswerPackDTO answerPackDTO1 = new AnswerPackDTO();
AnswerPackDTO answerPackDTO2 = new AnswerPackDTO();
AnswerPackDTO answerPackDTO3 = new AnswerPackDTO();
AnswerPackDTO answerPackDTO4 = new AnswerPackDTO();
AnswerPackDTO answerPackDTO5 = new AnswerPackDTO();
List<AnswerPackDTO> listanswers = new ArrayList<>();
answerPackDTO.setValue(listRandom.get(0));
answerPackDTO1.setValue(listRandom.get(1));
answerPackDTO2.setValue(listRandom.get(2));
answerPackDTO3.setValue(listRandom.get(3));
answerPackDTO4.setValue(listRandom.get(4));
answerPackDTO5.setValue(listRandom.get(5));
listanswers.add(answerPackDTO);
listanswers.add(answerPackDTO1);
listanswers.add(answerPackDTO2);
listanswers.add(answerPackDTO3);
listanswers.add(answerPackDTO4);
listanswers.add(answerPackDTO5);
AnswerQuestionDTO answerQuestionDTO =new AnswerQuestionDTO();
answerQuestionDTO.setAnswers(listanswers);
answerQuestionDTO.setCorrectAnswer(question.getCorrectAnswer());
answerQuestionDTO.setTitle(question.getTitle());
answerQuestionDTO.setAnswerNum(question.getAnswerNum());
answerQuestionDTO.setId(question.getId());
answerQuestionDTO.setQesImg(question.getQesImg());
answerQuestionDTO.setType(question.getType());
answerPacks.add(answerQuestionDTO);
});
answerDTO.setQuestionsPack(answerPacks);