v2board/app/Http/Controllers/Admin/StatController.php

226 lines
7.9 KiB
PHP
Raw Permalink Normal View History

2019-12-18 18:08:52 +08:00
<?php
namespace App\Http\Controllers\Admin;
2022-10-21 15:46:48 +08:00
use App\Models\CommissionLog;
2020-12-24 23:41:32 +08:00
use App\Models\ServerShadowsocks;
use App\Models\ServerTrojan;
2022-08-21 17:18:24 +08:00
use App\Models\StatUser;
2020-12-24 23:41:32 +08:00
use App\Services\ServerService;
2023-05-03 15:37:19 +08:00
use App\Services\StatisticalService;
2019-12-18 18:08:52 +08:00
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\ServerGroup;
2023-02-15 14:53:13 +08:00
use App\Models\ServerVmess;
2019-12-18 18:08:52 +08:00
use App\Models\Plan;
use App\Models\User;
use App\Models\Ticket;
use App\Models\Order;
2023-05-03 15:37:19 +08:00
use App\Models\Stat;
2020-12-24 23:41:32 +08:00
use App\Models\StatServer;
2020-01-12 01:27:36 +08:00
use Illuminate\Support\Facades\Cache;
2020-12-24 23:41:32 +08:00
use Illuminate\Support\Facades\DB;
2019-12-18 18:08:52 +08:00
2019-12-23 18:46:37 +08:00
class StatController extends Controller
2019-12-18 18:08:52 +08:00
{
2023-05-03 15:37:19 +08:00
public function getStat(Request $request)
{
$params = $request->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();
2023-05-24 01:08:13 +08:00
} else {
$statisticalService = new StatisticalService();
return [
'data' => $statisticalService->generateStatData()
];
2023-05-03 15:37:19 +08:00
}
2023-05-03 21:34:29 +08:00
$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;
}, []);
2023-05-03 15:37:19 +08:00
return [
2023-05-03 21:34:29 +08:00
'data' => $stats
2023-05-03 15:37:19 +08:00
];
}
2023-05-11 00:24:03 +08:00
public function getStatRecord(Request $request)
{
$request->validate([
2023-05-24 01:08:13 +08:00
'type' => 'required|in:paid_total,commission_total,register_count',
2023-05-11 00:24:03 +08:00
'start_at' => '',
2023-05-24 01:08:13 +08:00
'end_at' => ''
2023-05-11 00:24:03 +08:00
]);
$statisticalService = new StatisticalService();
$statisticalService->setStartAt($request->input('start_at'));
$statisticalService->setEndAt($request->input('end_at'));
return [
'data' => $statisticalService->getStatRecord($request->input('type'))
];
}
2023-05-24 01:08:13 +08:00
public function getRanking(Request $request)
{
$request->validate([
'type' => 'required|in:server_traffic_rank,user_consumption_rank,invite_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)
];
}
2020-01-11 13:36:52 +08:00
public function getOverride(Request $request)
{
2023-05-03 15:37:19 +08:00
return [
2019-12-18 18:08:52 +08:00
'data' => [
2020-01-20 13:57:30 +08:00
'month_income' => Order::where('created_at', '>=', strtotime(date('Y-m-1')))
->where('created_at', '<', time())
2021-03-13 00:03:53 +08:00
->whereNotIn('status', [0, 2])
2020-01-20 13:57:30 +08:00
->sum('total_amount'),
'month_register_total' => User::where('created_at', '>=', strtotime(date('Y-m-1')))
->where('created_at', '<', time())
->count(),
2022-02-07 14:54:13 +08:00
'ticket_pending_total' => Ticket::where('status', 0)
2019-12-18 18:08:52 +08:00
->count(),
2022-02-07 14:54:13 +08:00
'commission_pending_total' => Order::where('commission_status', 0)
2019-12-18 18:14:26 +08:00
->where('invite_user_id', '!=', NULL)
2021-03-13 00:03:53 +08:00
->whereNotIn('status', [0, 2])
2020-05-26 15:46:44 +08:00
->where('commission_balance', '>', 0)
2019-12-18 18:08:52 +08:00
->count(),
2020-05-10 23:09:22 +08:00
'day_income' => Order::where('created_at', '>=', strtotime(date('Y-m-d')))
->where('created_at', '<', time())
2021-03-13 00:03:53 +08:00
->whereNotIn('status', [0, 2])
2020-05-10 23:09:22 +08:00
->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')))
2021-03-13 00:03:53 +08:00
->whereNotIn('status', [0, 2])
2022-10-12 01:35:07 +08:00
->sum('total_amount'),
2022-10-21 15:46:48 +08:00
'commission_month_payout' => CommissionLog::where('created_at', '>=', strtotime(date('Y-m-1')))
2022-10-12 01:35:07 +08:00
->where('created_at', '<', time())
2022-10-21 15:46:48 +08:00
->sum('get_amount'),
'commission_last_month_payout' => CommissionLog::where('created_at', '>=', strtotime('-1 month', strtotime(date('Y-m-1'))))
2022-10-12 01:35:07 +08:00
->where('created_at', '<', strtotime(date('Y-m-1')))
2022-10-21 15:46:48 +08:00
->sum('get_amount'),
2019-12-18 18:08:52 +08:00
]
2023-05-03 15:37:19 +08:00
];
2019-12-18 18:08:52 +08:00
}
2020-12-24 23:41:32 +08:00
public function getOrder(Request $request)
{
2023-05-03 15:37:19 +08:00
$statistics = Stat::where('record_type', 'd')
2020-12-24 23:41:32 +08:00
->limit(31)
2021-01-26 18:33:11 +08:00
->orderBy('record_at', 'DESC')
2020-12-24 23:41:32 +08:00
->get()
->toArray();
$result = [];
foreach ($statistics as $statistic) {
$date = date('m-d', $statistic['record_at']);
2023-05-03 15:37:19 +08:00
$result[] = [
2020-12-24 23:41:32 +08:00
'type' => '收款金额',
'date' => $date,
2023-05-03 21:34:29 +08:00
'value' => $statistic['paid_total'] / 100
2023-05-03 15:37:19 +08:00
];
$result[] = [
2020-12-24 23:41:32 +08:00
'type' => '收款笔数',
'date' => $date,
2023-05-03 21:34:29 +08:00
'value' => $statistic['paid_count']
2023-05-03 15:37:19 +08:00
];
$result[] = [
2022-03-11 01:12:49 +08:00
'type' => '佣金金额(已发放)',
2020-12-24 23:41:32 +08:00
'date' => $date,
2023-05-03 15:37:19 +08:00
'value' => $statistic['commission_total'] / 100
];
$result[] = [
2022-03-11 01:12:49 +08:00
'type' => '佣金笔数(已发放)',
2020-12-24 23:41:32 +08:00
'date' => $date,
'value' => $statistic['commission_count']
2023-05-03 15:37:19 +08:00
];
2020-12-24 23:41:32 +08:00
}
2021-02-01 18:04:03 +08:00
$result = array_reverse($result);
2023-05-03 15:37:19 +08:00
return [
2020-12-24 23:41:32 +08:00
'data' => $result
2023-05-03 15:37:19 +08:00
];
2020-12-24 23:41:32 +08:00
}
public function getServerLastRank()
{
$servers = [
'shadowsocks' => ServerShadowsocks::where('parent_id', null)->get()->toArray(),
2023-02-15 14:53:13 +08:00
'v2ray' => ServerVmess::where('parent_id', null)->get()->toArray(),
'trojan' => ServerTrojan::where('parent_id', null)->get()->toArray(),
'vmess' => ServerVmess::where('parent_id', null)->get()->toArray()
2020-12-24 23:41:32 +08:00
];
2022-03-10 11:04:09 +08:00
$startAt = strtotime('-1 day', strtotime(date('Y-m-d')));
$endAt = strtotime(date('Y-m-d'));
2021-09-21 18:11:14 +08:00
$statistics = StatServer::select([
2023-05-03 21:34:29 +08:00
'server_id',
'server_type',
'u',
'd',
DB::raw('(u+d) as total')
])
2022-03-10 11:04:09 +08:00
->where('record_at', '>=', $startAt)
->where('record_at', '<', $endAt)
2020-12-24 23:41:32 +08:00
->where('record_type', 'd')
->limit(10)
2021-02-26 22:54:38 +08:00
->orderBy('total', 'DESC')
2020-12-24 23:41:32 +08:00
->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'];
}
}
2021-02-26 22:54:38 +08:00
$statistics[$k]['total'] = $statistics[$k]['total'] / 1073741824;
2020-12-24 23:41:32 +08:00
}
array_multisort(array_column($statistics, 'total'), SORT_DESC, $statistics);
2023-05-03 15:37:19 +08:00
return [
2020-12-24 23:41:32 +08:00
'data' => $statistics
2023-05-03 15:37:19 +08:00
];
2020-12-24 23:41:32 +08:00
}
2022-08-21 17:18:24 +08:00
public function getStatUser(Request $request)
{
$request->validate([
'user_id' => 'required|integer'
]);
$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
];
}
2023-05-03 15:37:19 +08:00
2019-12-18 18:08:52 +08:00
}
2021-02-01 18:04:03 +08:00