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