MyList.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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, 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 (startTimeNum !== null) {
  41. where.push('l.created_at >= FROM_UNIXTIME(? / 1000)')
  42. params.push(startTimeNum)
  43. sumWhere.push('created_at >= FROM_UNIXTIME(? / 1000)')
  44. sumParams.push(startTimeNum)
  45. }
  46. if (endTimeNum !== null) {
  47. where.push('l.created_at <= FROM_UNIXTIME(? / 1000)')
  48. params.push(endTimeNum)
  49. sumWhere.push('created_at <= FROM_UNIXTIME(? / 1000)')
  50. sumParams.push(endTimeNum)
  51. }
  52. const whereSql = where.join(' AND ')
  53. const listSql = `
  54. SELECT
  55. l.id,
  56. l.user_uuid,
  57. l.delta,
  58. l.balance_before,
  59. l.balance_after,
  60. l.biz_type,
  61. l.biz_id,
  62. l.operator_uuid,
  63. l.remark,
  64. UNIX_TIMESTAMP(l.created_at) * 1000 AS created_at
  65. FROM lepao_count_ledger l
  66. WHERE ${whereSql}
  67. ORDER BY l.id DESC
  68. LIMIT ${pagesize} OFFSET ${offset}
  69. `
  70. const countSql = `SELECT COUNT(*) AS total FROM lepao_count_ledger l WHERE ${whereSql}`
  71. const summarySql = `
  72. SELECT
  73. COALESCE(SUM(delta), 0) AS total_delta,
  74. COUNT(*) AS total_records
  75. FROM lepao_count_ledger
  76. WHERE ${sumWhere.join(' AND ')}
  77. `
  78. const rows = await db.query(listSql, params)
  79. const countRows = await db.query(countSql, params)
  80. const summaryRows = await db.query(summarySql, sumParams)
  81. if (!rows || !countRows || !summaryRows) {
  82. return res.json({ ...BaseStdResponse.DATABASE_ERR })
  83. }
  84. return res.json({
  85. ...BaseStdResponse.OK,
  86. data: rows,
  87. summary: summaryRows[0] || { total_delta: 0, total_records: 0 },
  88. pagination: {
  89. current,
  90. pagesize,
  91. total: Number(countRows[0]?.total || 0)
  92. }
  93. })
  94. }
  95. }
  96. module.exports.MyList = MyList