haogh 1 anno fa
parent
commit
d77c196bd0

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

@@ -34,4 +34,6 @@ public interface StudentApplyDao extends BaseMapper<StudentApplyEntity> {
 
     List<StudentApplyVO> listStudentApply(@Param("timePeriodId") Long timePeriodId, @Param("roomId") Long roomId);
 
+    Integer getNoApplyCount(@Param(value = "taskId") Long taskId, @Param(value = "categoryId") Long categoryId, @Param(value = "timePeriodIds") List<Long> timePeriodIds);
+
 }

+ 3 - 0
src/main/java/com/qmth/exam/reserve/dao/StudentDao.java

@@ -1,6 +1,7 @@
 package com.qmth.exam.reserve.dao;
 
 import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+import com.qmth.exam.reserve.bean.stdapply.CategoryVO;
 import com.qmth.exam.reserve.bean.student.StudentInfo;
 import com.qmth.exam.reserve.entity.StudentEntity;
 import org.apache.ibatis.annotations.Param;
@@ -13,4 +14,6 @@ public interface StudentDao extends BaseMapper<StudentEntity> {
 
     StudentInfo findInfoById(@Param("studentId") Long studentId);
 
+    List<CategoryVO> listNoFinishCategory(@Param("taskId") Long taskId, @Param("cancel") Boolean cancel);
+
 }

+ 3 - 0
src/main/java/com/qmth/exam/reserve/service/StudentService.java

@@ -1,6 +1,7 @@
 package com.qmth.exam.reserve.service;
 
 import com.baomidou.mybatisplus.extension.service.IService;
+import com.qmth.exam.reserve.bean.stdapply.CategoryVO;
 import com.qmth.exam.reserve.bean.student.StudentInfo;
 import com.qmth.exam.reserve.bean.student.WechatBindReq;
 import com.qmth.exam.reserve.entity.StudentEntity;
@@ -25,4 +26,6 @@ public interface StudentService extends IService<StudentEntity> {
 
     List<StudentEntity> listNoFinishStudent(Long taskId, Boolean cancel);
 
+    List<CategoryVO> listNoFinishCategory(Long taskId, Boolean cancel);
+
 }

+ 9 - 22
src/main/java/com/qmth/exam/reserve/service/impl/StudentApplyServiceImpl.java

@@ -655,16 +655,15 @@ 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<CategoryVO> categoryList = studentService.listNoFinishCategory(taskId, Boolean.FALSE);
         List<TimePeriodEntity> timeList = listTimePeriod(taskId);
         timeList = listNoCancelExamTimePeriod(timeList, taskId);
         if (timeList == null || timeList.isEmpty()) {
             throw new StatusException("可以取消预约的考试时段为0");
         }
-        checkTeachingCapacity(noFinishApplyMap, timeList);
+        // 考位是否充足
+        checkTeachingCapacity(taskId, categoryList, timeList);
         RLock lock = (RLock) concurrentService.getLock(CacheConstants.LOCK_AUTO_APPLY);
         if (lock.isLocked()) {
             throw new StatusException("其他老师正在执行自动分配,请不要重复执行!");
@@ -673,9 +672,9 @@ public class StudentApplyServiceImpl extends ServiceImpl<StudentApplyDao, Studen
         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);
+    private void checkTeachingCapacity(Long taskId, List<CategoryVO> categoryList, List<TimePeriodEntity> timeList) {
+        for (CategoryVO vo : categoryList) {
+            List<ExamSiteEntity> siteList = listExamSite(vo.getId(), null);
             // 总考位数量
             Integer total = siteList.stream().mapToInt(ExamSiteEntity::getCapacity).sum() * timeList.size();
             // 已经预约的数量
@@ -683,26 +682,14 @@ public class StudentApplyServiceImpl extends ServiceImpl<StudentApplyDao, Studen
             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));
+            Integer noApplyNum = baseMapper.getNoApplyCount(taskId,vo.getId(),timePeriodIds);
             if (noApplyNum > total - haveApplyNum) {
-                CategoryEntity category = categoryService.getById(key);
+                CategoryEntity category = categoryService.getById(vo.getId());
                 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);

+ 6 - 0
src/main/java/com/qmth/exam/reserve/service/impl/StudentServiceImpl.java

@@ -4,6 +4,7 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import com.qmth.boot.core.exception.StatusException;
+import com.qmth.exam.reserve.bean.stdapply.CategoryVO;
 import com.qmth.exam.reserve.bean.student.StudentInfo;
 import com.qmth.exam.reserve.bean.student.WechatBindReq;
 import com.qmth.exam.reserve.cache.impl.CategoryCacheService;
@@ -140,4 +141,9 @@ public class StudentServiceImpl extends ServiceImpl<StudentDao, StudentEntity> i
         return this.baseMapper.listNoFinishStudent(taskId, cancel);
     }
 
+    @Override
+    public List<CategoryVO> listNoFinishCategory(Long taskId, Boolean cancel) {
+        return baseMapper.listNoFinishCategory(taskId, cancel);
+    }
+
 }

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

@@ -136,4 +136,27 @@
 	
 	</select>
 
+    <select id="getNoApplyCount" resultType="int">
+        SELECT
+        a.total - b.have_total noApplyNum
+        FROM
+        ( SELECT sum( apply_number ) total FROM t_student ts WHERE apply_task_id = #{taskId} AND category_id = #{categoryId} ) a,
+        (
+        SELECT
+        count( sa.id ) have_total
+        FROM
+        t_student_apply sa,
+        t_exam_site s
+        WHERE
+        sa.exam_site_id = s.id
+        AND sa.cancel = 0
+        AND sa.student_id IN ( SELECT id FROM t_student ts WHERE apply_task_id = #{taskId} AND category_id = #{categoryId} )
+        AND sa.time_period_id in
+        <foreach collection="timePeriodIds" item="item" index="index" separator="," open="(" close=")">
+            #{item}
+        </foreach>
+        ) b
+
+    </select>
+
 </mapper>

+ 12 - 1
src/main/resources/mapper/StudentMapper.xml

@@ -2,7 +2,7 @@
 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 <mapper namespace="com.qmth.exam.reserve.dao.StudentDao">
     <select id="listNoFinishStudent" resultType="com.qmth.exam.reserve.entity.StudentEntity">
-        SELECT s.*
+        SELECT s.id,s.apply_number,s.category_id
         FROM t_student s
                  LEFT JOIN (SELECT student_id, count(1) nums
                             FROM t_student_apply a
@@ -31,4 +31,15 @@
         where stu.id = #{studentId}
     </select>
 
+    <select id="listNoFinishCategory" resultType="com.qmth.exam.reserve.bean.stdapply.CategoryVO">
+        SELECT distinct s.category_id id
+        FROM t_student s
+                 LEFT JOIN (SELECT student_id, count(1) nums
+                            FROM t_student_apply a
+                            WHERE a.cancel = #{cancel}
+                            GROUP BY student_id) ts ON ts.student_id = s.id
+        WHERE apply_task_id = #{taskId}
+          AND s.apply_number - ifnull(ts.nums, 0) > 0
+    </select>
+
 </mapper>