deason 1 year ago
parent
commit
3b6ea3b12f

+ 6 - 2
src/main/java/com/qmth/exam/reserve/cache/CacheConstants.java

@@ -8,17 +8,21 @@ public interface CacheConstants {
     /**
      * 预约任务 - 某考点某时段的“可预约总量”的缓存
      * $cache:apply_total:{examSiteId}_{timePeriodId}
-     * Value:总量
      */
     String CACHE_APPLY_TOTAL = "$cache:apply_total:%s_%s";
 
     /**
      * 预约任务 - 某考点某时段的“已预约数量”的缓存
      * $cache:apply_finish:{examSiteId}_{timePeriodId}
-     * Value:已约量
      */
     String CACHE_APPLY_FINISH = "$cache:apply_finish:%s_%s";
 
+    /**
+     * 某考生的“允许预约时段次数”的缓存
+     * $cache:student_apply_number:{studentId}
+     */
+    String CACHE_STUDENT_APPLY_NUMBER = "$cache:student_apply_number:%s";
+
     /**
      * 某考生预约操作锁
      * $lock:student_apply:{studentId}

+ 22 - 1
src/main/java/com/qmth/exam/reserve/cache/impl/ApplyTaskCacheService.java

@@ -4,6 +4,7 @@ import com.qmth.exam.reserve.cache.CacheConstants;
 import com.qmth.exam.reserve.cache.RedisClient;
 import com.qmth.exam.reserve.service.ExamSiteService;
 import com.qmth.exam.reserve.service.StudentApplyService;
+import com.qmth.exam.reserve.service.StudentService;
 import org.redisson.api.RAtomicLong;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -23,9 +24,29 @@ public class ApplyTaskCacheService implements CacheConstants {
     @Autowired
     private ExamSiteService examSiteService;
 
+    @Autowired
+    private StudentService studentService;
+
     @Autowired
     private StudentApplyService studentApplyService;
 
+    /**
+     * 获取某考生的“允许预约时段次数”
+     */
+    public int getStudentApplyNumber(Long studentId) {
+        String cacheKey = String.format(CACHE_STUDENT_APPLY_NUMBER, studentId);
+        Integer value = redisClient.get(cacheKey, Integer.class);
+        if (value != null) {
+            return value;
+        }
+
+        value = studentService.findStudentApplyNumberById(studentId);
+        redisClient.set(cacheKey, value, 3, TimeUnit.HOURS);
+        log.info("{} = {}", cacheKey, value);
+
+        return value;
+    }
+
     /**
      * 获取某考点某时段的“可预约总量”
      */
@@ -38,7 +59,7 @@ public class ApplyTaskCacheService implements CacheConstants {
 
         // 当前考点当前时段容量 = 当前考点总容量
         value = examSiteService.countExamSiteCapacityById(examSiteId);
-        redisClient.set(cacheKey, value, 1, TimeUnit.DAYS);
+        redisClient.set(cacheKey, value, 3, TimeUnit.HOURS);
         log.info("{} = {}", cacheKey, value);
 
         return value;

+ 4 - 2
src/main/java/com/qmth/exam/reserve/service/StudentService.java

@@ -1,12 +1,12 @@
 package com.qmth.exam.reserve.service;
 
-import java.util.List;
-
 import com.baomidou.mybatisplus.extension.service.IService;
 import com.qmth.exam.reserve.bean.student.StudentInfo;
 import com.qmth.exam.reserve.bean.student.WechatBindReq;
 import com.qmth.exam.reserve.entity.StudentEntity;
 
+import java.util.List;
+
 public interface StudentService extends IService<StudentEntity> {
 
     StudentEntity findByStudentCode(Long applyTaskId, String studentCode);
@@ -17,6 +17,8 @@ public interface StudentService extends IService<StudentEntity> {
 
     StudentInfo findInfoByStudentId(Long studentId);
 
+    int findStudentApplyNumberById(Long studentId);
+
     void bindingWechat(WechatBindReq req);
 
     void unbindWechatByStudentId(Long studentId);

+ 17 - 11
src/main/java/com/qmth/exam/reserve/service/impl/ExamReserveServiceImpl.java

@@ -15,7 +15,10 @@ import com.qmth.exam.reserve.bean.login.LoginUser;
 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.ApplyTaskEntity;
+import com.qmth.exam.reserve.entity.ExamSiteEntity;
+import com.qmth.exam.reserve.entity.StudentApplyEntity;
+import com.qmth.exam.reserve.entity.TimePeriodEntity;
 import com.qmth.exam.reserve.enums.ApplyStatus;
 import com.qmth.exam.reserve.service.*;
 import com.qmth.exam.reserve.util.DateUtil;
@@ -110,15 +113,12 @@ public class ExamReserveServiceImpl implements ExamReserveService {
                 throw new StatusException(Constants.SYSTEM_BUSY);
             }
 
-            StudentEntity stu = studentService.findLessInfoByStudentId(student.getId());
-            if (stu == null) {
-                log.warn("预约失败,学生信息不存在!studentId:{}", student.getId());
-                throw new StatusException("学生信息不存在");
-            }
-
+            // 考生剩余预约次数 = 允许预约次数 - 已完成预约次数
+            int studentApplyNumber = applyTaskCacheService.getStudentApplyNumber(student.getId());
             int studentApplyFinishCount = this.getStudentApplyFinishCount(student.getId());
-            if (stu.getApplyNumber() - studentApplyFinishCount < 1) {
-                String msg = "当前学生无剩余可约时段次数,已完成预约" + stu.getApplyNumber() + "次";
+            int unApplyNumber = studentApplyNumber - studentApplyFinishCount;
+            if (unApplyNumber < 1) {
+                String msg = "当前学生无剩余可约时段次数,已完成预约" + studentApplyNumber + "次";
                 log.warn(msg);
                 throw new StatusException(msg);
             }
@@ -417,13 +417,19 @@ public class ExamReserveServiceImpl implements ExamReserveService {
                     info.setStatus(ApplyStatus.AVAILABLE);
                 }
 
+                // todo 已约
+
                 periodList.add(info);
             }
         }
 
-        // todo
+        // 考生剩余预约次数 = 允许预约次数 - 已完成预约次数
+        int studentApplyNumber = applyTaskCacheService.getStudentApplyNumber(student.getId());
+        int studentApplyFinishCount = this.getStudentApplyFinishCount(student.getId());
+        int unApplyNumber = studentApplyNumber - studentApplyFinishCount;
+
         ApplyTimePeriodResult result = new ApplyTimePeriodResult();
-        result.setUnApplyNumber(1);
+        result.setUnApplyNumber(Math.max(unApplyNumber, 0));
         result.setPeriodList(periodList);
 
         return result;

+ 24 - 11
src/main/java/com/qmth/exam/reserve/service/impl/StudentServiceImpl.java

@@ -1,14 +1,5 @@
 package com.qmth.exam.reserve.service.impl;
 
-import java.util.List;
-
-import org.apache.commons.lang3.StringUtils;
-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 com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
@@ -20,6 +11,14 @@ import com.qmth.exam.reserve.entity.CategoryEntity;
 import com.qmth.exam.reserve.entity.StudentEntity;
 import com.qmth.exam.reserve.service.CategoryService;
 import com.qmth.exam.reserve.service.StudentService;
+import org.apache.commons.lang3.StringUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
+
+import java.util.List;
 
 @Service
 public class StudentServiceImpl extends ServiceImpl<StudentDao, StudentEntity> implements StudentService {
@@ -60,11 +59,25 @@ public class StudentServiceImpl extends ServiceImpl<StudentDao, StudentEntity> i
         return baseMapper.selectOne(wrapper);
     }
 
+    @Override
+    public int findStudentApplyNumberById(Long studentId) {
+        // 只查询applyNumber等少量字段
+        LambdaQueryWrapper<StudentEntity> wrapper = new LambdaQueryWrapper<>();
+        wrapper.select(StudentEntity::getApplyNumber);
+        wrapper.eq(StudentEntity::getId, studentId);
+        StudentEntity stu = this.getOne(wrapper);
+        if (stu == null) {
+            log.warn("学生信息不存在!studentId:{}", studentId);
+            throw new StatusException("学生信息不存在");
+        }
+        return stu.getApplyNumber();
+    }
+
     @Override
     public StudentEntity findLessInfoByStudentId(Long studentId) {
-        // 只查询id、applyNumber、applyFinished等少量字段
+        // 只查询id、name等少量字段
         LambdaQueryWrapper<StudentEntity> wrapper = new LambdaQueryWrapper<>();
-        wrapper.select(StudentEntity::getId, StudentEntity::getApplyNumber, StudentEntity::getApplyFinished);
+        wrapper.select(StudentEntity::getId, StudentEntity::getName);
         wrapper.eq(StudentEntity::getId, studentId);
         return this.getOne(wrapper);
     }