haogh 1 سال پیش
والد
کامیت
6ebc4da781

+ 1 - 1
src/main/java/com/qmth/exam/reserve/dao/StudentApplyDao.java

@@ -16,7 +16,7 @@ public interface StudentApplyDao extends BaseMapper<StudentApplyEntity> {
 
     IPage<StudentApplyVO> page(Page<StudentApplyVO> page, @Param(value = "req") StudentApplyReq req);
 
-    Integer getHaveApplyCount(@Param(value = "examSiteIds") List<Long> examSiteIds,
+    Integer getHaveApplyCount(@Param(value = "examSiteIds") List<Long> examSiteIds, @Param(value = "timePeriodIds") List<Long> timePeriodIds,
                               @Param(value = "cancel") Boolean cancel);
 
     List<ApplyVO> getStudentApplyList(@Param("studentId") Long studentId, @Param("cancel") Boolean cancel);

+ 5 - 1
src/main/java/com/qmth/exam/reserve/service/StudentAutoAssignService.java

@@ -1,6 +1,10 @@
 package com.qmth.exam.reserve.service;
 
+import com.qmth.exam.reserve.entity.TimePeriodEntity;
+
+import java.util.List;
+
 public interface StudentAutoAssignService {
 
-    void autoAssign(Long taskId, Long userId);
+    void autoAssign(Long taskId, Long userId, List<TimePeriodEntity>timeList);
 }

+ 74 - 72
src/main/java/com/qmth/exam/reserve/service/impl/StudentApplyServiceImpl.java

@@ -17,6 +17,7 @@ import java.util.Map;
 import java.util.stream.Collectors;
 
 import com.qmth.exam.reserve.bean.applytask.CurrentApplyTaskVO;
+import com.qmth.exam.reserve.entity.base.BaseEntity;
 import com.qmth.exam.reserve.enums.Role;
 import com.qmth.exam.reserve.service.*;
 import org.apache.commons.lang3.StringUtils;
@@ -74,8 +75,8 @@ public class StudentApplyServiceImpl extends ServiceImpl<StudentApplyDao, Studen
 
     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 static final String[] EXCEL_HEADER = new String[]{"学号", "姓名", "证件号", "所属教学点", "预约考点1", "预约时段1", "预约考点2",
+            "预约时段2", "预约考点3", "预约时段3", "预约考点4", "预约时段4"};
 
     @Autowired
     private TimePeriodService timePeriodService;
@@ -192,7 +193,7 @@ public class StudentApplyServiceImpl extends ServiceImpl<StudentApplyDao, Studen
     @Transactional
     @Override
     public List<Map<String, Object>> importPreExam(LoginUser user, Long teachingId, Integer level,
-            InputStream inputStream) {
+                                                   InputStream inputStream) {
         checkOpenTime(user.getOrgId());
         List<DataMap> lineList;
         ExcelReader reader = ExcelReader.create(ExcelType.XLSX, inputStream, 0);
@@ -260,7 +261,7 @@ public class StudentApplyServiceImpl extends ServiceImpl<StudentApplyDao, Studen
             }
             if (user.getRole().equals(Role.TEACHING) && categoryId != null && !categoryId.equals(teachingId)) {
                 msg.append(" 不是本教学点的考生");
-             }
+            }
             String agentName1 = trimAndNullIfBlank(line.get(EXCEL_HEADER[4]));
             if (StringUtils.isBlank(agentName1)) {
                 msg.append(" 预约考点1不能为空");
@@ -416,7 +417,7 @@ public class StudentApplyServiceImpl extends ServiceImpl<StudentApplyDao, Studen
         Date startTime = new Date(task.getSelfApplyStartTime());
         Date endTime = new Date(task.getOpenApplyEndTime());
         Date now = new Date();
-        if(now.before(startTime) || now.after(endTime)) {
+        if (now.before(startTime) || now.after(endTime)) {
             throw new StatusException("未到开放时间!");
         }
     }
@@ -435,7 +436,7 @@ public class StudentApplyServiceImpl extends ServiceImpl<StudentApplyDao, Studen
     private void checkAvailableTimePeriod(List<StudentImportVO> applyList) {
         for (StudentImportVO vo : applyList) {
             List<AgentAndTimeVO> agentTimeList = vo.getAgentTimeList();
-            List<StudentApplyEntity> haveApplyList = listStudentApply(vo.getStudentId(), Boolean.FALSE);
+            List<StudentApplyEntity> haveApplyList = listStudentApply(vo.getStudentId());
             int studentApplyFinishCount = cacheService.getStudentApplyFinishCount(vo.getStudentId());
             if (studentApplyFinishCount == 0) {
                 studentApplyFinishCount = haveApplyList.size();
@@ -465,7 +466,7 @@ public class StudentApplyServiceImpl extends ServiceImpl<StudentApplyDao, Studen
     }
 
     private List<AgentAndTimeVO> listAvailableTime(List<StudentApplyEntity> haveApplyList,
-            List<AgentAndTimeVO> agentTimeList) {
+                                                   List<AgentAndTimeVO> agentTimeList) {
         List<AgentAndTimeVO> availableList = new ArrayList<>();
         for (AgentAndTimeVO time : agentTimeList) {
             boolean flag = false;
@@ -483,7 +484,7 @@ public class StudentApplyServiceImpl extends ServiceImpl<StudentApplyDao, Studen
     }
 
     private void checkStudentApplyTime(List<StudentImportVO> applyList, Integer days,
-            List<Map<String, Object>> failRecords) {
+                                       List<Map<String, Object>> failRecords) {
         for (int i = 0; i < applyList.size(); i++) {
             StudentImportVO vo = applyList.get(i);
             List<AgentAndTimeVO> agentTimeList = vo.getAgentTimeList();
@@ -517,7 +518,7 @@ public class StudentApplyServiceImpl extends ServiceImpl<StudentApplyDao, Studen
     }
 
     private List<StudentApplyEntity> saveStdApply(int row, StudentImportVO vo, Long userId,
-            List<Map<String, Object>> failRecords) {
+                                                  List<Map<String, Object>> failRecords) {
         List<StudentApplyEntity> ApplyList = new ArrayList<>();
         List<AgentAndTimeVO> agentTimeList = vo.getAgentTimeList();
         String studentApplyLockKey = String.format(CacheConstants.LOCK_STUDENT_APPLY, vo.getStudentId());
@@ -654,61 +655,62 @@ public class StudentApplyServiceImpl extends ServiceImpl<StudentApplyDao, Studen
     @Override
     public void autoAssign(Long taskId, Long userId) {
         checkAfterOpenTime();
+        // 未完成预约的考生
+        List<StudentEntity> studentList = studentService.listNoFinishStudent(taskId, Boolean.FALSE);
+        Map<Long, List<StudentEntity>> noFinishApplyMap = studentList.stream().collect(Collectors.groupingBy(StudentEntity::getCategoryId));
+        // 考位是否充足
+        List<TimePeriodEntity> timeList = listTimePeriod(taskId);
+        timeList = listNoCancelExamTimePeriod(timeList, taskId);
+        if (timeList == null || timeList.isEmpty()) {
+            throw new StatusException("可以取消预约的考试时段为0");
+        }
+        checkTeachingCapacity(noFinishApplyMap, timeList);
+        RLock lock = (RLock) concurrentService.getLock(CacheConstants.LOCK_AUTO_APPLY);
+        if (lock.isLocked()) {
+            throw new StatusException("其他老师正在执行自动分配,请不要重复执行!");
+        }
         //异步执行
-        studentAutoAssignService.autoAssign(taskId, userId);
-//        RLock lock = (RLock) concurrentService.getLock(CacheConstants.LOCK_AUTO_APPLY);
-//        try {
-//            if (!lock.tryLock()) {
-//                log.warn("获取锁失败,不允许同时执行自动分配!lockKey:{}", CacheConstants.LOCK_AUTO_APPLY);
-//                throw new StatusException("其他老师正在执行自动分配,请不要重复执行!");
-//            }
-//
-//            log.warn("获取锁成功!lockKey:{}", CacheConstants.LOCK_AUTO_APPLY);
-//
-//            // 1、未完成预约的考生
-//            List<StudentEntity> studentList = studentService.listNoFinishStudent(taskId, Boolean.FALSE);
-//            Map<Long, List<StudentEntity>> noFinishApplyMap = studentList.stream()
-//                    .collect(Collectors.groupingBy(StudentEntity::getCategoryId));
-//            // 2、考位是否充足
-//            List<TimePeriodEntity> timeList = listTimePeriod(taskId);
-//            timeList = listNoCancelExamTimePeriod(timeList, taskId);
-//            checkTeachingCapacity(noFinishApplyMap, timeList, taskId);
-//            // 3、按照教学点安排考位。规则:不能和已预约的时间上有冲突
-//            for (Long key : noFinishApplyMap.keySet()) {
-//                List<ExamSiteEntity> siteList = listExamSite(key, null);
-//                List<StudentEntity> teachingStudentList = noFinishApplyMap.get(key);
-//                for (ExamSiteEntity site : siteList) {
-//                    for (TimePeriodEntity time : timeList) {
-//                        // 该时段已预约的考生
-//                        Integer haveApplyNum = cacheService.getApplyFinishCount(site.getId(), time.getId());
-//                        if (haveApplyNum == 0) {
-//                            haveApplyNum = getHaveApplyNum(site.getId(), time.getId());
-//                        }
-//                        // 剩余的考位
-//                        Integer remainNum = site.getCapacity() - haveApplyNum;
-//
-//                        assignStudentApply(userId, site.getId(), time.getId(), teachingStudentList, remainNum);
-//                    }
-//                }
-//                // 4、判断是否还有剩余考生未完成预约,提醒考位不够
-//                if (!teachingStudentList.isEmpty())
-//                    throw new StatusException("【" + categoryService.getById(key).getName() + "】教学点考位不足");
-//            }
-//        } catch (Exception e) {
-//            log.error(e.getMessage());
-//            throw new StatusException(e.getMessage());
-//        } finally {
-//            try {
-//                // 解锁前检查当前线程是否持有该锁
-//                if (lock.isLocked() && lock.isHeldByCurrentThread()) {
-//                    lock.unlock();
-//                    log.info("解锁成功!lockKey:{}", CacheConstants.LOCK_AUTO_APPLY);
-//                }
-//            } catch (Exception e) {
-//                log.warn(e.getMessage());
-//            }
-//        }
+        studentAutoAssignService.autoAssign(taskId, userId, timeList);
+    }
+
+    private void checkTeachingCapacity(Map<Long, List<StudentEntity>> map, List<TimePeriodEntity> timeList) {
+        for (Long key : map.keySet()) {
+            List<ExamSiteEntity> siteList = listExamSite(key, null);
+            // 总考位数量
+            Integer total = siteList.stream().mapToInt(ExamSiteEntity::getCapacity).sum() * timeList.size();
+            // 已经预约的数量
+            List<Long> examSiteIds = siteList.stream().map(BaseEntity::getId).collect(Collectors.toList());
+            List<Long> timePeriodIds = timeList.stream().map(BaseEntity::getId).collect(Collectors.toList());
+            Integer haveApplyNum = baseMapper.getHaveApplyCount(examSiteIds, timePeriodIds, 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) {
+        int noApplyNum = 0;
+        for (StudentEntity student : list) {
+            if (student.getApplyNumber() == 1) {
+                noApplyNum++;
+            } else if (student.getApplyNumber() > 1) {
+                int haveApplyNum = cacheService.getStudentApplyFinishCount(student.getId());
+                noApplyNum = noApplyNum + (student.getApplyNumber() - haveApplyNum);
+            }
+        }
+        return noApplyNum;
+    }
+
+    private List<TimePeriodEntity> listNoCancelExamTimePeriod(List<TimePeriodEntity> timeList, Long taskId) {
+        ApplyTaskEntity task = applyTaskService.getById(taskId);
+        Long longToday = DateUtil.getLongTimeByDate(DateUtil.formatShortSplitDateString(new Date()) + " 00:00:00");
+        Date today = new Date(longToday);
+        Date otherDay = DateUtil.addValues(today, Calendar.DAY_OF_MONTH, task.getAllowApplyCancelDays());
+        Long longOtherDay = DateUtil.getLongTimeByDate(DateUtil.formatShortSplitDateString(otherDay) + " 23:59:59");
+        return timeList.stream().filter(time -> time.getStartTime() > longOtherDay).collect(Collectors.toList());
     }
 
     private StudentApplyEntity findStudentApply(StudentApplyEntity studentApply) {
@@ -729,10 +731,10 @@ public class StudentApplyServiceImpl extends ServiceImpl<StudentApplyDao, Studen
     }
 
 
-    private List<StudentApplyEntity> listStudentApply(Long stdId, Boolean cancel) {
+    private List<StudentApplyEntity> listStudentApply(Long stdId) {
         LambdaQueryWrapper<StudentApplyEntity> lm = new LambdaQueryWrapper<>();
         lm.eq(StudentApplyEntity::getStudentId, stdId);
-        lm.eq(StudentApplyEntity::getCancel, cancel);
+        lm.eq(StudentApplyEntity::getCancel, Boolean.FALSE);
         return this.baseMapper.selectList(lm);
     }
 
@@ -817,7 +819,7 @@ public class StudentApplyServiceImpl extends ServiceImpl<StudentApplyDao, Studen
     }
 
     private void layoutStudentByTimePeriod(Long taskId, ExamSiteEntity examSite, List<ExamRoomEntity> roomList,
-            Map<Long, List<StudentApplyEntity>> timeLayoutStudentMap) {
+                                           Map<Long, List<StudentApplyEntity>> timeLayoutStudentMap) {
         for (Long timePeriodId : timeLayoutStudentMap.keySet()) {
             List<StudentApplyEntity> studentApplyList = timeLayoutStudentMap.get(timePeriodId);
             layoutStudentToRoom(taskId, examSite, roomList, studentApplyList, timePeriodService.getById(timePeriodId));
@@ -825,11 +827,11 @@ public class StudentApplyServiceImpl extends ServiceImpl<StudentApplyDao, Studen
     }
 
     private void layoutStudentToRoom(Long taskId, ExamSiteEntity examSite, List<ExamRoomEntity> roomList,
-            List<StudentApplyEntity> studentApplyList, TimePeriodEntity timePeriod) {
+                                     List<StudentApplyEntity> studentApplyList, TimePeriodEntity timePeriod) {
         Integer timePeriodOrder = getTimePeriodOrder(taskId, timePeriod);
         for (ExamRoomEntity room : roomList) {
             int num = 0;
-            for (Iterator<StudentApplyEntity> iterator = studentApplyList.iterator(); iterator.hasNext();) {
+            for (Iterator<StudentApplyEntity> iterator = studentApplyList.iterator(); iterator.hasNext(); ) {
                 StudentApplyEntity student = iterator.next();
                 if (num >= room.getCapacity())
                     break;
@@ -867,7 +869,7 @@ public class StudentApplyServiceImpl extends ServiceImpl<StudentApplyDao, Studen
     }
 
     private String generateTicketNumber(Integer timePeriodOrder, String examSiteCode, String roomCode,
-            String seatNumber) {
+                                        String seatNumber) {
         return DateUtil.formatShortDateString(new Date()) + timePeriodOrder + examSiteCode + roomCode + seatNumber;
     }
 
@@ -930,7 +932,7 @@ public class StudentApplyServiceImpl extends ServiceImpl<StudentApplyDao, Studen
     }
 
     public List<File> downloadByExamSite(ZipWriter writer, String taskName, ExamSiteEntity site,
-            List<TimePeriodEntity> timeList) throws IOException {
+                                         List<TimePeriodEntity> timeList) throws IOException {
         List<File> fileList = new ArrayList<>();
         List<ExamRoomEntity> roomList = listExamRoom(site.getId());
         MaterialTitleInfo title = new MaterialTitleInfo();
@@ -986,7 +988,7 @@ public class StudentApplyServiceImpl extends ServiceImpl<StudentApplyDao, Studen
         String todayStr = DateUtil.formatShortSplitDateString(new Date());
         Long longToday = DateUtil.getLongTimeByDate(todayStr + " 00:00:00");
         Date today = new Date(longToday);
-        if (isInTimePeriod(today,timePeriodList)) {
+        if (isInTimePeriod(today, timePeriodList)) {
             SignInVO vo = new SignInVO();
             vo.setExamDate(longToday);
             signInList.add(vo);
@@ -1006,9 +1008,9 @@ public class StudentApplyServiceImpl extends ServiceImpl<StudentApplyDao, Studen
     }
 
     private boolean isInTimePeriod(Date date, List<TimePeriodEntity> timePeriodList) {
-        for(TimePeriodEntity timePeriod : timePeriodList) {
+        for (TimePeriodEntity timePeriod : timePeriodList) {
             Date day = new Date(timePeriod.getStartTime());
-            if(DateUtils.isSameDay(day, date)) {
+            if (DateUtils.isSameDay(day, date)) {
                 return true;
             }
         }

+ 17 - 78
src/main/java/com/qmth/exam/reserve/service/impl/StudentAutoAssignServiceImpl.java

@@ -9,9 +9,7 @@ import com.qmth.exam.reserve.cache.CacheConstants;
 import com.qmth.exam.reserve.cache.impl.ApplyTaskCacheService;
 import com.qmth.exam.reserve.dao.StudentApplyDao;
 import com.qmth.exam.reserve.entity.*;
-import com.qmth.exam.reserve.entity.base.BaseEntity;
 import com.qmth.exam.reserve.service.*;
-import com.qmth.exam.reserve.util.DateUtil;
 import org.redisson.api.RLock;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -33,24 +31,18 @@ public class StudentAutoAssignServiceImpl extends ServiceImpl<StudentApplyDao, S
     @Autowired
     private StudentService studentService;
 
-    @Autowired
-    private TimePeriodService timePeriodService;
-
     @Autowired
     private ApplyTaskCacheService cacheService;
 
     @Autowired
     private CategoryService categoryService;
 
-    @Autowired
-    private ApplyTaskService applyTaskService;
-
     @Autowired
     private ExamSiteService examSiteService;
 
     @Async
     @Override
-    public void autoAssign(Long taskId, Long userId) {
+    public void autoAssign(Long taskId, Long userId, List<TimePeriodEntity> timeList) {
         log.warn("开始自动预约考试");
         long start = System.currentTimeMillis();
         RLock lock = (RLock) concurrentService.getLock(CacheConstants.LOCK_AUTO_APPLY);
@@ -66,12 +58,9 @@ public class StudentAutoAssignServiceImpl extends ServiceImpl<StudentApplyDao, S
             List<StudentEntity> studentList = studentService.listNoFinishStudent(taskId, Boolean.FALSE);
             Map<Long, List<StudentEntity>> noFinishApplyMap = studentList.stream().collect(Collectors.groupingBy(StudentEntity::getCategoryId));
             // 2、考位是否充足
-            List<TimePeriodEntity> timeList = listTimePeriod(taskId);
-            timeList = listNoCancelExamTimePeriod(timeList, taskId);
-            if (timeList == null || timeList.isEmpty()) {
-                throw new StatusException("可以取消预约的考试时段为0");
-            }
-            checkTeachingCapacity(noFinishApplyMap, timeList, taskId);
+            //List<TimePeriodEntity> timeList = listTimePeriod(taskId);
+            //timeList = listNoCancelExamTimePeriod(timeList, taskId);
+            //checkTeachingCapacity(noFinishApplyMap, timeList, taskId);
             // 3、按照教学点安排考位。规则:不能和已预约的时间上有冲突
             for (Long key : noFinishApplyMap.keySet()) {
                 List<ExamSiteEntity> siteList = listExamSite(key);
@@ -84,7 +73,11 @@ public class StudentAutoAssignServiceImpl extends ServiceImpl<StudentApplyDao, S
                             haveApplyNum = getHaveApplyNum(site.getId(), time.getId());
                         }
                         // 剩余的考位
-                        Integer remainNum = site.getCapacity() - haveApplyNum;
+                        int remainNum = site.getCapacity() - haveApplyNum;
+                        if (remainNum < 0) {
+                            log.warn("已预约的考生数超出考点的最大容量,redis中有脏数据。siteId:{} - timeId:{}", site.getId(), time.getId());
+                            throw new StatusException("数据异常");
+                        }
 
                         assignStudentApply(userId, site.getId(), time.getId(), teachingStudentList, remainNum);
                     }
@@ -112,41 +105,6 @@ public class StudentAutoAssignServiceImpl extends ServiceImpl<StudentApplyDao, S
         log.warn("完成自动预约,耗时:{}s", (end - start) / 1000);
     }
 
-    private List<TimePeriodEntity> listTimePeriod(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<TimePeriodEntity> listNoCancelExamTimePeriod(List<TimePeriodEntity> timeList, Long taskId) {
-        ApplyTaskEntity task = applyTaskService.getById(taskId);
-        Long longToday = DateUtil.getLongTimeByDate(DateUtil.formatShortSplitDateString(new Date()) + " 00:00:00");
-        Date today = new Date(longToday);
-        Date otherDay = DateUtil.addValues(today, Calendar.DAY_OF_MONTH, task.getAllowApplyCancelDays());
-        Long longOtherDay = DateUtil.getLongTimeByDate(DateUtil.formatShortSplitDateString(otherDay) + " 23:59:59");
-        return timeList.stream().filter(time -> time.getStartTime() > longOtherDay).collect(Collectors.toList());
-    }
-
-    private void checkTeachingCapacity(Map<Long, List<StudentEntity>> map, List<TimePeriodEntity> timeList, Long taskId) {
-        for (Long key : map.keySet()) {
-            List<ExamSiteEntity> siteList = listExamSite(key);
-            // 总考位数量
-            Integer total = siteList.stream().mapToInt(ExamSiteEntity::getCapacity).sum() * timeList.size();
-            // 已经预约的数量
-            Integer haveApplyNum =  baseMapper.getHaveApplyCount(siteList.stream().map(BaseEntity::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 List<ExamSiteEntity> listExamSite(Long categoryId) {
         LambdaQueryWrapper<ExamSiteEntity> wrapper = new LambdaQueryWrapper<>();
@@ -163,16 +121,13 @@ public class StudentAutoAssignServiceImpl extends ServiceImpl<StudentApplyDao, S
         return baseMapper.selectCount(wrapper);
     }
 
-    private List<StudentApplyEntity> assignStudentApply(Long userId, Long siteId, Long timeId,
-                                                        List<StudentEntity> teachingStudentList, Integer remainNum) {
-        List<StudentApplyEntity> insertApplyList = new ArrayList<>();
+    private void assignStudentApply(Long userId, Long siteId, Long timeId, List<StudentEntity> teachingStudentList, Integer remainNum) {
         int num = 0;
         String studentApplyLockKey = null;
         RLock studentApplyLock = null;
         for (Iterator<StudentEntity> iterator = teachingStudentList.iterator(); iterator.hasNext(); ) {
             StudentEntity student = iterator.next();
-            if (num >= remainNum)
-                break;
+            if (num >= remainNum) break;
             studentApplyLockKey = String.format(CacheConstants.LOCK_STUDENT_APPLY, student.getId());
             studentApplyLock = (RLock) concurrentService.getLock(studentApplyLockKey);
             try {
@@ -182,8 +137,9 @@ public class StudentAutoAssignServiceImpl extends ServiceImpl<StudentApplyDao, S
                 } else {
                     log.warn("获取锁成功!lockKey:{}", studentApplyLockKey);
 
-                    List<StudentApplyEntity> studentApplyList = listStudentApply(student.getId());
-                    int toApplyNum = student.getApplyNumber() - studentApplyList.size();
+                    //List<StudentApplyEntity> studentApplyList = listStudentApply(student.getId());
+                    int haveApplyCount = cacheService.getStudentApplyFinishCount(student.getId());
+                    int toApplyNum = student.getApplyNumber() - haveApplyCount;
                     if (toApplyNum > 0 && !haveApplySameTimePeriod(siteId, timeId, student.getId())) {
                         StudentApplyEntity studentApply = new StudentApplyEntity();
                         studentApply.setStudentId(student.getId());
@@ -199,10 +155,9 @@ public class StudentAutoAssignServiceImpl extends ServiceImpl<StudentApplyDao, S
                         } else {
                             baseMapper.insert(studentApply);
                         }
-                        insertApplyList.add(studentApply);
 
                         num++;
-                        if (student.getApplyNumber() - (studentApplyList.size() + 1) == 0) {
+                        if (student.getApplyNumber() - (haveApplyCount + 1) == 0) {
                             iterator.remove();
                         }
 
@@ -217,8 +172,7 @@ public class StudentAutoAssignServiceImpl extends ServiceImpl<StudentApplyDao, S
                         // 先推送至预约队列
                         cacheService.pushStudentApplyRecordQueue(bean);
                         cacheService.saveStudentApplyRecord(bean);
-                        cacheService.increaseApplyFinishCount(studentApply.getExamSiteId(),
-                                studentApply.getTimePeriodId());
+                        cacheService.increaseApplyFinishCount(studentApply.getExamSiteId(), studentApply.getTimePeriodId());
                     }
                 }
             } catch (Exception e) {
@@ -237,28 +191,13 @@ public class StudentAutoAssignServiceImpl extends ServiceImpl<StudentApplyDao, S
             }
 
         }
-        return insertApplyList;
-    }
-
-    private Integer getNoApplyNum(List<StudentEntity> list) {
-        int noApplyNum = 0;
-        for (StudentEntity student : list) {
-            if (student.getApplyNumber() == 1) {
-                noApplyNum++;
-            } else if (student.getApplyNumber() > 1) {
-                // listStudentApply(student.getId(), Boolean.FALSE).size()
-                int haveApplyNum = cacheService.getStudentApplyFinishCount(student.getId());
-                noApplyNum = noApplyNum + (student.getApplyNumber() - haveApplyNum);
-            }
-        }
-        return noApplyNum;
     }
 
     private List<StudentApplyEntity> listStudentApply(Long stdId) {
         LambdaQueryWrapper<StudentApplyEntity> lm = new LambdaQueryWrapper<>();
         lm.eq(StudentApplyEntity::getStudentId, stdId);
         lm.eq(StudentApplyEntity::getCancel, Boolean.FALSE);
-        return this.baseMapper.selectList(lm);
+        return baseMapper.selectList(lm);
     }
 
     private StudentApplyEntity findStudentApply(StudentApplyEntity studentApply) {

+ 4 - 0
src/main/resources/mapper/StudentApplyMapper.xml

@@ -53,6 +53,10 @@
         <foreach collection="examSiteIds" item="item" index="index" separator="," open="(" close=")">
             #{item}
         </foreach>
+        and sa.time_period_id in
+        <foreach collection="timePeriodIds" item="item" index="index" separator="," open="(" close=")">
+            #{item}
+        </foreach>
     </select>
 
     <select id="countApplyFinishForExamSiteAndTimePeriod" resultType="java.lang.Integer">