package com.qmth.exam.reserve.service.impl; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; import com.qmth.boot.core.exception.StatusException; import com.qmth.exam.reserve.bean.RichTextBean; import com.qmth.exam.reserve.bean.apply.ApplyTimePeriodResult; import com.qmth.exam.reserve.bean.apply.ApplyVO; import com.qmth.exam.reserve.bean.apply.TicketInfo; import com.qmth.exam.reserve.bean.apply.TimePeriodInfo; import com.qmth.exam.reserve.bean.applytask.CurrentApplyTaskVO; import com.qmth.exam.reserve.bean.login.LoginUser; import com.qmth.exam.reserve.dao.StudentApplyDao; import com.qmth.exam.reserve.entity.ApplyTaskEntity; import com.qmth.exam.reserve.entity.ExamSiteEntity; import com.qmth.exam.reserve.entity.StudentApplyEntity; import com.qmth.exam.reserve.entity.TimePeriodEntity; import com.qmth.exam.reserve.enums.ApplyStatus; import com.qmth.exam.reserve.service.*; import com.qmth.exam.reserve.util.DateUtil; import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.time.FastDateFormat; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.text.ParseException; import java.util.*; @Service public class ExamReserveServiceImpl implements ExamReserveService { private static final Logger log = LoggerFactory.getLogger(ExamReserveServiceImpl.class); @Autowired private StudentApplyService studentApplyService; @Autowired private ApplyTaskService applyTaskService; @Autowired private TimePeriodService timePeriodService; @Autowired private ExamSiteService examSiteService; @Override @Transactional public void saveStudentApply(Long studentId, Long examSiteId, Long timePeriodId) { if (examSiteId == null) { throw new StatusException("考点ID不能为空"); } if (timePeriodId == null) { throw new StatusException("预约时段ID不能为空"); } LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>(); wrapper.eq(StudentApplyEntity::getStudentId, studentId); wrapper.eq(StudentApplyEntity::getExamSiteId, examSiteId); wrapper.eq(StudentApplyEntity::getTimePeriodId, timePeriodId); StudentApplyEntity existEntity = studentApplyService.getOne(wrapper); if (existEntity != null) { if (existEntity.getCancel()) { existEntity.setCancel(false); existEntity.setOperateId(studentId); existEntity.setUpdateTime(System.currentTimeMillis()); studentApplyService.updateById(existEntity); } return; } // todo // 考前多少天,禁止考生自主预约(考前是指待预约时段的开始时间) // 系统自动预约“任务执行期间”不允许预约 StudentApplyEntity entity = new StudentApplyEntity(); entity.setStudentId(studentId); entity.setExamSiteId(examSiteId); entity.setTimePeriodId(timePeriodId); entity.setOperateId(studentId); entity.setCancel(false); studentApplyService.save(entity); } @Override @Transactional public void cancelStudentApply(Long studentId, Long applyId) { LambdaUpdateWrapper updateWrapper = new LambdaUpdateWrapper<>(); updateWrapper.set(StudentApplyEntity::getCancel, true); updateWrapper.set(StudentApplyEntity::getOperateId, studentId); updateWrapper.set(StudentApplyEntity::getUpdateTime, System.currentTimeMillis()); updateWrapper.eq(StudentApplyEntity::getId, applyId); studentApplyService.update(updateWrapper); // todo // 考前多少天,禁止考生自主取消预约(考前是指已预约时段的开始时间) // 系统自动预约“任务执行期间”不允许取消预约 } @Override public List getStudentApplyList(LoginUser student, Boolean cancel) { CurrentApplyTaskVO curApplyTask = applyTaskService.currentApplyTask(student.getOrgId()); if (curApplyTask == null) { return new ArrayList<>(); } StudentApplyDao baseMapper = (StudentApplyDao) studentApplyService.getBaseMapper(); List list = baseMapper.getStudentApplyList(student.getId(), cancel); if (CollectionUtils.isEmpty(list)) { return new ArrayList<>(); } // 考前N天,禁止考生自主取消预约 Date allowDate = DateUtil.changeDateAndTimeEnd(new Date(), curApplyTask.getAllowApplyCancelDays()); for (ApplyVO vo : list) { vo.setShowTicket(false); vo.setAllowCancel(false); if (StringUtils.isNotEmpty(vo.getTicketNumber())) { // 准考证号已生成,则可查看 vo.setShowTicket(true); } Date curDate = new Date(vo.getTimePeriodStart()); if (curDate.after(allowDate)) { // “当前时段开始时间”在“允许取消时间”之后,可以取消预约 vo.setAllowCancel(true); } } return list; } @Override public List getStudentApplyListForCurrent(LoginUser student) { // 默认过滤掉“已取消预约”的记录 List list = this.getStudentApplyList(student, false); Date now = new Date(); List newList = new ArrayList<>(); for (ApplyVO vo : list) { Date curDate = new Date(vo.getTimePeriodEnd()); if (curDate.before(now)) { // “当前时段结束时间”在“当前时间”之前,则过滤掉 continue; } newList.add(vo); } return newList; } @Override public TicketInfo getApplyTicket(Long studentId, Long applyId) { StudentApplyDao baseMapper = (StudentApplyDao) studentApplyService.getBaseMapper(); TicketInfo info = baseMapper.getStudentApplyTicket(applyId); if (info == null || info.getCancel()) { throw new StatusException("准考证信息不存在"); } if (!info.getStudentId().equals(studentId)) { throw new StatusException("准考证信息不匹配"); } if (StringUtils.isEmpty(info.getTicketNumber())) { throw new StatusException("准考证信息尚未生成"); } return info; } @Override public List getApplyDateList(LoginUser student) { // 日期格式:yyyyMMdd List dateList = new ArrayList<>(); CurrentApplyTaskVO curApplyTask = applyTaskService.currentApplyTask(student.getOrgId()); if (curApplyTask == null) { return dateList; } LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>(); wrapper.select(TimePeriodEntity::getStartTime);// 只查询startTime等字段 wrapper.eq(TimePeriodEntity::getApplyTaskId, curApplyTask.getTaskId()); List timePeriods = timePeriodService.list(wrapper); if (CollectionUtils.isEmpty(timePeriods)) { return dateList; } // 考前N天,禁止考生自主预约 Date allowDate = DateUtil.changeDateAndTimeEnd(new Date(), curApplyTask.getAllowApplyDays()); Set dates = new HashSet<>(); FastDateFormat fdf = FastDateFormat.getInstance("yyyyMMdd"); for (TimePeriodEntity timePeriod : timePeriods) { Date curDate = new Date(timePeriod.getStartTime()); if (curDate.before(allowDate)) { // 跳过过期时段,“当前时段开始时间”在“允许预约时间”之前,则禁止预约 continue; } dates.add(fdf.format(curDate)); } dateList.addAll(dates); Collections.sort(dateList); return dateList; } @Override public ApplyTimePeriodResult getApplyTimePeriodList(LoginUser student, Long examSiteId, String date) { if (examSiteId == null) { throw new StatusException("考点ID不能为空"); } if (StringUtils.isEmpty(date)) { throw new StatusException("预约日期不能为空"); } Date startTime, endTime; try { FastDateFormat fdf = FastDateFormat.getInstance("yyyyMMddHHmmss"); startTime = fdf.parse(date + "000000"); endTime = fdf.parse(date + "235959"); } catch (ParseException e) { log.error(e.getMessage(), e); throw new StatusException("预约日期值的格式不正确"); } CurrentApplyTaskVO curApplyTask = applyTaskService.currentApplyTask(student.getOrgId()); if (curApplyTask == null) { throw new StatusException("当前预约任务不存在"); } LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>(); wrapper.select(TimePeriodEntity::getId, TimePeriodEntity::getStartTime, TimePeriodEntity::getEndTime); wrapper.eq(TimePeriodEntity::getApplyTaskId, curApplyTask.getTaskId()); wrapper.between(TimePeriodEntity::getStartTime, startTime.getTime(), endTime.getTime()); List timePeriods = timePeriodService.list(wrapper); List periodList = new ArrayList<>(); if (CollectionUtils.isNotEmpty(timePeriods)) { for (TimePeriodEntity timePeriod : timePeriods) { TimePeriodInfo info = new TimePeriodInfo(); info.setTimePeriodId(timePeriod.getId()); info.setTimePeriodStart(timePeriod.getStartTime()); info.setTimePeriodEnd(timePeriod.getEndTime()); info.setStatus(ApplyStatus.AVAILABLE); info.setAvailableCount(1); periodList.add(info); } } // todo ApplyTimePeriodResult result = new ApplyTimePeriodResult(); result.setUnApplyNumber(1); result.setPeriodList(periodList); return result; } @Override public RichTextBean getExamNotice(Long applyTaskId) { if (applyTaskId == null) { throw new StatusException("预约任务ID不能为空"); } ApplyTaskEntity applyTask = applyTaskService.getById(applyTaskId); if (applyTask == null) { throw new StatusException("考试须知信息不存在"); } RichTextBean bean = new RichTextBean(); bean.setContent(applyTask.getNotice()); return bean; } @Override public RichTextBean getExamSiteGuide(Long examSiteId) { if (examSiteId == null) { throw new StatusException("考点ID不能为空"); } ExamSiteEntity examSite = examSiteService.getById(examSiteId); if (examSite == null) { throw new StatusException("考场引导信息不存在"); } RichTextBean bean = new RichTextBean(); bean.setContent(examSite.getGuide()); return bean; } }