| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142 |
- const API = require("../../../../lib/API")
- const db = require("../../../../plugin/DataBase/db")
- const AccessControl = require("../../../../lib/AccessControl")
- const { BaseStdResponse } = require("../../../../BaseStdResponse")
- class AdminList extends API {
- constructor() {
- super()
- this.setPath('/Admin/Lepao/Count/Ledger/List')
- this.setMethod('GET')
- }
- async onRequest(req, res) {
- let {
- uuid,
- session,
- current = 1,
- pagesize = 20,
- user_uuid,
- username,
- student_num,
- biz_type,
- operator_uuid,
- start_time,
- end_time
- } = req.query
- current = Number(current)
- pagesize = Number(pagesize)
- const startTimeNum = start_time === undefined || start_time === null || start_time === '' ? null : Number(start_time)
- const endTimeNum = end_time === undefined || end_time === null || end_time === '' ? null : Number(end_time)
- if ([uuid, session].some(v => v === '' || v === null || v === undefined)) {
- return res.json({ ...BaseStdResponse.MISSING_PARAMETER })
- }
- if (!Number.isInteger(current) || current < 1 || !Number.isInteger(pagesize) || pagesize < 1 || pagesize > 100) {
- return res.json({ ...BaseStdResponse.ERR, msg: '参数错误' })
- }
- if ((startTimeNum !== null && !Number.isFinite(startTimeNum)) || (endTimeNum !== null && !Number.isFinite(endTimeNum))) {
- return res.json({ ...BaseStdResponse.ERR, msg: '时间参数错误' })
- }
- if (!await AccessControl.checkSession(uuid, session)) {
- return res.status(401).json({ ...BaseStdResponse.ACCESS_DENIED })
- }
- const permission = await AccessControl.getPermission(uuid)
- if (!permission.includes('admin') && !permission.includes('service')) {
- return res.json({ ...BaseStdResponse.PERMISSION_DENIED })
- }
- const where = ['1 = 1']
- const params = []
- const offset = (current - 1) * pagesize
- if (user_uuid) {
- where.push('l.user_uuid COLLATE utf8mb4_general_ci = ?')
- params.push(user_uuid)
- }
- if (username) {
- where.push('u.username LIKE ?')
- params.push(`%${username}%`)
- }
- if (student_num) {
- where.push('la.student_num LIKE ?')
- params.push(`%${student_num}%`)
- }
- if (biz_type) {
- where.push('l.biz_type COLLATE utf8mb4_general_ci = ?')
- params.push(biz_type)
- }
- if (operator_uuid) {
- where.push('l.operator_uuid COLLATE utf8mb4_general_ci = ?')
- params.push(operator_uuid)
- }
- if (startTimeNum !== null) {
- where.push('l.created_at >= FROM_UNIXTIME(? / 1000)')
- params.push(startTimeNum)
- }
- if (endTimeNum !== null) {
- where.push('l.created_at <= FROM_UNIXTIME(? / 1000)')
- params.push(endTimeNum)
- }
- const whereSql = where.join(' AND ')
- const listSql = `
- SELECT
- l.id,
- l.user_uuid,
- u.username,
- u.avatar AS user_avatar,
- la.student_num,
- l.delta,
- l.balance_before,
- l.balance_after,
- l.biz_type,
- l.biz_id,
- l.operator_uuid,
- op.username AS operator_name,
- op.avatar AS operator_avatar,
- l.remark,
- UNIX_TIMESTAMP(l.created_at) * 1000 AS created_at
- FROM lepao_count_ledger l
- LEFT JOIN users u ON u.uuid = l.user_uuid COLLATE utf8mb4_general_ci
- LEFT JOIN users op ON op.uuid = l.operator_uuid COLLATE utf8mb4_general_ci
- LEFT JOIN (
- SELECT create_user, MIN(student_num) AS student_num
- FROM lepao_account
- GROUP BY create_user
- ) la ON la.create_user = l.user_uuid COLLATE utf8mb4_general_ci
- WHERE ${whereSql}
- ORDER BY l.id DESC
- LIMIT ${pagesize} OFFSET ${offset}
- `
- const countSql = `
- SELECT COUNT(*) AS total
- FROM lepao_count_ledger l
- LEFT JOIN users u ON u.uuid = l.user_uuid COLLATE utf8mb4_general_ci
- LEFT JOIN (
- SELECT create_user, MIN(student_num) AS student_num
- FROM lepao_account
- GROUP BY create_user
- ) la ON la.create_user = l.user_uuid COLLATE utf8mb4_general_ci
- WHERE ${whereSql}
- `
- const rows = await db.query(listSql, params)
- const countRows = await db.query(countSql, params)
- if (!rows || !countRows) {
- return res.json({ ...BaseStdResponse.DATABASE_ERR })
- }
- return res.json({
- ...BaseStdResponse.OK,
- data: rows,
- pagination: {
- current,
- pagesize,
- total: Number(countRows[0]?.total || 0)
- }
- })
- }
- }
- module.exports.AdminList = AdminList
|