ExamUtilService.java 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. package com.ruoyi.web.service;
  2. import com.google.common.collect.Lists;
  3. import com.ruoyi.common.core.redis.RedisCache;
  4. import com.ruoyi.common.utils.StringUtils;
  5. import com.ruoyi.mxjb.domain.MxjbContants;
  6. import com.ruoyi.sy.domain.SyTestAnswers;
  7. import com.ruoyi.sy.domain.SyTestQuestion;
  8. import com.ruoyi.web.domain.PaperDto;
  9. import com.ruoyi.web.domain.QuestionDto;
  10. import org.apache.commons.collections.MapUtils;
  11. import org.apache.commons.collections4.CollectionUtils;
  12. import org.springframework.stereotype.Service;
  13. import java.util.ArrayList;
  14. import java.util.List;
  15. import java.util.Map;
  16. @Service
  17. public class ExamUtilService {
  18. private final RedisCache redisCache;
  19. private final CacheService cacheService;
  20. public ExamUtilService(RedisCache redisCache, CacheService cacheService) {
  21. this.redisCache = redisCache;
  22. this.cacheService = cacheService;
  23. }
  24. public String getExamineeAnswersKey(String type, Long examineeId) {
  25. return type + "EA::" + examineeId;
  26. }
  27. public void updateTempAnswerAndSave(PaperDto paperDto, Long examineeId, String examineeType) {
  28. String type = MxjbContants.getExamineeTypeKey(examineeType);
  29. String key = getExamineeAnswersKey(type, examineeId);
  30. Map<String, QuestionDto> questionDtoMap = redisCache.getCacheMap(key);
  31. if (MapUtils.isNotEmpty(questionDtoMap)) {
  32. Long lastQuestionId = questionDtoMap.get("0").getQuestionId();
  33. paperDto.getQuestions().forEach(t -> {
  34. QuestionDto dto = questionDtoMap.get(t.getQuestionId().toString());
  35. if (null != dto) {
  36. t.setAnswer(dto.getAnswer());
  37. t.setAttachments(dto.getAttachments());
  38. t.setDuration(dto.getDuration());
  39. t.setCurrent(t.getQuestionId().equals(lastQuestionId));
  40. }
  41. });
  42. } else if (CollectionUtils.isNotEmpty(paperDto.getQuestions())) {
  43. paperDto.getQuestions().get(0).setCurrent(true);
  44. }
  45. }
  46. public QuestionDto toQuestionDto(SyTestQuestion t, boolean fillParseAndAnswer, Map<Long, SyTestAnswers> questionAnswersMap) {
  47. QuestionDto dto = new QuestionDto();
  48. dto.setQuestionId(t.getQuestionId());
  49. dto.setSeq(t.getSeq());
  50. dto.setTypeId(cacheService.getQuestionType(t.getQtpye()));
  51. dto.setType(t.getQtpye());
  52. dto.setTitle(t.getTitle());
  53. dto.setScoreTotal(t.getScore());
  54. dto.setOptions(getOptions(t.getOptionA(), t.getOptionB(), t.getOptionC(), t.getOptionD(), t.getOptionE()));
  55. if (fillParseAndAnswer) {
  56. dto.setParse(t.getParse());
  57. }
  58. if (null != questionAnswersMap) {
  59. SyTestAnswers answers = questionAnswersMap.get(t.getQuestionId());
  60. if (null != answers) {
  61. dto.setDuration(answers.getDuration());
  62. setAnswerAttachemnts(dto, answers.getAnswer());
  63. } else {
  64. dto.setAnswer(StringUtils.EMPTY);
  65. dto.setAttachments(new ArrayList<>());
  66. }
  67. } else {
  68. dto.setAnswer(StringUtils.EMPTY);
  69. dto.setAttachments(new ArrayList<>());
  70. }
  71. dto.setSource(t.getQuesitonCateogry());
  72. return dto;
  73. }
  74. public List<String> getOptions(String... options) {
  75. List<String> optionList = Lists.newArrayList();
  76. for (String option : options) {
  77. if (StringUtils.isNotBlank(option)) {
  78. optionList.add(option);
  79. }
  80. }
  81. return optionList;
  82. }
  83. public void setAnswerAttachemnts(QuestionDto dto, String answer) {
  84. if (StringUtils.isNotBlank(answer)) {
  85. String[] answerArray = StringUtils.splitByWholeSeparatorPreserveAllTokens(answer, "$@");
  86. if (answerArray.length > 0) {
  87. dto.setAnswer(answerArray[0]);
  88. if (answerArray.length > 1) {
  89. List<String> attacheList = Lists.newArrayList();
  90. for (int i = 1; i < answerArray.length; i++) {
  91. if (StringUtils.isNotBlank(answerArray[i]) && !"null".equals(answerArray[i])) {
  92. attacheList.add(answerArray[i]);
  93. }
  94. }
  95. dto.setAttachments(attacheList);
  96. }
  97. }
  98. }
  99. }
  100. }