ApplyTaskCacheService.java 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  1. package com.qmth.exam.reserve.cache.impl;
  2. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  3. import com.qmth.boot.core.concurrent.service.ConcurrentService;
  4. import com.qmth.exam.reserve.bean.apply.ApplyRecordCacheBean;
  5. import com.qmth.exam.reserve.bean.applytask.CurrentApplyTaskVO;
  6. import com.qmth.exam.reserve.bean.examsite.ExamSiteCapacityInfo;
  7. import com.qmth.exam.reserve.cache.CacheConstants;
  8. import com.qmth.exam.reserve.cache.RedisClient;
  9. import com.qmth.exam.reserve.entity.StudentApplyEntity;
  10. import com.qmth.exam.reserve.entity.TimePeriodEntity;
  11. import com.qmth.exam.reserve.mq.ExamReserveMQProducer;
  12. import com.qmth.exam.reserve.service.*;
  13. import org.apache.commons.collections4.CollectionUtils;
  14. import org.apache.commons.collections4.MapUtils;
  15. import org.apache.commons.lang3.ArrayUtils;
  16. import org.redisson.api.RAtomicLong;
  17. import org.slf4j.Logger;
  18. import org.slf4j.LoggerFactory;
  19. import org.springframework.beans.factory.annotation.Autowired;
  20. import org.springframework.data.redis.core.script.DefaultRedisScript;
  21. import org.springframework.stereotype.Component;
  22. import java.util.*;
  23. import java.util.concurrent.TimeUnit;
  24. import java.util.concurrent.locks.Lock;
  25. @Component
  26. public class ApplyTaskCacheService implements CacheConstants {
  27. private static final Logger log = LoggerFactory.getLogger(ApplyTaskCacheService.class);
  28. @Autowired
  29. private RedisClient redisClient;
  30. @Autowired
  31. private ExamReserveMQProducer mqProducer;
  32. @Autowired
  33. private ApplyTaskService applyTaskService;
  34. @Autowired
  35. private ExamSiteService examSiteService;
  36. @Autowired
  37. private TimePeriodService timePeriodService;
  38. @Autowired
  39. private StudentService studentService;
  40. @Autowired
  41. private StudentApplyService studentApplyService;
  42. @Autowired
  43. private ConcurrentService concurrentService;
  44. /**
  45. * 获取当前启用的预约任务缓存
  46. */
  47. public CurrentApplyTaskVO currentApplyTask(Long orgId) {
  48. if (orgId == null) {
  49. throw new RuntimeException("参数orgId值不能为空");
  50. }
  51. String cacheKey = String.format(CACHE_CURRENT_APPLY_TASK, orgId);
  52. CurrentApplyTaskVO value = redisClient.get(cacheKey, CurrentApplyTaskVO.class);
  53. if (value != null) {
  54. return value;
  55. }
  56. value = applyTaskService.currentApplyTask(orgId);
  57. if (value == null) {
  58. return null;
  59. }
  60. redisClient.set(cacheKey, value, CACHE_TIME_OUT_10, TimeUnit.MINUTES);
  61. log.info("SET cacheKey:{} curApplyTaskId:{}", cacheKey, value.getTaskId());
  62. return value;
  63. }
  64. /**
  65. * 清除当前启用的预约任务缓存
  66. */
  67. public void clearCurrentApplyTaskCache(Long orgId) {
  68. String cacheKey = String.format(CACHE_CURRENT_APPLY_TASK, orgId);
  69. redisClient.delete(cacheKey);
  70. log.warn("DELETE cacheKey:{}", cacheKey);
  71. }
  72. /**
  73. * 获取某考生的“允许预约时段次数”缓存
  74. */
  75. public int getStudentApplyNumber(Long studentId) {
  76. String cacheKey = String.format(CACHE_STUDENT_APPLY_NUMBER, studentId);
  77. Integer value = redisClient.get(cacheKey, Integer.class);
  78. if (value != null) {
  79. return value;
  80. }
  81. value = studentService.findStudentApplyNumberById(studentId);
  82. redisClient.set(cacheKey, value, CACHE_TIME_OUT_10, TimeUnit.MINUTES);
  83. log.info("SET cacheKey:{} value:{}", cacheKey, value);
  84. return value;
  85. }
  86. /**
  87. * 清除某考生的“允许预约时段次数”缓存
  88. */
  89. public void clearStudentApplyNumberCache(Long studentId) {
  90. String cacheKey = String.format(CACHE_STUDENT_APPLY_NUMBER, studentId);
  91. redisClient.delete(cacheKey);
  92. log.warn("DELETE cacheKey:{}", cacheKey);
  93. }
  94. /**
  95. * 累加 预约队列业务流水号缓存
  96. */
  97. public long increaseBizId() {
  98. RAtomicLong atomic = redisClient.getRedissonClient().getAtomicLong(QUEUE_APPLY_BIZ_ID);
  99. return atomic.incrementAndGet();
  100. }
  101. /**
  102. * 获取某考点某时段的“剩余可约数量”缓存
  103. */
  104. public int getApplyAvailableCount(Long examSiteId, Long timePeriodId) {
  105. String cacheKey = String.format(CACHE_APPLY_AVAILABLE_COUNT, examSiteId, timePeriodId);
  106. RAtomicLong atomic = redisClient.getRedissonClient().getAtomicLong(cacheKey);
  107. return (int) atomic.get();//若缓存不存在时,会返回0
  108. }
  109. /**
  110. * 累加 某考点某时段的“剩余可约数量”缓存
  111. */
  112. public void increaseApplyAvailableCount(Long examSiteId, Long timePeriodId) {
  113. String cacheKey = String.format(CACHE_APPLY_AVAILABLE_COUNT, examSiteId, timePeriodId);
  114. RAtomicLong atomic = redisClient.getRedissonClient().getAtomicLong(cacheKey);
  115. long value = atomic.incrementAndGet();
  116. log.warn("累加成功,考点时段剩余可约数量:{} {}", value, cacheKey);
  117. }
  118. /**
  119. * 累减 某考点某时段的“剩余可约数量”缓存
  120. */
  121. public boolean decreaseApplyAvailableCount(Long examSiteId, Long timePeriodId) {
  122. String cacheKey = String.format(CACHE_APPLY_AVAILABLE_COUNT, examSiteId, timePeriodId);
  123. StringBuilder luaScript = new StringBuilder();
  124. luaScript.append("local key = KEYS[1];");
  125. luaScript.append("if (redis.call('exists', key) == 1) then");
  126. luaScript.append(" local stock = tonumber(redis.call('get', key));");
  127. luaScript.append(" local amount = tonumber(ARGV[1]);");
  128. luaScript.append(" if (stock >= amount) then");
  129. luaScript.append(" return redis.call('decrby', key, amount);");
  130. luaScript.append(" end;");
  131. luaScript.append(" return -1;");
  132. luaScript.append("end;");
  133. luaScript.append("return -2;");
  134. List<String> keys = Collections.singletonList(cacheKey);
  135. Object[] args = new Object[]{1};
  136. // 执行Lua脚本,传入键列表和参数列表
  137. DefaultRedisScript<Long> redisScript = new DefaultRedisScript<>(luaScript.toString(), Long.class);
  138. Long result = redisClient.getRedisTemplate().execute(redisScript, keys, args);
  139. Objects.requireNonNull(result, "缓存操作异常,返回结果为空!");
  140. if (-2 == result) {
  141. log.warn("扣减失败,考点时段剩余可约数量缓存不存在!{}", cacheKey);
  142. return false;
  143. } else if (-1 == result) {
  144. log.warn("扣减失败,考点时段剩余可约数量为0不足!{}", cacheKey);
  145. return false;
  146. }
  147. log.info("扣减成功,考点时段剩余可约数量:{} {}", result, cacheKey);
  148. return true;
  149. }
  150. /**
  151. * 刷新 某考点下所有时段的“剩余可约数量”缓存
  152. */
  153. @Deprecated
  154. public void refreshApplyAvailableCountCache(Long examSiteId, int oldCapacity, int newCapacity) {
  155. String lockKey = String.format(CacheConstants.LOCK_EXAM_SITE_CAPACITY, examSiteId);
  156. Lock lock = concurrentService.getLock(lockKey);
  157. try {
  158. if (!lock.tryLock()) {
  159. throw new RuntimeException("获取锁失败,不允许同时操作!" + lockKey);
  160. }
  161. // 获取所有时段ID集合
  162. List<TimePeriodEntity> timePeriods = timePeriodService.list(new LambdaQueryWrapper<TimePeriodEntity>().select(TimePeriodEntity::getId));
  163. if (CollectionUtils.isEmpty(timePeriods)) {
  164. return;
  165. }
  166. // 新、旧考点容量差额
  167. int diffCapacity = newCapacity - oldCapacity;
  168. for (TimePeriodEntity timePeriod : timePeriods) {
  169. String cacheKey = String.format(CACHE_APPLY_AVAILABLE_COUNT, examSiteId, timePeriod.getId());
  170. if (redisClient.exist(cacheKey)) {
  171. if (diffCapacity == 0) {
  172. // 考点总容量未变化,不用更新缓存
  173. continue;
  174. }
  175. // 总容量变化时,则更新(注意:在所有操作减少某个考场容量时,缩减后的实际容量不能低于考生已预约数量)
  176. RAtomicLong atomic = redisClient.getRedissonClient().getAtomicLong(cacheKey);
  177. long curAvailableCount = atomic.get();
  178. long newAvailableCount = Math.max(curAvailableCount + diffCapacity, 0);
  179. atomic.set(newAvailableCount);
  180. log.warn("考点时段剩余可约数量从{}更新为{} oldCapacity:{} newCapacity:{} {}",
  181. curAvailableCount, newAvailableCount, oldCapacity, newCapacity, cacheKey);
  182. } else {
  183. RAtomicLong atomic = redisClient.getRedissonClient().getAtomicLong(cacheKey);
  184. int finishCount = studentApplyService.countApplyFinishForExamSiteAndTimePeriod(examSiteId, timePeriod.getId());
  185. long newAvailableCount = Math.max(newCapacity - finishCount, 0);
  186. atomic.set(newAvailableCount);
  187. log.warn("缓存不存在,初始考点时段剩余可约数量:{} oldCapacity:{} newCapacity:{} finishCount:{} {}",
  188. newAvailableCount, oldCapacity, newCapacity, finishCount, cacheKey);
  189. }
  190. }
  191. } finally {
  192. try {
  193. lock.unlock();
  194. } catch (Exception e) {
  195. log.warn("解锁失败!lockKey:{} err:{}", lockKey, e.getMessage());
  196. }
  197. }
  198. }
  199. /**
  200. * 刷新 某考点某时段的“剩余可约数量”缓存
  201. */
  202. public void refreshApplyAvailableCountCache(Long examSiteId, Long timePeriodId, int oldCapacity, int newCapacity) {
  203. String lockKey = String.format(CacheConstants.LOCK_EXAM_SITE_CAPACITY, examSiteId);
  204. Lock lock = concurrentService.getLock(lockKey);
  205. try {
  206. if (!lock.tryLock()) {
  207. throw new RuntimeException("获取锁失败,不允许同时操作!" + lockKey);
  208. }
  209. // 新、旧考点容量差额
  210. int diffCapacity = newCapacity - oldCapacity;
  211. String cacheKey = String.format(CACHE_APPLY_AVAILABLE_COUNT, examSiteId, timePeriodId);
  212. if (redisClient.exist(cacheKey)) {
  213. if (diffCapacity == 0) {
  214. // 考点总容量未变化,不用更新缓存
  215. return;
  216. }
  217. // 总容量变化时,则更新(注意:在所有操作减少某个考场容量时,缩减后的实际容量不能低于考生已预约数量)
  218. RAtomicLong atomic = redisClient.getRedissonClient().getAtomicLong(cacheKey);
  219. long curAvailableCount = atomic.get();
  220. long newAvailableCount = Math.max(curAvailableCount + diffCapacity, 0);
  221. atomic.set(newAvailableCount);
  222. log.warn("考点时段剩余可约数量从{}更新为{} oldCapacity:{} newCapacity:{} {}",
  223. curAvailableCount, newAvailableCount, oldCapacity, newCapacity, cacheKey);
  224. } else {
  225. RAtomicLong atomic = redisClient.getRedissonClient().getAtomicLong(cacheKey);
  226. int finishCount = studentApplyService.countApplyFinishForExamSiteAndTimePeriod(examSiteId, timePeriodId);
  227. long newAvailableCount = Math.max(newCapacity - finishCount, 0);
  228. atomic.set(newAvailableCount);
  229. log.warn("缓存不存在,初始考点时段剩余可约数量:{} oldCapacity:{} newCapacity:{} finishCount:{} {}",
  230. newAvailableCount, oldCapacity, newCapacity, finishCount, cacheKey);
  231. }
  232. } finally {
  233. try {
  234. lock.unlock();
  235. } catch (Exception e) {
  236. log.warn("解锁失败!lockKey:{} err:{}", lockKey, e.getMessage());
  237. }
  238. }
  239. }
  240. /**
  241. * 初始或重置计算 所有考点所有时段的“剩余可约数量”缓存
  242. */
  243. public void initApplyAvailableCountCacheForAllExamSites(boolean skipExisted) {
  244. // 获取所有考点和考点总容量集合
  245. List<ExamSiteCapacityInfo> examSites = examSiteService.findAllExamSiteCapacityList();
  246. if (CollectionUtils.isEmpty(examSites)) {
  247. return;
  248. }
  249. // 获取所有时段ID集合
  250. List<TimePeriodEntity> timePeriods = timePeriodService.list(new LambdaQueryWrapper<TimePeriodEntity>().select(TimePeriodEntity::getId));
  251. if (CollectionUtils.isEmpty(timePeriods)) {
  252. return;
  253. }
  254. for (ExamSiteCapacityInfo examSite : examSites) {
  255. String lockKey = String.format(CacheConstants.LOCK_EXAM_SITE_CAPACITY, examSite.getExamSiteId());
  256. Lock lock = concurrentService.getLock(lockKey);
  257. try {
  258. if (!lock.tryLock()) {
  259. log.warn("获取锁失败,不允许同时操作!{}", lockKey);
  260. return;
  261. }
  262. for (TimePeriodEntity timePeriod : timePeriods) {
  263. String cacheKey = String.format(CACHE_APPLY_AVAILABLE_COUNT, examSite.getExamSiteId(), timePeriod.getId());
  264. boolean existCache = redisClient.exist(cacheKey);
  265. if (existCache && skipExisted) {
  266. // 跳过存在的缓存
  267. continue;
  268. }
  269. // 获取某考点某时段的“已预约数量”(仅查数据库会有误差,需等预约队列中全部完成数据库持久化后再执行!!!)
  270. int finishCount = studentApplyService.countApplyFinishForExamSiteAndTimePeriod(examSite.getExamSiteId(), timePeriod.getId());
  271. int curCapacity = examSiteService.getExamSiteTimePeriodCapacity(examSite.getExamSiteId(), timePeriod.getId());
  272. // 剩余可约数量
  273. int availableCount = Math.max(curCapacity - finishCount, 0);
  274. RAtomicLong atomic = redisClient.getRedissonClient().getAtomicLong(cacheKey);
  275. atomic.set(availableCount);
  276. log.warn("{}考点时段剩余可约数量:{} curCapacity:{} finishCount:{} {}",
  277. existCache ? "更新" : "初始", availableCount, curCapacity, finishCount, cacheKey);
  278. }
  279. } finally {
  280. try {
  281. lock.unlock();
  282. } catch (Exception e) {
  283. log.warn("解锁失败!lockKey:{} err:{}", lockKey, e.getMessage());
  284. }
  285. }
  286. }
  287. }
  288. /**
  289. * 初始 某些时段与所有考点的“剩余可约数量”缓存
  290. */
  291. public void initApplyAvailableCountCacheForTimePeriods(Long... timePeriodIds) {
  292. if (ArrayUtils.isEmpty(timePeriodIds)) {
  293. return;
  294. }
  295. // 获取所有考点和考点总容量集合
  296. List<ExamSiteCapacityInfo> examSites = examSiteService.findAllExamSiteCapacityList();
  297. if (CollectionUtils.isEmpty(examSites)) {
  298. return;
  299. }
  300. for (ExamSiteCapacityInfo examSite : examSites) {
  301. String lockKey = String.format(CacheConstants.LOCK_EXAM_SITE_CAPACITY, examSite.getExamSiteId());
  302. Lock lock = concurrentService.getLock(lockKey);
  303. try {
  304. if (!lock.tryLock()) {
  305. throw new RuntimeException("获取锁失败,不允许同时操作!" + lockKey);
  306. }
  307. for (Long timePeriodId : timePeriodIds) {
  308. String cacheKey = String.format(CACHE_APPLY_AVAILABLE_COUNT, examSite.getExamSiteId(), timePeriodId);
  309. boolean existCache = redisClient.exist(cacheKey);
  310. if (existCache) {
  311. // 跳过存在的缓存
  312. continue;
  313. }
  314. // 获取某考点某时段的“已预约数量”
  315. int finishCount = studentApplyService.countApplyFinishForExamSiteAndTimePeriod(examSite.getExamSiteId(), timePeriodId);
  316. int curCapacity = examSiteService.getExamSiteTimePeriodCapacity(examSite.getExamSiteId(), timePeriodId);
  317. // 剩余可约数量
  318. int availableCount = Math.max(curCapacity - finishCount, 0);
  319. RAtomicLong atomic = redisClient.getRedissonClient().getAtomicLong(cacheKey);
  320. atomic.set(availableCount);
  321. log.warn("初始考点时段剩余可约数量:{} curCapacity:{} finishCount:{} {}",
  322. availableCount, curCapacity, finishCount, cacheKey);
  323. }
  324. } finally {
  325. try {
  326. lock.unlock();
  327. } catch (Exception e) {
  328. log.warn("解锁失败!lockKey:{} err:{}", lockKey, e.getMessage());
  329. }
  330. }
  331. }
  332. }
  333. public void saveTimePeriodUsedCache(Long timePeriodId) {
  334. String cacheKey = String.format(CACHE_TIME_PERIOD_USED, timePeriodId);
  335. redisClient.set(cacheKey, true, CACHE_TIME_OUT_1, TimeUnit.DAYS);
  336. log.info("SET cacheKey:{}", cacheKey);
  337. }
  338. public boolean existTimePeriodUsedCache(Long timePeriodId) {
  339. String cacheKey = String.format(CACHE_TIME_PERIOD_USED, timePeriodId);
  340. return redisClient.exist(cacheKey);
  341. }
  342. /**
  343. * 获取某考生的 已完成预约次数缓存(查数据库)
  344. */
  345. public int getStudentApplyFinishCountFromDB(Long studentId) {
  346. LambdaQueryWrapper<StudentApplyEntity> wrapper = new LambdaQueryWrapper<>();
  347. wrapper.eq(StudentApplyEntity::getStudentId, studentId);
  348. wrapper.eq(StudentApplyEntity::getCancel, Boolean.FALSE);
  349. return studentApplyService.count(wrapper);
  350. }
  351. /**
  352. * 获取某考生的 已完成预约次数缓存
  353. */
  354. public int getStudentApplyFinishCount(Long studentId) {
  355. Map<String, ApplyRecordCacheBean> maps = this.getStudentApplyRecords(studentId);
  356. int studentApplyFinishCount = 0;
  357. for (ApplyRecordCacheBean bean : maps.values()) {
  358. if (!bean.getCancel()) {
  359. studentApplyFinishCount++;
  360. }
  361. }
  362. return studentApplyFinishCount;
  363. }
  364. /**
  365. * 获取某考生的 所有的“预约记录”缓存
  366. */
  367. public Map<String, ApplyRecordCacheBean> getStudentApplyRecords(Long studentId) {
  368. String cacheKey = String.format(CACHE_STUDENT_APPLY_RECORD, studentId);
  369. if (!redisClient.exist(cacheKey)) {
  370. Map<String, ApplyRecordCacheBean> maps = this.getStudentApplyRecordsFromDB(studentId);
  371. if (MapUtils.isEmpty(maps)) {
  372. return new HashMap<>();
  373. }
  374. for (ApplyRecordCacheBean bean : maps.values()) {
  375. this.saveStudentApplyRecord(bean);
  376. }
  377. redisClient.expire(cacheKey, CACHE_TIME_OUT_60, TimeUnit.DAYS);
  378. return maps;
  379. }
  380. return redisClient.getEntriesForHash(cacheKey, ApplyRecordCacheBean.class);
  381. }
  382. /**
  383. * 获取某考生的 所有的“预约记录”(查数据库)
  384. */
  385. public Map<String, ApplyRecordCacheBean> getStudentApplyRecordsFromDB(Long studentId) {
  386. LambdaQueryWrapper<StudentApplyEntity> wrapper = new LambdaQueryWrapper<>();
  387. wrapper.eq(StudentApplyEntity::getStudentId, studentId);
  388. List<StudentApplyEntity> list = studentApplyService.list(wrapper);
  389. if (CollectionUtils.isEmpty(list)) {
  390. return new HashMap<>();
  391. }
  392. Map<String, ApplyRecordCacheBean> maps = new HashMap<>();
  393. for (StudentApplyEntity entity : list) {
  394. ApplyRecordCacheBean bean = new ApplyRecordCacheBean();
  395. bean.setStudentId(entity.getStudentId());
  396. bean.setExamSiteId(entity.getExamSiteId());
  397. bean.setTimePeriodId(entity.getTimePeriodId());
  398. bean.setCancel(entity.getCancel());
  399. bean.setOperateId(entity.getOperateId());
  400. bean.setOperateTime(entity.getUpdateTime());
  401. String hashKey = String.format("%s_%s", entity.getExamSiteId(), entity.getTimePeriodId());
  402. maps.put(hashKey, bean);
  403. }
  404. return maps;
  405. }
  406. /**
  407. * 获取某考生的 某考点某时段的“预约记录”缓存
  408. */
  409. public ApplyRecordCacheBean getStudentApplyRecord(Long studentId, Long examSiteId, Long timePeriodId) {
  410. String cacheKey = String.format(CACHE_STUDENT_APPLY_RECORD, studentId);
  411. String hashKey = String.format("%s_%s", examSiteId, timePeriodId);
  412. ApplyRecordCacheBean cacheBean = redisClient.getForHash(cacheKey, hashKey, ApplyRecordCacheBean.class);
  413. if (cacheBean != null) {
  414. return cacheBean;
  415. }
  416. // 缓存不存在时,从数据库再查一次
  417. /*
  418. StudentApplyEntity entity = this.getStudentApplyRecordFormDB(studentId, examSiteId, timePeriodId);
  419. if (entity != null) {
  420. cacheBean = new ApplyRecordCacheBean();
  421. cacheBean.setStudentId(entity.getStudentId());
  422. cacheBean.setExamSiteId(entity.getExamSiteId());
  423. cacheBean.setTimePeriodId(entity.getTimePeriodId());
  424. cacheBean.setCancel(entity.getCancel());
  425. cacheBean.setOperateId(entity.getOperateId());
  426. cacheBean.setOperateTime(entity.getUpdateTime());
  427. return cacheBean;
  428. }*/
  429. return null;
  430. }
  431. /**
  432. * 获取某考生的 某考点某时段的“预约记录”(查数据库)
  433. */
  434. public StudentApplyEntity getStudentApplyRecordFormDB(Long studentId, Long examSiteId, Long timePeriodId) {
  435. LambdaQueryWrapper<StudentApplyEntity> wrapper = new LambdaQueryWrapper<>();
  436. wrapper.eq(StudentApplyEntity::getExamSiteId, examSiteId);
  437. wrapper.eq(StudentApplyEntity::getTimePeriodId, timePeriodId);
  438. wrapper.eq(StudentApplyEntity::getStudentId, studentId);
  439. return studentApplyService.getOne(wrapper);
  440. }
  441. /**
  442. * 保存某考生的 某考点某时段的“预约记录”缓存
  443. */
  444. public void saveStudentApplyRecord(ApplyRecordCacheBean value) {
  445. String cacheKey = String.format(CACHE_STUDENT_APPLY_RECORD, value.getStudentId());
  446. String hashKey = String.format("%s_%s", value.getExamSiteId(), value.getTimePeriodId());
  447. redisClient.setForHash(cacheKey, hashKey, value);
  448. log.info("SET cacheKey:{} hashKey:{} cancel:{}", cacheKey, hashKey, value.getCancel());
  449. }
  450. /**
  451. * 推送至考生预约队列
  452. */
  453. public boolean pushStudentApplyRecordQueue(ApplyRecordCacheBean value) {
  454. try {
  455. mqProducer.sendMessage(value);
  456. return true;
  457. } catch (Exception e) {
  458. log.error("【考生预约队列】消息推送失败! {}_{}_{}_{} bizId:{} err:{}", value.getStudentId(), value.getExamSiteId(),
  459. value.getTimePeriodId(), value.getCancel(), value.getBizId(), e.getMessage());
  460. }
  461. return false;
  462. }
  463. }