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

59 lines
1.5 KiB
PHP

<?php
namespace App\Console\Commands;
use App\Models\UserScore;
use App\Models\PartyCate;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use App\Models\CateRank as CateRankModel;
class CateRank extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'app:cate-rank {--sn=}';
/**
* The console command description.
*
* @var string
*/
protected $description = '生成党支部排名记录';
/**
* Execute the console command.
*/
public function handle()
{
$now = now();
$sn = $this->option('sn');
if (!$sn) {
$time = $now->copy()->subQuarter();
$sn = $time->year . '-' . $time->quarter;
}
$list = [];
CateRankModel::where('sn', $sn)->delete();
$cateList = PartyCate::withCount(['users'])->get();
foreach ($cateList as $item) {
$list[] = [
'sn' => $sn,
'cate_id' => $item->id,
'score' => $item->current_score,
'count' => $item->users_count,
'avg_score' => $item->users_count ? floor($item->score/$item->users_count) : 0,
'created_at' => $now,
'updated_at' => $now,
];
}
CateRankModel::insert($list);
// 清空党支部当前得分
PartyCate::where('current_score', '>', 0)->update([
'current_score' => 0
]);
}
}