|
|
|
|
@ -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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|