Procházet zdrojové kódy

会员资金合计:提现只算成功单、充值彩金按净额、排除未解冻奖励和模拟会员

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
doge před 18 hodinami
rodič
revize
66d857ec1b

+ 4 - 0
app/Http/Controllers/admin/User.php

@@ -105,6 +105,10 @@ class User extends Controller
     {
         try {
             $params = request()->validate(['member_id' => ['required', 'string', 'min:1', 'max:64']]);
+            // 和会员列表同一口径:只能查列表里看得到的会员(不含后台模拟会员)
+            if (!\App\Support\MemberAudience::visibleUsers(DB::table('users'))->where('member_id', $params['member_id'])->exists()) {
+                throw new Exception('会员不存在');
+            }
             return $this->success(\App\Services\MemberFundTotalsService::totals($params['member_id']));
         } catch (ValidationException $e) {
             return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());

+ 18 - 14
app/Services/MemberFundTotalsService.php

@@ -9,10 +9,8 @@ final class MemberFundTotalsService
 {
     /** 前台充值(公司入款、USDT)和三方充值 */
     public const DEPOSIT_TYPES = ['充值', '三方充值'];
-    /** 后台加款里算充值的:人工充值(related_id = 0 的是活动赠送,算彩金)和其他 */
+    /** 后台加款里算充值的:其他,以及 related_id 为空的人工充值 */
     public const MANUAL_DEPOSIT_TYPES = ['其他'];
-    /** 会员提现:申请时扣款为负、失败退款为正,相加就是净提现 */
-    public const WITHDRAW_TYPES = ['提现', '三方提现', '人工提现'];
     /** 赠送彩金。转盘、净亏邀请、邀请升级、充值渠道赠送、首充奖励入账时都记为“优惠活动” */
     public const BONUS_TYPES = ['注册赠送', '优惠活动', '神秘彩金', '即充即送', '充值返现', '老用户回归',
         '比比返', '笔笔返', '返水', '回水', '分享佣金领取'];
@@ -21,21 +19,27 @@ final class MemberFundTotalsService
     public static function totals(string $memberId): array
     {
         $in = fn (array $types) => "'" . implode("','", $types) . "'";
-        $deposit = $in(self::DEPOSIT_TYPES);
-        $manual = $in(self::MANUAL_DEPOSIT_TYPES);
-        $withdraw = $in(self::WITHDRAW_TYPES);
+        $deposit = $in(array_merge(self::DEPOSIT_TYPES, self::MANUAL_DEPOSIT_TYPES));
         $bonus = $in(self::BONUS_TYPES);
+        // 充值、彩金按带符号金额相加,冲正、收回的负数流水会抵减。
+        // 人工充值:related_id 为空是后台加款,不为空是系统自动发的奖励。type = 2 且 frozen_status = 1 的是还没解冻的旧奖励,不算已赠送。
         $row = DB::table('balance_logs')->where('member_id', $memberId)
-            ->whereIn('change_type', array_merge(self::DEPOSIT_TYPES, self::MANUAL_DEPOSIT_TYPES, self::WITHDRAW_TYPES, self::BONUS_TYPES, ['人工充值', '人工扣款']))
+            ->whereIn('change_type', array_merge(self::DEPOSIT_TYPES, self::MANUAL_DEPOSIT_TYPES, self::BONUS_TYPES, ['人工充值', '人工扣款']))
             ->selectRaw(<<<SQL
-                COALESCE(SUM(CASE WHEN amount > 0 AND (change_type IN ({$deposit}, {$manual})
-                    OR (change_type = '人工充值' AND (related_id IS NULL OR related_id <> 0))) THEN amount ELSE 0 END), 0) AS total_deposit,
-                COALESCE(SUM(CASE WHEN change_type IN ({$withdraw}) THEN -amount
-                    WHEN change_type = '人工扣款' AND amount < 0 THEN -amount ELSE 0 END), 0) AS total_withdraw,
-                COALESCE(SUM(CASE WHEN amount > 0 AND (change_type IN ({$bonus})
-                    OR (change_type = '人工充值' AND related_id = 0)) THEN amount ELSE 0 END), 0) AS total_bonus
+                COALESCE(SUM(CASE WHEN change_type IN ({$deposit})
+                    OR (change_type = '人工充值' AND related_id IS NULL) THEN amount ELSE 0 END), 0) AS total_deposit,
+                COALESCE(SUM(CASE WHEN change_type = '人工扣款' AND amount < 0 THEN -amount ELSE 0 END), 0) AS manual_debit,
+                COALESCE(SUM(CASE WHEN (change_type IN ({$bonus}) OR (change_type = '人工充值' AND related_id IS NOT NULL))
+                    AND NOT (type = 2 AND COALESCE(frozen_status, 0) = 1) THEN amount ELSE 0 END), 0) AS total_bonus
             SQL)->first();
+        // 提现只算已成功的单子:审核中、处理中、失败退回的都不算
+        $paid = fn (string $table, array $types, int $status) => DB::table('balance_logs as bl')
+            ->join($table . ' as o', 'o.id', '=', 'bl.related_id')->where('bl.member_id', $memberId)
+            ->whereIn('bl.change_type', $types)->where('bl.amount', '<', 0)->where('o.status', $status)
+            ->sum('bl.amount');
+        $withdraw = (float) $row->manual_debit - (float) $paid('withdraws', ['提现'], 1)
+            - (float) $paid('payment_orders', ['三方提现', '人工提现'], PaymentOrderService::STATUS_SUCCESS);
         $money = fn ($value) => number_format(max(0, (float) $value), 2, '.', '');
-        return ['total_deposit' => $money($row->total_deposit), 'total_withdraw' => $money($row->total_withdraw), 'total_bonus' => $money($row->total_bonus)];
+        return ['total_deposit' => $money($row->total_deposit), 'total_withdraw' => $money($withdraw), 'total_bonus' => $money($row->total_bonus)];
     }
 }

+ 23 - 9
tests/Integration/MemberFundTotalsTest.php

@@ -29,7 +29,14 @@ class MemberFundTotalsTest extends TestCase
         $db = new Manager($this->app);
         $db->addConnection(['driver' => 'sqlite', 'database' => ':memory:', 'prefix' => 'bot_']);
         $db->setAsGlobal(); $db->bootEloquent(); $this->app->instance('db', $db->getDatabaseManager());
-        DB::statement('CREATE TABLE bot_balance_logs (id INTEGER PRIMARY KEY,member_id TEXT,type INTEGER,change_type TEXT,amount DECIMAL(30,10),related_id INTEGER,created_at TEXT)');
+        $this->app->bind('db.schema', fn () => $db->schema());
+        DB::statement('CREATE TABLE bot_balance_logs (id INTEGER PRIMARY KEY,member_id TEXT,type INTEGER,change_type TEXT,amount DECIMAL(30,10),related_id INTEGER,frozen_status INTEGER,created_at TEXT)');
+        DB::statement('CREATE TABLE bot_users (id INTEGER PRIMARY KEY,member_id TEXT,user_type INTEGER DEFAULT 0)');
+        DB::statement('CREATE TABLE bot_withdraws (id INTEGER PRIMARY KEY,status INTEGER)');
+        DB::statement('CREATE TABLE bot_payment_orders (id INTEGER PRIMARY KEY,status INTEGER)');
+        DB::table('users')->insert([['member_id' => 'm1', 'user_type' => 0], ['member_id' => 'empty', 'user_type' => 0], ['member_id' => 'sim', 'user_type' => 2]]);
+        DB::table('withdraws')->insert([['id' => 23, 'status' => 1], ['id' => 25, 'status' => 0]]);
+        DB::table('payment_orders')->insert([['id' => 21, 'status' => 3], ['id' => 22, 'status' => 2], ['id' => 24, 'status' => 2], ['id' => 26, 'status' => 5]]);
         Request::macro('validate', function (array $rules) { return app('validator')->make($this->all(), $rules)->validate(); });
     }
 
@@ -47,26 +54,33 @@ class MemberFundTotalsTest extends TestCase
     public function test_totals_group_deposits_withdrawals_and_bonuses_for_one_member(): void
     {
         $rows = [
-            // 总充值:三方、前台提交审核通过、后台人工充值、其他
-            ['三方充值', 100, 11], ['充值', 200, 12], ['人工充值', 50, null], ['其他', 7, null],
-            // 总提现:三方提现扣款 + 失败退款、USDT 提现、人工提现、后台人工扣款
+            // 总充值:三方、前台提交审核通过、后台人工充值、其他;冲正的负数会抵减
+            ['三方充值', 100, 11], ['充值', 200, 12], ['人工充值', 50, null], ['其他', 7, null], ['充值', -20, 12],
+            // 总提现:只算成功的。21 失败已退款、25 和 26 还在审核,都不算
             ['三方提现', -300, 21], ['三方提现', 300, 21], ['三方提现', -80, 22], ['提现', -40, 23], ['人工提现', -10, 24], ['人工扣款', -5, null],
-            // 总赠送彩金:后台三种加款、活动(转盘等入账为优惠活动)、充值送、返水、分享佣金、活动用的人工充值(related_id = 0)
+            ['提现', -70, 25], ['三方提现', -90, 26],
+            // 总赠送彩金:后台三种加款、活动、充值送、返水、分享佣金、自动发放的人工充值(related_id 不为空);收回的负数会抵减
             ['注册赠送', 18, null], ['优惠活动', 6.66, null], ['神秘彩金', 8, null], ['即充即送', 5, null], ['充值返现', 3, null],
             ['笔笔返', 1.5, null], ['回水', 2, null], ['返水', 1, null], ['老用户回归', 4, null], ['分享佣金领取', 9, 31], ['人工充值', 28, 0],
+            ['优惠活动', -1.66, null],
             // 不算进任何一项
             ['订单补偿', 60, null], ['余额宝利息', 2, null], ['加拿大28投注', -30, 41], ['资产转移', -20, null], ['流水解冻', 6, null],
         ];
         foreach ($rows as [$type, $amount, $related]) {
             DB::table('balance_logs')->insert(['member_id' => 'm1', 'type' => 1, 'change_type' => $type, 'amount' => $amount, 'related_id' => $related, 'created_at' => '2026-11-05 12:00:00']);
         }
-        DB::table('balance_logs')->insert(['member_id' => 'm2', 'type' => 1, 'change_type' => '三方充值', 'amount' => 999, 'created_at' => '2026-11-05 12:00:00']);
+        // 还冻结着的旧奖励不算已赠送,解冻过的算
+        DB::table('balance_logs')->insert([
+            ['member_id' => 'm1', 'type' => 2, 'change_type' => '即充即送', 'amount' => 500, 'frozen_status' => 1, 'created_at' => '2026-11-05 12:00:00'],
+            ['member_id' => 'm1', 'type' => 2, 'change_type' => '老用户回归', 'amount' => 10, 'frozen_status' => 2, 'created_at' => '2026-11-05 12:00:00'],
+            ['member_id' => 'sim', 'type' => 1, 'change_type' => '三方充值', 'amount' => 999, 'frozen_status' => null, 'created_at' => '2026-11-05 12:00:00'],
+        ]);
 
         $body = $this->totals(['member_id' => 'm1']);
         $this->assertSame(0, $body['code'], $body['msg']);
-        $this->assertSame(['total_deposit' => '357.00', 'total_withdraw' => '135.00', 'total_bonus' => '86.16'], $body['data']);
+        $this->assertSame(['total_deposit' => '337.00', 'total_withdraw' => '135.00', 'total_bonus' => '94.50'], $body['data']);
 
-        $this->assertSame(['total_deposit' => '0.00', 'total_withdraw' => '0.00', 'total_bonus' => '0.00'], $this->totals(['member_id' => 'nobody'])['data']);
-        $this->assertNotSame(0, $this->totals([])['code']);
+        $this->assertSame(['total_deposit' => '0.00', 'total_withdraw' => '0.00', 'total_bonus' => '0.00'], $this->totals(['member_id' => 'empty'])['data']);
+        foreach ([[], ['member_id' => 'nobody'], ['member_id' => 'sim']] as $input) $this->assertNotSame(0, $this->totals($input)['code'], 'unknown and simulated members are refused');
     }
 }