public.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. const express = require('express');
  2. const router = express.Router();
  3. const Transaction = require('../models/Transaction');
  4. // 获取群组交易记录(不需要认证)
  5. router.get('/transactions', async (req, res) => {
  6. try {
  7. const { page = 1, limit = 10, groupId } = req.query;
  8. if (!groupId) {
  9. return res.status(400).json({ message: '缺少群组ID参数' });
  10. }
  11. console.log('查询参数:', { page, limit, groupId });
  12. // 获取交易记录
  13. const result = await Transaction.findAll(
  14. { groupId: groupId.toString() },
  15. parseInt(page),
  16. parseInt(limit)
  17. );
  18. console.log('查询结果:', result);
  19. // 获取统计数据
  20. const today = new Date();
  21. today.setHours(0, 0, 0, 0);
  22. const firstDayOfMonth = new Date(today.getFullYear(), today.getMonth(), 1);
  23. // 获取今日收入
  24. const todayResult = await Transaction.findAll(
  25. {
  26. groupId: groupId.toString(),
  27. startDate: today.toISOString().split('T')[0],
  28. type: 'deposit'
  29. },
  30. 1,
  31. 1000000
  32. );
  33. const todayIncome = todayResult.transactions.reduce((sum, t) => sum + parseFloat(t.amount), 0);
  34. // 获取本月收入
  35. const monthResult = await Transaction.findAll(
  36. {
  37. groupId: groupId.toString(),
  38. startDate: firstDayOfMonth.toISOString().split('T')[0],
  39. type: 'deposit'
  40. },
  41. 1,
  42. 1000000
  43. );
  44. const monthIncome = monthResult.transactions.reduce((sum, t) => sum + parseFloat(t.amount), 0);
  45. // 获取总收入
  46. const totalResult = await Transaction.findAll(
  47. {
  48. groupId: groupId.toString(),
  49. type: 'deposit'
  50. },
  51. 1,
  52. 1000000
  53. );
  54. const totalIncome = totalResult.transactions.reduce((sum, t) => sum + parseFloat(t.amount), 0);
  55. // 获取今日订单数
  56. const todayOrdersResult = await Transaction.findAll(
  57. {
  58. groupId: groupId.toString(),
  59. startDate: today.toISOString().split('T')[0]
  60. },
  61. 1,
  62. 1000000
  63. );
  64. const todayOrders = todayOrdersResult.transactions.length;
  65. // 计算总结数据
  66. const depositCount = totalResult.transactions.length;
  67. const withdrawResult = await Transaction.findAll(
  68. {
  69. groupId: groupId.toString(),
  70. type: 'withdrawal'
  71. },
  72. 1,
  73. 1000000
  74. );
  75. const withdrawCount = withdrawResult.transactions.length;
  76. const totalWithdraw = withdrawResult.transactions.reduce((sum, t) => sum + parseFloat(t.amount), 0);
  77. // 计算费率(示例:入款费率1%,下发费率0.5%)
  78. const depositFeeRate = 1;
  79. const withdrawFeeRate = 0.5;
  80. const singleWithdrawFee = 1; // 单笔附加费1元
  81. // 计算应下发金额(入款总额 * (1 - 入款费率))
  82. const shouldWithdraw = totalIncome * (1 - depositFeeRate / 100);
  83. // 计算下发附加费总额
  84. const totalWithdrawFee = withdrawCount * singleWithdrawFee;
  85. // 计算余额(应下发 - 总下发 - 下发附加费总额)
  86. const balance = shouldWithdraw - totalWithdraw - totalWithdrawFee;
  87. res.json({
  88. transactions: result.transactions,
  89. total: result.total,
  90. statistics: {
  91. todayIncome,
  92. monthIncome,
  93. totalIncome,
  94. todayOrders
  95. },
  96. summary: {
  97. totalDeposit: totalIncome,
  98. depositCount,
  99. depositFeeRate,
  100. shouldWithdraw,
  101. totalWithdraw,
  102. withdrawCount,
  103. withdrawFeeRate,
  104. singleWithdrawFee,
  105. totalWithdrawFee,
  106. balance
  107. }
  108. });
  109. } catch (error) {
  110. console.error('获取群组交易记录失败:', error);
  111. res.status(500).json({ message: '获取群组交易记录失败' });
  112. }
  113. });
  114. module.exports = router;