parent
0f51c717ef
commit
e84f2372d7
@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
class StatisticsMetadata extends SoftDeletesModel
|
||||
{
|
||||
protected $table = 'statistics_metadata';
|
||||
|
||||
/**
|
||||
* 根据 key 获取统计元数据
|
||||
* @param string $key
|
||||
* @return StatisticsMetadata|null
|
||||
*/
|
||||
public static function getByKey($key)
|
||||
{
|
||||
return self::where('key', $key)->first();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取所有统计元数据,以 key 为索引的数组
|
||||
* @return array
|
||||
*/
|
||||
public static function getAllAsArray()
|
||||
{
|
||||
$items = self::all();
|
||||
$result = [];
|
||||
foreach ($items as $item) {
|
||||
$result[$item->key] = [
|
||||
'name' => $item->name,
|
||||
'from' => $item->from,
|
||||
'verify' => $item->verify,
|
||||
];
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration {
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
Schema::create('statistics_metadata', function (Blueprint $table) {
|
||||
$table->comment('统计指标元数据');
|
||||
$table->increments('id');
|
||||
$table->string('key')->unique()->comment('统计项标识,如 course_signs_total');
|
||||
$table->string('name')->comment('统计项名称');
|
||||
$table->text('from')->nullable()->comment('统计逻辑说明');
|
||||
$table->text('verify')->nullable()->comment('验证方法说明');
|
||||
$table->text('remark')->nullable()->comment('备注');
|
||||
$table->dateTime('created_at')->nullable();
|
||||
$table->dateTime('updated_at')->nullable();
|
||||
$table->dateTime('deleted_at')->nullable();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function down()
|
||||
{
|
||||
Schema::dropIfExists('statistics_metadata');
|
||||
}
|
||||
};
|
||||
@ -0,0 +1,56 @@
|
||||
<?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) . ' 条记录');
|
||||
}
|
||||
}
|
||||
Loading…
Reference in new issue