StatController.php 5.6 KB

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