StatController.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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 App\Services\StatisticalService;
  9. use Illuminate\Http\Request;
  10. use App\Http\Controllers\Controller;
  11. use App\Models\ServerGroup;
  12. use App\Models\ServerVmess;
  13. use App\Models\Plan;
  14. use App\Models\User;
  15. use App\Models\Ticket;
  16. use App\Models\Order;
  17. use App\Models\Stat;
  18. use App\Models\StatServer;
  19. use Illuminate\Support\Facades\Cache;
  20. use Illuminate\Support\Facades\DB;
  21. class StatController extends Controller
  22. {
  23. public function getStat(Request $request)
  24. {
  25. $params = $request->validate([
  26. 'start_at' => '',
  27. 'end_at' => ''
  28. ]);
  29. if (isset($params['start_at']) && isset($params['end_at'])) {
  30. $stats = Stat::where('record_at', '>=', $params['start_at'])
  31. ->where('record_at', '<', $params['end_at'])
  32. ->get()
  33. ->makeHidden(['record_at', 'created_at', 'updated_at', 'id', 'record_type'])
  34. ->toArray();
  35. }
  36. $statisticalService = new StatisticalService();
  37. $stats = array_merge($stats ?? [], [$statisticalService->generateStatData()]);
  38. $stats = array_reduce($stats, function($carry, $item) {
  39. foreach($item as $key => $value) {
  40. if(isset($carry[$key]) && $carry[$key]) {
  41. $carry[$key] += $value;
  42. } else {
  43. $carry[$key] = $value;
  44. }
  45. }
  46. return $carry;
  47. }, []);
  48. return [
  49. 'data' => $stats
  50. ];
  51. }
  52. public function getOverride(Request $request)
  53. {
  54. return [
  55. 'data' => [
  56. 'month_income' => Order::where('created_at', '>=', strtotime(date('Y-m-1')))
  57. ->where('created_at', '<', time())
  58. ->whereNotIn('status', [0, 2])
  59. ->sum('total_amount'),
  60. 'month_register_total' => User::where('created_at', '>=', strtotime(date('Y-m-1')))
  61. ->where('created_at', '<', time())
  62. ->count(),
  63. 'ticket_pending_total' => Ticket::where('status', 0)
  64. ->count(),
  65. 'commission_pending_total' => Order::where('commission_status', 0)
  66. ->where('invite_user_id', '!=', NULL)
  67. ->whereNotIn('status', [0, 2])
  68. ->where('commission_balance', '>', 0)
  69. ->count(),
  70. 'day_income' => Order::where('created_at', '>=', strtotime(date('Y-m-d')))
  71. ->where('created_at', '<', time())
  72. ->whereNotIn('status', [0, 2])
  73. ->sum('total_amount'),
  74. 'last_month_income' => Order::where('created_at', '>=', strtotime('-1 month', strtotime(date('Y-m-1'))))
  75. ->where('created_at', '<', strtotime(date('Y-m-1')))
  76. ->whereNotIn('status', [0, 2])
  77. ->sum('total_amount'),
  78. 'commission_month_payout' => CommissionLog::where('created_at', '>=', strtotime(date('Y-m-1')))
  79. ->where('created_at', '<', time())
  80. ->sum('get_amount'),
  81. 'commission_last_month_payout' => CommissionLog::where('created_at', '>=', strtotime('-1 month', strtotime(date('Y-m-1'))))
  82. ->where('created_at', '<', strtotime(date('Y-m-1')))
  83. ->sum('get_amount'),
  84. ]
  85. ];
  86. }
  87. public function getOrder(Request $request)
  88. {
  89. $statistics = Stat::where('record_type', 'd')
  90. ->limit(31)
  91. ->orderBy('record_at', 'DESC')
  92. ->get()
  93. ->toArray();
  94. $result = [];
  95. foreach ($statistics as $statistic) {
  96. $date = date('m-d', $statistic['record_at']);
  97. $result[] = [
  98. 'type' => '收款金额',
  99. 'date' => $date,
  100. 'value' => $statistic['paid_total'] / 100
  101. ];
  102. $result[] = [
  103. 'type' => '收款笔数',
  104. 'date' => $date,
  105. 'value' => $statistic['paid_count']
  106. ];
  107. $result[] = [
  108. 'type' => '佣金金额(已发放)',
  109. 'date' => $date,
  110. 'value' => $statistic['commission_total'] / 100
  111. ];
  112. $result[] = [
  113. 'type' => '佣金笔数(已发放)',
  114. 'date' => $date,
  115. 'value' => $statistic['commission_count']
  116. ];
  117. }
  118. $result = array_reverse($result);
  119. return [
  120. 'data' => $result
  121. ];
  122. }
  123. public function getServerLastRank()
  124. {
  125. $servers = [
  126. 'shadowsocks' => ServerShadowsocks::where('parent_id', null)->get()->toArray(),
  127. 'v2ray' => ServerVmess::where('parent_id', null)->get()->toArray(),
  128. 'trojan' => ServerTrojan::where('parent_id', null)->get()->toArray(),
  129. 'vmess' => ServerVmess::where('parent_id', null)->get()->toArray()
  130. ];
  131. $startAt = strtotime('-1 day', strtotime(date('Y-m-d')));
  132. $endAt = strtotime(date('Y-m-d'));
  133. $statistics = StatServer::select([
  134. 'server_id',
  135. 'server_type',
  136. 'u',
  137. 'd',
  138. DB::raw('(u+d) as total')
  139. ])
  140. ->where('record_at', '>=', $startAt)
  141. ->where('record_at', '<', $endAt)
  142. ->where('record_type', 'd')
  143. ->limit(10)
  144. ->orderBy('total', 'DESC')
  145. ->get()
  146. ->toArray();
  147. foreach ($statistics as $k => $v) {
  148. foreach ($servers[$v['server_type']] as $server) {
  149. if ($server['id'] === $v['server_id']) {
  150. $statistics[$k]['server_name'] = $server['name'];
  151. }
  152. }
  153. $statistics[$k]['total'] = $statistics[$k]['total'] / 1073741824;
  154. }
  155. array_multisort(array_column($statistics, 'total'), SORT_DESC, $statistics);
  156. return [
  157. 'data' => $statistics
  158. ];
  159. }
  160. public function getStatUser(Request $request)
  161. {
  162. $request->validate([
  163. 'user_id' => 'required|integer'
  164. ]);
  165. $current = $request->input('current') ? $request->input('current') : 1;
  166. $pageSize = $request->input('pageSize') >= 10 ? $request->input('pageSize') : 10;
  167. $builder = StatUser::orderBy('record_at', 'DESC')->where('user_id', $request->input('user_id'));
  168. $total = $builder->count();
  169. $records = $builder->forPage($current, $pageSize)
  170. ->get();
  171. return [
  172. 'data' => $records,
  173. 'total' => $total
  174. ];
  175. }
  176. }