UserCourseProgressService.java 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. /**
  76. * 获取课程记录分组列表
  77. * @param recordIds
  78. * @return
  79. */
  80. public Map<Object, Collection<UserCourseProgress>> groupByRecordId(Collection recordIds){
  81. Map<Object, Collection<UserCourseProgress>> relationMap = new HashMap<>();
  82. Example example = new Example(UserCourseProgress.class);
  83. example.and(
  84. example.createCriteria()
  85. .andIn("recordId", recordIds)
  86. );
  87. List<UserCourseProgress> nos = select(userCourseProgressMapper, example);
  88. Collection<UserCourseProgress> list;
  89. for(UserCourseProgress no : nos){
  90. if (!relationMap.containsKey(no.getRecordId())){
  91. list = new ArrayList<>();
  92. relationMap.put(no.getRecordId(), list);
  93. }else{
  94. list = relationMap.get(no.getRecordId());
  95. }
  96. list.add(no);
  97. }
  98. return relationMap;
  99. }
  100. public UserCourseProgress add(UserCourseProgress progress){
  101. int result = insert(userCourseProgressMapper, progress);
  102. progress = one(userCourseProgressMapper, progress.getId());
  103. if(progress == null){
  104. throw new SystemException("统计添加失败");
  105. }
  106. return progress;
  107. }
  108. public UserCourseProgress edit(UserCourseProgress progress){
  109. UserCourseProgress in = one(userCourseProgressMapper, progress.getId());
  110. if(in == null){
  111. throw new ParameterException("统计不存在");
  112. }
  113. int result = update(userCourseProgressMapper, progress);
  114. return progress;
  115. }
  116. public boolean delete(Number id){
  117. UserCourseProgress in = one(userCourseProgressMapper, id);
  118. if(in == null){
  119. throw new ParameterException("统计不存在");
  120. }
  121. int result = delete(userCourseProgressMapper, id);
  122. return result > 0;
  123. }
  124. public UserCourseProgress get(Number id){
  125. UserCourseProgress in = one(userCourseProgressMapper, id);
  126. if(in == null){
  127. throw new ParameterException("统计不存在");
  128. }
  129. return in;
  130. }
  131. public Page<UserCourseProgress> select(int page, int pageSize){
  132. return select(userCourseProgressMapper, page, pageSize);
  133. }
  134. public Page<UserCourseProgress> select(Integer[] ids){
  135. return page(()->select(userCourseProgressMapper, ids), 1, ids.length);
  136. }
  137. public List<UserCourseProgress> select(Collection ids){
  138. return select(userCourseProgressMapper, ids);
  139. }
  140. }