From fda7e4a02bfa755e14b70289814aff2e3c836dea Mon Sep 17 00:00:00 2001 From: panliang <1163816051@qq.com> Date: Sun, 10 Dec 2023 13:56:32 +0800 Subject: [PATCH] console --- README.md | 4 +++ app/Console/Commands/CateRank.php | 15 ++++++----- app/Console/Commands/UserRank.php | 27 +++++++++---------- app/Console/Kernel.php | 3 ++- .../Controllers/Api/CateRankController.php | 2 +- database/seeders/PartyUserSeeder.php | 10 ++++++- 6 files changed, 36 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index 1fd18a6..8c47b51 100644 --- a/README.md +++ b/README.md @@ -3,3 +3,7 @@ - php >= 8.1 - Laravel 10.x - slowlyo/owl-admin latest + +## 安装 + +- 定时任务 diff --git a/app/Console/Commands/CateRank.php b/app/Console/Commands/CateRank.php index 5f23d80..3183635 100644 --- a/app/Console/Commands/CateRank.php +++ b/app/Console/Commands/CateRank.php @@ -15,14 +15,14 @@ class CateRank extends Command * * @var string */ - protected $signature = 'app:cate-rank {--quarter=1}'; + protected $signature = 'app:cate-rank {--sn=}'; /** * The console command description. * * @var string */ - protected $description = '生成党支部排名记录, quarter: 上一季度'; + protected $description = '生成党支部排名记录'; /** * Execute the console command. @@ -30,10 +30,11 @@ class CateRank extends 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; + $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(); @@ -43,7 +44,7 @@ class CateRank extends Command 'cate_id' => $item->id, 'score' => $item->current_score, 'count' => $item->users_count, - 'avg_score' => floor($item->score/$item->users_count), + 'avg_score' => $item->users_count ? floor($item->score/$item->users_count) : 0, 'created_at' => $now, 'updated_at' => $now, ]; diff --git a/app/Console/Commands/UserRank.php b/app/Console/Commands/UserRank.php index 736f4a6..a7c292b 100644 --- a/app/Console/Commands/UserRank.php +++ b/app/Console/Commands/UserRank.php @@ -15,14 +15,14 @@ class UserRank extends Command * * @var string */ - protected $signature = 'app:user-rank {--quarter=1}'; + protected $signature = 'app:user-rank {--sn=}'; /** * The console command description. * * @var string */ - protected $description = '生成党员排名记录, quarter: 上一季度'; + protected $description = '生成党员排名记录'; /** * Execute the console command. @@ -30,25 +30,22 @@ class UserRank extends 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; + $sn = $this->option('sn'); + if (!$sn) { + $time = $now->copy()->subQuarter(); + $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) { + $userList = PartyUser::get(); + foreach ($userList as $item) { $list[] = [ 'sn' => $sn, - 'cate_id' => $item->user->cate_id, - 'user_id' => $item->user_id, - 'score' => $item->score, + 'cate_id' => $item->cate_id, + 'user_id' => $item->id, + 'score' => $item->current_score, 'created_at' => $now, 'updated_at' => $now, ]; diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php index e6b9960..6b974d9 100644 --- a/app/Console/Kernel.php +++ b/app/Console/Kernel.php @@ -12,7 +12,8 @@ class Kernel extends ConsoleKernel */ protected function schedule(Schedule $schedule): void { - // $schedule->command('inspire')->hourly(); + $schedule->command('app:user-rank')->quarterly(); + $schedule->command('app:cate-rank')->quarterly(); } /** diff --git a/app/Http/Controllers/Api/CateRankController.php b/app/Http/Controllers/Api/CateRankController.php index 505ca4e..6b2705a 100644 --- a/app/Http/Controllers/Api/CateRankController.php +++ b/app/Http/Controllers/Api/CateRankController.php @@ -27,7 +27,7 @@ class CateRankController extends Controller 'id' => $item->id, 'name' => $item->name, 'count' => $item->users_count, - 'score' => floor($item->current_score / $item->users_count), + 'score' => $item->users_count ? floor($item->current_score / $item->users_count) : 0, ]); } $rankList = $rankList->sortByDesc('score'); diff --git a/database/seeders/PartyUserSeeder.php b/database/seeders/PartyUserSeeder.php index c99b0cd..ef4530d 100644 --- a/database/seeders/PartyUserSeeder.php +++ b/database/seeders/PartyUserSeeder.php @@ -20,7 +20,15 @@ class PartyUserSeeder extends Seeder { PartyCate::truncate(); PartyUser::truncate(); - PartyCate::factory()->count(20)->create(); + PartyCate::factory()->count(20)->create([ + 'scores' => [ + 'score_cate_1' => 0, + 'score_cate_2' => 0, + 'score_cate_3' => 0, + 'score_cate_4' => 0, + 'score_cate_5' => 0, + ] + ]); (new PartyUserFactory())->count(100)->create([ 'scores' => [ 'score_cate_1' => 0,