UserCourseProgressService.java 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. package com.qxgmat.service.inline;
  2. import com.github.pagehelper.Page;
  3. import com.nuliji.tools.AbstractService;
  4. import com.nuliji.tools.exception.ParameterException;
  5. import com.nuliji.tools.exception.SystemException;
  6. import com.nuliji.tools.mybatis.Example;
  7. import com.qxgmat.data.dao.UserCourseProgressMapper;
  8. import com.qxgmat.data.dao.entity.UserCourse;
  9. import com.qxgmat.data.dao.entity.UserCourseProgress;
  10. import org.slf4j.Logger;
  11. import org.slf4j.LoggerFactory;
  12. import org.springframework.stereotype.Service;
  13. import org.springframework.transaction.annotation.Transactional;
  14. import javax.annotation.Resource;
  15. import java.util.*;
  16. @Service
  17. public class UserCourseProgressService extends AbstractService {
  18. private static final Logger logger = LoggerFactory.getLogger(UserCourseProgressService.class);
  19. @Resource
  20. private UserCourseProgressMapper userCourseProgressMapper;
  21. /**
  22. * 更新课时进度
  23. * @param userId
  24. * @param courseId
  25. * @param courseNoId
  26. * @param progress
  27. * @return
  28. */
  29. @Transactional
  30. public UserCourseProgress updateProgress(Integer userId, Integer courseId, Integer courseNoId, Integer progress, Integer recordId){
  31. Example example = new Example(UserCourseProgress.class);
  32. example.and(
  33. example.createCriteria()
  34. .andEqualTo("userId", userId)
  35. .andEqualTo("recordId", recordId)
  36. .andEqualTo("courseId", courseId)
  37. .andEqualTo("courseNoId", courseNoId)
  38. );
  39. UserCourseProgress entity;
  40. UserCourseProgress in = one(userCourseProgressMapper, example);
  41. if (in != null){
  42. if (in.getProgress() <= progress) {
  43. in.setProgress(progress);
  44. entity = edit(in);
  45. }else if(in.getProgress() == 100){
  46. in.setProgress(progress);
  47. in.setTimes(in.getTimes() + 1);
  48. entity = edit(in);
  49. return entity;
  50. }else{
  51. entity = in;
  52. // 不用更新进度
  53. return entity;
  54. }
  55. }else{
  56. entity = add(UserCourseProgress.builder()
  57. .userId(userId)
  58. .recordId(recordId)
  59. .courseId(courseId)
  60. .courseNoId(courseNoId)
  61. .times(0)
  62. .progress(progress).build());
  63. }
  64. return entity;
  65. }
  66. public List<UserCourseProgress> listCourse(Integer recordId, Integer courseId){
  67. Example example = new Example(UserCourseProgress.class);
  68. example.and(
  69. example.createCriteria()
  70. .andEqualTo("recordId", recordId)
  71. .andEqualTo("courseId", courseId)
  72. );
  73. return select(userCourseProgressMapper, example);
  74. }
  75. public List<UserCourseProgress> getByRecordId(Integer recordId){
  76. Example example = new Example(UserCourseProgress.class);
  77. example.and(
  78. example.createCriteria()
  79. .andEqualTo("recordId", recordId)
  80. );
  81. return select(userCourseProgressMapper, example);
  82. }
  83. /**
  84. * 获取课程记录分组列表
  85. * @param recordIds
  86. * @return
  87. */
  88. public Map<Object, Collection<UserCourseProgress>> groupByRecordId(Collection recordIds){
  89. Map<Object, Collection<UserCourseProgress>> relationMap = new HashMap<>();
  90. if (recordIds == null || recordIds.size() == 0) return relationMap;
  91. Example example = new Example(UserCourseProgress.class);
  92. example.and(
  93. example.createCriteria()
  94. .andIn("recordId", recordIds)
  95. );
  96. List<UserCourseProgress> nos = select(userCourseProgressMapper, example);
  97. Collection<UserCourseProgress> list;
  98. for(UserCourseProgress no : nos){
  99. if (!relationMap.containsKey(no.getRecordId())){
  100. list = new ArrayList<>();
  101. relationMap.put(no.getRecordId(), list);
  102. }else{
  103. list = relationMap.get(no.getRecordId());
  104. }
  105. list.add(no);
  106. }
  107. return relationMap;
  108. }
  109. public UserCourseProgress add(UserCourseProgress progress){
  110. int result = insert(userCourseProgressMapper, progress);
  111. progress = one(userCourseProgressMapper, progress.getId());
  112. if(progress == null){
  113. throw new SystemException("统计添加失败");
  114. }
  115. return progress;
  116. }
  117. public UserCourseProgress edit(UserCourseProgress progress){
  118. UserCourseProgress in = one(userCourseProgressMapper, progress.getId());
  119. if(in == null){
  120. throw new ParameterException("统计不存在");
  121. }
  122. int result = update(userCourseProgressMapper, progress);
  123. return progress;
  124. }
  125. public boolean delete(Number id){
  126. UserCourseProgress in = one(userCourseProgressMapper, id);
  127. if(in == null){
  128. throw new ParameterException("统计不存在");
  129. }
  130. int result = delete(userCourseProgressMapper, id);
  131. return result > 0;
  132. }
  133. public UserCourseProgress get(Number id){
  134. UserCourseProgress in = one(userCourseProgressMapper, id);
  135. if(in == null){
  136. throw new ParameterException("统计不存在");
  137. }
  138. return in;
  139. }
  140. public Page<UserCourseProgress> select(int page, int pageSize){
  141. return select(userCourseProgressMapper, page, pageSize);
  142. }
  143. public Page<UserCourseProgress> select(Integer[] ids){
  144. return page(()->select(userCourseProgressMapper, ids), 1, ids.length);
  145. }
  146. public List<UserCourseProgress> select(Collection ids){
  147. return select(userCourseProgressMapper, ids);
  148. }
  149. }