StatController.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. <?php
  2. namespace App\Http\Controllers\V1\Admin;
  3. use App\Http\Controllers\Controller;
  4. use App\Models\CommissionLog;
  5. use App\Models\Order;
  6. use App\Models\ServerShadowsocks;
  7. use App\Models\ServerTrojan;
  8. use App\Models\ServerVmess;
  9. use App\Models\Stat;
  10. use App\Models\StatServer;
  11. use App\Models\StatUser;
  12. use App\Models\Ticket;
  13. use App\Models\User;
  14. use App\Services\StatisticalService;
  15. use Illuminate\Http\Request;
  16. use Illuminate\Support\Facades\Cache;
  17. use Illuminate\Support\Facades\DB;
  18. class StatController extends Controller
  19. {
  20. public function getStat(Request $request)
  21. {
  22. $params = $request->validate([
  23. 'start_at' => '',
  24. 'end_at' => ''
  25. ]);
  26. if (isset($params['start_at']) && isset($params['end_at'])) {
  27. $stats = Stat::where('record_at', '>=', $params['start_at'])
  28. ->where('record_at', '<', $params['end_at'])
  29. ->get()
  30. ->makeHidden(['record_at', 'created_at', 'updated_at', 'id', 'record_type'])
  31. ->toArray();
  32. } else {
  33. $statisticalService = new StatisticalService();
  34. return [
  35. 'data' => $statisticalService->generateStatData()
  36. ];
  37. }
  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:paid_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 getRanking(Request $request)
  67. {
  68. $request->validate([
  69. 'type' => 'required|in:server_traffic_rank,user_consumption_rank,invite_rank',
  70. 'start_at' => '',
  71. 'end_at' => '',
  72. 'limit' => 'nullable|integer'
  73. ]);
  74. $statisticalService = new StatisticalService();
  75. $statisticalService->setStartAt($request->input('start_at'));
  76. $statisticalService->setEndAt($request->input('end_at'));
  77. return [
  78. 'data' => $statisticalService->getRanking($request->input('type'), $request->input('limit') ?? 20)
  79. ];
  80. }
  81. public function getOverride(Request $request)
  82. {
  83. return [
  84. 'data' => [
  85. 'month_income' => Order::where('created_at', '>=', strtotime(date('Y-m-1')))
  86. ->where('created_at', '<', time())
  87. ->whereNotIn('status', [0, 2])
  88. ->sum('total_amount'),
  89. 'month_register_total' => User::where('created_at', '>=', strtotime(date('Y-m-1')))
  90. ->where('created_at', '<', time())
  91. ->count(),
  92. 'ticket_pending_total' => Ticket::where('status', 0)
  93. ->count(),
  94. 'commission_pending_total' => Order::where('commission_status', 0)
  95. ->where('invite_user_id', '!=', NULL)
  96. ->whereNotIn('status', [0, 2])
  97. ->where('commission_balance', '>', 0)
  98. ->count(),
  99. 'day_income' => Order::where('created_at', '>=', strtotime(date('Y-m-d')))
  100. ->where('created_at', '<', time())
  101. ->whereNotIn('status', [0, 2])
  102. ->sum('total_amount'),
  103. 'last_month_income' => Order::where('created_at', '>=', strtotime('-1 month', strtotime(date('Y-m-1'))))
  104. ->where('created_at', '<', strtotime(date('Y-m-1')))
  105. ->whereNotIn('status', [0, 2])
  106. ->sum('total_amount'),
  107. 'commission_month_payout' => CommissionLog::where('created_at', '>=', strtotime(date('Y-m-1')))
  108. ->where('created_at', '<', time())
  109. ->sum('get_amount'),
  110. 'commission_last_month_payout' => CommissionLog::where('created_at', '>=', strtotime('-1 month', strtotime(date('Y-m-1'))))
  111. ->where('created_at', '<', strtotime(date('Y-m-1')))
  112. ->sum('get_amount'),
  113. ]
  114. ];
  115. }
  116. public function getOrder(Request $request)
  117. {
  118. $statistics = Stat::where('record_type', 'd')
  119. ->limit(31)
  120. ->orderBy('record_at', 'DESC')
  121. ->get()
  122. ->toArray();
  123. $result = [];
  124. foreach ($statistics as $statistic) {
  125. $date = date('m-d', $statistic['record_at']);
  126. $result[] = [
  127. 'type' => '收款金额',
  128. 'date' => $date,
  129. 'value' => $statistic['paid_total'] / 100
  130. ];
  131. $result[] = [
  132. 'type' => '收款笔数',
  133. 'date' => $date,
  134. 'value' => $statistic['paid_count']
  135. ];
  136. $result[] = [
  137. 'type' => '佣金金额(已发放)',
  138. 'date' => $date,
  139. 'value' => $statistic['commission_total'] / 100
  140. ];
  141. $result[] = [
  142. 'type' => '佣金笔数(已发放)',
  143. 'date' => $date,
  144. 'value' => $statistic['commission_count']
  145. ];
  146. }
  147. $result = array_reverse($result);
  148. return [
  149. 'data' => $result
  150. ];
  151. }
  152. public function getServerLastRank()
  153. {
  154. $servers = [
  155. 'shadowsocks' => ServerShadowsocks::where('parent_id', null)->get()->toArray(),
  156. 'v2ray' => ServerVmess::where('parent_id', null)->get()->toArray(),
  157. 'trojan' => ServerTrojan::where('parent_id', null)->get()->toArray(),
  158. 'vmess' => ServerVmess::where('parent_id', null)->get()->toArray()
  159. ];
  160. $startAt = strtotime('-1 day', strtotime(date('Y-m-d')));
  161. $endAt = strtotime(date('Y-m-d'));
  162. $statistics = StatServer::select([
  163. 'server_id',
  164. 'server_type',
  165. 'u',
  166. 'd',
  167. DB::raw('(u+d) as total')
  168. ])
  169. ->where('record_at', '>=', $startAt)
  170. ->where('record_at', '<', $endAt)
  171. ->where('record_type', 'd')
  172. ->limit(10)
  173. ->orderBy('total', 'DESC')
  174. ->get()
  175. ->toArray();
  176. foreach ($statistics as $k => $v) {
  177. foreach ($servers[$v['server_type']] as $server) {
  178. if ($server['id'] === $v['server_id']) {
  179. $statistics[$k]['server_name'] = $server['name'];
  180. }
  181. }
  182. $statistics[$k]['total'] = $statistics[$k]['total'] / 1073741824;
  183. }
  184. array_multisort(array_column($statistics, 'total'), SORT_DESC, $statistics);
  185. return [
  186. 'data' => $statistics
  187. ];
  188. }
  189. public function getStatUser(Request $request)
  190. {
  191. $request->validate([
  192. 'user_id' => 'required|integer'
  193. ]);
  194. $current = $request->input('current') ? $request->input('current') : 1;
  195. $pageSize = $request->input('pageSize') >= 10 ? $request->input('pageSize') : 10;
  196. $builder = StatUser::orderBy('record_at', 'DESC')->where('user_id', $request->input('user_id'));
  197. $total = $builder->count();
  198. $records = $builder->forPage($current, $pageSize)
  199. ->get();
  200. return [
  201. 'data' => $records,
  202. 'total' => $total
  203. ];
  204. }
  205. }