StatController.php 5.6 KB

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