generated from panliang/owl-admin-starter
60 lines
1.6 KiB
PHP
60 lines
1.6 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 {--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 = [];
|
|
CateRankModel::where('sn', $sn)->delete();
|
|
$scoreList = UserScore::query()
|
|
->whereBetween('created_at', [$start, $end])
|
|
->select('cate_id', DB::raw('sum(`score`) as `score`'))
|
|
->groupBy('cate_id')
|
|
->get();
|
|
foreach ($scoreList as $item) {
|
|
$list[] = [
|
|
'sn' => $sn,
|
|
'cate_id' => $item->cate_id,
|
|
'score' => $item->score,
|
|
'created_at' => $now,
|
|
'updated_at' => $now,
|
|
];
|
|
}
|
|
CateRankModel::insert($list);
|
|
// 清空党支部当前得分
|
|
PartyCate::update([
|
|
'current_score' => 0
|
|
]);
|
|
}
|
|
}
|