MyList.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. const API = require("../../../../lib/API")
  2. const db = require("../../../../plugin/DataBase/db")
  3. const AccessControl = require("../../../../lib/AccessControl")
  4. const { BaseStdResponse } = require("../../../../BaseStdResponse")
  5. class MyList extends API {
  6. constructor() {
  7. super()
  8. this.setPath('/Lepao/Count/Ledger/MyList')
  9. this.setMethod('GET')
  10. }
  11. async onRequest(req, res) {
  12. let { uuid, session, current = 1, pagesize = 20, biz_type, remark, start_time, end_time } = req.query
  13. current = Number(current)
  14. pagesize = Number(pagesize)
  15. const startTimeNum = start_time === undefined || start_time === null || start_time === '' ? null : Number(start_time)
  16. const endTimeNum = end_time === undefined || end_time === null || end_time === '' ? null : Number(end_time)
  17. if ([uuid, session].some(v => v === '' || v === null || v === undefined)) {
  18. return res.json({ ...BaseStdResponse.MISSING_PARAMETER })
  19. }
  20. if (!Number.isInteger(current) || current < 1 || !Number.isInteger(pagesize) || pagesize < 1 || pagesize > 100) {
  21. return res.json({ ...BaseStdResponse.ERR, msg: '参数错误' })
  22. }
  23. if ((startTimeNum !== null && !Number.isFinite(startTimeNum)) || (endTimeNum !== null && !Number.isFinite(endTimeNum))) {
  24. return res.json({ ...BaseStdResponse.ERR, msg: '时间参数错误' })
  25. }
  26. if (!await AccessControl.checkSession(uuid, session)) {
  27. return res.status(401).json({ ...BaseStdResponse.ACCESS_DENIED })
  28. }
  29. const where = ['l.user_uuid = ?']
  30. const params = [uuid]
  31. const sumWhere = ['user_uuid = ?']
  32. const sumParams = [uuid]
  33. const offset = (current - 1) * pagesize
  34. if (biz_type) {
  35. where.push('l.biz_type = ?')
  36. params.push(biz_type)
  37. sumWhere.push('biz_type = ?')
  38. sumParams.push(biz_type)
  39. }
  40. if (remark) {
  41. where.push('l.remark LIKE ?')
  42. params.push(`%${remark}%`)
  43. sumWhere.push('remark LIKE ?')
  44. sumParams.push(`%${remark}%`)
  45. }
  46. if (startTimeNum !== null) {
  47. where.push('l.created_at >= FROM_UNIXTIME(? / 1000)')
  48. params.push(startTimeNum)
  49. sumWhere.push('created_at >= FROM_UNIXTIME(? / 1000)')
  50. sumParams.push(startTimeNum)
  51. }
  52. if (endTimeNum !== null) {
  53. where.push('l.created_at <= FROM_UNIXTIME(? / 1000)')
  54. params.push(endTimeNum)
  55. sumWhere.push('created_at <= FROM_UNIXTIME(? / 1000)')
  56. sumParams.push(endTimeNum)
  57. }
  58. const whereSql = where.join(' AND ')
  59. const listSql = `
  60. SELECT
  61. l.id,
  62. l.user_uuid,
  63. l.delta,
  64. l.balance_before,
  65. l.balance_after,
  66. l.biz_type,
  67. l.biz_id,
  68. l.operator_uuid,
  69. l.remark,
  70. UNIX_TIMESTAMP(l.created_at) * 1000 AS created_at
  71. FROM lepao_count_ledger l
  72. WHERE ${whereSql}
  73. ORDER BY l.id DESC
  74. LIMIT ${pagesize} OFFSET ${offset}
  75. `
  76. const countSql = `SELECT COUNT(*) AS total FROM lepao_count_ledger l WHERE ${whereSql}`
  77. const summarySql = `
  78. SELECT
  79. COALESCE(SUM(delta), 0) AS total_delta,
  80. COUNT(*) AS total_records
  81. FROM lepao_count_ledger
  82. WHERE ${sumWhere.join(' AND ')}
  83. `
  84. const rows = await db.query(listSql, params)
  85. const countRows = await db.query(countSql, params)
  86. const summaryRows = await db.query(summarySql, sumParams)
  87. if (!rows || !countRows || !summaryRows) {
  88. return res.json({ ...BaseStdResponse.DATABASE_ERR })
  89. }
  90. return res.json({
  91. ...BaseStdResponse.OK,
  92. data: rows,
  93. summary: summaryRows[0] || { total_delta: 0, total_records: 0 },
  94. pagination: {
  95. current,
  96. pagesize,
  97. total: Number(countRows[0]?.total || 0)
  98. }
  99. })
  100. }
  101. }
  102. module.exports.MyList = MyList