StatController.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. namespace App\Http\Controllers\V1\Admin;
  3. use App\Http\Controllers\Controller;
  4. use App\Models\CommissionLog;
  5. use App\Models\Order;
  6. use App\Models\ServerShadowsocks;
  7. use App\Models\ServerTrojan;
  8. use App\Models\ServerVmess;
  9. use App\Models\Stat;
  10. use App\Models\StatServer;
  11. use App\Models\StatUser;
  12. use App\Models\Ticket;
  13. use App\Models\User;
  14. use App\Services\StatisticalService;
  15. use Illuminate\Http\Request;
  16. use Illuminate\Support\Facades\Cache;
  17. use Illuminate\Support\Facades\DB;
  18. class StatController extends Controller
  19. {
  20. public function getOverride(Request $request)
  21. {
  22. return [
  23. 'data' => [
  24. 'month_income' => Order::where('created_at', '>=', strtotime(date('Y-m-1')))
  25. ->where('created_at', '<', time())
  26. ->whereNotIn('status', [0, 2])
  27. ->sum('total_amount'),
  28. 'month_register_total' => User::where('created_at', '>=', strtotime(date('Y-m-1')))
  29. ->where('created_at', '<', time())
  30. ->count(),
  31. 'ticket_pending_total' => Ticket::where('status', 0)
  32. ->count(),
  33. 'commission_pending_total' => Order::where('commission_status', 0)
  34. ->where('invite_user_id', '!=', NULL)
  35. ->whereNotIn('status', [0, 2])
  36. ->where('commission_balance', '>', 0)
  37. ->count(),
  38. 'day_income' => Order::where('created_at', '>=', strtotime(date('Y-m-d')))
  39. ->where('created_at', '<', time())
  40. ->whereNotIn('status', [0, 2])
  41. ->sum('total_amount'),
  42. 'last_month_income' => Order::where('created_at', '>=', strtotime('-1 month', strtotime(date('Y-m-1'))))
  43. ->where('created_at', '<', strtotime(date('Y-m-1')))
  44. ->whereNotIn('status', [0, 2])
  45. ->sum('total_amount'),
  46. 'commission_month_payout' => CommissionLog::where('created_at', '>=', strtotime(date('Y-m-1')))
  47. ->where('created_at', '<', time())
  48. ->sum('get_amount'),
  49. 'commission_last_month_payout' => CommissionLog::where('created_at', '>=', strtotime('-1 month', strtotime(date('Y-m-1'))))
  50. ->where('created_at', '<', strtotime(date('Y-m-1')))
  51. ->sum('get_amount'),
  52. ]
  53. ];
  54. }
  55. public function getOrder(Request $request)
  56. {
  57. $statistics = Stat::where('record_type', 'd')
  58. ->limit(31)
  59. ->orderBy('record_at', 'DESC')
  60. ->get()
  61. ->toArray();
  62. $result = [];
  63. foreach ($statistics as $statistic) {
  64. $date = date('m-d', $statistic['record_at']);
  65. $result[] = [
  66. 'type' => '收款金额',
  67. 'date' => $date,
  68. 'value' => $statistic['paid_total'] / 100
  69. ];
  70. $result[] = [
  71. 'type' => '收款笔数',
  72. 'date' => $date,
  73. 'value' => $statistic['paid_count']
  74. ];
  75. $result[] = [
  76. 'type' => '佣金金额(已发放)',
  77. 'date' => $date,
  78. 'value' => $statistic['commission_total'] / 100
  79. ];
  80. $result[] = [
  81. 'type' => '佣金笔数(已发放)',
  82. 'date' => $date,
  83. 'value' => $statistic['commission_count']
  84. ];
  85. }
  86. $result = array_reverse($result);
  87. return [
  88. 'data' => $result
  89. ];
  90. }
  91. public function getServerLastRank()
  92. {
  93. $servers = [
  94. 'shadowsocks' => ServerShadowsocks::where('parent_id', null)->get()->toArray(),
  95. 'v2ray' => ServerVmess::where('parent_id', null)->get()->toArray(),
  96. 'trojan' => ServerTrojan::where('parent_id', null)->get()->toArray(),
  97. 'vmess' => ServerVmess::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 [
  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. }