v2board/app/Console/Commands/V2boardStatistics.php

106 lines
2.8 KiB
PHP
Raw Normal View History

2020-12-20 16:57:33 +08:00
<?php
namespace App\Console\Commands;
2023-04-15 19:34:54 +08:00
use App\Models\StatUser;
use App\Services\StatisticalService;
2020-12-20 16:57:33 +08:00
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;
2022-03-11 13:34:10 +08:00
use App\Models\CommissionLog;
2023-04-15 19:34:54 +08:00
use Illuminate\Support\Facades\DB;
2020-12-20 16:57:33 +08:00
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
{
2022-01-22 02:06:53 +08:00
ini_set('memory_limit', -1);
2021-09-07 11:49:35 +08:00
$this->statOrder();
2020-12-20 21:06:23 +08:00
}
2023-04-15 19:34:54 +08:00
private function statUser()
{
$createdAt = time();
$recordAt = strtotime('-1 day', strtotime(date('Y-m-d')));
$statService = new StatisticalService($recordAt);
$stats = $statService->getStatUser();
DB::beginTransaction();
foreach ($stats as $stat) {
if (!StatUser::insert([
'user_id' => $stat['user_Id'],
'u' => $stat['u'],
'd' => $stat['d'],
'server_rate' => $stat['server_rate'],
'created_at' => $createdAt,
'updated_at' => $createdAt,
'record_type' => 'd',
'record_at' => $recordAt
])) {
DB::rollback();
break;
}
}
DB::commit();
}
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);
2022-03-11 13:34:10 +08:00
$orderBuilder = Order::where('paid_at', '>=', $startAt)
2021-07-31 01:21:49 +08:00
->where('paid_at', '<', $endAt)
2021-03-13 00:03:53 +08:00
->whereNotIn('status', [0, 2]);
2022-03-11 13:34:10 +08:00
$orderCount = $orderBuilder->count();
$orderAmount = $orderBuilder->sum('total_amount');
2022-10-21 15:44:15 +08:00
$commissionLogBuilder = CommissionLog::where('created_at', '>=', $startAt)
2022-10-12 01:35:07 +08:00
->where('created_at', '<', $endAt);
2022-10-21 15:44:15 +08:00
$commissionCount = $commissionLogBuilder->count();
$commissionAmount = $commissionLogBuilder->sum('get_amount');
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 16:57:33 +08:00
}