1
0
Fork 0
party-rank-server/app/Console/Commands/UserRank.php

58 lines
1.5 KiB
PHP

<?php
namespace App\Console\Commands;
use App\Models\UserScore;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use App\Models\UserRank as UserRankModel;
class UserRank extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'app:user-rank {--quarter=1}';
/**
* The console command description.
*
* @var string
*/
protected $description = '生成党员排名记录, quarter: 上一季度';
/**
* Execute the console command.
*/
public function handle()
{
$now = now();
$time = $now->copy()->subQuarters($this->option('quarter'));
$start = $time->copy()->startOfQuarter();
$end = $time->copy()->endOfQuarter();
$sn = $time->year . '-' . $time->quarter;
$list = [];
UserRankModel::where('sn', $sn)->delete();
$scoreList = UserScore::with(['user'])
->whereBetween('created_at', [$start, $end])
->select('user_id', DB::raw('sum(`score`) as `score`'))
->groupBy('user_id')
->get();
foreach ($scoreList as $item) {
$list[] = [
'sn' => $sn,
'cate_id' => $item->user->cate_id,
'user_id' => $item->user_id,
'score' => $item->score,
'created_at' => $now,
'updated_at' => $now,
];
}
UserRankModel::insert($list);
}
}