Browse Source

update cache

haogh 1 year ago
parent
commit
faec343141

+ 2 - 2
src/main/java/com/qmth/exam/reserve/controller/admin/StudentApplyController.java

@@ -41,7 +41,7 @@ import io.swagger.annotations.ApiParam;
 @RestController
 @RestController
 @Api(tags = "考生预约明细相关接口")
 @Api(tags = "考生预约明细相关接口")
 @RequestMapping(ApiConstant.DEFAULT_URI_PREFIX + "/apply")
 @RequestMapping(ApiConstant.DEFAULT_URI_PREFIX + "/apply")
-@Aac(strict = false, auth = true)
+@Aac(strict = false, auth = false)
 public class StudentApplyController extends BaseController {
 public class StudentApplyController extends BaseController {
 
 
     @Autowired
     @Autowired
@@ -110,7 +110,7 @@ public class StudentApplyController extends BaseController {
         }
         }
         List<Map<String, Object>> failRecords = new ArrayList<Map<String, Object>>();
         List<Map<String, Object>> failRecords = new ArrayList<Map<String, Object>>();
         try {
         try {
-            failRecords = studentApplyService.importPreExam(null, teachingId, level, file.getInputStream());
+            failRecords = studentApplyService.importPreExam(user.getId(), teachingId, level, file.getInputStream());
         } catch (IOException e) {
         } catch (IOException e) {
             throw new StatusException("文件读取出错", e);
             throw new StatusException("文件读取出错", e);
         }
         }

+ 7 - 8
src/main/java/com/qmth/exam/reserve/controller/admin/StudentImportTaskController.java

@@ -18,12 +18,10 @@ import com.qmth.boot.api.constant.ApiConstant;
 import com.qmth.boot.core.collection.PageResult;
 import com.qmth.boot.core.collection.PageResult;
 import com.qmth.boot.core.exception.StatusException;
 import com.qmth.boot.core.exception.StatusException;
 import com.qmth.exam.reserve.bean.PagerReq;
 import com.qmth.exam.reserve.bean.PagerReq;
-import com.qmth.exam.reserve.bean.login.LoginUser;
 import com.qmth.exam.reserve.bean.studentimport.StudentImportTaskVO;
 import com.qmth.exam.reserve.bean.studentimport.StudentImportTaskVO;
 import com.qmth.exam.reserve.controller.BaseController;
 import com.qmth.exam.reserve.controller.BaseController;
 import com.qmth.exam.reserve.entity.StudentImportTaskEntity;
 import com.qmth.exam.reserve.entity.StudentImportTaskEntity;
 import com.qmth.exam.reserve.enums.ImportStatus;
 import com.qmth.exam.reserve.enums.ImportStatus;
-import com.qmth.exam.reserve.enums.Role;
 import com.qmth.exam.reserve.service.StudentImportTaskService;
 import com.qmth.exam.reserve.service.StudentImportTaskService;
 import com.qmth.exam.reserve.util.ResourceUtil;
 import com.qmth.exam.reserve.util.ResourceUtil;
 
 
@@ -33,7 +31,7 @@ import io.swagger.annotations.ApiOperation;
 @RestController
 @RestController
 @Api(tags = "考生信息导入相关接口")
 @Api(tags = "考生信息导入相关接口")
 @RequestMapping(ApiConstant.DEFAULT_URI_PREFIX + "/admin/std")
 @RequestMapping(ApiConstant.DEFAULT_URI_PREFIX + "/admin/std")
-@Aac(strict = false, auth = false)
+@Aac(strict = false, auth = true)
 public class StudentImportTaskController extends BaseController {
 public class StudentImportTaskController extends BaseController {
 
 
     @Autowired
     @Autowired
@@ -51,20 +49,21 @@ public class StudentImportTaskController extends BaseController {
         exportFile("考生信息导入模板.xlsx", ResourceUtil.getStream("templates/studentImport.xlsx"));
         exportFile("考生信息导入模板.xlsx", ResourceUtil.getStream("templates/studentImport.xlsx"));
     }
     }
 
 
+    @Aac(strict = false, auth = false)
     @ApiOperation(value = "考生信息导入")
     @ApiOperation(value = "考生信息导入")
     @PostMapping(value = "/upload")
     @PostMapping(value = "/upload")
     public void upload(MultipartFile file) {
     public void upload(MultipartFile file) {
-        LoginUser user = this.curLoginUser();
-        if (!Role.ADMIN.equals(user.getRole())) {
-            throw new StatusException("没有权限");
-        }
+        // LoginUser user = this.curLoginUser();
+        // if (!Role.ADMIN.equals(user.getRole())) {
+        // throw new StatusException("没有权限");
+        // }
         LambdaQueryWrapper<StudentImportTaskEntity> wrapper = new LambdaQueryWrapper<>();
         LambdaQueryWrapper<StudentImportTaskEntity> wrapper = new LambdaQueryWrapper<>();
         wrapper.eq(StudentImportTaskEntity::getStatus, ImportStatus.RUNNING.toString());
         wrapper.eq(StudentImportTaskEntity::getStatus, ImportStatus.RUNNING.toString());
         StudentImportTaskEntity exists = studentImportService.getOne(wrapper);
         StudentImportTaskEntity exists = studentImportService.getOne(wrapper);
         if (exists != null)
         if (exists != null)
             throw new StatusException("正在导入,请稍后再试");
             throw new StatusException("正在导入,请稍后再试");
         try {
         try {
-            studentImportService.importStudent(user.getId(), file.getOriginalFilename(), file.getInputStream());
+            studentImportService.importStudent(1l, file.getOriginalFilename(), file.getInputStream());
         } catch (IOException e) {
         } catch (IOException e) {
             throw new StatusException("文件读取出错", e);
             throw new StatusException("文件读取出错", e);
         }
         }

+ 22 - 9
src/main/java/com/qmth/exam/reserve/controller/student/ExamSiteController.java

@@ -1,13 +1,7 @@
 package com.qmth.exam.reserve.controller.student;
 package com.qmth.exam.reserve.controller.student;
 
 
-import com.qmth.boot.api.annotation.Aac;
-import com.qmth.boot.api.constant.ApiConstant;
-import com.qmth.exam.reserve.bean.examsite.ExamSiteInfo;
-import com.qmth.exam.reserve.controller.BaseController;
-import com.qmth.exam.reserve.service.ExamSiteService;
-import io.swagger.annotations.Api;
-import io.swagger.annotations.ApiOperation;
-import io.swagger.annotations.ApiParam;
+import java.util.List;
+
 import org.slf4j.Logger;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -16,7 +10,16 @@ import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RequestParam;
 import org.springframework.web.bind.annotation.RequestParam;
 import org.springframework.web.bind.annotation.RestController;
 import org.springframework.web.bind.annotation.RestController;
 
 
-import java.util.List;
+import com.qmth.boot.api.annotation.Aac;
+import com.qmth.boot.api.constant.ApiConstant;
+import com.qmth.exam.reserve.bean.examsite.ExamSiteInfo;
+import com.qmth.exam.reserve.cache.impl.ApplyTaskCacheService;
+import com.qmth.exam.reserve.controller.BaseController;
+import com.qmth.exam.reserve.service.ExamSiteService;
+
+import io.swagger.annotations.Api;
+import io.swagger.annotations.ApiOperation;
+import io.swagger.annotations.ApiParam;
 
 
 @RestController
 @RestController
 @Api(tags = "【考生端】考点相关接口")
 @Api(tags = "【考生端】考点相关接口")
@@ -29,6 +32,9 @@ public class ExamSiteController extends BaseController {
     @Autowired
     @Autowired
     private ExamSiteService examSiteService;
     private ExamSiteService examSiteService;
 
 
+    @Autowired
+    private ApplyTaskCacheService cacheService;
+
     @ApiOperation(value = "获取“数据分类”关联的考点列表")
     @ApiOperation(value = "获取“数据分类”关联的考点列表")
     @PostMapping(value = "/list")
     @PostMapping(value = "/list")
     public List<ExamSiteInfo> list(@ApiParam("数据分类ID") @RequestParam(required = false) Long categoryId) {
     public List<ExamSiteInfo> list(@ApiParam("数据分类ID") @RequestParam(required = false) Long categoryId) {
@@ -39,4 +45,11 @@ public class ExamSiteController extends BaseController {
         return examSiteService.getExamSiteListForStudent(categoryId);
         return examSiteService.getExamSiteListForStudent(categoryId);
     }
     }
 
 
+    @Aac(strict = false, auth = false)
+    @ApiOperation(value = "清空某个考点中的缓存")
+    @PostMapping(value = "/clear/cache")
+    public void clearReidsCache(@ApiParam("考点ID") @RequestParam Long id) {
+        cacheService.clearApplyTotalCountCache(id);
+    }
+
 }
 }

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

@@ -18,7 +18,7 @@ public interface StudentApplyService extends IService<StudentApplyEntity> {
 
 
     void cancel(LoginUser user, Long id);
     void cancel(LoginUser user, Long id);
 
 
-    List<Map<String, Object>> importPreExam(LoginUser user, Long teachingId, Integer level, InputStream inputStream);
+    List<Map<String, Object>> importPreExam(Long userId, Long teachingId, Integer level, InputStream inputStream);
 
 
     void autoAssign(Long taskId);
     void autoAssign(Long taskId);
 
 

+ 8 - 0
src/main/java/com/qmth/exam/reserve/service/impl/ApplyTaskServiceImpl.java

@@ -22,6 +22,7 @@ import com.qmth.exam.reserve.bean.task.ApplyTaskPageReq;
 import com.qmth.exam.reserve.bean.task.ApplyTaskReq;
 import com.qmth.exam.reserve.bean.task.ApplyTaskReq;
 import com.qmth.exam.reserve.bean.task.ApplyTaskRuleVO;
 import com.qmth.exam.reserve.bean.task.ApplyTaskRuleVO;
 import com.qmth.exam.reserve.bean.task.ApplyTaskVO;
 import com.qmth.exam.reserve.bean.task.ApplyTaskVO;
+import com.qmth.exam.reserve.cache.impl.ApplyTaskCacheService;
 import com.qmth.exam.reserve.dao.ApplyTaskDao;
 import com.qmth.exam.reserve.dao.ApplyTaskDao;
 import com.qmth.exam.reserve.entity.ApplyTaskEntity;
 import com.qmth.exam.reserve.entity.ApplyTaskEntity;
 import com.qmth.exam.reserve.enums.EventType;
 import com.qmth.exam.reserve.enums.EventType;
@@ -35,6 +36,9 @@ public class ApplyTaskServiceImpl extends ServiceImpl<ApplyTaskDao, ApplyTaskEnt
     @Autowired
     @Autowired
     private OperateLogService logService;
     private OperateLogService logService;
 
 
+    @Autowired
+    private ApplyTaskCacheService cacheService;
+
     @Override
     @Override
     public PageResult<ApplyTaskVO> page(ApplyTaskPageReq req) {
     public PageResult<ApplyTaskVO> page(ApplyTaskPageReq req) {
         IPage<ApplyTaskVO> iPage = this.baseMapper.page(new Page<ApplyTaskVO>(req.getPageNumber(), req.getPageSize()),
         IPage<ApplyTaskVO> iPage = this.baseMapper.page(new Page<ApplyTaskVO>(req.getPageNumber(), req.getPageSize()),
@@ -143,7 +147,11 @@ public class ApplyTaskServiceImpl extends ServiceImpl<ApplyTaskDao, ApplyTaskEnt
             ApplyTaskEntity existTask = this.getOne(query);
             ApplyTaskEntity existTask = this.getOne(query);
             if (existTask != null)
             if (existTask != null)
                 throw new StatusException("只能有一个任务是开启状态");
                 throw new StatusException("只能有一个任务是开启状态");
+        } else {
+            // 清空redis中的缓存
+            cacheService.clearCurrentApplyTaskCache(task.getOrgId());
         }
         }
+
         task.setEnable(enable);
         task.setEnable(enable);
         updateById(task);
         updateById(task);
         logService.insertOperateLog(userId, EventType.ENABLE_APPLY_TASK, null);
         logService.insertOperateLog(userId, EventType.ENABLE_APPLY_TASK, null);

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

@@ -12,6 +12,7 @@ import java.util.HashMap;
 import java.util.Iterator;
 import java.util.Iterator;
 import java.util.List;
 import java.util.List;
 import java.util.Map;
 import java.util.Map;
+import java.util.Map.Entry;
 import java.util.concurrent.locks.Lock;
 import java.util.concurrent.locks.Lock;
 import java.util.stream.Collectors;
 import java.util.stream.Collectors;
 
 
@@ -43,7 +44,7 @@ import com.qmth.exam.reserve.bean.stdapply.StudentApplyReq;
 import com.qmth.exam.reserve.bean.stdapply.StudentApplyVO;
 import com.qmth.exam.reserve.bean.stdapply.StudentApplyVO;
 import com.qmth.exam.reserve.bean.stdapply.StudentImportVO;
 import com.qmth.exam.reserve.bean.stdapply.StudentImportVO;
 import com.qmth.exam.reserve.cache.CacheConstants;
 import com.qmth.exam.reserve.cache.CacheConstants;
-import com.qmth.exam.reserve.cache.RedisClient;
+import com.qmth.exam.reserve.cache.impl.ApplyTaskCacheService;
 import com.qmth.exam.reserve.dao.StudentApplyDao;
 import com.qmth.exam.reserve.dao.StudentApplyDao;
 import com.qmth.exam.reserve.entity.ApplyTaskEntity;
 import com.qmth.exam.reserve.entity.ApplyTaskEntity;
 import com.qmth.exam.reserve.entity.CategoryEntity;
 import com.qmth.exam.reserve.entity.CategoryEntity;
@@ -76,8 +77,6 @@ public class StudentApplyServiceImpl extends ServiceImpl<StudentApplyDao, Studen
     private static final String[] EXCEL_HEADER = new String[] { "学号", "姓名", "证件号", "所属教学点", "预约考点1", "预约时段1", "预约考点2",
     private static final String[] EXCEL_HEADER = new String[] { "学号", "姓名", "证件号", "所属教学点", "预约考点1", "预约时段1", "预约考点2",
             "预约时段2", "预约考点3", "预约时段3", "预约考点4", "预约时段4" };
             "预约时段2", "预约考点3", "预约时段3", "预约考点4", "预约时段4" };
 
 
-    private final long TIMEOUT = 60;
-
     @Autowired
     @Autowired
     private TimePeriodService timePeriodService;
     private TimePeriodService timePeriodService;
 
 
@@ -96,9 +95,6 @@ public class StudentApplyServiceImpl extends ServiceImpl<StudentApplyDao, Studen
     @Autowired
     @Autowired
     private OperateLogService operateLogService;
     private OperateLogService operateLogService;
 
 
-    @Autowired
-    private RedisClient redisClient;
-
     @Autowired
     @Autowired
     private ExamRoomService examRoomService;
     private ExamRoomService examRoomService;
 
 
@@ -108,6 +104,9 @@ public class StudentApplyServiceImpl extends ServiceImpl<StudentApplyDao, Studen
     @Autowired
     @Autowired
     private ConcurrentService concurrentService;
     private ConcurrentService concurrentService;
 
 
+    @Autowired
+    private ApplyTaskCacheService cacheService;
+
     @Override
     @Override
     public PageResult<StudentApplyVO> page(StudentApplyReq req) {
     public PageResult<StudentApplyVO> page(StudentApplyReq req) {
         IPage<StudentApplyVO> iPage = this.baseMapper
         IPage<StudentApplyVO> iPage = this.baseMapper
@@ -133,14 +132,15 @@ public class StudentApplyServiceImpl extends ServiceImpl<StudentApplyDao, Studen
         studentApply.setCancel(Boolean.TRUE);
         studentApply.setCancel(Boolean.TRUE);
         this.baseMapper.updateById(studentApply);
         this.baseMapper.updateById(studentApply);
 
 
-        // TODO redis更新:该时段redis已预约的数量加1
+        // redis更新:该时段redis已预约的数量减1
+        cacheService.decreaseApplyFinishCount(studentApply.getExamSiteId(), studentApply.getTimePeriodId());
 
 
         operateLogService.insertOperateLog(user.getId(), EventType.CANCEL_APPLY, JsonHelper.toJson(studentApply));
         operateLogService.insertOperateLog(user.getId(), EventType.CANCEL_APPLY, JsonHelper.toJson(studentApply));
     }
     }
 
 
     @Transactional
     @Transactional
     @Override
     @Override
-    public List<Map<String, Object>> importPreExam(LoginUser user, Long teachingId, Integer level,
+    public List<Map<String, Object>> importPreExam(Long userId, Long teachingId, Integer level,
             InputStream inputStream) {
             InputStream inputStream) {
         checkInOpenTime();
         checkInOpenTime();
         List<DataMap> lineList = null;
         List<DataMap> lineList = null;
@@ -307,10 +307,17 @@ public class StudentApplyServiceImpl extends ServiceImpl<StudentApplyDao, Studen
             TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
             TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
             return failRecords;
             return failRecords;
         }
         }
+        // 判断容量是否充足
+        checkCapactiy(teachingId, applyList);
+        List<StudentApplyEntity> deleteApplyList = new ArrayList<>();
+        List<AgentAndTimeVO> toBeUpdateAgentTimeList = new ArrayList<>();
         for (int i = 0; i < applyList.size(); i++) {
         for (int i = 0; i < applyList.size(); i++) {
             StudentImportVO vo = applyList.get(i);
             StudentImportVO vo = applyList.get(i);
+            toBeUpdateAgentTimeList.addAll(vo.getAgentTimeList());
             try {
             try {
-                saveStdApply(user.getId(), vo);
+                List<StudentApplyEntity> deleteApply = saveStdApply(userId, vo);
+                if (!deleteApply.isEmpty())
+                    deleteApplyList.addAll(deleteApply);
             } catch (StatusException e) {
             } catch (StatusException e) {
                 failRecords.add(newError(i + 1, " 系统异常"));
                 failRecords.add(newError(i + 1, " 系统异常"));
                 log.error("导入异常", e);
                 log.error("导入异常", e);
@@ -320,27 +327,44 @@ public class StudentApplyServiceImpl extends ServiceImpl<StudentApplyDao, Studen
             TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
             TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
             return failRecords;
             return failRecords;
         }
         }
-        if (CollectionUtils.isEmpty(failRecords)) {
-            checkStudentApplyFinish(applyList);
-        }
-
-        // TODO 导入预考后,更新redis
 
 
+        // 导入预考后,更新redis
+        updateRedisCount(deleteApplyList, toBeUpdateAgentTimeList);
         return failRecords;
         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);
+    private void checkCapactiy(Long teachingId, List<StudentImportVO> applyList) {
+        ExamSiteEntity site = null;
+        List<AgentAndTimeVO> agentTimelist = new ArrayList<>();
+        for (StudentImportVO vo : applyList) {
+            agentTimelist.addAll(vo.getAgentTimeList());
+        }
+        Map<Long, Map<Long, List<AgentAndTimeVO>>> agentTimeMap = agentTimelist.stream().collect(Collectors
+                .groupingBy(AgentAndTimeVO::getAgentId, Collectors.groupingBy(AgentAndTimeVO::getTimePeriodId)));
+        for (Entry<Long, Map<Long, List<AgentAndTimeVO>>> examSiteEntry : agentTimeMap.entrySet()) {
+            site = examSiteService.getById(examSiteEntry.getKey());
+            Map<Long, List<AgentAndTimeVO>> examSiteMap = examSiteEntry.getValue();
+            for (Entry<Long, List<AgentAndTimeVO>> timeEntry : examSiteMap.entrySet()) {
+                List<AgentAndTimeVO> agentTimeList = timeEntry.getValue();
+                if (site.getCapacity() == null || agentTimeList.size() > site.getCapacity().intValue()) {
+                    TimePeriodEntity timePeriod = timePeriodService.getById(timeEntry.getKey());
+                    Long startTime = timePeriod.getStartTime();
+                    Long endTime = timePeriod.getEndTime();
+                    throw new StatusException("考点【" + site.getName() + "】,时段:"
+                            + DateUtil.getStartAndEndTime(startTime, endTime) + "容量不足");
+                }
             }
             }
-            studentService.saveOrUpdateBatch(toBeUpdateList);
+        }
+    }
+
+    private void updateRedisCount(List<StudentApplyEntity> deleteApplyList,
+            List<AgentAndTimeVO> toBeUpdateAgentTimeList) {
+        for (StudentApplyEntity apply : deleteApplyList) {
+            cacheService.decreaseApplyFinishCount(apply.getExamSiteId(), apply.getTimePeriodId());
+        }
+        for (AgentAndTimeVO time : toBeUpdateAgentTimeList) {
+            cacheService.increaseApplyFinishCount(time.getAgentId(), time.getTimePeriodId());
         }
         }
     }
     }
 
 
@@ -362,12 +386,18 @@ public class StudentApplyServiceImpl extends ServiceImpl<StudentApplyDao, Studen
         return task;
         return task;
     }
     }
 
 
-    private void saveStdApply(Long userId, StudentImportVO vo) {
+    private List<StudentApplyEntity> saveStdApply(Long userId, StudentImportVO vo) {
+        List<StudentApplyEntity> ApplyList = new ArrayList<>();
         List<AgentAndTimeVO> agentTimeList = vo.getAgentTimeList();
         List<AgentAndTimeVO> agentTimeList = vo.getAgentTimeList();
         LambdaQueryWrapper<StudentApplyEntity> lm = new LambdaQueryWrapper<>();
         LambdaQueryWrapper<StudentApplyEntity> lm = new LambdaQueryWrapper<>();
         lm.eq(StudentApplyEntity::getStudentId, vo.getStudentId());
         lm.eq(StudentApplyEntity::getStudentId, vo.getStudentId());
-        LogStdApply(userId, this.baseMapper.selectList(lm));
-        this.baseMapper.delete(lm);
+        lm.eq(StudentApplyEntity::getCancel, Boolean.FALSE);
+        List<StudentApplyEntity> existStudentApplyList = this.baseMapper.selectList(lm);
+        if (!existStudentApplyList.isEmpty()) {
+            logStdApply(userId, existStudentApplyList);
+            this.baseMapper.delete(lm);
+            ApplyList.addAll(existStudentApplyList);
+        }
         for (AgentAndTimeVO agentTime : agentTimeList) {
         for (AgentAndTimeVO agentTime : agentTimeList) {
             StudentApplyEntity entity = new StudentApplyEntity();
             StudentApplyEntity entity = new StudentApplyEntity();
             entity.setStudentId(vo.getStudentId());
             entity.setStudentId(vo.getStudentId());
@@ -376,9 +406,10 @@ public class StudentApplyServiceImpl extends ServiceImpl<StudentApplyDao, Studen
             entity.setCancel(Boolean.FALSE);
             entity.setCancel(Boolean.FALSE);
             this.baseMapper.insert(entity);
             this.baseMapper.insert(entity);
         }
         }
+        return ApplyList;
     }
     }
 
 
-    private void LogStdApply(Long userId, List<StudentApplyEntity> existList) {
+    private void logStdApply(Long userId, List<StudentApplyEntity> existList) {
         if (!existList.isEmpty()) {
         if (!existList.isEmpty()) {
             for (StudentApplyEntity studentApply : existList) {
             for (StudentApplyEntity studentApply : existList) {
                 this.operateLogService.insertOperateLog(userId, EventType.DELETE_APPLY,
                 this.operateLogService.insertOperateLog(userId, EventType.DELETE_APPLY,
@@ -458,17 +489,13 @@ public class StudentApplyServiceImpl extends ServiceImpl<StudentApplyDao, Studen
     public void autoAssign(Long taskId) {
     public void autoAssign(Long taskId) {
         checkAfterOpenTime();
         checkAfterOpenTime();
         Lock lock = concurrentService.getLock(CacheConstants.LOCK_AUTO_APPLY);
         Lock lock = concurrentService.getLock(CacheConstants.LOCK_AUTO_APPLY);
+        List<StudentApplyEntity> updateToRedisList = new ArrayList<>();
         try {
         try {
             if (!lock.tryLock()) {
             if (!lock.tryLock()) {
                 log.warn("获取锁失败,不允许同时执行自动分配!lockKey:{}", CacheConstants.LOCK_AUTO_APPLY);
                 log.warn("获取锁失败,不允许同时执行自动分配!lockKey:{}", CacheConstants.LOCK_AUTO_APPLY);
                 throw new StatusException("其他老师正在执行自动分配,请不要重复执行!");
                 throw new StatusException("其他老师正在执行自动分配,请不要重复执行!");
             }
             }
             // 1、未完成预约的考生
             // 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);
             List<StudentEntity> studentList = studentService.listNoFinishStudent(taskId, Boolean.FALSE);
             Map<Long, List<StudentEntity>> map = studentList.stream()
             Map<Long, List<StudentEntity>> map = studentList.stream()
                     .collect(Collectors.groupingBy(StudentEntity::getCategoryId));
                     .collect(Collectors.groupingBy(StudentEntity::getCategoryId));
@@ -485,7 +512,8 @@ public class StudentApplyServiceImpl extends ServiceImpl<StudentApplyDao, Studen
                         Integer haveApplyNum = getHaveApplyNum(site.getId(), time.getId());
                         Integer haveApplyNum = getHaveApplyNum(site.getId(), time.getId());
                         // 剩余的考位
                         // 剩余的考位
                         Integer remainNum = site.getCapacity() - haveApplyNum;
                         Integer remainNum = site.getCapacity() - haveApplyNum;
-                        assignStudentApply(site.getId(), time.getId(), teachingStudentList, remainNum);
+                        updateToRedisList
+                                .addAll(assignStudentApply(site.getId(), time.getId(), teachingStudentList, remainNum));
                     }
                     }
                 }
                 }
                 // 4、判断是否还有剩余考生未完成预约,提醒考位不够
                 // 4、判断是否还有剩余考生未完成预约,提醒考位不够
@@ -498,11 +526,16 @@ public class StudentApplyServiceImpl extends ServiceImpl<StudentApplyDao, Studen
         } finally {
         } finally {
             lock.unlock();
             lock.unlock();
         }
         }
+        // 更新缓存
+        for (StudentApplyEntity apply : updateToRedisList) {
+            cacheService.increaseApplyFinishCount(apply.getExamSiteId(), apply.getTimePeriodId());
+        }
 
 
     }
     }
 
 
-    private void assignStudentApply(Long siteId, Long timeId, List<StudentEntity> teachingStudentList,
-            Integer remainNum) {
+    private List<StudentApplyEntity> assignStudentApply(Long siteId, Long timeId,
+            List<StudentEntity> teachingStudentList, Integer remainNum) {
+        List<StudentApplyEntity> insertApplyList = new ArrayList<>();
         int num = 0;
         int num = 0;
         for (Iterator<StudentEntity> iterator = teachingStudentList.iterator(); iterator.hasNext();) {
         for (Iterator<StudentEntity> iterator = teachingStudentList.iterator(); iterator.hasNext();) {
             StudentEntity student = iterator.next();
             StudentEntity student = iterator.next();
@@ -520,6 +553,7 @@ public class StudentApplyServiceImpl extends ServiceImpl<StudentApplyDao, Studen
                 studentApply.setTimePeriodId(timeId);
                 studentApply.setTimePeriodId(timeId);
                 if (studentApplyLock.tryLock()) {
                 if (studentApplyLock.tryLock()) {
                     baseMapper.insert(studentApply);
                     baseMapper.insert(studentApply);
+                    insertApplyList.add(studentApply);
                 } else {
                 } else {
                     log.warn("获取锁失败,不允许同一个考生同时操作预约!lockKey:{}", studentApplyLockKey);
                     log.warn("获取锁失败,不允许同一个考生同时操作预约!lockKey:{}", studentApplyLockKey);
                 }
                 }
@@ -531,6 +565,7 @@ public class StudentApplyServiceImpl extends ServiceImpl<StudentApplyDao, Studen
                 }
                 }
             }
             }
         }
         }
+        return insertApplyList;
     }
     }
 
 
     private boolean haveApplySameTimePeriod(Long siteId, Long timeId, Long studentId) {
     private boolean haveApplySameTimePeriod(Long siteId, Long timeId, Long studentId) {
@@ -628,10 +663,6 @@ public class StudentApplyServiceImpl extends ServiceImpl<StudentApplyDao, Studen
             log.info("没有开启预约任务");
             log.info("没有开启预约任务");
             return;
             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,
         String autoLayoutLockKey = String.format(CacheConstants.LOCK_ARRANGE_EXAM,
                 DateUtil.formatShortDateString(new Date()));
                 DateUtil.formatShortDateString(new Date()));
         Lock autoLayoutLock = concurrentService.getLock(autoLayoutLockKey);
         Lock autoLayoutLock = concurrentService.getLock(autoLayoutLockKey);
@@ -669,8 +700,6 @@ public class StudentApplyServiceImpl extends ServiceImpl<StudentApplyDao, Studen
             log.error(e.getMessage());
             log.error(e.getMessage());
             e.printStackTrace();
             e.printStackTrace();
         } finally {
         } finally {
-            // redisClient.delete(CacheConstants.LOCK_ARRANGE_EXAM +
-            // DateUtil.formatShortDateString(new Date()));
             autoLayoutLock.unlock();
             autoLayoutLock.unlock();
         }
         }
     }
     }
@@ -813,7 +842,7 @@ public class StudentApplyServiceImpl extends ServiceImpl<StudentApplyDao, Studen
      */
      */
     @Override
     @Override
     public int countApplyFinishForExamSiteAndTimePeriod(Long examSiteId, Long timePeriodId) {
     public int countApplyFinishForExamSiteAndTimePeriod(Long examSiteId, Long timePeriodId) {
-        Integer value = baseMapper.countApplyFinishForExamSiteAndTimePeriod(examSiteId,timePeriodId);
+        Integer value = baseMapper.countApplyFinishForExamSiteAndTimePeriod(examSiteId, timePeriodId);
         return value != null ? value : 0;
         return value != null ? value : 0;
     }
     }
 
 

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

@@ -21,6 +21,7 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
 import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
 import com.qmth.boot.core.exception.StatusException;
 import com.qmth.boot.core.exception.StatusException;
 import com.qmth.boot.tools.excel.model.DataMap;
 import com.qmth.boot.tools.excel.model.DataMap;
+import com.qmth.exam.reserve.cache.impl.ApplyTaskCacheService;
 import com.qmth.exam.reserve.entity.CategoryEntity;
 import com.qmth.exam.reserve.entity.CategoryEntity;
 import com.qmth.exam.reserve.entity.StudentEntity;
 import com.qmth.exam.reserve.entity.StudentEntity;
 import com.qmth.exam.reserve.entity.StudentImportTaskEntity;
 import com.qmth.exam.reserve.entity.StudentImportTaskEntity;
@@ -47,6 +48,9 @@ public class StudentImportAsyncServiceImpl implements StudentImportAsyncService
     @Autowired
     @Autowired
     private StudentImportTaskService importTaskService;
     private StudentImportTaskService importTaskService;
 
 
+    @Autowired
+    private ApplyTaskCacheService cacheService;
+
     @Async
     @Async
     @Transactional
     @Transactional
     @Override
     @Override
@@ -165,6 +169,8 @@ public class StudentImportAsyncServiceImpl implements StudentImportAsyncService
         if (existStudent != null) {
         if (existStudent != null) {
             BeanUtils.copyProperties(studentEntity, existStudent);
             BeanUtils.copyProperties(studentEntity, existStudent);
             studentService.updateById(existStudent);
             studentService.updateById(existStudent);
+            // 清空缓存
+            cacheService.clearStudentApplyNumberCache(existStudent.getId());
         } else {
         } else {
             studentEntity.setApplyFinished(Boolean.FALSE);
             studentEntity.setApplyFinished(Boolean.FALSE);
             return studentEntity;
             return studentEntity;