123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811 |
- package com.qmth.exam.reserve.service.impl;
- import java.io.File;
- import java.io.IOException;
- import java.io.InputStream;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.Calendar;
- import java.util.Comparator;
- import java.util.Date;
- import java.util.HashMap;
- import java.util.Iterator;
- import java.util.List;
- import java.util.Map;
- import java.util.concurrent.locks.Lock;
- import java.util.stream.Collectors;
- import org.apache.commons.lang3.StringUtils;
- import org.apache.commons.lang3.time.DateUtils;
- 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 org.springframework.transaction.interceptor.TransactionAspectSupport;
- import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
- import com.baomidou.mybatisplus.core.metadata.IPage;
- import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
- import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
- import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
- import com.qmth.boot.core.collection.PageResult;
- import com.qmth.boot.core.concurrent.service.ConcurrentService;
- import com.qmth.boot.core.exception.StatusException;
- import com.qmth.boot.tools.excel.ExcelReader;
- import com.qmth.boot.tools.excel.enums.ExcelType;
- import com.qmth.boot.tools.excel.model.DataMap;
- import com.qmth.boot.tools.io.ZipWriter;
- import com.qmth.exam.reserve.bean.login.LoginUser;
- import com.qmth.exam.reserve.bean.stdapply.AgentAndTimeVO;
- import com.qmth.exam.reserve.bean.stdapply.MaterialTitleInfo;
- import com.qmth.exam.reserve.bean.stdapply.StudentApplyReq;
- import com.qmth.exam.reserve.bean.stdapply.StudentApplyVO;
- import com.qmth.exam.reserve.bean.stdapply.StudentImportVO;
- import com.qmth.exam.reserve.cache.CacheConstants;
- import com.qmth.exam.reserve.cache.RedisClient;
- import com.qmth.exam.reserve.dao.StudentApplyDao;
- import com.qmth.exam.reserve.entity.ApplyTaskEntity;
- import com.qmth.exam.reserve.entity.CategoryEntity;
- import com.qmth.exam.reserve.entity.ExamRoomEntity;
- import com.qmth.exam.reserve.entity.ExamSiteEntity;
- import com.qmth.exam.reserve.entity.StudentApplyEntity;
- import com.qmth.exam.reserve.entity.StudentEntity;
- import com.qmth.exam.reserve.entity.TimePeriodEntity;
- import com.qmth.exam.reserve.enums.CategoryLevel;
- import com.qmth.exam.reserve.enums.EventType;
- import com.qmth.exam.reserve.service.ApplyTaskService;
- import com.qmth.exam.reserve.service.CategoryService;
- import com.qmth.exam.reserve.service.ExamRoomService;
- import com.qmth.exam.reserve.service.ExamSiteService;
- import com.qmth.exam.reserve.service.MaterialGenerateService;
- import com.qmth.exam.reserve.service.OperateLogService;
- import com.qmth.exam.reserve.service.StudentApplyService;
- import com.qmth.exam.reserve.service.StudentService;
- import com.qmth.exam.reserve.service.TimePeriodService;
- import com.qmth.exam.reserve.util.DateUtil;
- import com.qmth.exam.reserve.util.JsonHelper;
- import com.qmth.exam.reserve.util.PageUtil;
- @Service
- public class StudentApplyServiceImpl extends ServiceImpl<StudentApplyDao, StudentApplyEntity>
- implements StudentApplyService {
- private final static Logger log = LoggerFactory.getLogger(StudentApplyServiceImpl.class);
- private static final String[] EXCEL_HEADER = new String[] { "学号", "姓名", "证件号", "所属教学点", "预约考点1", "预约时段1", "预约考点2",
- "预约时段2", "预约考点3", "预约时段3", "预约考点4", "预约时段4" };
- private final long TIMEOUT = 60;
- @Autowired
- private TimePeriodService timePeriodService;
- @Autowired
- private ApplyTaskService applyTaskService;
- @Autowired
- private CategoryService categoryService;
- @Autowired
- private StudentService studentService;
- @Autowired
- private ExamSiteService examSiteService;
- @Autowired
- private OperateLogService operateLogService;
- @Autowired
- private RedisClient redisClient;
- @Autowired
- private ExamRoomService examRoomService;
- @Autowired
- private MaterialGenerateService materialService;
- @Autowired
- private ConcurrentService concurrentService;
- @Override
- public PageResult<StudentApplyVO> page(StudentApplyReq req) {
- IPage<StudentApplyVO> iPage = this.baseMapper
- .page(new Page<StudentApplyVO>(req.getPageNumber(), req.getPageSize()), req);
- return PageUtil.of(iPage);
- }
- @Transactional
- @Override
- public void cancel(LoginUser user, Long id) {
- // 时间判断
- StudentApplyEntity studentApply = this.baseMapper.selectById(id);
- if (studentApply == null || studentApply.getTimePeriodId() == null)
- throw new StatusException("考生没有预约,无法取消!");
- TimePeriodEntity timePeroid = timePeriodService.getById(studentApply.getTimePeriodId());
- if (timePeroid == null)
- throw new StatusException("考试时段不存在,请检查考试时段数据!");
- ApplyTaskEntity task = getApplyTask();
- Date applyDate = DateUtils.truncate(new Date(timePeroid.getStartTime()), Calendar.DATE);
- Date canCancelDay = DateUtil.addValues(applyDate, Calendar.DAY_OF_MONTH, -task.getAllowApplyCancelDays());
- if (new Date().after(canCancelDay))
- throw new StatusException("可取消时间已过,无法取消!");
- studentApply.setCancel(Boolean.TRUE);
- this.baseMapper.updateById(studentApply);
- // TODO redis更新:该时段redis已预约的数量加1
- operateLogService.insertOperateLog(user.getId(), EventType.CANCEL_APPLY, JsonHelper.toJson(studentApply));
- }
- @Transactional
- @Override
- public List<Map<String, Object>> importPreExam(LoginUser user, Long teachingId, Integer level,
- InputStream inputStream) {
- checkInOpenTime();
- List<DataMap> lineList = null;
- ExcelReader reader = ExcelReader.create(ExcelType.XLSX, inputStream, 0);
- try {
- lineList = reader.getDataMapList();
- } catch (Exception e) {
- throw new StatusException("Excel 解析失败");
- }
- if (!Arrays.equals(EXCEL_HEADER, reader.getColumnNames())) {
- throw new StatusException("Excel表头错误");
- }
- if (CollectionUtils.isEmpty(lineList)) {
- throw new StatusException("Excel无内容");
- }
- List<Map<String, Object>> failRecords = new ArrayList<Map<String, Object>>();
- Map<String, Long> teachingCache = getTeachingCache(level);
- Map<String, Long> agentCache = getAgentCache(teachingId);
- Map<String, Long> timeCache = getTimePeriodCache();
- List<StudentImportVO> applyList = new ArrayList<>();
- AgentAndTimeVO agentTime = new AgentAndTimeVO();
- for (int i = 0; i < lineList.size(); i++) {
- List<AgentAndTimeVO> agentTimeList = new ArrayList<>();
- DataMap line = lineList.get(i);
- StudentImportVO apply = new StudentImportVO();
- StringBuilder msg = new StringBuilder();
- String studentCode = trimAndNullIfBlank(line.get(EXCEL_HEADER[0]));
- if (StringUtils.isBlank(studentCode)) {
- msg.append(" 学号不能为空");
- }
- String name = trimAndNullIfBlank(line.get(EXCEL_HEADER[1]));
- if (StringUtils.isBlank(name)) {
- msg.append(" 姓名不能为空");
- }
- String identityNumber = trimAndNullIfBlank(line.get(EXCEL_HEADER[2]));
- if (StringUtils.isBlank(identityNumber)) {
- msg.append(" 证件号不能为空");
- }
- StudentEntity student = null;
- try {
- student = checkStd(studentCode, name, identityNumber);
- apply.setStudentId(student.getId());
- } catch (StatusException e) {
- msg.append(" " + e.getMessage());
- failRecords.add(newError(i + 1, msg.toString()));
- continue;
- }
- String teachingName = trimAndNullIfBlank(line.get(EXCEL_HEADER[3]));
- if (StringUtils.isBlank(teachingName)) {
- msg.append(" 所属教学点不能为空");
- }
- Long categoryId = teachingCache.get(teachingName);
- if (categoryId == null) {
- msg.append(" 所属教学点不存在");
- }
- if (categoryId != null && !student.getCategoryId().equals(categoryId)) {
- msg.append(" 考生所属教学点和库中的考生教学点不匹配");
- }
- if (categoryId != null && !categoryId.equals(teachingId)) {
- msg.append(" 不是本教学点的考生");
- }
- String agentName1 = trimAndNullIfBlank(line.get(EXCEL_HEADER[4]));
- if (StringUtils.isBlank(agentName1)) {
- msg.append(" 预约考点1不能为空");
- }
- agentTime = new AgentAndTimeVO();
- Long agentId = agentCache.get(agentName1);
- if (agentId == null) {
- msg.append(" 预约考点1不存在");
- }
- String timePeriod1 = trimAndNullIfBlank(line.get(EXCEL_HEADER[5]));
- if (StringUtils.isBlank(timePeriod1)) {
- msg.append(" 预约时段1不能为空");
- }
- Long timePeriodId = null;
- try {
- timePeriodId = checkTimePeriod(timePeriod1, timeCache);
- agentTime.setAgentId(agentId);
- agentTime.setTimePeriodId(timePeriodId);
- agentTimeList.add(agentTime);
- } catch (StatusException e) {
- msg.append(" " + e.getMessage());
- }
- String agentName2 = trimAndNullIfBlank(line.get(EXCEL_HEADER[6]));
- String timePeriod2 = trimAndNullIfBlank(line.get(EXCEL_HEADER[7]));
- if (StringUtils.isBlank(agentName2) && StringUtils.isBlank(timePeriod2)) {
- apply.setAgentTimeList(agentTimeList);
- applyList.add(apply);
- if (msg.length() > 0)
- failRecords.add(newError(i + 1, msg.toString()));
- continue;
- } else {
- agentId = agentCache.get(agentName2);
- if (agentId == null)
- msg.append(" 预约考点2不存在");
- try {
- timePeriodId = checkTimePeriod(timePeriod2, timeCache);
- agentTime = new AgentAndTimeVO();
- agentTime.setAgentId(agentId);
- agentTime.setTimePeriodId(timePeriodId);
- agentTimeList.add(agentTime);
- } catch (StatusException e) {
- msg.append(" " + e.getMessage());
- }
- }
- String agentName3 = trimAndNullIfBlank(line.get(EXCEL_HEADER[8]));
- String timePeriod3 = trimAndNullIfBlank(line.get(EXCEL_HEADER[9]));
- if (StringUtils.isBlank(agentName3) && StringUtils.isBlank(timePeriod3)) {
- apply.setAgentTimeList(agentTimeList);
- applyList.add(apply);
- if (msg.length() > 0)
- failRecords.add(newError(i + 1, msg.toString()));
- continue;
- } else {
- agentId = agentCache.get(agentName3);
- if (agentId == null)
- msg.append(" 预约考点3不存在");
- try {
- timePeriodId = checkTimePeriod(timePeriod3, timeCache);
- agentTime = new AgentAndTimeVO();
- agentTime.setAgentId(agentId);
- agentTime.setTimePeriodId(timePeriodId);
- agentTimeList.add(agentTime);
- } catch (StatusException e) {
- msg.append(" " + e.getMessage());
- }
- }
- String agentName4 = trimAndNullIfBlank(line.get(EXCEL_HEADER[10]));
- String timePeriod4 = trimAndNullIfBlank(line.get(EXCEL_HEADER[11]));
- if (StringUtils.isBlank(agentName4) && StringUtils.isBlank(timePeriod4)) {
- apply.setAgentTimeList(agentTimeList);
- applyList.add(apply);
- if (msg.length() > 0)
- failRecords.add(newError(i + 1, msg.toString()));
- continue;
- } else {
- agentId = agentCache.get(agentName4);
- if (agentId == null)
- msg.append(" 预约考点4不存在");
- try {
- timePeriodId = checkTimePeriod(timePeriod4, timeCache);
- agentTime = new AgentAndTimeVO();
- agentTime.setAgentId(agentId);
- agentTime.setTimePeriodId(timePeriodId);
- agentTimeList.add(agentTime);
- apply.setAgentTimeList(agentTimeList);
- applyList.add(apply);
- } catch (StatusException e) {
- msg.append(" " + e.getMessage());
- }
- }
- }
- if (CollectionUtils.isNotEmpty(failRecords)) {
- TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
- return failRecords;
- }
- for (int i = 0; i < applyList.size(); i++) {
- StudentImportVO vo = applyList.get(i);
- try {
- saveStdApply(user.getId(), vo);
- } catch (StatusException e) {
- failRecords.add(newError(i + 1, " 系统异常"));
- log.error("导入异常", e);
- }
- }
- if (CollectionUtils.isNotEmpty(failRecords)) {
- TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
- return failRecords;
- }
- if (CollectionUtils.isEmpty(failRecords)) {
- checkStudentApplyFinish(applyList);
- }
- // TODO 导入预考后,更新redis
- return failRecords;
- }
- private void checkStudentApplyFinish(List<StudentImportVO> applyList) {
- List<StudentEntity> toBeUpdateList = new ArrayList<>();
- for (StudentImportVO importVO : applyList) {
- StudentEntity student = studentService.getById(importVO.getStudentId());
- List<StudentApplyEntity> haveApplyList = listStudentApply(importVO.getStudentId(), Boolean.FALSE);
- if (student.getApplyNumber().intValue() == haveApplyList.size()) {
- // 更新考生的完成状态
- student.setApplyFinished(Boolean.TRUE);
- toBeUpdateList.add(student);
- }
- studentService.saveOrUpdateBatch(toBeUpdateList);
- }
- }
- private void checkInOpenTime() {
- ApplyTaskEntity task = getApplyTask();
- Date start = new Date(task.getSelfApplyStartTime());
- Date end = new Date(task.getSelfApplyEndTime());
- if (!DateUtil.isBetwwen(start, end)) {
- throw new StatusException("导入预考,必须要在第一阶段导入!");
- }
- }
- private ApplyTaskEntity getApplyTask() {
- LambdaQueryWrapper<ApplyTaskEntity> wrapper = new LambdaQueryWrapper<ApplyTaskEntity>()
- .eq(ApplyTaskEntity::getEnable, Boolean.TRUE);
- ApplyTaskEntity task = applyTaskService.getOne(wrapper);
- if (task == null)
- throw new StatusException("未开启预约任务");
- return task;
- }
- private void saveStdApply(Long userId, StudentImportVO vo) {
- List<AgentAndTimeVO> agentTimeList = vo.getAgentTimeList();
- LambdaQueryWrapper<StudentApplyEntity> lm = new LambdaQueryWrapper<>();
- lm.eq(StudentApplyEntity::getStudentId, vo.getStudentId());
- LogStdApply(userId, this.baseMapper.selectList(lm));
- this.baseMapper.delete(lm);
- for (AgentAndTimeVO agentTime : agentTimeList) {
- StudentApplyEntity entity = new StudentApplyEntity();
- entity.setStudentId(vo.getStudentId());
- entity.setExamSiteId(agentTime.getAgentId());
- entity.setTimePeriodId(agentTime.getTimePeriodId());
- entity.setCancel(Boolean.FALSE);
- this.baseMapper.insert(entity);
- }
- }
- private void LogStdApply(Long userId, List<StudentApplyEntity> existList) {
- if (!existList.isEmpty()) {
- for (StudentApplyEntity studentApply : existList) {
- this.operateLogService.insertOperateLog(userId, EventType.DELETE_APPLY,
- JsonHelper.toJson(studentApply));
- }
- }
- }
- private Long checkTimePeriod(String timePeriod, Map<String, Long> timeCache) {
- if (timePeriod.split(" ").length != 2) {
- throw new StatusException(" 预约时段格式不正确");
- }
- String[] arr = timePeriod.split("-");
- String startTime = arr[0] + ":00";
- String endTime = startTime.substring(0, startTime.indexOf("日") + 1) + " " + arr[1] + ":00";
- Long startTimeLong = DateUtil.getLongTimeByZHDate(startTime);
- Long endTimeLong = DateUtil.getLongTimeByZHDate(endTime);
- if (timeCache.get(startTimeLong + "-" + endTimeLong) == null) {
- throw new StatusException(" 预约时段不存在");
- }
- return timeCache.get(startTimeLong + "-" + endTimeLong);
- }
- private Map<String, Long> getTimePeriodCache() {
- LambdaQueryWrapper<TimePeriodEntity> lm = new LambdaQueryWrapper<>();
- lm.eq(TimePeriodEntity::getApplyTaskId, getApplyTask().getId());
- List<TimePeriodEntity> timeList = timePeriodService.list(lm);
- return timeList.stream().collect(Collectors.toMap(item -> {
- return item.getStartTime() + "-" + item.getEndTime();
- }, TimePeriodEntity::getId));
- }
- private Map<String, Long> getTeachingCache(Integer level) {
- LambdaQueryWrapper<CategoryEntity> lm = new LambdaQueryWrapper<>();
- lm.eq(CategoryEntity::getEnable, Boolean.TRUE);
- lm.eq(CategoryEntity::getLevel, level == null ? CategoryLevel.TEACHING.getValue() : level);
- List<CategoryEntity> categoryList = categoryService.list(lm);
- return categoryList.stream().collect(Collectors.toMap(CategoryEntity::getName, CategoryEntity::getId));
- }
- private Map<String, Long> getAgentCache(Long categoryId) {
- LambdaQueryWrapper<ExamSiteEntity> lm = new LambdaQueryWrapper<>();
- lm.eq(ExamSiteEntity::getEnable, Boolean.TRUE);
- lm.eq(ExamSiteEntity::getCategoryId, categoryId);
- List<ExamSiteEntity> categoryList = examSiteService.list(lm);
- return categoryList.stream().collect(Collectors.toMap(ExamSiteEntity::getName, ExamSiteEntity::getId));
- }
- private StudentEntity checkStd(String studentCode, String name, String identityNumber) {
- LambdaQueryWrapper<StudentEntity> lm = new LambdaQueryWrapper<>();
- lm.eq(StudentEntity::getStudentCode, studentCode);
- lm.eq(StudentEntity::getName, name);
- lm.eq(StudentEntity::getIdentityNumber, identityNumber);
- StudentEntity student = studentService.getOne(lm);
- if (student == null) {
- throw new StatusException(" 考生信息填写错误");
- }
- return student;
- }
- private Map<String, Object> newError(int lineNum, String msg) {
- Map<String, Object> map = new HashMap<>();
- map.put("lineNum", lineNum);
- map.put("msg", msg);
- return map;
- }
- private String trimAndNullIfBlank(String s) {
- if (StringUtils.isBlank(s)) {
- return null;
- }
- return s.trim();
- }
- @Transactional
- @Override
- public void autoAssign(Long taskId) {
- checkAfterOpenTime();
- Lock lock = concurrentService.getLock(CacheConstants.LOCK_AUTO_APPLY);
- try {
- if (!lock.tryLock()) {
- log.warn("获取锁失败,不允许同时执行自动分配!lockKey:{}", CacheConstants.LOCK_AUTO_APPLY);
- throw new StatusException("其他老师正在执行自动分配,请不要重复执行!");
- }
- // 1、未完成预约的考生
- /*
- * LambdaQueryWrapper<StudentEntity> lm = new LambdaQueryWrapper<>();
- * lm.eq(StudentEntity::getApplyFinished, Boolean.FALSE); List<StudentEntity>
- * studentList = studentService.list(lm);
- */
- List<StudentEntity> studentList = studentService.listNoFinishStudent(taskId, Boolean.FALSE);
- Map<Long, List<StudentEntity>> map = studentList.stream()
- .collect(Collectors.groupingBy(StudentEntity::getCategoryId));
- // 2、考位是否充足
- List<TimePeriodEntity> timeList = listTimePeroid(taskId);
- checkTeachingCapacity(map, timeList, taskId);
- // 3、按照教学点安排考位。规则:不能和已预约的时间上有冲突
- for (Long key : map.keySet()) {
- List<ExamSiteEntity> siteList = listExamSite(key, null);
- List<StudentEntity> teachingStudentList = map.get(key);
- for (TimePeriodEntity time : timeList) {
- for (ExamSiteEntity site : siteList) {
- // 该时段已预约的考生
- Integer haveApplyNum = getHaveApplyNum(site.getId(), time.getId());
- // 剩余的考位
- Integer remainNum = site.getCapacity() - haveApplyNum;
- assignStudentApply(site.getId(), time.getId(), teachingStudentList, remainNum);
- }
- }
- // 4、判断是否还有剩余考生未完成预约,提醒考位不够
- if (teachingStudentList.size() > 0)
- throw new StatusException("【" + categoryService.getById(key).getName() + "】教学点考位不足");
- }
- } catch (Exception e) {
- log.error(e.getMessage());
- throw new StatusException(e.getMessage());
- } finally {
- lock.unlock();
- }
- }
- private void assignStudentApply(Long siteId, Long timeId, List<StudentEntity> teachingStudentList,
- Integer remainNum) {
- int num = 0;
- for (Iterator<StudentEntity> iterator = teachingStudentList.iterator(); iterator.hasNext();) {
- StudentEntity student = iterator.next();
- String studentApplyLockKey = String.format(CacheConstants.LOCK_STUDENT_APPLY, student.getId());
- Lock studentApplyLock = concurrentService.getLock(studentApplyLockKey);
- if (num >= remainNum)
- break;
- List<StudentApplyEntity> studentApplyList = listStudentApply(student.getId(), Boolean.FALSE);
- int toApplyNum = student.getApplyNumber() - studentApplyList.size();
- if (toApplyNum > 0 && !haveApplySameTimePeriod(siteId, timeId, student.getId())) {
- StudentApplyEntity studentApply = new StudentApplyEntity();
- studentApply.setStudentId(student.getId());
- studentApply.setExamSiteId(siteId);
- studentApply.setCancel(Boolean.FALSE);
- studentApply.setTimePeriodId(timeId);
- if (studentApplyLock.tryLock()) {
- baseMapper.insert(studentApply);
- } else {
- log.warn("获取锁失败,不允许同一个考生同时操作预约!lockKey:{}", studentApplyLockKey);
- }
- num++;
- if (toApplyNum - (studentApplyList.size() + 1) == 0) {
- iterator.remove();
- student.setApplyFinished(true);
- this.studentService.updateById(student);
- }
- }
- }
- }
- private boolean haveApplySameTimePeriod(Long siteId, Long timeId, Long studentId) {
- LambdaQueryWrapper<StudentApplyEntity> wrapper = new LambdaQueryWrapper<>();
- wrapper.eq(StudentApplyEntity::getExamSiteId, siteId);
- wrapper.eq(StudentApplyEntity::getTimePeriodId, timeId);
- wrapper.eq(StudentApplyEntity::getStudentId, studentId);
- wrapper.eq(StudentApplyEntity::getCancel, Boolean.FALSE);
- StudentApplyEntity studentApply = baseMapper.selectOne(wrapper);
- return studentApply == null ? false : true;
- }
- private Integer getHaveApplyNum(Long siteId, Long timeId) {
- LambdaQueryWrapper<StudentApplyEntity> wrapper = new LambdaQueryWrapper<>();
- wrapper.eq(StudentApplyEntity::getExamSiteId, siteId);
- wrapper.eq(StudentApplyEntity::getTimePeriodId, timeId);
- wrapper.eq(StudentApplyEntity::getCancel, Boolean.FALSE);
- List<StudentApplyEntity> haveAplyList = baseMapper.selectList(wrapper);
- return haveAplyList == null ? 0 : haveAplyList.size();
- }
- private void checkAfterOpenTime() {
- ApplyTaskEntity task = getApplyTask();
- Date selfEndTime = new Date(task.getSelfApplyEndTime());
- Date openStartTime = new Date(task.getOpenApplyStartTime());
- if (!DateUtil.isBetwwen(selfEndTime, openStartTime)) {
- throw new StatusException("自动分配,时间必须要在第一阶段结束之后,第三阶段开始之前");
- }
- }
- private void checkTeachingCapacity(Map<Long, List<StudentEntity>> map, List<TimePeriodEntity> timeList,
- Long taskId) {
- for (Long key : map.keySet()) {
- List<ExamSiteEntity> siteList = listExamSite(key, null);
- // 总考位数量
- Integer total = siteList.stream().collect(Collectors.summingInt(ExamSiteEntity::getCapacity))
- * timeList.size();
- // 已经预约的数量
- Integer haveApplyNum = this.getBaseMapper().getHaveApplyCount(
- siteList.stream().map(site -> site.getId()).collect(Collectors.toList()), Boolean.FALSE);
- // 未预约的数量
- Integer noApplyNum = getNoApplyNum(map.get(key));
- if (noApplyNum > total - haveApplyNum) {
- CategoryEntity category = categoryService.getById(key);
- throw new StatusException("【" + category.getName() + "】教学点考位不足!剩余的考位数量:【" + (total - haveApplyNum)
- + "】,实际需要的考位数量:【" + noApplyNum + "】");
- }
- }
- }
- private Integer getNoApplyNum(List<StudentEntity> list) {
- Integer noApplyNum = 0;
- for (StudentEntity student : list) {
- if (student.getApplyNumber().intValue() == 1) {
- noApplyNum++;
- } else if (student.getApplyNumber().intValue() > 1) {
- noApplyNum = noApplyNum
- + (student.getApplyNumber() - listStudentApply(student.getId(), Boolean.FALSE).size());
- }
- }
- return noApplyNum;
- }
- private List<StudentApplyEntity> listStudentApply(Long stdId, Boolean cancel) {
- LambdaQueryWrapper<StudentApplyEntity> lm = new LambdaQueryWrapper<>();
- lm.eq(StudentApplyEntity::getStudentId, stdId);
- lm.eq(StudentApplyEntity::getCancel, cancel);
- return this.baseMapper.selectList(lm);
- }
- private List<TimePeriodEntity> listTimePeroid(Long taskId) {
- LambdaQueryWrapper<TimePeriodEntity> wrapper = new LambdaQueryWrapper<>();
- wrapper.eq(TimePeriodEntity::getApplyTaskId, taskId);
- wrapper.orderByAsc(TimePeriodEntity::getStartTime);
- List<TimePeriodEntity> timeList = timePeriodService.list(wrapper);
- if (timeList.isEmpty()) {
- throw new StatusException("考试时段未设置");
- }
- return timeList;
- }
- private List<ExamSiteEntity> listExamSite(Long categoryId, Long examsiteId) {
- LambdaQueryWrapper<ExamSiteEntity> wrapper = new LambdaQueryWrapper<>();
- wrapper.eq(ExamSiteEntity::getCategoryId, categoryId);
- wrapper.eq(examsiteId != null, ExamSiteEntity::getId, examsiteId);
- wrapper.eq(ExamSiteEntity::getEnable, Boolean.TRUE);
- return examSiteService.list(wrapper);
- }
- @Transactional
- @Override
- public void autoLayout(Long teachingId) {
- ApplyTaskEntity applyTask = getApplyTask();
- if (applyTask == null) {
- log.info("没有开启预约任务");
- return;
- }
- // boolean isSuccess = redisClient.tryLock(
- // CacheConstants.LOCK_ARRANGE_EXAM + DateUtil.formatShortDateString(new
- // Date()), FastUUID.get(), TIMEOUT,
- // TimeUnit.MINUTES);
- String autoLayoutLockKey = String.format(CacheConstants.LOCK_ARRANGE_EXAM,
- DateUtil.formatShortDateString(new Date()));
- Lock autoLayoutLock = concurrentService.getLock(autoLayoutLockKey);
- try {
- if (!autoLayoutLock.tryLock()) {
- log.warn("获取锁失败,已有线程在执行排考!lockKey:{}", autoLayoutLock);
- return;
- }
- // 1.根据当前日期,查询不能取消的时段
- List<TimePeriodEntity> timePeriodList = listTimePeroid(applyTask.getId());
- List<TimePeriodEntity> noCancelTimePeroidList = listNoCancelApplyTimePeroid(timePeriodList,
- applyTask.getAllowApplyCancelDays());
- if (noCancelTimePeroidList.isEmpty()) {
- log.info("当前时间不在取消预约范围内");
- return;
- }
- // 2.查询考试日期的待排考的考生
- List<StudentApplyEntity> toBeLayoutStudentList = this.baseMapper.listTimePeriod(
- noCancelTimePeroidList.stream().map(item -> item.getId()).collect(Collectors.toList()),
- Boolean.FALSE);
- // 3.开始排考
- Map<Long, List<StudentApplyEntity>> toBeLayoutStudentMap = toBeLayoutStudentList.stream()
- .collect(Collectors.groupingBy(StudentApplyEntity::getExamSiteId));
- for (Long examSiteId : toBeLayoutStudentMap.keySet()) {
- Map<Long, List<StudentApplyEntity>> timeLayoutStudentMap = toBeLayoutStudentMap.get(examSiteId).stream()
- .collect(Collectors.groupingBy(StudentApplyEntity::getTimePeriodId));
- List<ExamRoomEntity> roomList = listExamRoom(examSiteId);
- if (roomList.isEmpty()) {
- throw new StatusException(examSiteId + ":未设置考场");
- }
- ExamSiteEntity examSite = examSiteService.getById(examSiteId);
- layoutStudentByTimePeriod(applyTask.getId(), examSite, roomList, timeLayoutStudentMap);
- }
- } catch (StatusException e) {
- log.error(e.getMessage());
- e.printStackTrace();
- } finally {
- // redisClient.delete(CacheConstants.LOCK_ARRANGE_EXAM +
- // DateUtil.formatShortDateString(new Date()));
- autoLayoutLock.unlock();
- }
- }
- private void layoutStudentByTimePeriod(Long taskId, ExamSiteEntity examSite, List<ExamRoomEntity> roomList,
- Map<Long, List<StudentApplyEntity>> timeLayoutStudentMap) {
- for (Long timePeriodId : timeLayoutStudentMap.keySet()) {
- List<StudentApplyEntity> studentApplyList = timeLayoutStudentMap.get(timePeriodId);
- layoutStudentToRoom(taskId, examSite, roomList, studentApplyList, timePeriodService.getById(timePeriodId));
- }
- }
- private void layoutStudentToRoom(Long taskId, ExamSiteEntity examSite, List<ExamRoomEntity> roomList,
- List<StudentApplyEntity> studentApplyList, TimePeriodEntity timePeriod) {
- Integer timePeriodOrder = getTimePeriodOrder(taskId, timePeriod);
- for (ExamRoomEntity room : roomList) {
- Integer num = 0;
- for (Iterator<StudentApplyEntity> iterator = studentApplyList.iterator(); iterator.hasNext();) {
- StudentApplyEntity student = iterator.next();
- if (num >= room.getCapacity())
- break;
- String seatNumber = StringUtils.leftPad(String.valueOf(++num), 3, '0');
- student.setExamRoomId(room.getId());
- student.setSeatNumber(seatNumber);
- student.setTicketNumber(
- generateTicketNumber(timePeriodOrder, examSite.getCode(), room.getCode(), seatNumber));
- this.baseMapper.updateById(student);
- iterator.remove();
- }
- }
- }
- private Integer getTimePeriodOrder(Long taskId, TimePeriodEntity timePeriod) {
- List<TimePeriodEntity> timeList = listTimePeroid(taskId);
- List<TimePeriodEntity> sameDayTimeList = listSameDayTimePeroid(timeList, timePeriod.getStartTime());
- for (int i = 0; i < sameDayTimeList.size(); i++) {
- TimePeriodEntity time = sameDayTimeList.get(i);
- if (time.getStartTime().equals(timePeriod.getStartTime()))
- return i + 1;
- }
- return 0;
- }
- private List<TimePeriodEntity> listSameDayTimePeroid(List<TimePeriodEntity> timeList, Long startTime) {
- Date day = new Date(startTime);
- List<TimePeriodEntity> resultList = new ArrayList<>();
- for (TimePeriodEntity time : timeList) {
- if (DateUtils.isSameDay(day, new Date(time.getStartTime())))
- resultList.add(time);
- }
- return resultList.stream().sorted(Comparator.comparing(TimePeriodEntity::getStartTime))
- .collect(Collectors.toList());
- }
- private String generateTicketNumber(Integer timePeriodOrder, String examSiteCode, String roomCode,
- String seatNumber) {
- return DateUtil.formatShortDateString(new Date()) + timePeriodOrder + examSiteCode + roomCode + seatNumber;
- }
- public List<ExamRoomEntity> listExamRoom(Long examSiteId) {
- LambdaQueryWrapper<ExamRoomEntity> wrapper = new LambdaQueryWrapper<>();
- wrapper.eq(ExamRoomEntity::getExamSiteId, examSiteId);
- wrapper.orderByAsc(ExamRoomEntity::getCode);
- return examRoomService.list(wrapper);
- }
- private List<TimePeriodEntity> listNoCancelApplyTimePeroid(List<TimePeriodEntity> list,
- Integer allowApplyCancelDays) {
- Date noCancelDate = getNoCancelApplyDate(allowApplyCancelDays);
- List<TimePeriodEntity> noCancelTimePeroidList = new ArrayList<>();
- for (TimePeriodEntity time : list) {
- if (DateUtils.isSameDay(noCancelDate, new Date(time.getStartTime())))
- noCancelTimePeroidList.add(time);
- }
- return noCancelTimePeroidList;
- }
- private Date getNoCancelApplyDate(Integer allowApplyCancelDays) {
- return DateUtil.addValues(Calendar.DAY_OF_MONTH, allowApplyCancelDays);
- }
- @Override
- public File downloadSignIn(Long teachingId, Long agentId) {
- ApplyTaskEntity applyTask = getApplyTask();
- List<TimePeriodEntity> timePeriodList = listTimePeroid(applyTask.getId());
- List<TimePeriodEntity> noCancelTimePeroidList = listNoCancelApplyTimePeroid(timePeriodList,
- applyTask.getAllowApplyCancelDays());
- if (noCancelTimePeroidList.isEmpty()) {
- throw new StatusException("当前时间没有可下载的签到表");
- }
- List<ExamSiteEntity> siteList = listExamSite(teachingId, agentId);
- if (siteList.isEmpty())
- throw new StatusException("当前教学点下没有可用的考点");
- CategoryEntity category = categoryService.getById(teachingId);
- File tempFolder = new File("temp");
- if (!tempFolder.exists()) {
- tempFolder.mkdirs();
- }
- ZipWriter writer = null;
- File zipFile = new File(tempFolder, category.getName() + "签到表.zip");
- try {
- writer = ZipWriter.create(zipFile);
- for (ExamSiteEntity site : siteList) {
- downloadByExamSite(writer, applyTask.getName(), site, noCancelTimePeroidList);
- }
- } catch (IOException e) {
- e.printStackTrace();
- throw new StatusException("文件写入异常");
- }
- writer.close();
- return zipFile;
- }
- public List<File> downloadByExamSite(ZipWriter writer, String taskName, ExamSiteEntity site,
- List<TimePeriodEntity> timeList) throws IOException {
- List<File> fileList = new ArrayList<>();
- List<ExamRoomEntity> roomList = listExamRoom(site.getId());
- MaterialTitleInfo title = new MaterialTitleInfo();
- title.setTaskName(taskName);
- title.setSiteName(site.getName());
- for (TimePeriodEntity time : timeList) {
- title.setTimePeriod(DateUtil.getStartAndEndTime(time.getStartTime(), time.getEndTime()));
- for (ExamRoomEntity room : roomList) {
- title.setAddress(room.getAddress());
- title.setRoomCode(room.getCode());
- List<StudentApplyVO> studentList = baseMapper.listStudentApply(time.getId(), room.getId());
- if (!studentList.isEmpty()) {
- File file = materialService.generateSignInForm(title, studentList);
- writer.write(file, title.getSiteName(),
- title.getTimePeriod() + " 第【" + title.getRoomCode() + "】考场" + ".pdf");
- }
- }
- }
- return fileList;
- }
- }
|