StatController.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. namespace App\Http\Controllers\Admin;
  3. use App\Models\ServerShadowsocks;
  4. use App\Models\ServerTrojan;
  5. use App\Services\ServerService;
  6. use Illuminate\Http\Request;
  7. use App\Http\Controllers\Controller;
  8. use App\Models\ServerGroup;
  9. use App\Models\Server;
  10. use App\Models\Plan;
  11. use App\Models\User;
  12. use App\Models\Ticket;
  13. use App\Models\Order;
  14. use App\Models\StatOrder;
  15. use App\Models\StatServer;
  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 response([
  23. 'data' => [
  24. 'month_income' => Order::where('created_at', '>=', strtotime(date('Y-m-1')))
  25. ->where('created_at', '<', time())
  26. ->whereIn('status', [3, 4])
  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_pendding_total' => Ticket::where('status', 0)
  32. ->count(),
  33. 'commission_pendding_total' => Order::where('commission_status', 0)
  34. ->where('invite_user_id', '!=', NULL)
  35. ->where('status', [3, 4])
  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. ->whereIn('status', [3, 4])
  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. ->whereIn('status', [3, 4])
  45. ->sum('total_amount')
  46. ]
  47. ]);
  48. }
  49. public function getOrder(Request $request)
  50. {
  51. $statistics = StatOrder::where('record_type', 'd')
  52. ->limit(31)
  53. ->orderBy('record_at', 'DESC')
  54. ->get()
  55. ->toArray();
  56. $result = [];
  57. foreach ($statistics as $statistic) {
  58. $date = date('m-d', $statistic['record_at']);
  59. array_push($result, [
  60. 'type' => '收款金额',
  61. 'date' => $date,
  62. 'value' => $statistic['order_amount'] / 100
  63. ]);
  64. array_push($result, [
  65. 'type' => '收款笔数',
  66. 'date' => $date,
  67. 'value' => $statistic['order_count']
  68. ]);
  69. array_push($result, [
  70. 'type' => '佣金金额',
  71. 'date' => $date,
  72. 'value' => $statistic['commission_amount'] / 100
  73. ]);
  74. array_push($result, [
  75. 'type' => '佣金笔数',
  76. 'date' => $date,
  77. 'value' => $statistic['commission_count']
  78. ]);
  79. }
  80. $result = array_reverse($result);
  81. return response([
  82. 'data' => $result
  83. ]);
  84. }
  85. public function getServerLastRank()
  86. {
  87. $servers = [
  88. 'shadowsocks' => ServerShadowsocks::where('parent_id', null)->get()->toArray(),
  89. 'vmess' => Server::where('parent_id', null)->get()->toArray(),
  90. 'trojan' => ServerTrojan::where('parent_id', null)->get()->toArray()
  91. ];
  92. $timestamp = strtotime('-1 day', strtotime(date('Y-m-d')));
  93. $statistics = StatServer::select([
  94. 'server_id',
  95. 'server_type',
  96. 'u',
  97. 'd'
  98. ])
  99. ->where('record_at', '>=', $timestamp)
  100. ->where('record_type', 'd')
  101. ->limit(10)
  102. ->orderBy('record_at', 'DESC')
  103. ->get()
  104. ->toArray();
  105. foreach ($statistics as $k => $v) {
  106. foreach ($servers[$v['server_type']] as $server) {
  107. if ($server['id'] === $v['server_id']) {
  108. $statistics[$k]['server_name'] = $server['name'];
  109. }
  110. }
  111. $statistics[$k]['total'] = ($v['u'] + $v['d']) / 1073741824;
  112. }
  113. array_multisort(array_column($statistics, 'total'), SORT_DESC, $statistics);
  114. return response([
  115. 'data' => $statistics
  116. ]);
  117. }
  118. }