123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 |
- const express = require('express');
- const router = express.Router();
- const Transaction = require('../models/Transaction');
- const Group = require('../models/Group');
- // 获取群组交易记录(不需要认证)
- router.get('/transactions', async (req, res) => {
- try {
- const { page = 1, limit = 10, groupId } = req.query;
- if (!groupId) {
- return res.status(400).json({ message: '缺少群组ID参数' });
- }
- console.log('查询参数:', { page, limit, groupId });
- // 获取交易记录
- const result = await Transaction.findAll(
- { groupId: groupId.toString() },
- parseInt(page),
- parseInt(limit)
- );
- console.log('查询结果:', result);
- // 获取统计数据
- const today = new Date();
- today.setHours(0, 0, 0, 0);
- const firstDayOfMonth = new Date(today.getFullYear(), today.getMonth(), 1);
- // 获取今日收入
- const todayResult = await Transaction.findAll(
- {
- groupId: groupId.toString(),
- startDate: today.toISOString().split('T')[0],
- type: 'deposit'
- },
- 1,
- 1000000
- );
- const todayIncome = todayResult.transactions.reduce((sum, t) => sum + parseFloat(t.amount), 0);
- // 获取本月收入
- const monthResult = await Transaction.findAll(
- {
- groupId: groupId.toString(),
- startDate: firstDayOfMonth.toISOString().split('T')[0],
- type: 'deposit'
- },
- 1,
- 1000000
- );
- const monthIncome = monthResult.transactions.reduce((sum, t) => sum + parseFloat(t.amount), 0);
- // 获取总收入
- const totalResult = await Transaction.findAll(
- {
- groupId: groupId.toString(),
- type: 'deposit'
- },
- 1,
- 1000000
- );
- const totalIncome = totalResult.transactions.reduce((sum, t) => sum + parseFloat(t.amount), 0);
- // 获取今日订单数
- const todayOrdersResult = await Transaction.findAll(
- {
- groupId: groupId.toString(),
- startDate: today.toISOString().split('T')[0]
- },
- 1,
- 1000000
- );
- const todayOrders = todayOrdersResult.transactions.length;
- // 获取群组信息(包含费率和汇率)
- const groupInfo = await Group.findByGroupId(groupId.toString());
- if (!groupInfo) {
- return res.status(404).json({ message: '群组不存在' });
- }
- // 计算入款相关数据
- const depositCount = totalResult.transactions.length;
- const depositFeeRate = parseFloat(groupInfo.in_fee_rate) || 0;
- const depositExchangeRate = parseFloat(groupInfo.in_exchange_rate) || 1;
- // 计算出款相关数据
- const withdrawResult = await Transaction.findAll(
- {
- groupId: groupId.toString(),
- type: 'withdrawal'
- },
- 1,
- 1000000
- );
- const withdrawCount = withdrawResult.transactions.length;
- const totalWithdraw = withdrawResult.transactions.reduce((sum, t) => sum + parseFloat(t.amount), 0);
- const withdrawFeeRate = parseFloat(groupInfo.out_fee_rate) || 0;
- const withdrawExchangeRate = parseFloat(groupInfo.out_exchange_rate) || 1;
- // 计算余额
- const balance = totalIncome - totalWithdraw;
- res.json({
- transactions: result.transactions,
- total: result.total,
- statistics: {
- todayIncome,
- monthIncome,
- totalIncome,
- todayOrders
- },
- summary: {
- totalDeposit: totalIncome,
- depositCount,
- depositFeeRate,
- depositExchangeRate,
- totalWithdraw,
- withdrawCount,
- withdrawFeeRate,
- withdrawExchangeRate,
- balance
- }
- });
- } catch (error) {
- console.error('获取群组交易记录失败:', error);
- res.status(500).json({ message: '获取群组交易记录失败' });
- }
- });
- module.exports = router;
|