ResetMonthTermNum.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. const API = require("../../lib/API.js")
  2. const db = require('../../plugin/DataBase/db')
  3. const Redis = require('../../plugin/DataBase/Redis')
  4. const { BaseStdResponse } = require("../../BaseStdResponse")
  5. class ResetMonthTermNum extends API {
  6. constructor() {
  7. super()
  8. this.noEncrypt()
  9. this.setPath('/Corn/ResetMonthTermNum')
  10. this.setMethod('GET')
  11. }
  12. async onRequest(req, res) {
  13. try {
  14. res.json({
  15. ...BaseStdResponse.OK
  16. })
  17. // 1) 清空所有账号本月累计里程(term_num)
  18. const r = await db.query('UPDATE lepao_account SET term_num = 0')
  19. if (!r) {
  20. this.logger.error('月初重置 term_num 失败:数据库更新失败')
  21. return
  22. }
  23. // 2) 清理 Redis 当月缓存(jkes_month:{student}:{YYYY-MM})
  24. const now = new Date()
  25. const y = now.getFullYear()
  26. const m = String(now.getMonth() + 1).padStart(2, '0')
  27. const pattern = `jkes_month:*:${y}-${m}`
  28. try {
  29. const toDel = []
  30. for await (const key of Redis.scanIterator({ MATCH: pattern, COUNT: 500 })) {
  31. toDel.push(key)
  32. if (toDel.length >= 500) {
  33. await Redis.del(toDel)
  34. toDel.length = 0
  35. }
  36. }
  37. if (toDel.length) {
  38. await Redis.del(toDel)
  39. }
  40. } catch (e) {
  41. this.logger.warn(`月初清理 Redis 月缓存失败:${e.message || e}`)
  42. }
  43. this.logger.info(`月初重置完成:term_num=0,pattern=${pattern}`)
  44. } catch (error) {
  45. this.logger.error(error)
  46. }
  47. }
  48. }
  49. module.exports.ResetMonthTermNum = ResetMonthTermNum