StatController.php 7.2 KB

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