StatController.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. namespace App\Http\Controllers\Admin;
  3. use App\Models\ServerShadowsocks;
  4. use App\Models\ServerTrojan;
  5. use App\Models\StatUser;
  6. use App\Services\ServerService;
  7. use Illuminate\Http\Request;
  8. use App\Http\Controllers\Controller;
  9. use App\Models\ServerGroup;
  10. use App\Models\ServerV2ray;
  11. use App\Models\Plan;
  12. use App\Models\User;
  13. use App\Models\Ticket;
  14. use App\Models\Order;
  15. use App\Models\StatOrder;
  16. use App\Models\StatServer;
  17. use Illuminate\Support\Facades\Cache;
  18. use Illuminate\Support\Facades\DB;
  19. class StatController extends Controller
  20. {
  21. public function getOverride(Request $request)
  22. {
  23. return response([
  24. 'data' => [
  25. 'month_income' => Order::where('created_at', '>=', strtotime(date('Y-m-1')))
  26. ->where('created_at', '<', time())
  27. ->whereNotIn('status', [0, 2])
  28. ->sum('total_amount'),
  29. 'month_register_total' => User::where('created_at', '>=', strtotime(date('Y-m-1')))
  30. ->where('created_at', '<', time())
  31. ->count(),
  32. 'ticket_pending_total' => Ticket::where('status', 0)
  33. ->count(),
  34. 'commission_pending_total' => Order::where('commission_status', 0)
  35. ->where('invite_user_id', '!=', NULL)
  36. ->whereNotIn('status', [0, 2])
  37. ->where('commission_balance', '>', 0)
  38. ->count(),
  39. 'day_income' => Order::where('created_at', '>=', strtotime(date('Y-m-d')))
  40. ->where('created_at', '<', time())
  41. ->whereNotIn('status', [0, 2])
  42. ->sum('total_amount'),
  43. 'last_month_income' => Order::where('created_at', '>=', strtotime('-1 month', strtotime(date('Y-m-1'))))
  44. ->where('created_at', '<', strtotime(date('Y-m-1')))
  45. ->whereNotIn('status', [0, 2])
  46. ->sum('total_amount'),
  47. 'commission_month_payout' => Order::where('actual_commission_balance' ,'!=', NULL)
  48. ->where('created_at', '>=', strtotime(date('Y-m-1')))
  49. ->where('created_at', '<', time())
  50. ->sum('actual_commission_balance'),
  51. 'commission_last_month_payout' => Order::where('actual_commission_balance' ,'!=', NULL)
  52. ->where('created_at', '>=', strtotime('-1 month', strtotime(date('Y-m-1'))))
  53. ->where('created_at', '<', strtotime(date('Y-m-1')))
  54. ->sum('actual_commission_balance'),
  55. ]
  56. ]);
  57. }
  58. public function getOrder(Request $request)
  59. {
  60. $statistics = StatOrder::where('record_type', 'd')
  61. ->limit(31)
  62. ->orderBy('record_at', 'DESC')
  63. ->get()
  64. ->toArray();
  65. $result = [];
  66. foreach ($statistics as $statistic) {
  67. $date = date('m-d', $statistic['record_at']);
  68. array_push($result, [
  69. 'type' => '收款金额',
  70. 'date' => $date,
  71. 'value' => $statistic['order_amount'] / 100
  72. ]);
  73. array_push($result, [
  74. 'type' => '收款笔数',
  75. 'date' => $date,
  76. 'value' => $statistic['order_count']
  77. ]);
  78. array_push($result, [
  79. 'type' => '佣金金额(已发放)',
  80. 'date' => $date,
  81. 'value' => $statistic['commission_amount'] / 100
  82. ]);
  83. array_push($result, [
  84. 'type' => '佣金笔数(已发放)',
  85. 'date' => $date,
  86. 'value' => $statistic['commission_count']
  87. ]);
  88. }
  89. $result = array_reverse($result);
  90. return response([
  91. 'data' => $result
  92. ]);
  93. }
  94. public function getServerLastRank()
  95. {
  96. $servers = [
  97. 'shadowsocks' => ServerShadowsocks::where('parent_id', null)->get()->toArray(),
  98. 'vmess' => ServerV2ray::where('parent_id', null)->get()->toArray(),
  99. 'trojan' => ServerTrojan::where('parent_id', null)->get()->toArray()
  100. ];
  101. $startAt = strtotime('-1 day', strtotime(date('Y-m-d')));
  102. $endAt = strtotime(date('Y-m-d'));
  103. $statistics = StatServer::select([
  104. 'server_id',
  105. 'server_type',
  106. 'u',
  107. 'd',
  108. DB::raw('(u+d) as total')
  109. ])
  110. ->where('record_at', '>=', $startAt)
  111. ->where('record_at', '<', $endAt)
  112. ->where('record_type', 'd')
  113. ->limit(10)
  114. ->orderBy('total', 'DESC')
  115. ->get()
  116. ->toArray();
  117. foreach ($statistics as $k => $v) {
  118. foreach ($servers[$v['server_type']] as $server) {
  119. if ($server['id'] === $v['server_id']) {
  120. $statistics[$k]['server_name'] = $server['name'];
  121. }
  122. }
  123. $statistics[$k]['total'] = $statistics[$k]['total'] / 1073741824;
  124. }
  125. array_multisort(array_column($statistics, 'total'), SORT_DESC, $statistics);
  126. return response([
  127. 'data' => $statistics
  128. ]);
  129. }
  130. public function getStatUser(Request $request)
  131. {
  132. $request->validate([
  133. 'user_id' => 'required|integer'
  134. ]);
  135. $current = $request->input('current') ? $request->input('current') : 1;
  136. $pageSize = $request->input('pageSize') >= 10 ? $request->input('pageSize') : 10;
  137. $builder = StatUser::orderBy('record_at', 'DESC')->where('user_id', $request->input('user_id'));
  138. $total = $builder->count();
  139. $records = $builder->forPage($current, $pageSize)
  140. ->get();
  141. return [
  142. 'data' => $records,
  143. 'total' => $total
  144. ];
  145. }
  146. }