123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 |
- const Transaction = require('../models/Transaction');
- const Group = require('../models/Group');
- const getTransactions = async (req, res) => {
- try {
- const { page = 1, limit = 10, startDate, endDate, type, groupId } = req.query;
- const result = await Transaction.findAll(
- { startDate, endDate, type, groupId },
- parseInt(page),
- parseInt(limit)
- );
- res.json(result);
- } catch (error) {
- res.status(500).json({ message: '服务器错误' });
- }
- };
- const createTransaction = async (req, res) => {
- try {
- const { groupId, type, amount } = req.body;
- const group = await Group.findByGroupId(groupId);
- if (!group) {
- return res.status(404).json({ message: '群组不存在' });
- }
- const id = await Transaction.create({
- groupId,
- groupName: group.group_name,
- type,
- amount
- });
- const transaction = await Transaction.findById(id);
- res.status(201).json(transaction);
- } catch (error) {
- res.status(500).json({ message: '服务器错误' });
- }
- };
- const deleteTransaction = async (req, res) => {
- try {
- const transaction = await Transaction.findById(req.params.id);
- if (!transaction) {
- return res.status(404).json({ message: '交易不存在' });
- }
- await Transaction.delete(req.params.id);
- res.json({ message: '交易已删除' });
- } catch (error) {
- res.status(500).json({ message: '服务器错误' });
- }
- };
- const getDashboardData = async (req, res) => {
- try {
- const data = await Transaction.getDashboardData();
- res.json(data);
- } catch (error) {
- res.status(500).json({ message: '服务器错误' });
- }
- };
- const exportTransactions = async (req, res) => {
- try {
- const { startDate, endDate, type, groupId } = req.query;
- const result = await Transaction.findAll(
- { startDate, endDate, type, groupId },
- 1,
- 1000000
- );
-
- const csvData = [
- ['时间', '群组', '类型', '金额'].join(','),
- ...result.transactions.map(t => [
- new Date(t.time).toLocaleString(),
- t.group_name,
- t.type === 'deposit' ? '入款' : '下发',
- t.amount.toFixed(2)
- ].join(','))
- ].join('\n');
- res.setHeader('Content-Type', 'text/csv');
- res.setHeader('Content-Disposition', 'attachment; filename=transactions.csv');
- res.send(csvData);
- } catch (error) {
- res.status(500).json({ message: '服务器错误' });
- }
- };
- module.exports = {
- getTransactions,
- createTransaction,
- deleteTransaction,
- getDashboardData,
- exportTransactions
- };
|