Browse Source

updateTo 预约结果直接保存入库

deason 1 year ago
parent
commit
fea41fb650

+ 44 - 32
src/main/java/com/qmth/exam/reserve/cache/impl/ApplyTaskCacheService.java

@@ -11,6 +11,7 @@ import com.qmth.exam.reserve.service.ExamSiteService;
 import com.qmth.exam.reserve.service.StudentApplyService;
 import com.qmth.exam.reserve.service.StudentService;
 import org.apache.commons.collections4.CollectionUtils;
+import org.apache.commons.collections4.MapUtils;
 import org.redisson.api.RAtomicLong;
 import org.redisson.api.RQueue;
 import org.slf4j.Logger;
@@ -167,14 +168,19 @@ public class ApplyTaskCacheService implements CacheConstants {
             return studentApplyService.count(wrapper);
         }*/
 
-        Map<String, ApplyRecordCacheBean> maps = this.getStudentApplyRecords(studentId);
-        int studentApplyFinishCount = 0;
-        for (ApplyRecordCacheBean bean : maps.values()) {
-            if (!bean.getCancel()) {
-                studentApplyFinishCount++;
-            }
-        }
-        return studentApplyFinishCount;
+        // Map<String, ApplyRecordCacheBean> maps = this.getStudentApplyRecords(studentId);
+        // int studentApplyFinishCount = 0;
+        // for (ApplyRecordCacheBean bean : maps.values()) {
+        //     if (!bean.getCancel()) {
+        //         studentApplyFinishCount++;
+        //     }
+        // }
+        // return studentApplyFinishCount;
+
+        LambdaQueryWrapper<StudentApplyEntity> wrapper = new LambdaQueryWrapper<>();
+        wrapper.eq(StudentApplyEntity::getStudentId, studentId);
+        wrapper.eq(StudentApplyEntity::getCancel, Boolean.FALSE);
+        return studentApplyService.count(wrapper);
     }
 
     /**
@@ -183,16 +189,26 @@ public class ApplyTaskCacheService implements CacheConstants {
     public Map<String, ApplyRecordCacheBean> getStudentApplyRecords(Long studentId) {
         String cacheKey = String.format(CACHE_STUDENT_APPLY_RECORD, studentId);
         if (!redisClient.exist(cacheKey)) {
-            return this.initStudentApplyRecords(studentId);
+            Map<String, ApplyRecordCacheBean> maps = this.getStudentApplyRecordsFormDB(studentId);
+            if (MapUtils.isEmpty(maps)) {
+                return new HashMap<>();
+            }
+
+            for (ApplyRecordCacheBean bean : maps.values()) {
+                this.saveStudentApplyRecord(bean);
+            }
+
+            redisClient.expire(cacheKey, 30, TimeUnit.DAYS);
+            return maps;
         }
 
         return redisClient.getEntriesForHash(cacheKey, ApplyRecordCacheBean.class);
     }
 
     /**
-     * 初始某考生的 所有的“预约记录”缓存
+     * 获取某考生的 所有的“预约记录”
      */
-    public Map<String, ApplyRecordCacheBean> initStudentApplyRecords(Long studentId) {
+    public Map<String, ApplyRecordCacheBean> getStudentApplyRecordsFormDB(Long studentId) {
         LambdaQueryWrapper<StudentApplyEntity> wrapper = new LambdaQueryWrapper<>();
         wrapper.eq(StudentApplyEntity::getStudentId, studentId);
         List<StudentApplyEntity> list = studentApplyService.list(wrapper);
@@ -213,12 +229,8 @@ public class ApplyTaskCacheService implements CacheConstants {
 
             String hashKey = String.format("%s_%s", entity.getExamSiteId(), entity.getTimePeriodId());
             maps.put(hashKey, bean);
-            this.saveStudentApplyRecord(bean);
         }
 
-        String cacheKey = String.format(CACHE_STUDENT_APPLY_RECORD, studentId);
-        redisClient.expire(cacheKey, 30, TimeUnit.DAYS);
-
         return maps;
     }
 
@@ -235,29 +247,29 @@ public class ApplyTaskCacheService implements CacheConstants {
      * 保存某考生的 某考点某时段的“预约记录”缓存
      */
     public void saveStudentApplyRecord(ApplyRecordCacheBean value) {
-        String cacheKey = String.format(CACHE_STUDENT_APPLY_RECORD, value.getStudentId());
-        String hashKey = String.format("%s_%s", value.getExamSiteId(), value.getTimePeriodId());
-        redisClient.setForHash(cacheKey, hashKey, value);
-        log.info("SET cacheKey:{} hashKey:{} cancel:{}", cacheKey, hashKey, value.getCancel());
+        // String cacheKey = String.format(CACHE_STUDENT_APPLY_RECORD, value.getStudentId());
+        // String hashKey = String.format("%s_%s", value.getExamSiteId(), value.getTimePeriodId());
+        // redisClient.setForHash(cacheKey, hashKey, value);
+        // log.info("SET cacheKey:{} hashKey:{} cancel:{}", cacheKey, hashKey, value.getCancel());
     }
 
     /**
      * 推送至考生预约记录队列
      */
     public void pushStudentApplyRecordQueue(ApplyRecordCacheBean value) {
-        if (value == null) {
-            return;
-        }
-
-        RQueue<ApplyRecordCacheBean> queue = redisClient.getRedissonClient()
-                .getQueue(QUEUE_STUDENT_APPLY_RECORD);
-
-        boolean success = queue.offer(value);
-        log.info("{}_{}_{}_{} offerQueue:{}", value.getStudentId(), value.getExamSiteId(),
-                value.getTimePeriodId(), value.getCancel(), success);
-        if (!success) {
-            throw new RuntimeException("推送至考生预约记录队列失败");
-        }
+        // if (value == null) {
+        //     return;
+        // }
+        //
+        // RQueue<ApplyRecordCacheBean> queue = redisClient.getRedissonClient()
+        //         .getQueue(QUEUE_STUDENT_APPLY_RECORD);
+        //
+        // boolean success = queue.offer(value);
+        // log.info("{}_{}_{}_{} offerQueue:{}", value.getStudentId(), value.getExamSiteId(),
+        //         value.getTimePeriodId(), value.getCancel(), success);
+        // if (!success) {
+        //     throw new RuntimeException("推送至考生预约记录队列失败");
+        // }
     }
 
     /**

+ 1 - 1
src/main/java/com/qmth/exam/reserve/job/StudentApplyRecordJob.java

@@ -40,7 +40,7 @@ public class StudentApplyRecordJob {
      * 定时将“考生预约记录队列”中的数据保存至数据库
      * 注:每N秒执行一次
      */
-    @Scheduled(fixedDelay = 10000, initialDelay = 30000)
+    // @Scheduled(fixedDelay = 10000, initialDelay = 30000)
     public void saveStudentApplyRecordJob() {
         long start = System.currentTimeMillis();
 

+ 39 - 35
src/main/java/com/qmth/exam/reserve/service/impl/ExamReserveServiceImpl.java

@@ -30,7 +30,6 @@ 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.text.ParseException;
 import java.util.*;
@@ -136,8 +135,8 @@ public class ExamReserveServiceImpl implements ExamReserveService {
             applyTaskCacheService.increaseApplyFinishCount(examSiteId, timePeriodId);
 
             try {
-                // StudentApplyEntity existApply = this.getStudentApply(student.getId(), examSiteId, timePeriodId);
-                ApplyRecordCacheBean existApply = applyTaskCacheService.getStudentApplyRecord(student.getId(), examSiteId, timePeriodId);
+                StudentApplyEntity existApply = this.getStudentApply(student.getId(), examSiteId, timePeriodId);
+                // ApplyRecordCacheBean existApply = applyTaskCacheService.getStudentApplyRecord(student.getId(), examSiteId, timePeriodId);
                 if (existApply != null) {
                     if (!existApply.getCancel()) {
                         // 某考点某时段的“已预约数量” 累减1(释放1个被占数量)
@@ -148,30 +147,36 @@ public class ExamReserveServiceImpl implements ExamReserveService {
                     }
 
                     // 存在“已取消”预约记录,则恢复预约
-                    existApply.setCancel(false);
-                    existApply.setOperateId(student.getId());
-                    existApply.setOperateTime(System.currentTimeMillis());
-
-                    // 推送至预约队列
-                    applyTaskCacheService.pushStudentApplyRecordQueue(existApply);
-
-                    // 保存至预约缓存
-                    applyTaskCacheService.saveStudentApplyRecord(existApply);
+                    // existApply.setCancel(false);
+                    // existApply.setOperateId(student.getId());
+                    // existApply.setOperateTime(System.currentTimeMillis());
+                    // // 推送至预约队列
+                    // applyTaskCacheService.pushStudentApplyRecordQueue(existApply);
+                    // // 保存至预约缓存
+                    // applyTaskCacheService.saveStudentApplyRecord(existApply);
+
+                    this.updateStudentApplyForCancel(existApply.getId(), false, student.getId());
                 } else {
                     // 不存在则新增预约记录
-                    ApplyRecordCacheBean newApply = new ApplyRecordCacheBean();
+                    // ApplyRecordCacheBean newApply = new ApplyRecordCacheBean();
+                    // newApply.setStudentId(student.getId());
+                    // newApply.setExamSiteId(examSiteId);
+                    // newApply.setTimePeriodId(timePeriodId);
+                    // newApply.setCancel(false);
+                    // newApply.setOperateId(student.getId());
+                    // newApply.setOperateTime(System.currentTimeMillis());
+                    // // 推送至预约队列
+                    // applyTaskCacheService.pushStudentApplyRecordQueue(newApply);
+                    // // 保存至预约缓存
+                    // applyTaskCacheService.saveStudentApplyRecord(newApply);
+
+                    StudentApplyEntity newApply = new StudentApplyEntity();
                     newApply.setStudentId(student.getId());
                     newApply.setExamSiteId(examSiteId);
                     newApply.setTimePeriodId(timePeriodId);
-                    newApply.setCancel(false);
                     newApply.setOperateId(student.getId());
-                    newApply.setOperateTime(System.currentTimeMillis());
-
-                    // 推送至预约队列
-                    applyTaskCacheService.pushStudentApplyRecordQueue(newApply);
-
-                    // 保存至预约缓存
-                    applyTaskCacheService.saveStudentApplyRecord(newApply);
+                    newApply.setCancel(false);
+                    studentApplyService.save(newApply);
                 }
 
                 log.warn("预约成功!studentId:{} examSiteId:{} timePeriodId:{}", student.getId(), examSiteId, timePeriodId);
@@ -217,8 +222,8 @@ public class ExamReserveServiceImpl implements ExamReserveService {
             return;
         }
 
-        // StudentApplyEntity existApply = this.getStudentApply(student.getId(), examSiteId, timePeriodId);
-        ApplyRecordCacheBean existApply = applyTaskCacheService.getStudentApplyRecord(student.getId(), examSiteId, timePeriodId);
+        StudentApplyEntity existApply = this.getStudentApply(student.getId(), examSiteId, timePeriodId);
+        // ApplyRecordCacheBean existApply = applyTaskCacheService.getStudentApplyRecord(student.getId(), examSiteId, timePeriodId);
         if (existApply == null) {
             log.warn("取消预约失败,预约记录不存在!examSiteId:{} timePeriodId:{}", examSiteId, timePeriodId);
             return;
@@ -262,15 +267,15 @@ public class ExamReserveServiceImpl implements ExamReserveService {
                 throw new StatusException(Constants.SYSTEM_BUSY);
             }
 
-            existApply.setCancel(true);
-            existApply.setOperateId(student.getId());
-            existApply.setOperateTime(System.currentTimeMillis());
+            // existApply.setCancel(true);
+            // existApply.setOperateId(student.getId());
+            // existApply.setOperateTime(System.currentTimeMillis());
+            // // 推送至预约队列
+            // applyTaskCacheService.pushStudentApplyRecordQueue(existApply);
+            // // 保存至预约缓存
+            // applyTaskCacheService.saveStudentApplyRecord(existApply);
 
-            // 推送至预约队列
-            applyTaskCacheService.pushStudentApplyRecordQueue(existApply);
-
-            // 保存至预约缓存
-            applyTaskCacheService.saveStudentApplyRecord(existApply);
+            this.updateStudentApplyForCancel(existApply.getId(), true, student.getId());
 
             // 某考点某时段的“已预约数量” 累减1
             applyTaskCacheService.decreaseApplyFinishCount(examSiteId, timePeriodId);
@@ -305,7 +310,7 @@ public class ExamReserveServiceImpl implements ExamReserveService {
         return studentApplyService.getOne(wrapper);
     }
 
-    @Transactional
+    // @Transactional
     public void updateStudentApplyForCancel(Long applyId, boolean cancel, Long operateId) {
         LambdaUpdateWrapper<StudentApplyEntity> updateWrapper = new LambdaUpdateWrapper<>();
         updateWrapper.set(StudentApplyEntity::getCancel, cancel);
@@ -313,7 +318,6 @@ public class ExamReserveServiceImpl implements ExamReserveService {
         updateWrapper.set(StudentApplyEntity::getUpdateTime, System.currentTimeMillis());
         updateWrapper.eq(StudentApplyEntity::getId, applyId);
         studentApplyService.update(updateWrapper);
-        log.warn("更新考生预约记录!applyId:{} cancel:{}", applyId, cancel);
     }
 
     private boolean hasTicketNumber(Long studentId, Long examSiteId, Long timePeriodId) {
@@ -443,7 +447,7 @@ public class ExamReserveServiceImpl implements ExamReserveService {
     @Override
     public List<ApplyVO> getStudentApplyListForCurrent(LoginUser student) {
         // 默认过滤掉“已取消预约”的记录
-        List<ApplyVO> list = this.getStudentApplyListFromCache(student, false);
+        List<ApplyVO> list = this.getStudentApplyList(student, false);
 
         Date now = new Date();
         List<ApplyVO> newList = new ArrayList<>();
@@ -547,7 +551,7 @@ public class ExamReserveServiceImpl implements ExamReserveService {
         List<TimePeriodEntity> timePeriods = timePeriodService.list(wrapper);
 
         // 获取某考生的 所有的“预约记录”
-        Map<String, ApplyRecordCacheBean> maps = applyTaskCacheService.getStudentApplyRecords(student.getId());
+        Map<String, ApplyRecordCacheBean> maps = applyTaskCacheService.getStudentApplyRecordsFormDB(student.getId());
         int studentApplyFinishCount = 0;
         for (ApplyRecordCacheBean bean : maps.values()) {
             if (!bean.getCancel()) {