ExamReserveServiceImpl.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. package com.qmth.exam.reserve.service.impl;
  2. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  3. import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
  4. import com.qmth.boot.core.exception.StatusException;
  5. import com.qmth.exam.reserve.bean.RichTextBean;
  6. import com.qmth.exam.reserve.bean.apply.ApplyTimePeriodResult;
  7. import com.qmth.exam.reserve.bean.apply.ApplyVO;
  8. import com.qmth.exam.reserve.bean.apply.TicketInfo;
  9. import com.qmth.exam.reserve.bean.apply.TimePeriodInfo;
  10. import com.qmth.exam.reserve.bean.applytask.CurrentApplyTaskVO;
  11. import com.qmth.exam.reserve.bean.login.LoginUser;
  12. import com.qmth.exam.reserve.dao.StudentApplyDao;
  13. import com.qmth.exam.reserve.entity.ApplyTaskEntity;
  14. import com.qmth.exam.reserve.entity.ExamSiteEntity;
  15. import com.qmth.exam.reserve.entity.StudentApplyEntity;
  16. import com.qmth.exam.reserve.entity.TimePeriodEntity;
  17. import com.qmth.exam.reserve.enums.ApplyStatus;
  18. import com.qmth.exam.reserve.service.*;
  19. import com.qmth.exam.reserve.util.DateUtil;
  20. import org.apache.commons.collections4.CollectionUtils;
  21. import org.apache.commons.lang3.StringUtils;
  22. import org.apache.commons.lang3.time.FastDateFormat;
  23. import org.slf4j.Logger;
  24. import org.slf4j.LoggerFactory;
  25. import org.springframework.beans.factory.annotation.Autowired;
  26. import org.springframework.stereotype.Service;
  27. import org.springframework.transaction.annotation.Transactional;
  28. import java.text.ParseException;
  29. import java.util.*;
  30. @Service
  31. public class ExamReserveServiceImpl implements ExamReserveService {
  32. private static final Logger log = LoggerFactory.getLogger(ExamReserveServiceImpl.class);
  33. @Autowired
  34. private StudentApplyService studentApplyService;
  35. @Autowired
  36. private ApplyTaskService applyTaskService;
  37. @Autowired
  38. private TimePeriodService timePeriodService;
  39. @Autowired
  40. private ExamSiteService examSiteService;
  41. @Override
  42. @Transactional
  43. public void saveStudentApply(Long studentId, Long examSiteId, Long timePeriodId) {
  44. if (examSiteId == null) {
  45. throw new StatusException("考点ID不能为空");
  46. }
  47. if (timePeriodId == null) {
  48. throw new StatusException("预约时段ID不能为空");
  49. }
  50. LambdaQueryWrapper<StudentApplyEntity> wrapper = new LambdaQueryWrapper<>();
  51. wrapper.eq(StudentApplyEntity::getStudentId, studentId);
  52. wrapper.eq(StudentApplyEntity::getExamSiteId, examSiteId);
  53. wrapper.eq(StudentApplyEntity::getTimePeriodId, timePeriodId);
  54. StudentApplyEntity existEntity = studentApplyService.getOne(wrapper);
  55. if (existEntity != null) {
  56. if (existEntity.getCancel()) {
  57. existEntity.setCancel(false);
  58. existEntity.setOperateId(studentId);
  59. existEntity.setUpdateTime(System.currentTimeMillis());
  60. studentApplyService.updateById(existEntity);
  61. }
  62. return;
  63. }
  64. // todo
  65. // 考前多少天,禁止考生自主预约(考前是指待预约时段的开始时间)
  66. // 系统自动预约“任务执行期间”不允许预约
  67. StudentApplyEntity entity = new StudentApplyEntity();
  68. entity.setStudentId(studentId);
  69. entity.setExamSiteId(examSiteId);
  70. entity.setTimePeriodId(timePeriodId);
  71. entity.setOperateId(studentId);
  72. entity.setCancel(false);
  73. studentApplyService.save(entity);
  74. }
  75. @Override
  76. @Transactional
  77. public void cancelStudentApply(Long studentId, Long applyId) {
  78. LambdaUpdateWrapper<StudentApplyEntity> updateWrapper = new LambdaUpdateWrapper<>();
  79. updateWrapper.set(StudentApplyEntity::getCancel, true);
  80. updateWrapper.set(StudentApplyEntity::getOperateId, studentId);
  81. updateWrapper.set(StudentApplyEntity::getUpdateTime, System.currentTimeMillis());
  82. updateWrapper.eq(StudentApplyEntity::getId, applyId);
  83. studentApplyService.update(updateWrapper);
  84. // todo
  85. // 考前多少天,禁止考生自主取消预约(考前是指已预约时段的开始时间)
  86. // 系统自动预约“任务执行期间”不允许取消预约
  87. }
  88. @Override
  89. public List<ApplyVO> getStudentApplyList(LoginUser student, Boolean cancel) {
  90. CurrentApplyTaskVO curApplyTask = applyTaskService.currentApplyTask(student.getOrgId());
  91. if (curApplyTask == null) {
  92. return new ArrayList<>();
  93. }
  94. StudentApplyDao baseMapper = (StudentApplyDao) studentApplyService.getBaseMapper();
  95. List<ApplyVO> list = baseMapper.getStudentApplyList(student.getId(), cancel);
  96. if (CollectionUtils.isEmpty(list)) {
  97. return new ArrayList<>();
  98. }
  99. // 考前N天,禁止考生自主取消预约
  100. Date allowDate = DateUtil.changeDateAndTimeEnd(new Date(), curApplyTask.getAllowApplyCancelDays());
  101. for (ApplyVO vo : list) {
  102. vo.setShowTicket(false);
  103. vo.setAllowCancel(false);
  104. if (StringUtils.isNotEmpty(vo.getTicketNumber())) {
  105. // 准考证号已生成,则可查看
  106. vo.setShowTicket(true);
  107. }
  108. Date curDate = new Date(vo.getTimePeriodStart());
  109. if (curDate.after(allowDate)) {
  110. // “当前时段开始时间”在“允许取消时间”之后,可以取消预约
  111. vo.setAllowCancel(true);
  112. }
  113. }
  114. return list;
  115. }
  116. @Override
  117. public List<ApplyVO> getStudentApplyListForCurrent(LoginUser student) {
  118. // 默认过滤掉“已取消预约”的记录
  119. List<ApplyVO> list = this.getStudentApplyList(student, false);
  120. Date now = new Date();
  121. List<ApplyVO> newList = new ArrayList<>();
  122. for (ApplyVO vo : list) {
  123. Date curDate = new Date(vo.getTimePeriodEnd());
  124. if (curDate.before(now)) {
  125. // “当前时段结束时间”在“当前时间”之前,则过滤掉
  126. continue;
  127. }
  128. newList.add(vo);
  129. }
  130. return newList;
  131. }
  132. @Override
  133. public TicketInfo getApplyTicket(Long studentId, Long applyId) {
  134. StudentApplyDao baseMapper = (StudentApplyDao) studentApplyService.getBaseMapper();
  135. TicketInfo info = baseMapper.getStudentApplyTicket(applyId);
  136. if (info == null || info.getCancel()) {
  137. throw new StatusException("准考证信息不存在");
  138. }
  139. if (!info.getStudentId().equals(studentId)) {
  140. throw new StatusException("准考证信息不匹配");
  141. }
  142. if (StringUtils.isEmpty(info.getTicketNumber())) {
  143. throw new StatusException("准考证信息尚未生成");
  144. }
  145. return info;
  146. }
  147. @Override
  148. public List<String> getApplyDateList(LoginUser student) {
  149. // 日期格式:yyyyMMdd
  150. List<String> dateList = new ArrayList<>();
  151. CurrentApplyTaskVO curApplyTask = applyTaskService.currentApplyTask(student.getOrgId());
  152. if (curApplyTask == null) {
  153. return dateList;
  154. }
  155. LambdaQueryWrapper<TimePeriodEntity> wrapper = new LambdaQueryWrapper<>();
  156. wrapper.select(TimePeriodEntity::getStartTime);// 只查询startTime等字段
  157. wrapper.eq(TimePeriodEntity::getApplyTaskId, curApplyTask.getTaskId());
  158. List<TimePeriodEntity> timePeriods = timePeriodService.list(wrapper);
  159. if (CollectionUtils.isEmpty(timePeriods)) {
  160. return dateList;
  161. }
  162. // 考前N天,禁止考生自主预约
  163. Date allowDate = DateUtil.changeDateAndTimeEnd(new Date(), curApplyTask.getAllowApplyDays());
  164. Set<String> dates = new HashSet<>();
  165. FastDateFormat fdf = FastDateFormat.getInstance("yyyyMMdd");
  166. for (TimePeriodEntity timePeriod : timePeriods) {
  167. Date curDate = new Date(timePeriod.getStartTime());
  168. if (curDate.before(allowDate)) {
  169. // 跳过过期时段,“当前时段开始时间”在“允许预约时间”之前,则禁止预约
  170. continue;
  171. }
  172. dates.add(fdf.format(curDate));
  173. }
  174. dateList.addAll(dates);
  175. Collections.sort(dateList);
  176. return dateList;
  177. }
  178. @Override
  179. public ApplyTimePeriodResult getApplyTimePeriodList(LoginUser student, Long examSiteId, String date) {
  180. if (examSiteId == null) {
  181. throw new StatusException("考点ID不能为空");
  182. }
  183. if (StringUtils.isEmpty(date)) {
  184. throw new StatusException("预约日期不能为空");
  185. }
  186. Date startTime, endTime;
  187. try {
  188. FastDateFormat fdf = FastDateFormat.getInstance("yyyyMMddHHmmss");
  189. startTime = fdf.parse(date + "000000");
  190. endTime = fdf.parse(date + "235959");
  191. } catch (ParseException e) {
  192. log.error(e.getMessage(), e);
  193. throw new StatusException("预约日期值的格式不正确");
  194. }
  195. CurrentApplyTaskVO curApplyTask = applyTaskService.currentApplyTask(student.getOrgId());
  196. if (curApplyTask == null) {
  197. throw new StatusException("当前预约任务不存在");
  198. }
  199. LambdaQueryWrapper<TimePeriodEntity> wrapper = new LambdaQueryWrapper<>();
  200. wrapper.select(TimePeriodEntity::getId, TimePeriodEntity::getStartTime, TimePeriodEntity::getEndTime);
  201. wrapper.eq(TimePeriodEntity::getApplyTaskId, curApplyTask.getTaskId());
  202. wrapper.between(TimePeriodEntity::getStartTime, startTime.getTime(), endTime.getTime());
  203. List<TimePeriodEntity> timePeriods = timePeriodService.list(wrapper);
  204. List<TimePeriodInfo> periodList = new ArrayList<>();
  205. if (CollectionUtils.isNotEmpty(timePeriods)) {
  206. for (TimePeriodEntity timePeriod : timePeriods) {
  207. TimePeriodInfo info = new TimePeriodInfo();
  208. info.setTimePeriodId(timePeriod.getId());
  209. info.setTimePeriodStart(timePeriod.getStartTime());
  210. info.setTimePeriodEnd(timePeriod.getEndTime());
  211. info.setStatus(ApplyStatus.AVAILABLE);
  212. info.setAvailableCount(1);
  213. periodList.add(info);
  214. }
  215. }
  216. // todo
  217. ApplyTimePeriodResult result = new ApplyTimePeriodResult();
  218. result.setUnApplyNumber(1);
  219. result.setPeriodList(periodList);
  220. return result;
  221. }
  222. @Override
  223. public RichTextBean getExamNotice(Long applyTaskId) {
  224. if (applyTaskId == null) {
  225. throw new StatusException("预约任务ID不能为空");
  226. }
  227. ApplyTaskEntity applyTask = applyTaskService.getById(applyTaskId);
  228. if (applyTask == null) {
  229. throw new StatusException("考试须知信息不存在");
  230. }
  231. RichTextBean bean = new RichTextBean();
  232. bean.setContent(applyTask.getNotice());
  233. return bean;
  234. }
  235. @Override
  236. public RichTextBean getExamSiteGuide(Long examSiteId) {
  237. if (examSiteId == null) {
  238. throw new StatusException("考点ID不能为空");
  239. }
  240. ExamSiteEntity examSite = examSiteService.getById(examSiteId);
  241. if (examSite == null) {
  242. throw new StatusException("考场引导信息不存在");
  243. }
  244. RichTextBean bean = new RichTextBean();
  245. bean.setContent(examSite.getGuide());
  246. return bean;
  247. }
  248. }