ApplyTaskCacheService.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. package com.qmth.exam.reserve.cache.impl;
  2. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  3. import com.qmth.exam.reserve.bean.apply.ApplyRecordCacheBean;
  4. import com.qmth.exam.reserve.bean.applytask.CurrentApplyTaskVO;
  5. import com.qmth.exam.reserve.bean.examsite.ExamSiteCapacityInfo;
  6. import com.qmth.exam.reserve.cache.CacheConstants;
  7. import com.qmth.exam.reserve.cache.RedisClient;
  8. import com.qmth.exam.reserve.entity.StudentApplyEntity;
  9. import com.qmth.exam.reserve.entity.TimePeriodEntity;
  10. import com.qmth.exam.reserve.service.*;
  11. import org.apache.commons.collections4.CollectionUtils;
  12. import org.apache.commons.collections4.MapUtils;
  13. import org.redisson.api.RAtomicLong;
  14. import org.redisson.api.RQueue;
  15. import org.slf4j.Logger;
  16. import org.slf4j.LoggerFactory;
  17. import org.springframework.beans.factory.annotation.Autowired;
  18. import org.springframework.stereotype.Component;
  19. import java.util.HashMap;
  20. import java.util.List;
  21. import java.util.Map;
  22. import java.util.concurrent.TimeUnit;
  23. @Component
  24. public class ApplyTaskCacheService implements CacheConstants {
  25. private static final Logger log = LoggerFactory.getLogger(ApplyTaskCacheService.class);
  26. @Autowired
  27. private RedisClient redisClient;
  28. @Autowired
  29. private ApplyTaskService applyTaskService;
  30. @Autowired
  31. private ExamSiteService examSiteService;
  32. @Autowired
  33. private TimePeriodService timePeriodService;
  34. @Autowired
  35. private StudentService studentService;
  36. @Autowired
  37. private StudentApplyService studentApplyService;
  38. /**
  39. * 获取当前启用的预约任务缓存
  40. */
  41. public CurrentApplyTaskVO currentApplyTask(Long orgId) {
  42. String cacheKey = String.format(CACHE_CURRENT_APPLY_TASK, orgId);
  43. CurrentApplyTaskVO value = redisClient.get(cacheKey, CurrentApplyTaskVO.class);
  44. if (value != null) {
  45. return value;
  46. }
  47. value = applyTaskService.currentApplyTask(orgId);
  48. if (value == null) {
  49. return null;
  50. }
  51. redisClient.set(cacheKey, value, CACHE_TIME_OUT_10, TimeUnit.MINUTES);
  52. log.info("SET cacheKey:{} curApplyTaskId:{}", cacheKey, value.getTaskId());
  53. return value;
  54. }
  55. /**
  56. * 清除当前启用的预约任务缓存
  57. */
  58. public void clearCurrentApplyTaskCache(Long orgId) {
  59. String cacheKey = String.format(CACHE_CURRENT_APPLY_TASK, orgId);
  60. redisClient.delete(cacheKey);
  61. log.warn("DELETE cacheKey:{}", cacheKey);
  62. }
  63. /**
  64. * 获取某考生的“允许预约时段次数”缓存
  65. */
  66. public int getStudentApplyNumber(Long studentId) {
  67. String cacheKey = String.format(CACHE_STUDENT_APPLY_NUMBER, studentId);
  68. Integer value = redisClient.get(cacheKey, Integer.class);
  69. if (value != null) {
  70. return value;
  71. }
  72. value = studentService.findStudentApplyNumberById(studentId);
  73. redisClient.set(cacheKey, value, CACHE_TIME_OUT_10, TimeUnit.MINUTES);
  74. log.info("SET cacheKey:{} value:{}", cacheKey, value);
  75. return value;
  76. }
  77. /**
  78. * 清除某考生的“允许预约时段次数”缓存
  79. */
  80. public void clearStudentApplyNumberCache(Long studentId) {
  81. String cacheKey = String.format(CACHE_STUDENT_APPLY_NUMBER, studentId);
  82. redisClient.delete(cacheKey);
  83. log.warn("DELETE cacheKey:{}", cacheKey);
  84. }
  85. /**
  86. * 获取某考点的“可预约总量”缓存
  87. */
  88. public int getApplyTotalCount(Long examSiteId) {
  89. String cacheKey = String.format(CACHE_APPLY_TOTAL, examSiteId);
  90. Integer value = redisClient.get(cacheKey, Integer.class);
  91. if (value != null) {
  92. return value;
  93. }
  94. value = examSiteService.countExamSiteCapacityById(examSiteId);
  95. redisClient.set(cacheKey, value, CACHE_TIME_OUT_10, TimeUnit.MINUTES);
  96. log.info("SET cacheKey:{} value:{}", cacheKey, value);
  97. return value;
  98. }
  99. /**
  100. * 清除某考点的“可预约总量”缓存
  101. */
  102. public void clearApplyTotalCountCache(Long examSiteId) {
  103. String cacheKey = String.format(CACHE_APPLY_TOTAL, examSiteId);
  104. redisClient.delete(cacheKey);
  105. log.warn("DELETE cacheKey:{}", cacheKey);
  106. }
  107. /**
  108. * 获取某考点某时段的“已预约数量”(查数据库)
  109. */
  110. public int getApplyFinishCountFormDB(Long examSiteId, Long timePeriodId) {
  111. return studentApplyService.countApplyFinishForExamSiteAndTimePeriod(examSiteId, timePeriodId);
  112. }
  113. /**
  114. * 获取某考点某时段的“已预约数量”缓存
  115. */
  116. public int getApplyFinishCount(Long examSiteId, Long timePeriodId) {
  117. String cacheKey = String.format(CACHE_APPLY_FINISH, examSiteId, timePeriodId);
  118. if (redisClient.exist(cacheKey)) {
  119. return (int) redisClient.getRedissonClient().getAtomicLong(cacheKey).get();
  120. }
  121. RAtomicLong atomic = redisClient.getRedissonClient().getAtomicLong(cacheKey);
  122. int value = this.getApplyFinishCountFormDB(examSiteId, timePeriodId);
  123. atomic.set(value);
  124. atomic.expire(CACHE_TIME_OUT_30, TimeUnit.DAYS);
  125. log.info("SET cacheKey:{} value:{}", cacheKey, value);
  126. return value;
  127. }
  128. /**
  129. * 获取当前预约时段剩余可约数量缓存
  130. */
  131. public int getApplyAvailableCount(Long examSiteId, Long timePeriodId) {
  132. int totalCount = this.getApplyTotalCount(examSiteId);
  133. int finishCount = this.getApplyFinishCount(examSiteId, timePeriodId);
  134. int availableCount = totalCount - finishCount;
  135. return Math.max(availableCount, 0);
  136. }
  137. /**
  138. * 获取某考点某时段的“剩余可约数量”缓存
  139. */
  140. public int getApplyAvailableCount2(Long examSiteId, Long timePeriodId) {
  141. String cacheKey = String.format(CACHE_APPLY_AVAILABLE_COUNT, examSiteId, timePeriodId);
  142. RAtomicLong atomic = redisClient.getRedissonClient().getAtomicLong(cacheKey);
  143. return (int) atomic.get();
  144. }
  145. /**
  146. * 累加 某考点某时段的“剩余可约数量”缓存
  147. */
  148. public void increaseApplyAvailableCount(Long examSiteId, Long timePeriodId) {
  149. String cacheKey = String.format(CACHE_APPLY_AVAILABLE_COUNT, examSiteId, timePeriodId);
  150. RAtomicLong atomic = redisClient.getRedissonClient().getAtomicLong(cacheKey);
  151. atomic.incrementAndGet();
  152. }
  153. /**
  154. * 累减 某考点某时段的“剩余可约数量”缓存
  155. */
  156. public void decreaseApplyAvailableCount(Long examSiteId, Long timePeriodId) {
  157. String cacheKey = String.format(CACHE_APPLY_AVAILABLE_COUNT, examSiteId, timePeriodId);
  158. RAtomicLong atomic = redisClient.getRedissonClient().getAtomicLong(cacheKey);
  159. if (atomic.get() > 0) {
  160. atomic.decrementAndGet();
  161. }
  162. }
  163. /**
  164. * 初始化 某考点某时段的“剩余可约数量”缓存
  165. */
  166. public void initApplyAvailableCountCache(Long examSiteId, int oldCapacity, int newCapacity) {
  167. // 获取所有时段ID集合
  168. List<TimePeriodEntity> timePeriods = timePeriodService.list(new LambdaQueryWrapper<TimePeriodEntity>().select(TimePeriodEntity::getId));
  169. if (CollectionUtils.isEmpty(timePeriods)) {
  170. return;
  171. }
  172. // 新、旧考点容量差额
  173. int diffCapacity = newCapacity - oldCapacity;
  174. for (TimePeriodEntity timePeriod : timePeriods) {
  175. String cacheKey = String.format(CACHE_APPLY_AVAILABLE_COUNT, examSiteId, timePeriod.getId());
  176. if (redisClient.exist(cacheKey)) {
  177. if (diffCapacity == 0) {
  178. // 考点总容量未变化,不用更新缓存
  179. continue;
  180. }
  181. // 总容量变化时,则更新考点剩余容量缓存
  182. RAtomicLong atomic = redisClient.getRedissonClient().getAtomicLong(cacheKey);
  183. long availableCount = Math.max(atomic.get() + diffCapacity, 0);
  184. atomic.set(availableCount);
  185. log.warn("SET cacheKey:{} value:{}", cacheKey, availableCount);
  186. } else {
  187. // 初始考点剩余容量缓存
  188. RAtomicLong atomic = redisClient.getRedissonClient().getAtomicLong(cacheKey);
  189. atomic.set(newCapacity);
  190. log.debug("SET cacheKey:{} value:{}", cacheKey, newCapacity);
  191. }
  192. }
  193. }
  194. /**
  195. * 初始化 所有考点所有时段的“剩余可约数量”缓存
  196. */
  197. public void initApplyAvailableCountCacheForAllExamSites() {
  198. List<ExamSiteCapacityInfo> examSites = examSiteService.findAllExamSiteCapacityList();
  199. for (ExamSiteCapacityInfo examSite : examSites) {
  200. this.initApplyAvailableCountCache(examSite.getExamSiteId(), examSite.getCapacity(), examSite.getCapacity());
  201. }
  202. }
  203. /**
  204. * 累加 某考点某时段的“已预约数量”缓存
  205. */
  206. public void increaseApplyFinishCount(Long examSiteId, Long timePeriodId) {
  207. String cacheKey = String.format(CACHE_APPLY_FINISH, examSiteId, timePeriodId);
  208. RAtomicLong atomic = redisClient.getRedissonClient().getAtomicLong(cacheKey);
  209. atomic.incrementAndGet();
  210. }
  211. /**
  212. * 累减 某考点某时段的“已预约数量”缓存
  213. */
  214. public void decreaseApplyFinishCount(Long examSiteId, Long timePeriodId) {
  215. String cacheKey = String.format(CACHE_APPLY_FINISH, examSiteId, timePeriodId);
  216. RAtomicLong atomic = redisClient.getRedissonClient().getAtomicLong(cacheKey);
  217. if (atomic.get() > 0) {
  218. atomic.decrementAndGet();
  219. }
  220. }
  221. /**
  222. * 获取某考生的 已完成预约次数缓存(查数据库)
  223. */
  224. public int getStudentApplyFinishCountFromDB(Long studentId) {
  225. LambdaQueryWrapper<StudentApplyEntity> wrapper = new LambdaQueryWrapper<>();
  226. wrapper.eq(StudentApplyEntity::getStudentId, studentId);
  227. wrapper.eq(StudentApplyEntity::getCancel, Boolean.FALSE);
  228. return studentApplyService.count(wrapper);
  229. }
  230. /**
  231. * 获取某考生的 已完成预约次数缓存
  232. */
  233. public int getStudentApplyFinishCount(Long studentId) {
  234. Map<String, ApplyRecordCacheBean> maps = this.getStudentApplyRecords(studentId);
  235. int studentApplyFinishCount = 0;
  236. for (ApplyRecordCacheBean bean : maps.values()) {
  237. if (!bean.getCancel()) {
  238. studentApplyFinishCount++;
  239. }
  240. }
  241. return studentApplyFinishCount;
  242. }
  243. /**
  244. * 获取某考生的 所有的“预约记录”缓存
  245. */
  246. public Map<String, ApplyRecordCacheBean> getStudentApplyRecords(Long studentId) {
  247. String cacheKey = String.format(CACHE_STUDENT_APPLY_RECORD, studentId);
  248. if (!redisClient.exist(cacheKey)) {
  249. Map<String, ApplyRecordCacheBean> maps = this.getStudentApplyRecordsFromDB(studentId);
  250. if (MapUtils.isEmpty(maps)) {
  251. return new HashMap<>();
  252. }
  253. for (ApplyRecordCacheBean bean : maps.values()) {
  254. this.saveStudentApplyRecord(bean);
  255. }
  256. redisClient.expire(cacheKey, CACHE_TIME_OUT_30, TimeUnit.DAYS);
  257. return maps;
  258. }
  259. return redisClient.getEntriesForHash(cacheKey, ApplyRecordCacheBean.class);
  260. }
  261. /**
  262. * 获取某考生的 所有的“预约记录”(查数据库)
  263. */
  264. public Map<String, ApplyRecordCacheBean> getStudentApplyRecordsFromDB(Long studentId) {
  265. LambdaQueryWrapper<StudentApplyEntity> wrapper = new LambdaQueryWrapper<>();
  266. wrapper.eq(StudentApplyEntity::getStudentId, studentId);
  267. List<StudentApplyEntity> list = studentApplyService.list(wrapper);
  268. if (CollectionUtils.isEmpty(list)) {
  269. return new HashMap<>();
  270. }
  271. Map<String, ApplyRecordCacheBean> maps = new HashMap<>();
  272. for (StudentApplyEntity entity : list) {
  273. ApplyRecordCacheBean bean = new ApplyRecordCacheBean();
  274. bean.setStudentId(entity.getStudentId());
  275. bean.setExamSiteId(entity.getExamSiteId());
  276. bean.setTimePeriodId(entity.getTimePeriodId());
  277. bean.setCancel(entity.getCancel());
  278. bean.setOperateId(entity.getOperateId());
  279. bean.setOperateTime(entity.getUpdateTime());
  280. String hashKey = String.format("%s_%s", entity.getExamSiteId(), entity.getTimePeriodId());
  281. maps.put(hashKey, bean);
  282. }
  283. return maps;
  284. }
  285. /**
  286. * 获取某考生的 某考点某时段的“预约记录”缓存
  287. */
  288. public ApplyRecordCacheBean getStudentApplyRecord(Long studentId, Long examSiteId, Long timePeriodId) {
  289. String cacheKey = String.format(CACHE_STUDENT_APPLY_RECORD, studentId);
  290. String hashKey = String.format("%s_%s", examSiteId, timePeriodId);
  291. return redisClient.getForHash(cacheKey, hashKey, ApplyRecordCacheBean.class);
  292. }
  293. /**
  294. * 获取某考生的 某考点某时段的“预约记录”(查数据库)
  295. */
  296. public StudentApplyEntity getStudentApplyRecordFormDB(Long studentId, Long examSiteId, Long timePeriodId) {
  297. LambdaQueryWrapper<StudentApplyEntity> wrapper = new LambdaQueryWrapper<>();
  298. wrapper.eq(StudentApplyEntity::getExamSiteId, examSiteId);
  299. wrapper.eq(StudentApplyEntity::getTimePeriodId, timePeriodId);
  300. wrapper.eq(StudentApplyEntity::getStudentId, studentId);
  301. return studentApplyService.getOne(wrapper);
  302. }
  303. /**
  304. * 保存某考生的 某考点某时段的“预约记录”缓存
  305. */
  306. public void saveStudentApplyRecord(ApplyRecordCacheBean value) {
  307. String cacheKey = String.format(CACHE_STUDENT_APPLY_RECORD, value.getStudentId());
  308. String hashKey = String.format("%s_%s", value.getExamSiteId(), value.getTimePeriodId());
  309. redisClient.setForHash(cacheKey, hashKey, value);
  310. log.info("SET cacheKey:{} hashKey:{} cancel:{}", cacheKey, hashKey, value.getCancel());
  311. }
  312. /**
  313. * 推送至考生预约记录队列
  314. */
  315. public void pushStudentApplyRecordQueue(ApplyRecordCacheBean value) {
  316. if (value == null) {
  317. return;
  318. }
  319. RQueue<ApplyRecordCacheBean> queue = redisClient.getRedissonClient()
  320. .getQueue(QUEUE_STUDENT_APPLY_RECORD);
  321. boolean success = queue.offer(value);
  322. log.info("{}_{}_{}_{} offerQueue:{}", value.getStudentId(), value.getExamSiteId(),
  323. value.getTimePeriodId(), value.getCancel(), success);
  324. if (!success) {
  325. throw new RuntimeException("推送至考生预约记录队列失败");
  326. }
  327. }
  328. /**
  329. * 获取考生预约记录队列数量
  330. */
  331. public int getStudentApplyRecordQueueSize() {
  332. RQueue<ApplyRecordCacheBean> queue = redisClient.getRedissonClient()
  333. .getQueue(QUEUE_STUDENT_APPLY_RECORD);
  334. return queue.size();
  335. }
  336. }