Bläddra i källkod

总额和下发只显示当日数据

Taio_O 2 veckor sedan
förälder
incheckning
af55656732
2 ändrade filer med 52 tillägg och 27 borttagningar
  1. 36 18
      admin/index.js
  2. 16 9
      admin/models/Transaction.js

+ 36 - 18
admin/index.js

@@ -220,6 +220,16 @@ bot.on('message', async (msg) => {
         }
 
         if (!isNaN(amount)) {
+            // 检查是否是今天的第一笔交易
+            const [todayFirstRecord] = await pool.query(`
+                SELECT COUNT(*) as count
+                FROM transactions
+                WHERE group_id = ?
+                AND DATE(time) = CURDATE()
+            `, [msg.chat.id.toString()]);
+
+            const isFirstRecord = todayFirstRecord[0].count === 0;
+
             const transactionData = {
                 groupId: msg.chat.id.toString(),
                 groupName: msg.chat.title || '未命名群组',
@@ -227,11 +237,10 @@ bot.on('message', async (msg) => {
                 type: 'deposit',
                 exchangeRate: exchangeRate,
                 feeRate: feeRate,
-                operatorId: msg.from.id
+                operatorId: msg.from.id,
+                isFirstRecord: isFirstRecord // 添加标记,表示是否是今天的第一笔交易
             };
 
-            // console.log(transactionData);
-
             try {
                 const result = await Transaction.deposit(transactionData);
                 if (result.success) {
@@ -784,8 +793,8 @@ async function generateBillMessage(chatId) {
             ORDER BY t.time DESC
         `, [chatId.toString()]);
 
-        // 获取最新一条交易的累计数据
-        const [latestRecord] = await pool.query(`
+        // 获取今天的最新一条交易的累计数据
+        const [todayLatestRecord] = 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,
@@ -798,6 +807,7 @@ async function generateBillMessage(chatId) {
             FROM transactions t
             LEFT JOIN groups g ON t.group_id = g.group_id
             WHERE t.group_id = ? 
+            AND DATE(t.time) = CURDATE()
             ORDER BY t.time DESC
             LIMIT 1
         `, [chatId.toString()]);
@@ -824,19 +834,27 @@ async function generateBillMessage(chatId) {
         message += `<b>入款费率</b>:<code>${inFeeRate}%</code>\n`;
         message += `<b>入款汇率</b>:<code>${inExchangeRate}</code>\n`;
         
-        // 使用最新一条交易的累计数据
-        const totalDeposit = parseFloat(latestRecord[0]?.total_deposit) || 0;
-        const totalUDeposit = parseFloat(latestRecord[0]?.total_u_deposit) || 0;
-        const totalUWithdrawal = parseFloat(latestRecord[0]?.total_u_withdrawal) || 0;
-
-        message += `<b>入款总额</b>:<code>${totalDeposit.toFixed(2)}</code>\n`;
-        message += `<b>入款合计</b>:<code>${totalDeposit.toFixed(2)}|${totalUDeposit.toFixed(2)}U</code>\n\n`;
-
-        // 添加余额信息(使用最新一条交易的累计数据)
-        const remainingAmount = totalUDeposit - totalUWithdrawal;
-        message += `<b>应下发</b>:<code>${(remainingAmount < 0 ? 0 : remainingAmount).toFixed(2)}U</code>\n`;
-        message += `<b>已下发</b>:<code>${totalUWithdrawal.toFixed(2)}U</code>\n`;
-        message += `<b>未下发</b>:<code>${(remainingAmount < 0 ? 0 : remainingAmount).toFixed(2)}U</code>`;
+        // 只有在今天有交易记录时才显示总额信息
+        if (todayLatestRecord && todayLatestRecord.length > 0) {
+            const totalDeposit = parseFloat(todayLatestRecord[0].total_deposit) || 0;
+            const totalUDeposit = parseFloat(todayLatestRecord[0].total_u_deposit) || 0;
+            const totalUWithdrawal = parseFloat(todayLatestRecord[0].total_u_withdrawal) || 0;
+
+            message += `<b>入款总额</b>:<code>${totalDeposit.toFixed(2)}</code>\n`;
+            message += `<b>入款合计</b>:<code>${totalDeposit.toFixed(2)}|${totalUDeposit.toFixed(2)}U</code>\n\n`;
+
+            // 添加余额信息
+            const remainingAmount = totalUDeposit - totalUWithdrawal;
+            message += `<b>应下发</b>:<code>${(remainingAmount < 0 ? 0 : remainingAmount).toFixed(2)}U</code>\n`;
+            message += `<b>已下发</b>:<code>${totalUWithdrawal.toFixed(2)}U</code>\n`;
+            message += `<b>未下发</b>:<code>${(remainingAmount < 0 ? 0 : remainingAmount).toFixed(2)}U</code>`;
+        } else {
+            message += `<b>入款总额</b>:<code>0.00</code>\n`;
+            message += `<b>入款合计</b>:<code>0.00|0.00U</code>\n\n`;
+            message += `<b>应下发</b>:<code>0.00U</code>\n`;
+            message += `<b>已下发</b>:<code>0.00U</code>\n`;
+            message += `<b>未下发</b>:<code>0.00U</code>`;
+        }
 
         console.log(`账单消息生成成功 - 群组ID: ${chatId}`);
         return message;

+ 16 - 9
admin/models/Transaction.js

@@ -123,9 +123,9 @@ const Transaction = {
             // 使用群内操作人的ID作为operator_id
             const operatorId = transactionData.operatorId || 1;
 
-            // 获取上一条记录的总金额数据
-            const [lastRecord] = await pool.query(
-                'SELECT totalDeposit, totalWithdrawal, totalUDeposit, totalUWithdrawal FROM transactions WHERE group_id = ? ORDER BY time DESC LIMIT 1',
+            // 检查是否是今天的第一笔交易
+            const [todayFirstRecord] = await pool.query(
+                'SELECT COUNT(*) as count FROM transactions WHERE group_id = ? AND DATE(time) = CURDATE()',
                 [transactionData.groupId]
             );
 
@@ -135,12 +135,19 @@ const Transaction = {
             let totalUDeposit = 0;
             let totalUWithdrawal = 0;
 
-            // 如果有上一条记录,使用其数据
-            if (lastRecord && lastRecord.length > 0) {
-                totalDeposit = parseFloat(lastRecord[0].totalDeposit) || 0;
-                totalWithdrawal = parseFloat(lastRecord[0].totalWithdrawal) || 0;
-                totalUDeposit = parseFloat(lastRecord[0].totalUDeposit) || 0;
-                totalUWithdrawal = parseFloat(lastRecord[0].totalUWithdrawal) || 0;
+            // 如果不是今天的第一笔交易,获取上一条记录的总金额数据
+            if (todayFirstRecord[0].count > 0) {
+                const [lastRecord] = await pool.query(
+                    'SELECT totalDeposit, totalWithdrawal, totalUDeposit, totalUWithdrawal FROM transactions WHERE group_id = ? ORDER BY time DESC LIMIT 1',
+                    [transactionData.groupId]
+                );
+
+                if (lastRecord && lastRecord.length > 0) {
+                    totalDeposit = parseFloat(lastRecord[0].totalDeposit) || 0;
+                    totalWithdrawal = parseFloat(lastRecord[0].totalWithdrawal) || 0;
+                    totalUDeposit = parseFloat(lastRecord[0].totalUDeposit) || 0;
+                    totalUWithdrawal = parseFloat(lastRecord[0].totalUWithdrawal) || 0;
+                }
             }
 
             // 计算本条交易的手续费和实际金额