PreviewService.java 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. package com.qxgmat.service.extend;
  2. import com.nuliji.tools.AbstractService;
  3. import com.nuliji.tools.Transform;
  4. import com.nuliji.tools.exception.ParameterException;
  5. import com.qxgmat.data.constants.enums.module.*;
  6. import com.qxgmat.data.dao.entity.*;
  7. import com.qxgmat.data.relation.entity.UserPreviewPaperRelation;
  8. import com.qxgmat.service.UserPaperService;
  9. import com.qxgmat.service.inline.*;
  10. import org.slf4j.Logger;
  11. import org.slf4j.LoggerFactory;
  12. import org.springframework.stereotype.Service;
  13. import javax.annotation.Resource;
  14. import java.util.*;
  15. import java.util.stream.Collectors;
  16. @Service
  17. public class PreviewService extends AbstractService {
  18. private static final Logger logger = LoggerFactory.getLogger(PreviewService.class);
  19. @Resource
  20. private PreviewPaperService previewPaperService;
  21. @Resource
  22. private UserReportService userReportService;
  23. @Resource
  24. private UserPaperService userPaperService;
  25. @Resource
  26. private UserOrderRecordService userOrderRecordService;
  27. @Resource
  28. private CourseService courseService;
  29. @Resource
  30. private CourseNoService courseNoService;
  31. @Resource
  32. private PreviewAssignService previewAssignService;
  33. @Resource
  34. private UserCourseAppointmentService userCourseAppointmentService;
  35. @Resource
  36. private UserCourseProgressService userCourseProgressService;
  37. @Resource
  38. private UserCourseService userCourseService;
  39. /**
  40. * 获取用户分组作业列表
  41. * @param userId
  42. * @param top
  43. * @return
  44. */
  45. public Map<Object, Collection<UserPreviewPaperRelation>> groupByCourseId(Integer userId, Collection recordIds, Integer top){
  46. Map<Object, Collection<UserPreviewPaperRelation>> relationMap = new HashMap<>();
  47. for(Object id : recordIds){
  48. Integer recordId = (Integer)id;
  49. relationMap.put(recordId, list(1, top, userId, recordId, null, 0));
  50. }
  51. return relationMap;
  52. }
  53. /**
  54. * 返回用户的预习作业列表
  55. * @param recordId
  56. * @param userId
  57. * @param endTime
  58. * @param times
  59. * @return
  60. */
  61. public List<UserPreviewPaperRelation> list(int page, int size, Integer recordId, Integer userId, String endTime, Integer times){
  62. // 根据不同的课程,执行不同的查询方案
  63. UserOrderRecord record = userOrderRecordService.get(recordId);
  64. if (record == null){
  65. throw new ParameterException("记录不存在");
  66. }
  67. if (!record.getUserId().equals(userId)){
  68. throw new ParameterException("记录不存在");
  69. }
  70. if (!record.getProductType().equals(ProductType.COURSE.key)){
  71. throw new ParameterException("记录不存在");
  72. }
  73. Course course = courseService.get(record.getProductId());
  74. CourseModule courseModule = CourseModule.ValueOf(course.getCourseModule());
  75. List<PreviewAssign> previewAssignList = new ArrayList<>(0);
  76. switch(courseModule){
  77. case VS:
  78. // 查询记录对应预约情况
  79. List<UserCourseAppointment> appointmentList = userCourseAppointmentService.listByRecord(recordId);
  80. Collection appointmentIds = Transform.getIds(appointmentList, UserCourseAppointment.class, "id");
  81. previewAssignList = previewAssignService.listByAppointment(page, size, course.getId(), appointmentIds, userId, endTime, times);
  82. break;
  83. case ONLINE:
  84. // 查询记录对应时间段内
  85. previewAssignList = previewAssignService.listByTime(page, size, course.getId(), record.getTimeId(), userId, endTime, times);
  86. replaceTitle(previewAssignList);
  87. break;
  88. case VIDEO:
  89. // 获取课时,并关联当前记录的paper
  90. if (endTime != null){
  91. // 无论列表还是卡片,都只显示2条
  92. size = 2;
  93. page = 1;
  94. List<CourseNo> courseNoList = courseNoService.allCourse(course.getId());
  95. // 查询课时进度
  96. List<UserCourseProgress> progressList =userCourseProgressService.listCourse(record.getId(), course.getId());
  97. Map progressMap = Transform.getMap(progressList, UserCourseProgress.class, "courseNoId");
  98. Collections.reverse(courseNoList);
  99. int min = 1;
  100. for(CourseNo no : courseNoList){
  101. // 如果进度不过半,按当前课时+下一课时
  102. // 如果进度过半,下2次课时
  103. UserCourseProgress progress = (UserCourseProgress)progressMap.get(no.getId());
  104. if(progress == null) continue;
  105. if (progress.getProgress() > 50) {
  106. min = no.getNo() + 1;
  107. }else{
  108. min = no.getNo();
  109. }
  110. break;
  111. }
  112. int max = min+size -1;
  113. int finalMin = min;
  114. List<CourseNo> select = courseNoList.stream().filter(row->row.getNo() >= finalMin && row.getNo()<=max).collect(Collectors.toList());
  115. previewAssignList = previewAssignService.listByCourseNos(course.getId(), getIds(select, CourseNo.class, "id"));
  116. }else{
  117. previewAssignList = previewAssignService.listByCourse(page, size, course.getId(), userId, times);
  118. }
  119. replaceTitle(previewAssignList);
  120. break;
  121. }
  122. Collection assignIds = Transform.getIds(previewAssignList, PreviewAssign.class, "id");
  123. // 获取详细数据
  124. List<UserPaper> list = userPaperService.listWithOrigin(userId, PaperOrigin.PREVIEW, assignIds, recordId);
  125. Collection ids = Transform.getIds(list, UserPaper.class, "id");
  126. List<UserPreviewPaperRelation> pr = Transform.convert(list, UserPreviewPaperRelation.class);
  127. // 获取最后一次作业结果
  128. List<UserReport> reportList = userReportService.listWithLater(ids);
  129. Transform.combine(pr, reportList, UserPreviewPaperRelation.class, "id", "report", UserReport.class, "paperId");
  130. return pr;
  131. }
  132. /**
  133. * 根据试卷,获取当前关联的学习记录
  134. * @param assignId
  135. * @return
  136. */
  137. public Integer getRecord(Integer userId, Integer assignId){
  138. PreviewAssign assign = previewAssignService.get(assignId);
  139. if (assign == null){
  140. throw new ParameterException("记录不存在");
  141. }
  142. Course course = courseService.get(assign.getCourseId());
  143. CourseModule courseModule = CourseModule.ValueOf(course.getCourseModule());
  144. switch(courseModule){
  145. case VS:
  146. UserCourseAppointment appointment = userCourseAppointmentService.get(assign.getCourseAppointment());
  147. return appointment.getRecordId();
  148. case ONLINE:
  149. UserOrderRecord timeRecord = userOrderRecordService.getByUserAndTime(userId, assign.getCourseId(), assign.getCourseTime());
  150. return timeRecord.getId();
  151. case VIDEO:
  152. // 获取当前该课程记录
  153. UserCourse record = userCourseService.getCourseBase(userId, assign.getCourseId());
  154. return record != null ? record.getRecordId() : 0;
  155. default:
  156. return 0;
  157. }
  158. }
  159. private void replaceTitle(List<PreviewAssign> previewAssignList){
  160. Collection previewIds = Transform.getIds(previewAssignList, PreviewAssign.class, "paperId");
  161. List<PreviewPaper> paperList = previewPaperService.select(previewIds);
  162. Map paperTitle = Transform.getMap(paperList, PreviewPaper.class, "id", "title");
  163. Transform.combine(previewAssignList, paperTitle, PreviewAssign.class, "paperId", "title");
  164. }
  165. }