67 lines
2.4 KiB
PHP
67 lines
2.4 KiB
PHP
<?php
|
|
|
|
namespace app\admin\command;
|
|
|
|
use think\console\Command;
|
|
use think\console\Input;
|
|
use think\console\Output;
|
|
use think\Db;
|
|
|
|
class CarImport extends Command
|
|
{
|
|
protected function configure()
|
|
{
|
|
$this->setName('car:import')
|
|
->setDescription('导入常见汽车品牌和系列到 brands 与 series 表');
|
|
}
|
|
|
|
protected function execute(Input $input, Output $output)
|
|
{
|
|
$brands = [
|
|
'奔驰' => ['C级', 'E级', 'S级', 'GLC', 'GLE', 'GLS'],
|
|
'宝马' => ['3系', '5系', '7系', 'X1', 'X3', 'X5'],
|
|
'奥迪' => ['A3', 'A4', 'A6', 'A8', 'Q3', 'Q5', 'Q7'],
|
|
'大众' => ['朗逸', '速腾', '宝来', '迈腾', '帕萨特', '途观', '途昂'],
|
|
'丰田' => ['卡罗拉', '凯美瑞', '汉兰达', 'RAV4', '普拉多'],
|
|
'本田' => ['思域', '雅阁', 'CR-V', 'XR-V', '奥德赛'],
|
|
'日产' => ['轩逸', '天籁', '奇骏', '逍客'],
|
|
'特斯拉' => ['Model 3', 'Model Y', 'Model S', 'Model X'],
|
|
'福特' => ['福克斯', '蒙迪欧', '锐界', '探险者'],
|
|
'比亚迪' => ['汉', '唐', '宋', '秦', '元PLUS']
|
|
];
|
|
|
|
$now = date('Y-m-d H:i:s');
|
|
$insertedBrands = 0;
|
|
$insertedSeries = 0;
|
|
|
|
foreach ($brands as $brandName => $seriesList) {
|
|
// 避免重复插入
|
|
$exists = Db::name('brands')->where('name', $brandName)->find();
|
|
if ($exists) {
|
|
$brandId = $exists['id'];
|
|
} else {
|
|
$brandId = Db::name('brands')->insertGetId([
|
|
'name' => $brandName,
|
|
'sort_order' => 0,
|
|
'created_at' => $now
|
|
]);
|
|
$insertedBrands++;
|
|
}
|
|
|
|
foreach ($seriesList as $seriesName) {
|
|
$existsSeries = Db::name('series')->where('brand_id', $brandId)->where('name', $seriesName)->find();
|
|
if (!$existsSeries) {
|
|
Db::name('series')->insert([
|
|
'brand_id' => $brandId,
|
|
'name' => $seriesName,
|
|
'created_at' => $now
|
|
]);
|
|
$insertedSeries++;
|
|
}
|
|
}
|
|
}
|
|
|
|
$output->writeln("✅ 导入完成:{$insertedBrands} 个品牌,{$insertedSeries} 个系列");
|
|
}
|
|
}
|