Переглянути джерело

1, 增加路由
2, 修复比率计算

liuzejian 1 рік тому
батько
коміт
ef3af519d5

+ 11 - 11
modules/Statistic/Http/Controllers/ChargeTJController.php

@@ -39,7 +39,7 @@ class ChargeTJController extends CatchController
             })->when($endAt, function ($query, $endAt){
                 return $query->where('day_at', '<=', $endAt);
             })->when($userId, function ($query, $userId){
-                return $query->whereIn('user_id', $userId);
+                return $query->where('user_id', $userId);
             })->when($miniprogramId, function ($query, $miniprogramId) {
                 return $query->where('miniprogram_id', $miniprogramId);
             })->when($puserId, function ($query, $puserId){
@@ -71,8 +71,8 @@ class ChargeTJController extends CatchController
         foreach ($result as $item) {
             $item->common_pay_money_per = $item->common_pay_uv ? bcdiv($item->common_pay_money, $item->common_pay_uv ,2 ) : 0;
             $item->vip_pay_money_per = $item->vip_pay_uv ? bcdiv($item->vip_pay_money, $item->vip_pay_uv ,2 ) : 0;
-            $item->common_pay_rate = $item->common_pay_count ? bcdiv(100 * ($item->common_pay_count + $item->common_unpay_count), $item->common_pay_count ,2 ) . '%' : 0 .'%';
-            $item->vip_pay_rate = $item->vip_pay_count ? bcdiv(100 * ($item->vip_pay_count + $item->vip_unpay_count), $item->vip_pay_count ,2 ) . '%' : 0 .'%';
+            $item->common_pay_rate = $item->common_pay_count ? bcdiv($item->common_pay_count,100 * ($item->common_pay_count + $item->common_unpay_count) ,2 ) . '%' : 0 .'%';
+            $item->vip_pay_rate = $item->vip_pay_count ? bcdiv($item->vip_pay_count ,100 * ($item->vip_pay_count + $item->vip_unpay_count), 2 ) . '%' : 0 .'%';
             $item->username = $users->get($item->user_id)->username ?? '';
             $item->miniprogram_name = $miniprograms->get($item->miniprogram_id)->name ?? '';
         }
@@ -130,8 +130,8 @@ class ChargeTJController extends CatchController
         }
 
         if($info) {
-            $info->company_pay_rate = $info->common_pay_count ? bcdiv(($info->common_pay_count + $info->common_unpay_count) * 100, $info->common_pay_count, 2) . '%' : '0%';
-            $info->vip_pay_rate = $info->vip_pay_count ? bcdiv(($info->vip_pay_count + $info->vip_unpay_count) * 100, $info->vip_pay_count, 2) . '%' : '0%';
+            $info->company_pay_rate = $info->common_pay_count ? bcdiv($info->common_pay_count,($info->common_pay_count + $info->common_unpay_count) * 100,  2) . '%' : '0%';
+            $info->vip_pay_rate = $info->vip_pay_count ? bcdiv($info->vip_pay_count, ($info->vip_pay_count + $info->vip_unpay_count) * 100, 2) . '%' : '0%';
         }
         return $info;
     }
@@ -163,8 +163,8 @@ class ChargeTJController extends CatchController
                 )->first();
         }
         if($info) {
-            $info->company_pay_rate = $info->common_pay_count ? bcdiv(($info->common_pay_count + $info->common_unpay_count) * 100, $info->common_pay_count, 2) . '%' : '0%';
-            $info->vip_pay_rate = $info->vip_pay_count ? bcdiv(($info->vip_pay_count + $info->vip_unpay_count) * 100, $info->vip_pay_count, 2) . '%' : '0%';
+            $info->company_pay_rate = $info->common_pay_count ? bcdiv($info->common_pay_count,($info->common_pay_count + $info->common_unpay_count) * 100,  2) . '%' : '0%';
+            $info->vip_pay_rate = $info->vip_pay_count ? bcdiv($info->vip_pay_count, ($info->vip_pay_count + $info->vip_unpay_count) * 100, 2) . '%' : '0%';
         }
         return $info;
     }
@@ -196,8 +196,8 @@ class ChargeTJController extends CatchController
                 )->first();
         }
         if($info) {
-            $info->company_pay_rate = $info->common_pay_count ? bcdiv(($info->common_pay_count + $info->common_unpay_count) * 100, $info->common_pay_count, 2) . '%' : '0%';
-            $info->vip_pay_rate = $info->vip_pay_count ? bcdiv(($info->vip_pay_count + $info->vip_unpay_count) * 100, $info->vip_pay_count, 2) . '%' : '0%';
+            $info->company_pay_rate = $info->common_pay_count ? bcdiv($info->common_pay_count,($info->common_pay_count + $info->common_unpay_count) * 100,  2) . '%' : '0%';
+            $info->vip_pay_rate = $info->vip_pay_count ? bcdiv($info->vip_pay_count,($info->vip_pay_count + $info->vip_unpay_count) * 100,  2) . '%' : '0%';
         }
         return $info;
     }
@@ -238,8 +238,8 @@ class ChargeTJController extends CatchController
             'vip_unpay_count' => $info->vip_unpay_count ?? 0 + $currentMonthInfo->vip_unpay_count ?? 0,
             'vip_pay_count' => $info->vip_pay_count ?? 0 + $currentMonthInfo->vip_pay_count ?? 0,
         ];
-        $result->company_pay_rate = $result->common_pay_count ? bcdiv(($result->common_pay_count + $result->common_unpay_count) * 100, $result->common_pay_count, 2) . '%' : '0%';
-        $result->vip_pay_rate = $result->vip_pay_count ? bcdiv(($result->vip_pay_count + $result->vip_unpay_count) * 100, $result->vip_pay_count, 2) . '%' : '0%';
+        $result->company_pay_rate = $result->common_pay_count ? bcdiv($result->common_pay_count,($result->common_pay_count + $result->common_unpay_count) * 100,  2) . '%' : '0%';
+        $result->vip_pay_rate = $result->vip_pay_count ? bcdiv($result->vip_pay_count,($result->vip_pay_count + $result->vip_unpay_count) * 100,  2) . '%' : '0%';
 
         return $result;
     }

+ 10 - 0
modules/Statistic/routes/route.php

@@ -1,6 +1,7 @@
 <?php
 
 use Illuminate\Support\Facades\Route;
+use Modules\Statistic\Http\Controllers\ChargeTJController;
 use Modules\Statistic\Http\Controllers\UserStatisticsController;
 
 Route::prefix('statistic')->group(function(){
@@ -9,4 +10,13 @@ Route::prefix('statistic')->group(function(){
     Route::prefix('users')->group(function (){
         Route::any('today',[UserStatisticsController::class,'todayData']);
     });
+    // 充值统计
+    Route::prefix('charge')->group(function() {
+        Route::get('list', [ChargeTJController::class, 'list']);
+        Route::get('todayCharge', [ChargeTJController::class, 'todayCharge']);
+        Route::get('listTotalCharge', [ChargeTJController::class, 'listTotalCharge']);
+        Route::get('currentMonthCharge', [ChargeTJController::class, 'currentMonthCharge']);
+        Route::get('lastMonthCharge', [ChargeTJController::class, 'lastMonthCharge']);
+        Route::get('totalCharge', [ChargeTJController::class, 'totalCharge']);
+    });
 });

+ 50 - 0
tests/Statistic/Http/Controllers/ChargeTJControllerTest.php

@@ -0,0 +1,50 @@
+<?php
+
+namespace Tests\Statistic\Http\Controllers;
+
+use Modules\Statistic\Http\Controllers\ChargeTJController;
+use PHPUnit\Framework\TestCase;
+use Tests\UsedTestCase;
+
+class ChargeTJControllerTest extends UsedTestCase
+{
+
+    public function testList()
+    {
+        $res = $this->withHeaders([
+            'Authorization' => 'Bearer '. $this->token,
+        ])->json('get','http://localhost/api/statistic/charge/list', [
+//            'user_id' => 10,
+            'limit' => 3,
+            'start_at' => '2023-06-03',
+            'end_at' => '2023-06-04',
+        ]);
+
+//        $res->dump();
+        $this->dumpJson($res);
+    }
+    public function testlistTotalCharge()
+    {
+        $res = $this->withHeaders([
+            'Authorization' => 'Bearer '. $this->token,
+        ])->json('get','http://localhost/api/statistic/charge/listTotalCharge', [
+//            'user_id' => 10,
+            'limit' => 3,
+            'start_at' => '2023-06-03',
+            'end_at' => '2023-06-04',
+        ]);
+
+//        $res->dump();
+        $this->dumpJson($res);
+    }
+    public function testtodayCharge()
+    {
+        $res = $this->withHeaders([
+            'Authorization' => 'Bearer '. $this->token,
+        ])->json('get','http://localhost/api/statistic/charge/todayCharge', [
+        ]);
+
+//        $res->dump();
+        $this->dumpJson($res);
+    }
+}

+ 3 - 3
tests/UsedTestCase.php

@@ -15,14 +15,14 @@ abstract class UsedTestCase extends BaseTestCase
         $tokenInfo = $this->post('http://localhost/api/login', [
 //            'email' => 'catch@admin.com',
             'remember' => false,
-//            'email' => 'xiaoli@qq.com',
+            'email' => 'xiaoli@qq.com',
             'password' => 'catchadmin',
-            'email' => 'aa4@test.com',
+//            'email' => 'aa4@test.com',
         ])->json();
         $this->token = $tokenInfo['data']['token'];
     }
 
     public function dumpJson($res) {
-        dump(\json_encode($res->json()));
+        dump(\json_encode($res->json(), JSON_PRETTY_PRINT|JSON_UNESCAPED_UNICODE));
     }
 }