v2board/app/Console/Commands/V2boardStatistics.php

101 lines
2.6 KiB
PHP
Raw Normal View History

2020-12-20 16:57:33 +08:00
<?php
namespace App\Console\Commands;
use App\Jobs\StatServerJob;
use Illuminate\Console\Command;
2020-12-22 15:21:18 +08:00
use App\Models\Order;
2020-12-24 23:41:32 +08:00
use App\Models\StatOrder;
use App\Models\ServerLog;
2020-12-20 16:57:33 +08:00
use Illuminate\Support\Facades\DB;
class V2boardStatistics extends Command
2020-12-20 16:57:33 +08:00
{
/**
* The name and signature of the console command.
*
* @var string
*/
2020-12-20 21:06:23 +08:00
protected $signature = 'v2board:statistics';
2020-12-20 16:57:33 +08:00
/**
* The console command description.
*
* @var string
*/
2020-12-20 21:06:23 +08:00
protected $description = '统计任务';
2020-12-20 16:57:33 +08:00
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
2020-12-20 21:06:23 +08:00
{
2020-12-24 23:41:32 +08:00
$this->statOrder();
$this->statServer();
2020-12-20 21:06:23 +08:00
}
2020-12-21 17:11:48 +08:00
private function statOrder()
{
2020-12-22 15:21:18 +08:00
$endAt = strtotime(date('Y-m-d'));
$startAt = strtotime('-1 day', $endAt);
2021-07-31 01:21:49 +08:00
$builder = Order::where('paid_at', '>=', $startAt)
->where('paid_at', '<', $endAt)
2021-03-13 00:03:53 +08:00
->whereNotIn('status', [0, 2]);
2020-12-22 15:21:18 +08:00
$orderCount = $builder->count();
$orderAmount = $builder->sum('total_amount');
2021-07-02 21:43:22 +08:00
$builder = $builder->where('commission_balance', '!=', 0);
2020-12-22 15:21:18 +08:00
$commissionCount = $builder->count();
$commissionAmount = $builder->sum('commission_balance');
2020-12-24 23:41:32 +08:00
$data = [
'order_count' => $orderCount,
'order_amount' => $orderAmount,
'commission_count' => $commissionCount,
'commission_amount' => $commissionAmount,
'record_type' => 'd',
'record_at' => $startAt
];
$statistic = StatOrder::where('record_at', $startAt)
->where('record_type', 'd')
->first();
if ($statistic) {
$statistic->update($data);
return;
}
StatOrder::create($data);
2020-12-21 17:11:48 +08:00
}
2020-12-20 21:06:23 +08:00
private function statServer()
2020-12-20 16:57:33 +08:00
{
$endAt = strtotime(date('Y-m-d'));
$startAt = strtotime('-1 day', $endAt);
$statistics = ServerLog::select([
2020-12-20 21:06:23 +08:00
'server_id',
'method as server_type',
DB::raw("sum(u) as u"),
DB::raw("sum(d) as d"),
])
2020-12-20 16:57:33 +08:00
->where('log_at', '>=', $startAt)
->where('log_at', '<', $endAt)
->groupBy('server_id', 'method')
->get()
->toArray();
foreach ($statistics as $statistic) {
2020-12-24 23:41:32 +08:00
$statistic['record_type'] = 'd';
2020-12-20 16:57:33 +08:00
$statistic['record_at'] = $startAt;
StatServerJob::dispatch($statistic);
}
}
}