UserCourseAppointmentService.java 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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.constants.enums.status.DirectionStatus;
  8. import com.qxgmat.data.dao.UserCourseAppointmentMapper;
  9. import com.qxgmat.data.dao.entity.CourseNo;
  10. import com.qxgmat.data.dao.entity.UserCourseAppointment;
  11. import org.slf4j.Logger;
  12. import org.slf4j.LoggerFactory;
  13. import org.springframework.stereotype.Service;
  14. import javax.annotation.Resource;
  15. import java.util.*;
  16. @Service
  17. public class UserCourseAppointmentService extends AbstractService {
  18. private static final Logger logger = LoggerFactory.getLogger(UserCourseAppointmentService.class);
  19. @Resource
  20. private UserCourseAppointmentMapper userCourseAppointmentMapper;
  21. public Page<UserCourseAppointment> listAdmin(int page, int size, Integer recordId, Integer userId, Integer courseId, String startTime, String endTime, String order, DirectionStatus direction){
  22. Example example = new Example(UserCourseAppointment.class);
  23. if(recordId != null){
  24. example.and(
  25. example.createCriteria()
  26. .andEqualTo("recordId", recordId)
  27. );
  28. }
  29. if (userId != null){
  30. example.and(
  31. example.createCriteria()
  32. .andEqualTo("userId", userId)
  33. );
  34. }
  35. if (courseId != null){
  36. example.and(
  37. example.createCriteria()
  38. .andEqualTo("courseId", courseId)
  39. );
  40. }
  41. // 在课程结束前完成预习作业逻辑
  42. if (startTime != null){
  43. example.and(
  44. example.createCriteria()
  45. .andGreaterThanOrEqualTo("endTime", startTime)
  46. );
  47. }
  48. if (endTime != null){
  49. example.and(
  50. example.createCriteria()
  51. .andLessThanOrEqualTo("endTime", endTime)
  52. );
  53. }
  54. if(order == null || order.isEmpty()) order = "id";
  55. switch(direction){
  56. case ASC:
  57. example.orderBy(order).asc();
  58. break;
  59. case DESC:
  60. default:
  61. example.orderBy(order).desc();
  62. }
  63. return select(userCourseAppointmentMapper, example, page, size);
  64. }
  65. /**
  66. * 获取课程课时分组列表
  67. * @param recordIds
  68. * @return
  69. */
  70. public Map<Object, Collection<UserCourseAppointment>> groupByRecordId(Collection recordIds){
  71. Map<Object, Collection<UserCourseAppointment>> relationMap = new HashMap<>();
  72. Example example = new Example(UserCourseAppointment.class);
  73. example.and(
  74. example.createCriteria()
  75. .andIn("recordId", recordIds)
  76. );
  77. example.setOrderByClause("recordId asc, no asc");
  78. List<UserCourseAppointment> nos = select(userCourseAppointmentMapper, example);
  79. Collection<UserCourseAppointment> list;
  80. for(UserCourseAppointment no : nos){
  81. if (!relationMap.containsKey(no.getRecordId())){
  82. list = new ArrayList<>();
  83. relationMap.put(no.getRecordId(), list);
  84. }else{
  85. list = relationMap.get(no.getRecordId());
  86. }
  87. list.add(no);
  88. }
  89. return relationMap;
  90. }
  91. public List<UserCourseAppointment> listByRecord(Integer recordId){
  92. Example example = new Example(UserCourseAppointment.class);
  93. example.and(
  94. example.createCriteria()
  95. .andEqualTo("recordId", recordId)
  96. );
  97. return select(userCourseAppointmentMapper, example);
  98. }
  99. public UserCourseAppointment addAppointment(UserCourseAppointment appointment){
  100. List<UserCourseAppointment> list = listByRecord(appointment.getRecordId());
  101. Integer max = 0;
  102. for(UserCourseAppointment no:list){
  103. if (max < no.getNo()) max = no.getNo();
  104. }
  105. appointment.setNo(max + 1);
  106. return add(appointment);
  107. }
  108. public Boolean deleteAppointment(Number id){
  109. UserCourseAppointment in = get(id);
  110. List<UserCourseAppointment> nos = listByRecord(in.getRecordId());
  111. for(UserCourseAppointment no : nos){
  112. if (no.getNo() < in.getNo()){
  113. continue;
  114. }
  115. edit(UserCourseAppointment.builder().id(no.getId()).no(no.getNo() - 1).build());
  116. }
  117. return delete(id);
  118. }
  119. public UserCourseAppointment add(UserCourseAppointment appointment){
  120. int result = insert(userCourseAppointmentMapper, appointment);
  121. appointment = one(userCourseAppointmentMapper, appointment.getId());
  122. if(appointment == null){
  123. throw new SystemException("预约添加失败");
  124. }
  125. return appointment;
  126. }
  127. public UserCourseAppointment edit(UserCourseAppointment appointment){
  128. UserCourseAppointment in = one(userCourseAppointmentMapper, appointment.getId());
  129. if(in == null){
  130. throw new ParameterException("预约不存在");
  131. }
  132. int result = update(userCourseAppointmentMapper, appointment);
  133. return appointment;
  134. }
  135. public boolean delete(Number id){
  136. UserCourseAppointment in = one(userCourseAppointmentMapper, id);
  137. if(in == null){
  138. throw new ParameterException("预约不存在");
  139. }
  140. int result = delete(userCourseAppointmentMapper, id);
  141. return result > 0;
  142. }
  143. public UserCourseAppointment get(Number id){
  144. UserCourseAppointment in = one(userCourseAppointmentMapper, id);
  145. if(in == null){
  146. throw new ParameterException("预约不存在");
  147. }
  148. return in;
  149. }
  150. public Page<UserCourseAppointment> select(int page, int pageSize){
  151. return select(userCourseAppointmentMapper, page, pageSize);
  152. }
  153. public Page<UserCourseAppointment> select(Integer[] ids){
  154. return page(()->select(userCourseAppointmentMapper, ids), 1, ids.length);
  155. }
  156. public List<UserCourseAppointment> select(Collection ids){
  157. return select(userCourseAppointmentMapper, ids);
  158. }
  159. }