doge 2 цаг өмнө
parent
commit
c2c50682b3

+ 13 - 16
app/Http/Controllers/admin/Balance.php

@@ -28,32 +28,29 @@ class Balance extends Controller
                 'end_time' => ['nullable', 'date', 'date_format:Y-m-d', 'required_with:start_time'],
                 'first_name' => ['nullable'],
                 'is_fancai' => ['nullable', 'integer','in:0,1'],
+                // 用户类型/注册来源:-1 tg用户,0 后台客服账号,1 真实用户,2 虚拟用户
+                'from' => ['nullable'],
             ]);
             $page = request()->input('page', 1);
             $limit = request()->input('limit', 10);
             $query = BalanceLog::join('users', 'users.member_id', '=', 'balance_logs.member_id')
                         ->where(BalanceLogService::getWhere($params));
             if (!empty($params['is_fancai'])) {
-                
                 $data['change_types'] = ['即充即送','老用户回归','余额宝利息'];
-                $query = $query->whereIn('balance_logs.change_type',$data['change_types'])->where('type',2);
-                
+                $query = $query->whereIn('balance_logs.change_type', $data['change_types'])
+                    ->where('balance_logs.type', 2);
             } else {
                 $data['change_types'] = BalanceLogService::$RW;
             }
-            $data['total'] = $query->count();
-            $totalAmount = '--';
-            //如果指定用户和类型,则统计变动金额
-            if (isset($params['member_id']) && isset($params['change_type'])) {
-                $totalAmount = $query->sum('amount');
-                $totalAmount = bcadd($totalAmount, 0, 2);
-            }
-            $data['total_amount'] =$totalAmount;
-            $data['data'] = $query->select("balance_logs.*", "users.first_name","users.admin_note as user_admin_note")->orderByDesc('id')
-                ->forPage($page, $limit)->with(['member'])
-                ->get()->toArray();
-
-            
+            // 统计与列表共用筛选条件;clone 避免 count/sum 影响后续分页查询
+            $data['total'] = (clone $query)->count();
+            $data['total_amount'] = bcadd((clone $query)->sum('balance_logs.amount'), 0, 2);
+            $data['data'] = $query->select("balance_logs.*", "users.first_name", "users.admin_note as user_admin_note", "users.from as user_from")
+                ->orderByDesc('balance_logs.id')
+                ->forPage($page, $limit)
+                ->with(['member'])
+                ->get()
+                ->toArray();
         } catch (ValidationException $e) {
             return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
         } catch (Exception $e) {

+ 5 - 0
app/Services/BalanceLogService.php

@@ -100,6 +100,11 @@ class BalanceLogService extends BaseService
         if (isset($search['first_name']) && !empty($search['first_name'])) {
             $where[] = ['users.first_name', 'like', "%" . $search['first_name'] . "%"];
         }
+        // 用户类型/注册来源:-1 tg用户,0 后台客服账号,1 真实用户,2 虚拟用户
+        // 不能用 empty(),否则 from=0(后台客服)会被误判为空
+        if (array_key_exists('from', $search) && $search['from'] !== null && $search['from'] !== '') {
+            $where[] = ['users.from', '=', $search['from']];
+        }
 
         return $where;
     }

+ 3 - 1
app/Services/UserService.php

@@ -75,7 +75,9 @@ class UserService extends BaseService
         if (isset($search['level']) && !empty($search['level'])) {
             $where[] = ['users.level', '=', $search['level']];
         }
-        if (isset($search['from']) && !empty($search['from'])) {
+        // 用户类型/注册来源:-1 tg用户,0 后台客服账号,1 真实用户,2 虚拟用户
+        // 不能用 empty(),否则 from=0(后台客服)会被误判为空
+        if (array_key_exists('from', $search) && $search['from'] !== null && $search['from'] !== '') {
             $where[] = ['users.from', '=', $search['from']];
         }
         if (!empty($search['start_time'])) {