From 7dedf91026e9925ad5c1d950a7117bad380fadfd Mon Sep 17 00:00:00 2001 From: lion <120344285@qq.com> Date: Thu, 30 Apr 2026 10:24:08 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BatchCreateVenueAdminAccountsCommand.php | 50 +++++++++++-------- 1 file changed, 28 insertions(+), 22 deletions(-) diff --git a/app/Console/Commands/BatchCreateVenueAdminAccountsCommand.php b/app/Console/Commands/BatchCreateVenueAdminAccountsCommand.php index 2415b47..e786e2b 100644 --- a/app/Console/Commands/BatchCreateVenueAdminAccountsCommand.php +++ b/app/Console/Commands/BatchCreateVenueAdminAccountsCommand.php @@ -14,7 +14,7 @@ class BatchCreateVenueAdminAccountsCommand extends Command { protected $signature = 'venues:create-admin-accounts {--output= : 导出 xlsx 绝对或相对路径(默认 storage/app/exports/venue_admins_时间戳.xlsx)} - {--dry-run : 只计算并导出,不写入数据库}'; + {--dry-run : 仅生成 Excel,不向 users / user_venue 写入(预览或离线存档)}'; protected $description = '为每个场馆创建一个场馆管理员账号(姓名=馆名,用户名=拼音首字母缩写,密码按规则),并绑定该场馆;导出含明文密码的 Excel'; @@ -69,34 +69,40 @@ class BatchCreateVenueAdminAccountsCommand extends Command $outPath = base_path($outPath); } + $dryRun = (bool) $this->option('dry-run'); + + if ($dryRun) { + $this->warn('当前为 --dry-run:只生成 Excel,不会写入 users / user_venue。去掉 --dry-run 才会入库。'); + } else { + $this->info('即将写入数据库(users、user_venue),随后生成 Excel…'); + DB::transaction(function () use ($rows) { + foreach ($rows as $row) { + $user = User::updateOrCreate( + ['username' => $row['username']], + [ + 'name' => $row['name'], + 'email' => null, + 'password' => $row['password_plain'], + 'role' => 'venue_admin', + 'is_active' => true, + ] + ); + + $user->venues()->sync([$row['venue_id']]); + } + }); + $this->info('数据库写入完成:已创建/更新 '.count($rows).' 个场馆管理员并完成场馆绑定。'); + } + $this->writeXlsx($outPath, $rows); $this->info("Excel 已生成:{$outPath}"); - if ($this->option('dry-run')) { - $this->warn('dry-run:未写入用户数据。'); + if ($dryRun) { + $this->warn('本次未写入数据库。若需要入库请执行:php artisan venues:create-admin-accounts(不要带 --dry-run)'); return self::SUCCESS; } - DB::transaction(function () use ($rows) { - foreach ($rows as $row) { - $user = User::updateOrCreate( - ['username' => $row['username']], - [ - 'name' => $row['name'], - 'email' => null, - 'password' => $row['password_plain'], - 'role' => 'venue_admin', - 'is_active' => true, - ] - ); - - $user->venues()->sync([$row['venue_id']]); - } - }); - - $this->info('已创建/更新 '.count($rows).' 个场馆管理员账号并完成场馆绑定。'); - return self::SUCCESS; }