StatController.php 7.9 KB

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