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

60 lines
1.4 KiB
PHP

<?php
namespace App\Console\Commands;
use App\Models\PartyUser;
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 {--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 = [];
UserRankModel::where('sn', $sn)->delete();
$userList = PartyUser::get();
foreach ($userList as $item) {
$list[] = [
'sn' => $sn,
'cate_id' => $item->cate_id,
'user_id' => $item->id,
'score' => $item->current_score,
'created_at' => $now,
'updated_at' => $now,
];
}
UserRankModel::insert($list);
// 清空党员当前得分
PartyUser::where('current_score', '>', 0)->update([
'current_score' => 0
]);
}
}