From 1b8ec77bccad6287dac8ecb94fd0623e4ecbf067 Mon Sep 17 00:00:00 2001 From: v2board Date: Wed, 24 May 2023 11:35:27 +0800 Subject: [PATCH] update: rollback --- app/Http/Controllers/User/StatController.php | 223 ++----------------- 1 file changed, 13 insertions(+), 210 deletions(-) diff --git a/app/Http/Controllers/User/StatController.php b/app/Http/Controllers/User/StatController.php index 9b1fbbab..a842452e 100644 --- a/app/Http/Controllers/User/StatController.php +++ b/app/Http/Controllers/User/StatController.php @@ -1,225 +1,28 @@ validate([ - 'start_at' => '', - 'end_at' => '' - ]); - - if (isset($params['start_at']) && isset($params['end_at'])) { - $stats = Stat::where('record_at', '>=', $params['start_at']) - ->where('record_at', '<', $params['end_at']) - ->get() - ->makeHidden(['record_at', 'created_at', 'updated_at', 'id', 'record_type']) - ->toArray(); - } else { - $statisticalService = new StatisticalService(); - return [ - 'data' => $statisticalService->generateStatData() - ]; - } - - $stats = array_reduce($stats, function($carry, $item) { - foreach($item as $key => $value) { - if(isset($carry[$key]) && $carry[$key]) { - $carry[$key] += $value; - } else { - $carry[$key] = $value; - } - } - return $carry; - }, []); - - return [ - 'data' => $stats - ]; - } - - public function getStatRecord(Request $request) - { - $request->validate([ - 'type' => 'required|in:paid_total,commission_total,register_count', - 'start_at' => '', - 'end_at' => '' - ]); - - $statisticalService = new StatisticalService(); - $statisticalService->setStartAt($request->input('start_at')); - $statisticalService->setEndAt($request->input('end_at')); - return [ - 'data' => $statisticalService->getStatRecord($request->input('type')) - ]; - } - - public function getRanking(Request $request) - { - $request->validate([ - 'type' => 'required|in:server_traffic_rank,user_consumption_rank', - 'start_at' => '', - 'end_at' => '', - 'limit' => 'nullable|integer' - ]); - - $statisticalService = new StatisticalService(); - $statisticalService->setStartAt($request->input('start_at')); - $statisticalService->setEndAt($request->input('end_at')); - return [ - 'data' => $statisticalService->getRanking($request->input('type'), $request->input('limit') ?? 20) - ]; - } - - public function getOverride(Request $request) - { - return [ - 'data' => [ - 'month_income' => Order::where('created_at', '>=', strtotime(date('Y-m-1'))) - ->where('created_at', '<', time()) - ->whereNotIn('status', [0, 2]) - ->sum('total_amount'), - 'month_register_total' => User::where('created_at', '>=', strtotime(date('Y-m-1'))) - ->where('created_at', '<', time()) - ->count(), - 'ticket_pending_total' => Ticket::where('status', 0) - ->count(), - 'commission_pending_total' => Order::where('commission_status', 0) - ->where('invite_user_id', '!=', NULL) - ->whereNotIn('status', [0, 2]) - ->where('commission_balance', '>', 0) - ->count(), - 'day_income' => Order::where('created_at', '>=', strtotime(date('Y-m-d'))) - ->where('created_at', '<', time()) - ->whereNotIn('status', [0, 2]) - ->sum('total_amount'), - 'last_month_income' => Order::where('created_at', '>=', strtotime('-1 month', strtotime(date('Y-m-1')))) - ->where('created_at', '<', strtotime(date('Y-m-1'))) - ->whereNotIn('status', [0, 2]) - ->sum('total_amount'), - 'commission_month_payout' => CommissionLog::where('created_at', '>=', strtotime(date('Y-m-1'))) - ->where('created_at', '<', time()) - ->sum('get_amount'), - 'commission_last_month_payout' => CommissionLog::where('created_at', '>=', strtotime('-1 month', strtotime(date('Y-m-1')))) - ->where('created_at', '<', strtotime(date('Y-m-1'))) - ->sum('get_amount'), - ] - ]; - } - - public function getOrder(Request $request) - { - $statistics = Stat::where('record_type', 'd') - ->limit(31) - ->orderBy('record_at', 'DESC') - ->get() - ->toArray(); - $result = []; - foreach ($statistics as $statistic) { - $date = date('m-d', $statistic['record_at']); - $result[] = [ - 'type' => '收款金额', - 'date' => $date, - 'value' => $statistic['paid_total'] / 100 - ]; - $result[] = [ - 'type' => '收款笔数', - 'date' => $date, - 'value' => $statistic['paid_count'] - ]; - $result[] = [ - 'type' => '佣金金额(已发放)', - 'date' => $date, - 'value' => $statistic['commission_total'] / 100 - ]; - $result[] = [ - 'type' => '佣金笔数(已发放)', - 'date' => $date, - 'value' => $statistic['commission_count'] - ]; - } - $result = array_reverse($result); - return [ - 'data' => $result - ]; - } - - public function getServerLastRank() - { - $servers = [ - 'shadowsocks' => ServerShadowsocks::where('parent_id', null)->get()->toArray(), - 'v2ray' => ServerVmess::where('parent_id', null)->get()->toArray(), - 'trojan' => ServerTrojan::where('parent_id', null)->get()->toArray(), - 'vmess' => ServerVmess::where('parent_id', null)->get()->toArray() - ]; - $startAt = strtotime('-1 day', strtotime(date('Y-m-d'))); - $endAt = strtotime(date('Y-m-d')); - $statistics = StatServer::select([ - 'server_id', - 'server_type', + $builder = StatUser::select([ 'u', 'd', - DB::raw('(u+d) as total') + 'record_at', + 'user_id', + 'server_rate' ]) - ->where('record_at', '>=', $startAt) - ->where('record_at', '<', $endAt) - ->where('record_type', 'd') - ->limit(10) - ->orderBy('total', 'DESC') - ->get() - ->toArray(); - foreach ($statistics as $k => $v) { - foreach ($servers[$v['server_type']] as $server) { - if ($server['id'] === $v['server_id']) { - $statistics[$k]['server_name'] = $server['name']; - } - } - $statistics[$k]['total'] = $statistics[$k]['total'] / 1073741824; - } - array_multisort(array_column($statistics, 'total'), SORT_DESC, $statistics); - return [ - 'data' => $statistics - ]; - } - - public function getStatUser(Request $request) - { - $request->validate([ - 'user_id' => 'required|integer' + ->where('user_id', $request->user['id']) + ->where('record_at', '>=', strtotime(date('Y-m-1'))) + ->orderBy('record_at', 'DESC'); + return response([ + 'data' => $builder->get() ]); - $current = $request->input('current') ? $request->input('current') : 1; - $pageSize = $request->input('pageSize') >= 10 ? $request->input('pageSize') : 10; - $builder = StatUser::orderBy('record_at', 'DESC')->where('user_id', $request->input('user_id')); - - $total = $builder->count(); - $records = $builder->forPage($current, $pageSize) - ->get(); - return [ - 'data' => $records, - 'total' => $total - ]; } - } -