Transaction.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. const { pool } = require('../config/database');
  2. // 创建交易表
  3. const createTransactionTable = async () => {
  4. try {
  5. await pool.query(`
  6. CREATE TABLE IF NOT EXISTS transactions (
  7. id INT AUTO_INCREMENT PRIMARY KEY,
  8. group_id VARCHAR(50) NOT NULL,
  9. group_name VARCHAR(100) NOT NULL,
  10. type ENUM('deposit', 'withdrawal') NOT NULL,
  11. amount DECIMAL(10,2) NOT NULL,
  12. time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  13. INDEX idx_group_time (group_id, time)
  14. )
  15. `);
  16. } catch (error) {
  17. console.error('创建交易表失败:', error);
  18. }
  19. };
  20. // 初始化表
  21. createTransactionTable();
  22. // 交易相关方法
  23. const Transaction = {
  24. // 获取交易列表
  25. findAll: async (query = {}, page = 1, limit = 10) => {
  26. let sql = 'SELECT * FROM transactions WHERE 1=1';
  27. const params = [];
  28. if (query.startDate) {
  29. sql += ' AND time >= ?';
  30. params.push(query.startDate);
  31. }
  32. if (query.endDate) {
  33. sql += ' AND time <= ?';
  34. params.push(query.endDate);
  35. }
  36. if (query.type) {
  37. sql += ' AND type = ?';
  38. params.push(query.type);
  39. }
  40. if (query.groupId) {
  41. sql += ' AND group_id = ?';
  42. params.push(query.groupId);
  43. }
  44. // 获取总数
  45. const [countResult] = await pool.query(
  46. sql.replace('SELECT *', 'SELECT COUNT(*) as total'),
  47. params
  48. );
  49. const total = countResult[0].total;
  50. // 获取分页数据
  51. sql += ' ORDER BY time DESC LIMIT ? OFFSET ?';
  52. params.push(limit, (page - 1) * limit);
  53. const [rows] = await pool.query(sql, params);
  54. return {
  55. transactions: rows,
  56. total,
  57. page: parseInt(page),
  58. pages: Math.ceil(total / limit)
  59. };
  60. },
  61. // 创建交易
  62. create: async (transactionData) => {
  63. const [result] = await pool.query(
  64. 'INSERT INTO transactions (group_id, group_name, type, amount) VALUES (?, ?, ?, ?)',
  65. [transactionData.groupId, transactionData.groupName, transactionData.type, transactionData.amount]
  66. );
  67. return result.insertId;
  68. },
  69. // 删除交易
  70. delete: async (id) => {
  71. await pool.query('DELETE FROM transactions WHERE id = ?', [id]);
  72. },
  73. // 获取仪表板数据
  74. getDashboardData: async () => {
  75. const today = new Date();
  76. today.setHours(0, 0, 0, 0);
  77. const [
  78. totalGroups,
  79. totalTransactions,
  80. totalAmount,
  81. todayTransactions,
  82. recentTransactions
  83. ] = await Promise.all([
  84. pool.query('SELECT COUNT(*) as count FROM groups').then(([rows]) => rows[0].count),
  85. pool.query('SELECT COUNT(*) as count FROM transactions').then(([rows]) => rows[0].count),
  86. pool.query('SELECT SUM(amount) as total FROM transactions').then(([rows]) => rows[0].total || 0),
  87. pool.query('SELECT COUNT(*) as count FROM transactions WHERE time >= ?', [today]).then(([rows]) => rows[0].count),
  88. pool.query('SELECT * FROM transactions ORDER BY time DESC LIMIT 5').then(([rows]) => rows)
  89. ]);
  90. // 获取活跃群组
  91. const [activeGroups] = await pool.query(`
  92. SELECT
  93. g.group_name as name,
  94. COUNT(t.id) as totalTransactions,
  95. SUM(CASE WHEN t.time >= ? THEN 1 ELSE 0 END) as todayTransactions
  96. FROM groups g
  97. LEFT JOIN transactions t ON g.group_id = t.group_id
  98. GROUP BY g.id
  99. ORDER BY todayTransactions DESC
  100. LIMIT 5
  101. `, [today]);
  102. return {
  103. totalGroups,
  104. totalTransactions,
  105. totalAmount,
  106. todayTransactions,
  107. recentTransactions,
  108. activeGroups
  109. };
  110. }
  111. };
  112. module.exports = Transaction;