You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

57 lines
1.5 KiB

<?php
namespace Database\Seeders;
use App\Models\StatisticsMetadata;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\File;
class StatisticsMetadataSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
// 读取 JSON 文件
$jsonPath = base_path('统计指标说明.json');
if (!File::exists($jsonPath)) {
$this->command->warn('统计指标说明.json 文件不存在,跳过数据填充');
return;
}
$jsonContent = File::get($jsonPath);
$data = json_decode($jsonContent, true);
if (json_last_error() !== JSON_ERROR_NONE) {
$this->command->error('JSON 文件解析失败:' . json_last_error_msg());
return;
}
if (!is_array($data)) {
$this->command->error('JSON 数据格式不正确,应为对象/数组');
return;
}
// 清空现有数据(可选,根据需要决定是否保留)
// StatisticsMetadata::truncate();
// 插入数据
foreach ($data as $key => $item) {
StatisticsMetadata::updateOrCreate(
['key' => $key],
[
'name' => $item['name'] ?? '',
'from' => $item['from'] ?? '',
'verify' => $item['verify'] ?? '',
]
);
}
$this->command->info('统计指标元数据填充完成,共处理 ' . count($data) . ' 条记录');
}
}