StatController.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. ]
  48. ]);
  49. }
  50. public function getOrder(Request $request)
  51. {
  52. $statistics = StatOrder::where('record_type', 'd')
  53. ->limit(31)
  54. ->orderBy('record_at', 'DESC')
  55. ->get()
  56. ->toArray();
  57. $result = [];
  58. foreach ($statistics as $statistic) {
  59. $date = date('m-d', $statistic['record_at']);
  60. array_push($result, [
  61. 'type' => '收款金额',
  62. 'date' => $date,
  63. 'value' => $statistic['order_amount'] / 100
  64. ]);
  65. array_push($result, [
  66. 'type' => '收款笔数',
  67. 'date' => $date,
  68. 'value' => $statistic['order_count']
  69. ]);
  70. array_push($result, [
  71. 'type' => '佣金金额(已发放)',
  72. 'date' => $date,
  73. 'value' => $statistic['commission_amount'] / 100
  74. ]);
  75. array_push($result, [
  76. 'type' => '佣金笔数(已发放)',
  77. 'date' => $date,
  78. 'value' => $statistic['commission_count']
  79. ]);
  80. }
  81. $result = array_reverse($result);
  82. return response([
  83. 'data' => $result
  84. ]);
  85. }
  86. public function getServerLastRank()
  87. {
  88. $servers = [
  89. 'shadowsocks' => ServerShadowsocks::where('parent_id', null)->get()->toArray(),
  90. 'vmess' => ServerV2ray::where('parent_id', null)->get()->toArray(),
  91. 'trojan' => ServerTrojan::where('parent_id', null)->get()->toArray()
  92. ];
  93. $startAt = strtotime('-1 day', strtotime(date('Y-m-d')));
  94. $endAt = strtotime(date('Y-m-d'));
  95. $statistics = StatServer::select([
  96. 'server_id',
  97. 'server_type',
  98. 'u',
  99. 'd',
  100. DB::raw('(u+d) as total')
  101. ])
  102. ->where('record_at', '>=', $startAt)
  103. ->where('record_at', '<', $endAt)
  104. ->where('record_type', 'd')
  105. ->limit(10)
  106. ->orderBy('total', 'DESC')
  107. ->get()
  108. ->toArray();
  109. foreach ($statistics as $k => $v) {
  110. foreach ($servers[$v['server_type']] as $server) {
  111. if ($server['id'] === $v['server_id']) {
  112. $statistics[$k]['server_name'] = $server['name'];
  113. }
  114. }
  115. $statistics[$k]['total'] = $statistics[$k]['total'] / 1073741824;
  116. }
  117. array_multisort(array_column($statistics, 'total'), SORT_DESC, $statistics);
  118. return response([
  119. 'data' => $statistics
  120. ]);
  121. }
  122. public function getStatUser(Request $request)
  123. {
  124. $request->validate([
  125. 'user_id' => 'required|integer'
  126. ]);
  127. $current = $request->input('current') ? $request->input('current') : 1;
  128. $pageSize = $request->input('pageSize') >= 10 ? $request->input('pageSize') : 10;
  129. $builder = StatUser::orderBy('record_at', 'DESC')->where('user_id', $request->input('user_id'));
  130. $total = $builder->count();
  131. $records = $builder->forPage($current, $pageSize)
  132. ->get();
  133. return [
  134. 'data' => $records,
  135. 'total' => $total
  136. ];
  137. }
  138. }