AdminList.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 AdminList extends API {
  6. constructor() {
  7. super()
  8. this.setPath('/Admin/Lepao/Count/Ledger/List')
  9. this.setMethod('GET')
  10. }
  11. async onRequest(req, res) {
  12. let {
  13. uuid,
  14. session,
  15. current = 1,
  16. pagesize = 20,
  17. user_uuid,
  18. username,
  19. student_num,
  20. biz_type,
  21. operator_uuid,
  22. start_time,
  23. end_time
  24. } = req.query
  25. current = Number(current)
  26. pagesize = Number(pagesize)
  27. const startTimeNum = start_time === undefined || start_time === null || start_time === '' ? null : Number(start_time)
  28. const endTimeNum = end_time === undefined || end_time === null || end_time === '' ? null : Number(end_time)
  29. if ([uuid, session].some(v => v === '' || v === null || v === undefined)) {
  30. return res.json({ ...BaseStdResponse.MISSING_PARAMETER })
  31. }
  32. if (!Number.isInteger(current) || current < 1 || !Number.isInteger(pagesize) || pagesize < 1 || pagesize > 100) {
  33. return res.json({ ...BaseStdResponse.ERR, msg: '参数错误' })
  34. }
  35. if ((startTimeNum !== null && !Number.isFinite(startTimeNum)) || (endTimeNum !== null && !Number.isFinite(endTimeNum))) {
  36. return res.json({ ...BaseStdResponse.ERR, msg: '时间参数错误' })
  37. }
  38. if (!await AccessControl.checkSession(uuid, session)) {
  39. return res.status(401).json({ ...BaseStdResponse.ACCESS_DENIED })
  40. }
  41. const permission = await AccessControl.getPermission(uuid)
  42. if (!permission.includes('admin') && !permission.includes('service')) {
  43. return res.json({ ...BaseStdResponse.PERMISSION_DENIED })
  44. }
  45. const where = ['1 = 1']
  46. const params = []
  47. const offset = (current - 1) * pagesize
  48. if (user_uuid) {
  49. where.push('l.user_uuid COLLATE utf8mb4_general_ci = ?')
  50. params.push(user_uuid)
  51. }
  52. if (username) {
  53. where.push('u.username LIKE ?')
  54. params.push(`%${username}%`)
  55. }
  56. if (student_num) {
  57. where.push('la.student_num LIKE ?')
  58. params.push(`%${student_num}%`)
  59. }
  60. if (biz_type) {
  61. where.push('l.biz_type COLLATE utf8mb4_general_ci = ?')
  62. params.push(biz_type)
  63. }
  64. if (operator_uuid) {
  65. where.push('l.operator_uuid COLLATE utf8mb4_general_ci = ?')
  66. params.push(operator_uuid)
  67. }
  68. if (startTimeNum !== null) {
  69. where.push('l.created_at >= FROM_UNIXTIME(? / 1000)')
  70. params.push(startTimeNum)
  71. }
  72. if (endTimeNum !== null) {
  73. where.push('l.created_at <= FROM_UNIXTIME(? / 1000)')
  74. params.push(endTimeNum)
  75. }
  76. const whereSql = where.join(' AND ')
  77. const listSql = `
  78. SELECT
  79. l.id,
  80. l.user_uuid,
  81. u.username,
  82. u.avatar AS user_avatar,
  83. la.student_num,
  84. l.delta,
  85. l.balance_before,
  86. l.balance_after,
  87. l.biz_type,
  88. l.biz_id,
  89. l.operator_uuid,
  90. op.username AS operator_name,
  91. op.avatar AS operator_avatar,
  92. l.remark,
  93. UNIX_TIMESTAMP(l.created_at) * 1000 AS created_at
  94. FROM lepao_count_ledger l
  95. LEFT JOIN users u ON u.uuid = l.user_uuid COLLATE utf8mb4_general_ci
  96. LEFT JOIN users op ON op.uuid = l.operator_uuid COLLATE utf8mb4_general_ci
  97. LEFT JOIN (
  98. SELECT create_user, MIN(student_num) AS student_num
  99. FROM lepao_account
  100. GROUP BY create_user
  101. ) la ON la.create_user = l.user_uuid COLLATE utf8mb4_general_ci
  102. WHERE ${whereSql}
  103. ORDER BY l.id DESC
  104. LIMIT ${pagesize} OFFSET ${offset}
  105. `
  106. const countSql = `
  107. SELECT COUNT(*) AS total
  108. FROM lepao_count_ledger l
  109. LEFT JOIN users u ON u.uuid = l.user_uuid COLLATE utf8mb4_general_ci
  110. LEFT JOIN (
  111. SELECT create_user, MIN(student_num) AS student_num
  112. FROM lepao_account
  113. GROUP BY create_user
  114. ) la ON la.create_user = l.user_uuid COLLATE utf8mb4_general_ci
  115. WHERE ${whereSql}
  116. `
  117. const rows = await db.query(listSql, params)
  118. const countRows = await db.query(countSql, params)
  119. if (!rows || !countRows) {
  120. return res.json({ ...BaseStdResponse.DATABASE_ERR })
  121. }
  122. return res.json({
  123. ...BaseStdResponse.OK,
  124. data: rows,
  125. pagination: {
  126. current,
  127. pagesize,
  128. total: Number(countRows[0]?.total || 0)
  129. }
  130. })
  131. }
  132. }
  133. module.exports.AdminList = AdminList