73 lines
2.3 KiB
PHP
73 lines
2.3 KiB
PHP
<?php
|
|
|
|
namespace app\admin\command;
|
|
|
|
use Overtrue\Pinyin\Converter;
|
|
use think\console\Command;
|
|
use think\console\Input;
|
|
use think\console\Output;
|
|
use think\Db;
|
|
use Overtrue\Pinyin\Pinyin;
|
|
|
|
class ItemImportCommand extends Command
|
|
{
|
|
protected function configure()
|
|
{
|
|
$this->setName('item:import')
|
|
->setDescription('倒入 item 数据');
|
|
}
|
|
|
|
protected function execute(Input $input, Output $output)
|
|
{
|
|
$jsonData = file_get_contents(__DIR__.'/items.json');
|
|
$items = json_decode($jsonData, true);
|
|
if (!$items) {
|
|
$output->writeln("Invalid JSON data.");
|
|
return;
|
|
}
|
|
|
|
// 清空表并重置自增 ID
|
|
Db::execute('TRUNCATE TABLE fa_item');
|
|
|
|
// 用于存储 oldId => newId 映射关系
|
|
$idMap = [];
|
|
|
|
// 先插入父级数据
|
|
foreach ($items as $item) {
|
|
if ($item['parentId'] === null) {
|
|
$data = [
|
|
'pid' => 0, // 父级项目 pid 设为 0
|
|
'level' => $item['level'],
|
|
'title' => $item['projectName'],
|
|
'key_word'=> $item['keyword'] ?? null,
|
|
// 'image' => $item['img'] ?? $item['projectImg'] ?? null,
|
|
'sort' => $item['orderNum'],
|
|
'status' => $item['state'] === '生效' ? 1 : 0
|
|
];
|
|
|
|
$newId = Db::name('item')->insertGetId($data);
|
|
$idMap[$item['id']] = $newId; // 记录映射关系
|
|
}
|
|
}
|
|
|
|
// 再插入子级数据
|
|
foreach ($items as $item) {
|
|
if ($item['parentId'] !== null && isset($idMap[$item['parentId']])) {
|
|
$data = [
|
|
'pid' => $idMap[$item['parentId']], // 关联正确的父级 ID
|
|
'level' => $item['level'],
|
|
'title' => $item['projectName'],
|
|
'key_word'=> $item['keyword'] ?? null,
|
|
// 'image' => $item['img'] ?? $item['projectImg'] ?? null,
|
|
'sort' => $item['orderNum'],
|
|
'status' => $item['state'] === '生效' ? 1 : 0
|
|
];
|
|
|
|
Db::name('item')->insert($data);
|
|
}
|
|
}
|
|
|
|
$output->writeln("Data imported successfully!");
|
|
}
|
|
}
|