Browse Source

总金额计算

Taio_O 3 tuần trước cách đây
mục cha
commit
d1400e921c
3 tập tin đã thay đổi với 80 bổ sung12 xóa
  1. 6 0
      admin/config/initDb.js
  2. 23 9
      admin/index.js
  3. 51 3
      admin/models/Transaction.js

+ 6 - 0
admin/config/initDb.js

@@ -71,6 +71,12 @@ async function initDatabase() {
                 status ENUM('pending', 'approved', 'rejected') DEFAULT 'pending' COMMENT '状态',
                 fee_rate DECIMAL(5,2) DEFAULT NULL COMMENT '费率',
                 exchange_rate DECIMAL(10,4) DEFAULT NULL COMMENT '汇率',
+                total_deposit DECIMAL(10,2) DEFAULT 0 COMMENT '总入款人民币金额',
+                total_withdrawal DECIMAL(10,2) DEFAULT 0 COMMENT '总下发人民币金额',
+                deposit_fee DECIMAL(10,2) DEFAULT 0 COMMENT '入款手续费',
+                withdrawal_fee DECIMAL(10,2) DEFAULT 0 COMMENT '下发手续费',
+                total_u_deposit DECIMAL(10,2) DEFAULT 0 COMMENT '总入款usdt金额',
+                total_u_withdrawal DECIMAL(10,2) DEFAULT 0 COMMENT '总下发usdt金额',
                 FOREIGN KEY (operator_id) REFERENCES users(id),
                 FOREIGN KEY (responder_id) REFERENCES users(id),
                 FOREIGN KEY (group_id) REFERENCES groups(group_id),

+ 23 - 9
admin/index.js

@@ -694,7 +694,13 @@ async function generateBillMessage(chatId) {
         const [records] = await pool.query(`
             SELECT t.*, 
                    COALESCE(t.fee_rate, g.in_fee_rate) as fee_rate,
-                   COALESCE(t.exchange_rate, g.in_exchange_rate) as exchange_rate
+                   COALESCE(t.exchange_rate, g.in_exchange_rate) as exchange_rate,
+                   t.total_deposit,
+                   t.total_withdrawal,
+                   t.deposit_fee,
+                   t.withdrawal_fee,
+                   t.total_u_deposit,
+                   t.total_u_withdrawal
             FROM transactions t
             LEFT JOIN groups g ON t.group_id = g.group_id
             WHERE t.group_id = ? 
@@ -706,14 +712,22 @@ async function generateBillMessage(chatId) {
             return '暂无交易记录';
         }
 
+        // 将记录按类型分类:入款和下发
         const deposits = records.filter(r => r.type === 'deposit');
         const withdrawals = records.filter(r => r.type === 'withdrawal');
 
-        const totalDeposit = deposits.reduce((sum, d) => sum + parseFloat(d.amount), 0);
-        const totalWithdrawal = withdrawals.reduce((sum, w) => sum + parseFloat(w.amount), 0);
-        const depositFee = totalDeposit * (inFeeRate / 100);
-        const withdrawalFee = totalWithdrawal * (outFeeRate / 100);
+        // 获取最新一条记录的总金额数据
+        const latestRecord = records[0];
+        const totalDeposit = parseFloat(latestRecord.total_deposit) || 0;
+        const totalWithdrawal = parseFloat(latestRecord.total_withdrawal) || 0;
+        const depositFee = parseFloat(latestRecord.deposit_fee) || 0;
+        const withdrawalFee = parseFloat(latestRecord.withdrawal_fee) || 0;
+        const totalUDeposit = parseFloat(latestRecord.total_u_deposit) || 0;
+        const totalUWithdrawal = parseFloat(latestRecord.total_u_withdrawal) || 0;
+
+        // 计算剩余金额:总入款 - 入款手续费 - 总下发 - 下发手续费
         const remaining = totalDeposit - depositFee - totalWithdrawal - withdrawalFee;
+        // 将剩余金额转换为U币:剩余金额 ÷ 入款汇率,保留2位小数
         const remainingU = (remaining / inExchangeRate).toFixed(2);
 
         // 获取当前日期
@@ -749,17 +763,17 @@ async function generateBillMessage(chatId) {
         message += `<b>入款费率</b>:<code>${inFeeRate}%</code>\n`;
         message += `<b>入款汇率</b>:<code>${inExchangeRate}</code>\n`;
         message += `<b>入款总额</b>:<code>${totalDeposit.toFixed(2)}</code>\n`;
-        message += `<b>入款合计</b>:<code>${(totalDeposit - depositFee).toFixed(2)}|${((totalDeposit - depositFee) / inExchangeRate).toFixed(2)}U</code>\n\n`;
+        message += `<b>入款合计</b>:<code>${(totalDeposit - depositFee).toFixed(2)}|${totalUDeposit.toFixed(2)}U</code>\n\n`;
 
         message += `<b>出款费率</b>:<code>${outFeeRate}%</code>\n`;
         message += `<b>出款汇率</b>:<code>${outExchangeRate}</code>\n`;
         message += `<b>出款总额</b>:<code>${totalWithdrawal.toFixed(2)}</code>\n`;
-        message += `<b>出款合计</b>:<code>${(totalWithdrawal - withdrawalFee).toFixed(2)}|${((totalWithdrawal - withdrawalFee) / outExchangeRate).toFixed(2)}U</code>\n\n`;
+        message += `<b>出款合计</b>:<code>${(totalWithdrawal - withdrawalFee).toFixed(2)}|${totalUWithdrawal.toFixed(2)}U</code>\n\n`;
 
         // 添加余额信息
         message += `<b>应下发</b>:<code>${remainingU}U</code>\n`;
-        message += `<b>已下发</b>:<code>${(totalWithdrawal / outExchangeRate).toFixed(2)}U</code>\n`;
-        message += `<b>未下发</b>:<code>${(remainingU - (totalWithdrawal / outExchangeRate)).toFixed(2)}U</code>`;
+        message += `<b>已下发</b>:<code>${totalUWithdrawal.toFixed(2)}U</code>\n`;
+        message += `<b>未下发</b>:<code>${(remainingU - totalUWithdrawal).toFixed(2)}U</code>`;
 
         return message;
     } catch (error) {

+ 51 - 3
admin/models/Transaction.js

@@ -123,17 +123,65 @@ const Transaction = {
             // 使用群内操作人的ID作为operator_id
             const operatorId = transactionData.operatorId || 1;
 
+            // 获取上一条记录的总金额数据
+            const [lastRecord] = await pool.query(
+                'SELECT total_deposit, total_withdrawal, total_u_deposit, total_u_withdrawal FROM transactions WHERE group_id = ? ORDER BY time DESC LIMIT 1',
+                [transactionData.groupId]
+            );
+
+            // 初始化总金额数据
+            let totalDeposit = 0;
+            let totalWithdrawal = 0;
+            let totalUDeposit = 0;
+            let totalUWithdrawal = 0;
+
+            // 如果有上一条记录,使用其数据
+            if (lastRecord && lastRecord.length > 0) {
+                totalDeposit = parseFloat(lastRecord[0].total_deposit) || 0;
+                totalWithdrawal = parseFloat(lastRecord[0].total_withdrawal) || 0;
+                totalUDeposit = parseFloat(lastRecord[0].total_u_deposit) || 0;
+                totalUWithdrawal = parseFloat(lastRecord[0].total_u_withdrawal) || 0;
+            }
+
+            // 计算本条交易的手续费
+            const currentFee = Math.abs(transactionData.amount) * (feeRate / 100);
+            
+            // 计算本条交易的实际金额(减去手续费)
+            const actualAmount = Math.abs(transactionData.amount) - currentFee;
+            
+            // 计算本条交易的U币金额
+            const uAmount = actualAmount / exchangeRate;
+
+            // 根据交易类型更新总金额
+            if (transactionData.type === 'deposit') {
+                totalDeposit += transactionData.amount;
+                totalUDeposit += uAmount;
+            } else {
+                totalWithdrawal += transactionData.amount;
+                totalUWithdrawal += uAmount;
+            }
+
             const [result] = await pool.query(
-                'INSERT INTO transactions (group_id, group_name, type, amount, remark, operator_id, fee_rate, exchange_rate) VALUES (?, ?, ?, ?, ?, ?, ?, ?)',
+                `INSERT INTO transactions (
+                    group_id, group_name, type, amount, remark, operator_id, 
+                    fee_rate, exchange_rate, total_deposit, total_withdrawal,
+                    deposit_fee, withdrawal_fee, total_u_deposit, total_u_withdrawal
+                ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
                 [
                     transactionData.groupId,
                     transactionData.groupName,
                     transactionData.type,
                     transactionData.amount,
                     transactionData.remark || null,
-                    operatorId,  // 使用群内操作人的ID
+                    operatorId,
                     feeRate,
-                    exchangeRate
+                    exchangeRate,
+                    totalDeposit,
+                    totalWithdrawal,
+                    transactionData.type === 'deposit' ? currentFee : 0,
+                    transactionData.type === 'withdrawal' ? currentFee : 0,
+                    totalUDeposit,
+                    totalUWithdrawal
                 ]
             );
             return result.insertId;