Browse Source

会员查看弹窗增加总充值、总提现、总赠送彩金

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
doge 18 hours ago
parent
commit
a070e5cae6

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

@@ -100,6 +100,19 @@ class User extends Controller
         return $this->success();
     }
 
+    /** 会员“查看”弹窗:历史总充值、总提现、总赠送彩金 */
+    public function fundTotals(): JsonResponse
+    {
+        try {
+            $params = request()->validate(['member_id' => ['required', 'string', 'min:1', 'max:64']]);
+            return $this->success(\App\Services\MemberFundTotalsService::totals($params['member_id']));
+        } catch (ValidationException $e) {
+            return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
+        } catch (Exception $e) {
+            return $this->error(HttpStatus::CUSTOM_ERROR, $e->getMessage());
+        }
+    }
+
     public function index(): JsonResponse
     {
         try {

+ 41 - 0
app/Services/MemberFundTotalsService.php

@@ -0,0 +1,41 @@
+<?php
+
+namespace App\Services;
+
+use Illuminate\Support\Facades\DB;
+
+/** 会员列表“查看”弹窗里的三项历史合计:总充值、总提现、总赠送彩金。全部来自 balance_logs。 */
+final class MemberFundTotalsService
+{
+    /** 前台充值(公司入款、USDT)和三方充值 */
+    public const DEPOSIT_TYPES = ['充值', '三方充值'];
+    /** 后台加款里算充值的:人工充值(related_id = 0 的是活动赠送,算彩金)和其他 */
+    public const MANUAL_DEPOSIT_TYPES = ['其他'];
+    /** 会员提现:申请时扣款为负、失败退款为正,相加就是净提现 */
+    public const WITHDRAW_TYPES = ['提现', '三方提现', '人工提现'];
+    /** 赠送彩金。转盘、净亏邀请、邀请升级、充值渠道赠送、首充奖励入账时都记为“优惠活动” */
+    public const BONUS_TYPES = ['注册赠送', '优惠活动', '神秘彩金', '即充即送', '充值返现', '老用户回归',
+        '比比返', '笔笔返', '返水', '回水', '分享佣金领取'];
+
+    /** @return array{total_deposit:string,total_withdraw:string,total_bonus:string} */
+    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);
+        $bonus = $in(self::BONUS_TYPES);
+        $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, ['人工充值', '人工扣款']))
+            ->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
+            SQL)->first();
+        $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)];
+    }
+}

+ 1 - 0
routes/admin.php

@@ -521,6 +521,7 @@ Route::middleware(['admin.jwt'])->group(function () {
 
         Route::prefix('/user')->group(function () {
             Route::get('/', [User::class, 'index']);
+            Route::get('/fundTotals', [User::class, 'fundTotals']);
             Route::get('/address', [User::class, 'address']);
             Route::post('/merge', [User::class, 'merge']);
             Route::post('/setNote', [User::class, 'setNote']);

+ 72 - 0
tests/Integration/MemberFundTotalsTest.php

@@ -0,0 +1,72 @@
+<?php
+
+namespace Tests\Integration;
+
+use App\Http\Controllers\admin\User;
+use Illuminate\Config\Repository;
+use Illuminate\Database\Capsule\Manager;
+use Illuminate\Foundation\Application;
+use Illuminate\Http\Request;
+use Illuminate\Support\Facades\DB;
+use Illuminate\Support\Facades\Facade;
+use PHPUnit\Framework\TestCase;
+
+class MemberFundTotalsTest extends TestCase
+{
+    private Application $app;
+
+    protected function setUp(): void
+    {
+        Facade::clearResolvedInstances();
+        $this->app = new Application(dirname(__DIR__, 2));
+        $this->app->instance('config', new Repository(['app' => ['locale' => 'zh']]));
+        $this->app->instance('log', new \Psr\Log\NullLogger());
+        $translator = new \Illuminate\Translation\Translator(new \Illuminate\Translation\ArrayLoader(), 'zh');
+        $this->app->instance('translator', $translator);
+        $this->app->instance('validator', new \Illuminate\Validation\Factory($translator, $this->app));
+        $this->app->instance(\Illuminate\Contracts\Routing\ResponseFactory::class, new \Tests\Support\JsonResponseFactory());
+        Facade::setFacadeApplication($this->app);
+        $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)');
+        Request::macro('validate', function (array $rules) { return app('validator')->make($this->all(), $rules)->validate(); });
+    }
+
+    protected function tearDown(): void
+    {
+        DB::disconnect(); Facade::clearResolvedInstances(); parent::tearDown();
+    }
+
+    private function totals(array $input): array
+    {
+        $this->app->instance('request', Request::create('/admin/user/fundTotals', 'GET', $input));
+        return (new User())->fundTotals()->getData(true);
+    }
+
+    public function test_totals_group_deposits_withdrawals_and_bonuses_for_one_member(): void
+    {
+        $rows = [
+            // 总充值:三方、前台提交审核通过、后台人工充值、其他
+            ['三方充值', 100, 11], ['充值', 200, 12], ['人工充值', 50, null], ['其他', 7, null],
+            // 总提现:三方提现扣款 + 失败退款、USDT 提现、人工提现、后台人工扣款
+            ['三方提现', -300, 21], ['三方提现', 300, 21], ['三方提现', -80, 22], ['提现', -40, 23], ['人工提现', -10, 24], ['人工扣款', -5, null],
+            // 总赠送彩金:后台三种加款、活动(转盘等入账为优惠活动)、充值送、返水、分享佣金、活动用的人工充值(related_id = 0)
+            ['注册赠送', 18, null], ['优惠活动', 6.66, null], ['神秘彩金', 8, null], ['即充即送', 5, null], ['充值返现', 3, null],
+            ['笔笔返', 1.5, null], ['回水', 2, null], ['返水', 1, null], ['老用户回归', 4, null], ['分享佣金领取', 9, 31], ['人工充值', 28, 0],
+            // 不算进任何一项
+            ['订单补偿', 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']);
+
+        $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' => '0.00', 'total_withdraw' => '0.00', 'total_bonus' => '0.00'], $this->totals(['member_id' => 'nobody'])['data']);
+        $this->assertNotSame(0, $this->totals([])['code']);
+    }
+}