123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393 |
- package com.qmth.exam.reserve.cache.impl;
- import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
- import com.qmth.exam.reserve.bean.apply.ApplyRecordCacheBean;
- import com.qmth.exam.reserve.bean.applytask.CurrentApplyTaskVO;
- import com.qmth.exam.reserve.bean.examsite.ExamSiteCapacityInfo;
- import com.qmth.exam.reserve.cache.CacheConstants;
- import com.qmth.exam.reserve.cache.RedisClient;
- import com.qmth.exam.reserve.entity.StudentApplyEntity;
- import com.qmth.exam.reserve.entity.TimePeriodEntity;
- import com.qmth.exam.reserve.service.*;
- 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;
- import org.slf4j.LoggerFactory;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Component;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import java.util.concurrent.TimeUnit;
- @Component
- public class ApplyTaskCacheService implements CacheConstants {
- private static final Logger log = LoggerFactory.getLogger(ApplyTaskCacheService.class);
- @Autowired
- private RedisClient redisClient;
- @Autowired
- private ApplyTaskService applyTaskService;
- @Autowired
- private ExamSiteService examSiteService;
- @Autowired
- private TimePeriodService timePeriodService;
- @Autowired
- private StudentService studentService;
- @Autowired
- private StudentApplyService studentApplyService;
- /**
- * 获取当前启用的预约任务缓存
- */
- public CurrentApplyTaskVO currentApplyTask(Long orgId) {
- String cacheKey = String.format(CACHE_CURRENT_APPLY_TASK, orgId);
- CurrentApplyTaskVO value = redisClient.get(cacheKey, CurrentApplyTaskVO.class);
- if (value != null) {
- return value;
- }
- value = applyTaskService.currentApplyTask(orgId);
- if (value == null) {
- return null;
- }
- redisClient.set(cacheKey, value, CACHE_TIME_OUT_10, TimeUnit.MINUTES);
- log.info("SET cacheKey:{} curApplyTaskId:{}", cacheKey, value.getTaskId());
- return value;
- }
- /**
- * 清除当前启用的预约任务缓存
- */
- public void clearCurrentApplyTaskCache(Long orgId) {
- String cacheKey = String.format(CACHE_CURRENT_APPLY_TASK, orgId);
- redisClient.delete(cacheKey);
- log.warn("DELETE cacheKey:{}", cacheKey);
- }
- /**
- * 获取某考生的“允许预约时段次数”缓存
- */
- 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, CACHE_TIME_OUT_10, TimeUnit.MINUTES);
- log.info("SET cacheKey:{} value:{}", cacheKey, value);
- return value;
- }
- /**
- * 清除某考生的“允许预约时段次数”缓存
- */
- public void clearStudentApplyNumberCache(Long studentId) {
- String cacheKey = String.format(CACHE_STUDENT_APPLY_NUMBER, studentId);
- redisClient.delete(cacheKey);
- log.warn("DELETE cacheKey:{}", cacheKey);
- }
- /**
- * 获取某考点的“可预约总量”缓存
- */
- public int getApplyTotalCount(Long examSiteId) {
- String cacheKey = String.format(CACHE_APPLY_TOTAL, examSiteId);
- Integer value = redisClient.get(cacheKey, Integer.class);
- if (value != null) {
- return value;
- }
- value = examSiteService.countExamSiteCapacityById(examSiteId);
- redisClient.set(cacheKey, value, CACHE_TIME_OUT_10, TimeUnit.MINUTES);
- log.info("SET cacheKey:{} value:{}", cacheKey, value);
- return value;
- }
- /**
- * 清除某考点的“可预约总量”缓存
- */
- public void clearApplyTotalCountCache(Long examSiteId) {
- String cacheKey = String.format(CACHE_APPLY_TOTAL, examSiteId);
- redisClient.delete(cacheKey);
- log.warn("DELETE cacheKey:{}", cacheKey);
- }
- /**
- * 获取某考点某时段的“已预约数量”(查数据库)
- */
- public int getApplyFinishCountFormDB(Long examSiteId, Long timePeriodId) {
- return studentApplyService.countApplyFinishForExamSiteAndTimePeriod(examSiteId, timePeriodId);
- }
- /**
- * 获取某考点某时段的“已预约数量”缓存
- */
- public int getApplyFinishCount(Long examSiteId, Long timePeriodId) {
- String cacheKey = String.format(CACHE_APPLY_FINISH, examSiteId, timePeriodId);
- if (redisClient.exist(cacheKey)) {
- return (int) redisClient.getRedissonClient().getAtomicLong(cacheKey).get();
- }
- RAtomicLong atomic = redisClient.getRedissonClient().getAtomicLong(cacheKey);
- int value = this.getApplyFinishCountFormDB(examSiteId, timePeriodId);
- atomic.set(value);
- atomic.expire(CACHE_TIME_OUT_30, TimeUnit.DAYS);
- log.info("SET cacheKey:{} value:{}", cacheKey, value);
- return value;
- }
- /**
- * 获取当前预约时段剩余可约数量缓存
- */
- public int getApplyAvailableCount(Long examSiteId, Long timePeriodId) {
- int totalCount = this.getApplyTotalCount(examSiteId);
- int finishCount = this.getApplyFinishCount(examSiteId, timePeriodId);
- int availableCount = totalCount - finishCount;
- return Math.max(availableCount, 0);
- }
- /**
- * 获取某考点某时段的“剩余可约数量”缓存
- */
- public int getApplyAvailableCount2(Long examSiteId, Long timePeriodId) {
- String cacheKey = String.format(CACHE_APPLY_AVAILABLE_COUNT, examSiteId, timePeriodId);
- RAtomicLong atomic = redisClient.getRedissonClient().getAtomicLong(cacheKey);
- return (int) atomic.get();
- }
- /**
- * 累加 某考点某时段的“剩余可约数量”缓存
- */
- public void increaseApplyAvailableCount(Long examSiteId, Long timePeriodId) {
- String cacheKey = String.format(CACHE_APPLY_AVAILABLE_COUNT, examSiteId, timePeriodId);
- RAtomicLong atomic = redisClient.getRedissonClient().getAtomicLong(cacheKey);
- atomic.incrementAndGet();
- }
- /**
- * 累减 某考点某时段的“剩余可约数量”缓存
- */
- public void decreaseApplyAvailableCount(Long examSiteId, Long timePeriodId) {
- String cacheKey = String.format(CACHE_APPLY_AVAILABLE_COUNT, examSiteId, timePeriodId);
- RAtomicLong atomic = redisClient.getRedissonClient().getAtomicLong(cacheKey);
- if (atomic.get() > 0) {
- atomic.decrementAndGet();
- }
- }
- /**
- * 初始化 某考点某时段的“剩余可约数量”缓存
- */
- public void initApplyAvailableCountCache(Long examSiteId, int oldCapacity, int newCapacity) {
- // 获取所有时段ID集合
- List<TimePeriodEntity> timePeriods = timePeriodService.list(new LambdaQueryWrapper<TimePeriodEntity>().select(TimePeriodEntity::getId));
- if (CollectionUtils.isEmpty(timePeriods)) {
- return;
- }
- // 新、旧考点容量差额
- int diffCapacity = newCapacity - oldCapacity;
- for (TimePeriodEntity timePeriod : timePeriods) {
- String cacheKey = String.format(CACHE_APPLY_AVAILABLE_COUNT, examSiteId, timePeriod.getId());
- if (redisClient.exist(cacheKey)) {
- if (diffCapacity == 0) {
- // 考点总容量未变化,不用更新缓存
- continue;
- }
- // 总容量变化时,则更新考点剩余容量缓存
- RAtomicLong atomic = redisClient.getRedissonClient().getAtomicLong(cacheKey);
- long availableCount = Math.max(atomic.get() + diffCapacity, 0);
- atomic.set(availableCount);
- log.warn("SET cacheKey:{} value:{}", cacheKey, availableCount);
- } else {
- // 初始考点剩余容量缓存
- RAtomicLong atomic = redisClient.getRedissonClient().getAtomicLong(cacheKey);
- atomic.set(newCapacity);
- log.debug("SET cacheKey:{} value:{}", cacheKey, newCapacity);
- }
- }
- }
- /**
- * 初始化 所有考点所有时段的“剩余可约数量”缓存
- */
- public void initApplyAvailableCountCacheForAllExamSites() {
- List<ExamSiteCapacityInfo> examSites = examSiteService.findAllExamSiteCapacityList();
- for (ExamSiteCapacityInfo examSite : examSites) {
- this.initApplyAvailableCountCache(examSite.getExamSiteId(), examSite.getCapacity(), examSite.getCapacity());
- }
- }
- /**
- * 累加 某考点某时段的“已预约数量”缓存
- */
- public void increaseApplyFinishCount(Long examSiteId, Long timePeriodId) {
- String cacheKey = String.format(CACHE_APPLY_FINISH, examSiteId, timePeriodId);
- RAtomicLong atomic = redisClient.getRedissonClient().getAtomicLong(cacheKey);
- atomic.incrementAndGet();
- }
- /**
- * 累减 某考点某时段的“已预约数量”缓存
- */
- public void decreaseApplyFinishCount(Long examSiteId, Long timePeriodId) {
- String cacheKey = String.format(CACHE_APPLY_FINISH, examSiteId, timePeriodId);
- RAtomicLong atomic = redisClient.getRedissonClient().getAtomicLong(cacheKey);
- if (atomic.get() > 0) {
- atomic.decrementAndGet();
- }
- }
- /**
- * 获取某考生的 已完成预约次数缓存(查数据库)
- */
- public int getStudentApplyFinishCountFromDB(Long studentId) {
- LambdaQueryWrapper<StudentApplyEntity> wrapper = new LambdaQueryWrapper<>();
- wrapper.eq(StudentApplyEntity::getStudentId, studentId);
- wrapper.eq(StudentApplyEntity::getCancel, Boolean.FALSE);
- return studentApplyService.count(wrapper);
- }
- /**
- * 获取某考生的 已完成预约次数缓存
- */
- public int getStudentApplyFinishCount(Long studentId) {
- Map<String, ApplyRecordCacheBean> maps = this.getStudentApplyRecords(studentId);
- int studentApplyFinishCount = 0;
- for (ApplyRecordCacheBean bean : maps.values()) {
- if (!bean.getCancel()) {
- studentApplyFinishCount++;
- }
- }
- return studentApplyFinishCount;
- }
- /**
- * 获取某考生的 所有的“预约记录”缓存
- */
- public Map<String, ApplyRecordCacheBean> getStudentApplyRecords(Long studentId) {
- String cacheKey = String.format(CACHE_STUDENT_APPLY_RECORD, studentId);
- if (!redisClient.exist(cacheKey)) {
- Map<String, ApplyRecordCacheBean> maps = this.getStudentApplyRecordsFromDB(studentId);
- if (MapUtils.isEmpty(maps)) {
- return new HashMap<>();
- }
- for (ApplyRecordCacheBean bean : maps.values()) {
- this.saveStudentApplyRecord(bean);
- }
- redisClient.expire(cacheKey, CACHE_TIME_OUT_30, TimeUnit.DAYS);
- return maps;
- }
- return redisClient.getEntriesForHash(cacheKey, ApplyRecordCacheBean.class);
- }
- /**
- * 获取某考生的 所有的“预约记录”(查数据库)
- */
- public Map<String, ApplyRecordCacheBean> getStudentApplyRecordsFromDB(Long studentId) {
- LambdaQueryWrapper<StudentApplyEntity> wrapper = new LambdaQueryWrapper<>();
- wrapper.eq(StudentApplyEntity::getStudentId, studentId);
- List<StudentApplyEntity> list = studentApplyService.list(wrapper);
- if (CollectionUtils.isEmpty(list)) {
- return new HashMap<>();
- }
- Map<String, ApplyRecordCacheBean> maps = new HashMap<>();
- for (StudentApplyEntity entity : list) {
- ApplyRecordCacheBean bean = new ApplyRecordCacheBean();
- bean.setStudentId(entity.getStudentId());
- bean.setExamSiteId(entity.getExamSiteId());
- bean.setTimePeriodId(entity.getTimePeriodId());
- bean.setCancel(entity.getCancel());
- bean.setOperateId(entity.getOperateId());
- bean.setOperateTime(entity.getUpdateTime());
- String hashKey = String.format("%s_%s", entity.getExamSiteId(), entity.getTimePeriodId());
- maps.put(hashKey, bean);
- }
- return maps;
- }
- /**
- * 获取某考生的 某考点某时段的“预约记录”缓存
- */
- public ApplyRecordCacheBean getStudentApplyRecord(Long studentId, Long examSiteId, Long timePeriodId) {
- String cacheKey = String.format(CACHE_STUDENT_APPLY_RECORD, studentId);
- String hashKey = String.format("%s_%s", examSiteId, timePeriodId);
- return redisClient.getForHash(cacheKey, hashKey, ApplyRecordCacheBean.class);
- }
- /**
- * 获取某考生的 某考点某时段的“预约记录”(查数据库)
- */
- public StudentApplyEntity getStudentApplyRecordFormDB(Long studentId, Long examSiteId, Long timePeriodId) {
- LambdaQueryWrapper<StudentApplyEntity> wrapper = new LambdaQueryWrapper<>();
- wrapper.eq(StudentApplyEntity::getExamSiteId, examSiteId);
- wrapper.eq(StudentApplyEntity::getTimePeriodId, timePeriodId);
- wrapper.eq(StudentApplyEntity::getStudentId, studentId);
- return studentApplyService.getOne(wrapper);
- }
- /**
- * 保存某考生的 某考点某时段的“预约记录”缓存
- */
- 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());
- }
- /**
- * 推送至考生预约记录队列
- */
- 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("推送至考生预约记录队列失败");
- }
- }
- /**
- * 获取考生预约记录队列数量
- */
- public int getStudentApplyRecordQueueSize() {
- RQueue<ApplyRecordCacheBean> queue = redisClient.getRedissonClient()
- .getQueue(QUEUE_STUDENT_APPLY_RECORD);
- return queue.size();
- }
- }
|