refactor: kb & station & terminal

This commit is contained in:
2026-03-23 20:17:17 +08:00
parent 63ea2686e1
commit b74ba1a3f8
81 changed files with 1016 additions and 2492 deletions

View File

@@ -3,7 +3,7 @@
namespace Database\Factories;
use App\Models\Document;
use App\Models\Group;
use App\Models\KnowledgeBase;
use App\Models\User;
use Illuminate\Database\Eloquent\Factories\Factory;
@@ -12,40 +12,21 @@ use Illuminate\Database\Eloquent\Factories\Factory;
*/
class DocumentFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* @var string
*/
protected $model = Document::class;
/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
public function definition(): array
{
// 使用中文 Faker 生成器
$faker = \Faker\Factory::create('zh_CN');
// 生成中文文档标题
$titles = [
'项目管理规范',
'技术文档模板',
'员工手册',
'产品需求文档',
'系统设计方案',
'测试报告',
'会议纪要',
'培训资料',
'操作指南',
'年度总结报告',
'项目管理规范', '技术文档模板', '员工手册', '产品需求文档',
'系统设计方案', '测试报告', '会议纪要', '培训资料',
'操作指南', '年度总结报告',
];
$title = $faker->randomElement($titles) . ' - ' . $faker->word();
$fileName = $faker->word() . '_' . date('Ymd') . '.docx';
return [
'title' => $title,
'description' => $faker->paragraph(3),
@@ -53,38 +34,15 @@ class DocumentFactory extends Factory
'file_name' => $fileName,
'file_size' => fake()->numberBetween(10000, 5000000),
'mime_type' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
'type' => fake()->randomElement(['global', 'dedicated']),
'group_id' => null,
'knowledge_base_id' => KnowledgeBase::factory(),
'uploaded_by' => User::factory(),
'markdown_path' => null,
'markdown_preview' => null,
'conversion_status' => 'pending',
'conversion_error' => null,
];
}
/**
* 指定文档为全局类型
*/
public function global(): static
{
return $this->state(fn (array $attributes) => [
'type' => 'global',
'group_id' => null,
]);
}
/**
* 指定文档为专用类型
*/
public function dedicated(?int $groupId = null): static
{
return $this->state(fn (array $attributes) => [
'type' => 'dedicated',
'group_id' => $groupId ?? Group::factory(),
]);
}
/**
* 指定文档已完成转换
*/
@@ -92,10 +50,10 @@ class DocumentFactory extends Factory
{
$faker = \Faker\Factory::create('zh_CN');
$uuid = fake()->uuid();
return $this->state(fn (array $attributes) => [
'markdown_path' => 'markdown/' . date('Y/m/d') . '/' . $uuid . '.md',
'markdown_preview' => $faker->text(500),
'conversion_status' => 'completed',
'conversion_error' => null,
]);
@@ -108,7 +66,7 @@ class DocumentFactory extends Factory
{
return $this->state(fn (array $attributes) => [
'markdown_path' => null,
'markdown_preview' => null,
'conversion_status' => 'failed',
'conversion_error' => 'Failed to convert document: Invalid file format',
]);
@@ -121,7 +79,7 @@ class DocumentFactory extends Factory
{
return $this->state(fn (array $attributes) => [
'markdown_path' => null,
'markdown_preview' => null,
'conversion_status' => 'processing',
'conversion_error' => null,
]);

View File

@@ -1,70 +0,0 @@
<?php
namespace Database\Factories;
use App\Models\Document;
use App\Models\DownloadLog;
use App\Models\User;
use Illuminate\Database\Eloquent\Factories\Factory;
/**
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\DownloadLog>
*/
class DownloadLogFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* @var string
*/
protected $model = DownloadLog::class;
/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
public function definition(): array
{
return [
'document_id' => Document::factory(),
'user_id' => User::factory(),
'downloaded_at' => fake()->dateTimeBetween('-1 year', 'now'),
'ip_address' => fake()->ipv4(),
];
}
/**
* 指定下载日志使用特定的文档
*/
public function forDocument(Document|int $document): static
{
$documentId = $document instanceof Document ? $document->id : $document;
return $this->state(fn (array $attributes) => [
'document_id' => $documentId,
]);
}
/**
* 指定下载日志使用特定的用户
*/
public function forUser(User|int $user): static
{
$userId = $user instanceof User ? $user->id : $user;
return $this->state(fn (array $attributes) => [
'user_id' => $userId,
]);
}
/**
* 指定下载日志使用最近的时间
*/
public function recent(): static
{
return $this->state(fn (array $attributes) => [
'downloaded_at' => fake()->dateTimeBetween('-7 days', 'now'),
]);
}
}

View File

@@ -1,49 +0,0 @@
<?php
namespace Database\Factories;
use App\Models\Group;
use Illuminate\Database\Eloquent\Factories\Factory;
/**
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Group>
*/
class GroupFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* @var string
*/
protected $model = Group::class;
/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
public function definition(): array
{
// 使用中文 Faker 生成器
$faker = \Faker\Factory::create('zh_CN');
// 生成中文分组名称(使用公司名或部门名)
$groupNames = [
'技术部',
'市场部',
'人力资源部',
'财务部',
'运营部',
'产品部',
'设计部',
'客服部',
'研发中心',
'销售部',
];
return [
'name' => $faker->randomElement($groupNames) . ' - ' . $faker->company(),
'description' => $faker->sentence(10),
];
}
}

View File

@@ -0,0 +1,22 @@
<?php
namespace Database\Factories;
use App\Models\Station;
use Illuminate\Database\Eloquent\Factories\Factory;
/**
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Station>
*/
class StationFactory extends Factory
{
protected $model = Station::class;
public function definition(): array
{
return [
'name' => 'BL' . fake()->unique()->numerify('##') . fake()->randomElement(['U', 'B', 'W', 'HB']),
'description' => null,
];
}
}

View File

@@ -28,7 +28,7 @@ class TerminalFactory extends Factory
'name' => fake()->randomElement(['生产线A', '生产线B', '生产线C', '质检站', '包装站']) . '-' . fake()->randomElement(['工位1', '工位2', '工位3']),
'code' => 'TERM-' . fake()->unique()->numerify('####'),
'ip_address' => fake()->localIpv4(),
'station_id' => null, // 需要关联实际的线站ID
'station_id' => null,
'diagram_url' => fake()->imageUrl(1920, 1080, 'diagram', true),
'voice_wakeup_enabled' => false,
'voice_wakeup_word' => null,

View File

@@ -1,29 +0,0 @@
<?php
namespace Database\Factories;
use App\Models\Terminal;
use App\Models\TerminalPrompt;
use Illuminate\Database\Eloquent\Factories\Factory;
/**
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\TerminalPrompt>
*/
class TerminalPromptFactory extends Factory
{
protected $model = TerminalPrompt::class;
/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
public function definition(): array
{
return [
'terminal_id' => Terminal::factory(),
'prompt_template' => '你是{station_id}光束线的AI助手。当前时间是{time}。请根据用户{user}的问题提供帮助。',
'variables' => [],
];
}
}

View File

@@ -1,32 +0,0 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('group_user', function (Blueprint $table) {
$table->id();
$table->foreignId('group_id')->constrained()->onDelete('cascade');
$table->foreignId('user_id')->constrained()->onDelete('cascade');
$table->timestamps();
// 添加唯一索引,确保同一用户不会重复加入同一分组
$table->unique(['group_id', 'user_id']);
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('group_user');
}
};

View File

@@ -6,9 +6,6 @@ use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('documents', function (Blueprint $table) {
@@ -19,21 +16,19 @@ return new class extends Migration
$table->string('file_name');
$table->bigInteger('file_size');
$table->string('mime_type', 100);
$table->enum('type', ['global', 'dedicated']);
$table->foreignId('group_id')->nullable()->constrained()->onDelete('set null');
$table->foreignId('uploaded_by')->constrained('users')->onDelete('cascade');
$table->foreignId('knowledge_base_id')->constrained('knowledge_bases')->cascadeOnDelete();
$table->foreignId('uploaded_by')->constrained('users')->cascadeOnDelete();
$table->string('markdown_path', 500)->nullable();
$table->enum('conversion_status', ['pending', 'processing', 'completed', 'failed'])->default('pending');
$table->text('conversion_error')->nullable();
$table->timestamps();
// 添加索引以优化查询性能
$table->index('type');
$table->index('group_id');
$table->index('knowledge_base_id');
$table->index('uploaded_by');
$table->index('conversion_status');
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('documents');

View File

@@ -1,52 +0,0 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('documents', function (Blueprint $table) {
// 添加 Markdown 文件路径字段
$table->string('markdown_path', 500)->nullable()->after('description');
// 添加 Markdown 内容摘要字段(用于快速预览)
$table->text('markdown_preview')->nullable()->after('markdown_path');
// 添加转换状态字段
$table->enum('conversion_status', ['pending', 'processing', 'completed', 'failed'])
->default('pending')
->after('markdown_preview');
// 添加转换错误信息字段
$table->text('conversion_error')->nullable()->after('conversion_status');
// 添加索引以优化查询性能
$table->index('conversion_status');
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('documents', function (Blueprint $table) {
// 删除索引
$table->dropIndex(['conversion_status']);
// 删除字段
$table->dropColumn([
'markdown_path',
'markdown_preview',
'conversion_status',
'conversion_error'
]);
});
}
};

View File

@@ -13,8 +13,10 @@ class CreateActivityLogTable extends Migration
$table->string('log_name')->nullable();
$table->text('description');
$table->nullableMorphs('subject', 'subject');
$table->string('event')->nullable();
$table->nullableMorphs('causer', 'causer');
$table->json('properties')->nullable();
$table->uuid('batch_uuid')->nullable();
$table->timestamps();
$table->index('log_name');
});

View File

@@ -1,22 +0,0 @@
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class AddEventColumnToActivityLogTable extends Migration
{
public function up()
{
Schema::connection(config('activitylog.database_connection'))->table(config('activitylog.table_name'), function (Blueprint $table) {
$table->string('event')->nullable()->after('subject_type');
});
}
public function down()
{
Schema::connection(config('activitylog.database_connection'))->table(config('activitylog.table_name'), function (Blueprint $table) {
$table->dropColumn('event');
});
}
}

View File

@@ -1,22 +0,0 @@
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class AddBatchUuidColumnToActivityLogTable extends Migration
{
public function up()
{
Schema::connection(config('activitylog.database_connection'))->table(config('activitylog.table_name'), function (Blueprint $table) {
$table->uuid('batch_uuid')->nullable()->after('properties');
});
}
public function down()
{
Schema::connection(config('activitylog.database_connection'))->table(config('activitylog.table_name'), function (Blueprint $table) {
$table->dropColumn('batch_uuid');
});
}
}

View File

@@ -6,24 +6,18 @@ use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('groups', function (Blueprint $table) {
Schema::create('stations', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('name')->unique();
$table->text('description')->nullable();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('groups');
Schema::dropIfExists('stations');
}
};

View File

@@ -6,9 +6,6 @@ use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('terminals', function (Blueprint $table) {
@@ -16,27 +13,22 @@ return new class extends Migration
$table->string('name')->comment('终端名称');
$table->string('code', 100)->unique()->comment('终端编码');
$table->string('ip_address', 45)->nullable()->comment('IP地址');
$table->string('mac_address', 17)->nullable()->unique()->comment('MAC地址 (AA:BB:CC:DD:EE:FF)');
$table->string('station_id', 50)->nullable()->comment('线站ID');
$table->string('mac_address', 17)->nullable()->unique()->comment('MAC地址');
$table->foreignId('station_id')->nullable()->constrained()->nullOnDelete();
$table->string('diagram_url', 500)->nullable()->comment('组态界面地址');
$table->string('scada_data_url', 500)->nullable()->comment('网关数据查询地址');
$table->string('scada_tags_url', 500)->nullable()->comment('网关点位定义查询地址');
$table->boolean('voice_wakeup_enabled')->default(false)->comment('语音唤醒是否启用');
$table->string('voice_wakeup_word', 100)->nullable()->comment('语音唤醒词');
$table->boolean('is_online')->default(false)->comment('在线状态');
$table->timestamp('last_online_at')->nullable()->comment('最后在线时间');
$table->boolean('voice_wakeup_enabled')->default(false);
$table->string('voice_wakeup_word', 100)->nullable();
$table->boolean('is_online')->default(false);
$table->timestamp('last_online_at')->nullable();
$table->timestamps();
$table->softDeletes();
// 添加索引
$table->index('station_id', 'idx_station');
$table->index('is_online', 'idx_online');
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('terminals');

View File

@@ -0,0 +1,24 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
public function up(): void
{
Schema::create('station_user', function (Blueprint $table) {
$table->id();
$table->foreignId('station_id')->constrained()->cascadeOnDelete();
$table->foreignId('user_id')->constrained()->cascadeOnDelete();
$table->timestamps();
$table->unique(['station_id', 'user_id']);
});
}
public function down(): void
{
Schema::dropIfExists('station_user');
}
};

View File

@@ -1,39 +0,0 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('terminal_knowledge_bases', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('terminal_id')->comment('终端ID');
$table->unsignedBigInteger('knowledge_base_id')->comment('知识库ID');
$table->integer('priority')->default(0)->comment('优先级');
$table->timestamps();
// 添加外键约束
$table->foreign('terminal_id')
->references('id')
->on('terminals')
->onDelete('cascade');
// 添加唯一索引,确保同一终端不会重复关联同一知识库
$table->unique(['terminal_id', 'knowledge_base_id'], 'uk_terminal_kb');
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('terminal_knowledge_bases');
}
};

View File

@@ -0,0 +1,24 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
public function up(): void
{
Schema::create('knowledge_base_station', function (Blueprint $table) {
$table->id();
$table->foreignId('station_id')->constrained()->cascadeOnDelete();
$table->foreignId('knowledge_base_id')->constrained()->cascadeOnDelete();
$table->timestamps();
$table->unique(['station_id', 'knowledge_base_id']);
});
}
public function down(): void
{
Schema::dropIfExists('knowledge_base_station');
}
};

View File

@@ -1,109 +0,0 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Spatie\Permission\Models\Permission;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
// 权限名称映射(旧名称 => 新名称)
$permissionMapping = [
// 文档管理
'document.viewAny' => 'document.view',
// 系统设置
'system-setting.viewAny' => 'system-setting.view',
// 操作日志
'activity-log.viewAny' => 'activity-log.view',
// 终端管理
'terminal.viewAny' => 'terminal.view',
// 操作指引
'guide.viewAny' => 'guide.view',
// 分组管理
'group.viewAny' => 'group.view',
// 用户管理
'user.viewAny' => 'user.view',
// 角色管理
'role.viewAny' => 'role.view',
];
foreach ($permissionMapping as $oldName => $newName) {
$oldPermission = Permission::where('name', $oldName)->first();
if ($oldPermission) {
// 检查新权限是否已存在
$newPermission = Permission::where('name', $newName)->first();
if (!$newPermission) {
// 如果新权限不存在,直接重命名
$oldPermission->name = $newName;
$oldPermission->save();
} else {
// 如果新权限已存在,需要合并权限关联
// 将旧权限的所有角色关联转移到新权限
$roles = $oldPermission->roles;
foreach ($roles as $role) {
if (!$role->hasPermissionTo($newName)) {
$role->givePermissionTo($newName);
}
}
// 将旧权限的所有用户关联转移到新权限
$users = $oldPermission->users;
foreach ($users as $user) {
if (!$user->hasPermissionTo($newName)) {
$user->givePermissionTo($newName);
}
}
// 删除旧权限
$oldPermission->delete();
}
}
}
// 删除不再需要的详情查看权限
$detailPermissions = [
'document.view',
'system-setting.view',
'activity-log.view',
'terminal.view',
'guide.view',
'group.view',
'user.view',
'role.view',
];
foreach ($detailPermissions as $permName) {
// 只删除描述为"查看xxx详情"的权限(如果存在重复)
$permissions = Permission::where('name', $permName)->get();
if ($permissions->count() > 1) {
// 保留第一个,删除其他
$permissions->skip(1)->each(function ($permission) {
$permission->delete();
});
}
}
// 清除权限缓存
app()[\Spatie\Permission\PermissionRegistrar::class]->forgetCachedPermissions();
}
/**
* Reverse the migrations.
*/
public function down(): void
{
// 不支持回滚,因为权限合并后无法准确还原
}
};

View File

@@ -40,20 +40,19 @@ return new class extends Migration
$table->index(['guide_id', 'sort_order']);
});
Schema::create('terminal_guides', function (Blueprint $table) {
Schema::create('guide_station', function (Blueprint $table) {
$table->id();
$table->foreignId('terminal_id')->constrained()->cascadeOnDelete();
$table->foreignId('station_id')->constrained()->cascadeOnDelete();
$table->foreignId('guide_id')->constrained()->cascadeOnDelete();
$table->integer('priority')->default(0);
$table->timestamps();
$table->unique(['terminal_id', 'guide_id'], 'uk_terminal_guide');
$table->unique(['station_id', 'guide_id']);
});
}
public function down(): void
{
Schema::dropIfExists('terminal_guides');
Schema::dropIfExists('guide_station');
Schema::dropIfExists('guide_pages');
Schema::dropIfExists('guides');
}

View File

@@ -1,29 +0,0 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
public function up(): void
{
Schema::table('documents', function (Blueprint $table) {
$table->foreignId('knowledge_base_id')
->nullable()
->after('group_id')
->constrained('knowledge_bases')
->nullOnDelete();
$table->index('knowledge_base_id');
});
}
public function down(): void
{
Schema::table('documents', function (Blueprint $table) {
$table->dropForeign(['knowledge_base_id']);
$table->dropColumn('knowledge_base_id');
});
}
};

View File

@@ -2,8 +2,7 @@
namespace Database\Seeders;
use App\Models\Document;
use App\Models\Group;
use App\Models\Station;
use App\Models\User;
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
@@ -13,231 +12,79 @@ class DatabaseSeeder extends Seeder
{
use WithoutModelEvents;
/**
* Seed the application's database.
*/
public function run(): void
{
$this->command->info('开始生成演示数据...');
// 0. 创建权限和角色(必须最先执行)
// 0. 权限和角色
$this->call(PermissionSeeder::class);
// 1. 创建系统设置
// 1. 系统设置
$this->call(SystemSettingSeeder::class);
// 2. 创建管理员用户
$this->command->info('创建管理员用户...');
// 2. 创建用户
$pass = 'TRG}E^5BvPcbyErc';
$this->command->info('创建用户...');
$admin = User::factory()->create([
'name' => '系统管理员',
'email' => 'admin@example.com',
'password' => Hash::make('TRG}E^5BvPcbyErc'),
'password' => Hash::make($pass),
]);
// 为管理员分配 super-admin 角色
$admin->assignRole('super-admin');
// 3. 创建普通用户
$this->command->info('创建普通用户...');
$user1 = User::factory()->create([
'name' => '张三',
'email' => 'zhangsan@example.com',
'password' => Hash::make('TRG}E^5BvPcbyErc'),
'password' => Hash::make($pass),
]);
$user1->assignRole('user');
$user2 = User::factory()->create([
'name' => '李四',
'email' => 'lisi@example.com',
'password' => Hash::make('TRG}E^5BvPcbyErc'),
'password' => Hash::make($pass),
]);
$user2->assignRole('user');
$user3 = User::factory()->create([
'name' => '王五',
'email' => 'wangwu@example.com',
'password' => Hash::make('TRG}E^5BvPcbyErc'),
'password' => Hash::make($pass),
]);
$user3->assignRole('admin');
$user4 = User::factory()->create([
'name' => '赵六',
'email' => 'zhaoliu@example.com',
'password' => Hash::make('TRG}E^5BvPcbyErc'),
'password' => Hash::make($pass),
]);
$user4->assignRole('user');
// 3. 创建分组(按照光束线)
$this->command->info('创建光束线分组...');
$beamlines = [
'BL02U1',
'BL07U',
'BL08U',
'BL13HB',
'BL13U',
'BL14B',
'BL14W',
'BL15U',
'BL16B',
'BL16U1',
];
$groups = [];
foreach ($beamlines as $beamline) {
$groups[$beamline] = Group::factory()->create([
'name' => $beamline,
'description' => null,
]);
}
// 4. 建立用户和分组的关联关系
$this->command->info('建立用户和分组的关联关系...');
// 管理员属于所有光束线分组
$admin->groups()->attach(array_column($groups, 'id'));
// 张三和李四属于 BL02U1
$user1->groups()->attach($groups['BL02U1']->id);
$user2->groups()->attach($groups['BL02U1']->id);
// 王五属于 BL07U
$user3->groups()->attach($groups['BL07U']->id);
// 赵六属于 BL08U
$user4->groups()->attach($groups['BL08U']->id);
// 5. 创建全局文档(仅元数据,不创建实际文件)
$this->command->info('创建全局文档...');
Document::create([
'title' => '公司员工手册',
'description' => '包含公司规章制度、员工福利、考勤制度等重要信息',
'file_path' => 'documents/' . date('Y/m/d') . '/' . fake()->uuid() . '.docx',
'file_name' => '员工手册_2024.docx',
'file_size' => fake()->numberBetween(100000, 500000),
'mime_type' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
'type' => 'global',
'group_id' => null,
'uploaded_by' => $admin->id,
'conversion_status' => 'pending',
]);
Document::create([
'title' => '办公室使用规范',
'description' => '办公室设施使用规范和注意事项',
'file_path' => 'documents/' . date('Y/m/d') . '/' . fake()->uuid() . '.docx',
'file_name' => '办公室规范.docx',
'file_size' => fake()->numberBetween(100000, 500000),
'mime_type' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
'type' => 'global',
'group_id' => null,
'uploaded_by' => $admin->id,
'conversion_status' => 'pending',
]);
Document::create([
'title' => '公司年度总结报告',
'description' => '2024年度公司发展总结和2025年规划',
'file_path' => 'documents/' . date('Y/m/d') . '/' . fake()->uuid() . '.docx',
'file_name' => '年度总结_2024.docx',
'file_size' => fake()->numberBetween(100000, 500000),
'mime_type' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
'type' => 'global',
'group_id' => null,
'uploaded_by' => $admin->id,
'conversion_status' => 'pending',
]);
Document::create([
'title' => '安全管理制度',
'description' => '公司信息安全和物理安全管理制度',
'file_path' => 'documents/' . date('Y/m/d') . '/' . fake()->uuid() . '.docx',
'file_name' => '安全管理制度.docx',
'file_size' => fake()->numberBetween(100000, 500000),
'mime_type' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
'type' => 'global',
'group_id' => null,
'uploaded_by' => $admin->id,
'conversion_status' => 'pending',
]);
// 6. 创建 BL02U1 光束线专用文档
$this->command->info('创建 BL02U1 光束线专用文档...');
Document::create([
'title' => 'BL02U1 操作手册',
'description' => 'BL02U1 光束线设备操作规程和注意事项',
'file_path' => 'documents/' . date('Y/m/d') . '/' . fake()->uuid() . '.docx',
'file_name' => 'BL02U1_操作手册.docx',
'file_size' => fake()->numberBetween(100000, 500000),
'mime_type' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
'type' => 'dedicated',
'group_id' => $groups['BL02U1']->id,
'uploaded_by' => $user1->id,
'conversion_status' => 'pending',
]);
Document::create([
'title' => 'BL02U1 维护记录',
'description' => 'BL02U1 光束线设备维护和检修记录',
'file_path' => 'documents/' . date('Y/m/d') . '/' . fake()->uuid() . '.docx',
'file_name' => 'BL02U1_维护记录.docx',
'file_size' => fake()->numberBetween(100000, 500000),
'mime_type' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
'type' => 'dedicated',
'group_id' => $groups['BL02U1']->id,
'uploaded_by' => $user2->id,
'conversion_status' => 'pending',
]);
// 7. 创建 BL07U 光束线专用文档
$this->command->info('创建 BL07U 光束线专用文档...');
Document::create([
'title' => 'BL07U 操作手册',
'description' => 'BL07U 光束线设备操作规程和注意事项',
'file_path' => 'documents/' . date('Y/m/d') . '/' . fake()->uuid() . '.docx',
'file_name' => 'BL07U_操作手册.docx',
'file_size' => fake()->numberBetween(100000, 500000),
'mime_type' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
'type' => 'dedicated',
'group_id' => $groups['BL07U']->id,
'uploaded_by' => $user3->id,
'conversion_status' => 'pending',
]);
// 8. 创建 BL08U 光束线专用文档
$this->command->info('创建 BL08U 光束线专用文档...');
Document::create([
'title' => 'BL08U 操作手册',
'description' => 'BL08U 光束线设备操作规程和注意事项',
'file_path' => 'documents/' . date('Y/m/d') . '/' . fake()->uuid() . '.docx',
'file_name' => 'BL08U_操作手册.docx',
'file_size' => fake()->numberBetween(100000, 500000),
'mime_type' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
'type' => 'dedicated',
'group_id' => $groups['BL08U']->id,
'uploaded_by' => $user4->id,
'conversion_status' => 'pending',
]);
$this->command->info('演示数据生成完成!');
// 9. 创建终端数据
// 3. 创建线站、知识库、终端
$this->call(TerminalSeeder::class);
// 10. 创建操作指引数据
// 4. 用户关联线站(管理员不关联 = 可访问全部)
$this->command->info('建立用户-线站关联...');
$bl02u1 = Station::where('name', 'BL02U1')->first();
$bl07u = Station::where('name', 'BL07U')->first();
$bl08u = Station::where('name', 'BL08U')->first();
$user1->stations()->attach($bl02u1);
$user2->stations()->attach($bl02u1);
$user3->stations()->attach($bl07u);
$user4->stations()->attach($bl08u);
// 5. 操作指引
$this->call(GuideSeeder::class);
$this->command->newLine();
$this->command->info('=== 生成的数据摘要 ===');
$this->command->info('用户数量: ' . User::count());
$this->command->info('分组数量: ' . Group::count());
$this->command->info('文档数量: ' . Document::count());
$this->command->info(' - 全局文档: ' . Document::global()->count());
$this->command->info(' - 专用文档: ' . Document::dedicated()->count());
$this->command->newLine();
$this->command->info('=== 测试账号信息 ===');
$this->command->info('超级管理员: admin@example.com / TRG}E^5BvPcbyErc (super-admin)');
$this->command->info('张三BL02U1: zhangsan@example.com / TRG}E^5BvPcbyErc (user)');
$this->command->info('李四BL02U1: lisi@example.com / TRG}E^5BvPcbyErc (user)');
$this->command->info('王五BL07U: wangwu@example.com / TRG}E^5BvPcbyErc (admin)');
$this->command->info('赵六BL08U: zhaoliu@example.com / TRG}E^5BvPcbyErc (user)');
$this->command->info("{$admin->name} : {$admin->email} / $pass");
$this->command->info("{$user1->name}{$bl02u1->name}: {$user1->email} / $pass");
$this->command->info("{$user2->name}{$bl02u1->name}: {$user2->email} / $pass");
$this->command->info("{$user3->name}{$bl07u->name}: {$user3->email} / $pass");
$this->command->info("{$user4->name}{$bl08u->name}: {$user4->email} / $pass");
}
}

View File

@@ -4,7 +4,7 @@ namespace Database\Seeders;
use App\Models\Guide;
use App\Models\GuidePage;
use App\Models\Terminal;
use App\Models\Station;
use App\Models\User;
use Illuminate\Database\Seeder;
@@ -20,7 +20,7 @@ class GuideSeeder extends Seeder
$this->command->info('开始创建操作指引数据...');
$admin = User::where('email', 'admin@example.com')->first();
$terminals = Terminal::all();
$stations = Station::all();
// 1. 如何用光(带分支)
$guide1 = $this->createHowToUseBeamGuide($admin);
@@ -31,20 +31,17 @@ class GuideSeeder extends Seeder
// 3. 漏水报警处理
$guide3 = $this->createWaterLeakAlarmGuide($admin);
// 将所有指引关联到所有终端
$this->command->info('关联指引到所有终端...');
foreach ($terminals as $terminal) {
$terminal->guides()->attach([
$guide1->id => ['priority' => 1],
$guide2->id => ['priority' => 2],
$guide3->id => ['priority' => 3],
]);
}
// 将指引关联到所有线站(非全局,关联所有线站 = 各线站均可见)
$this->command->info('关联指引到所有线站...');
$stationIds = $stations->pluck('id')->toArray();
$guide1->stations()->attach($stationIds);
$guide2->stations()->attach($stationIds);
$guide3->stations()->attach($stationIds);
$this->command->info('操作指引数据创建完成!');
$this->command->info(' - 指引数量: ' . Guide::count());
$this->command->info(' - 指引页面数量: ' . GuidePage::count());
$this->command->info(' - 关联终端数量: ' . $terminals->count());
$this->command->info(' - 关联线站数量: ' . $stations->count());
}
private function createHowToUseBeamGuide(User $admin): Guide

View File

@@ -46,11 +46,11 @@ class PermissionSeeder extends Seeder
'guide.publish' => '发布指引',
'guide.archive' => '归档指引',
// 分组管理权限
'group.view' => '查看分组',
'group.create' => '创建分组',
'group.update' => '编辑分组',
'group.delete' => '删除分组',
// 线站管理权限
'station.view' => '查看线站',
'station.create' => '创建线站',
'station.update' => '编辑线站',
'station.delete' => '删除线站',
// 用户管理权限
'user.view' => '查看用户',
@@ -126,6 +126,12 @@ class PermissionSeeder extends Seeder
'terminal.update',
'terminal.delete',
// 线站管理
'station.view',
'station.create',
'station.update',
'station.delete',
// 操作指引
'guide.view',
'guide.create',
@@ -134,12 +140,6 @@ class PermissionSeeder extends Seeder
'guide.publish',
'guide.archive',
// 分组管理
'group.view',
'group.create',
'group.update',
'group.delete',
// 用户管理
'user.view',
'user.create',
@@ -167,14 +167,14 @@ class PermissionSeeder extends Seeder
'document.create',
'document.download',
// 终端管理(仅查看
// 终端查看
'terminal.view',
// 操作指引(仅查看
'guide.view',
// 线站查看
'station.view',
// 分组管理(仅查看
'group.view',
// 操作指引查看
'guide.view',
];
$role->givePermissionTo($permissions);

View File

@@ -2,20 +2,18 @@
namespace Database\Seeders;
use App\Models\KnowledgeBase;
use App\Models\Station;
use App\Models\Terminal;
use App\Models\TerminalPrompt;
use Illuminate\Database\Seeder;
class TerminalSeeder extends Seeder
{
/**
* Run the database seeds.
*/
public function run(): void
{
$this->command->info('开始创建终端数据...');
$this->command->info('开始创建线站、知识库和终端...');
// 光束线列表
$beamlines = [
'BL02U1' => '192.168.1.21',
'BL07U' => '192.168.1.27',
@@ -29,19 +27,32 @@ class TerminalSeeder extends Seeder
'BL16U1' => '192.168.1.39',
];
// 默认提示词模板占位符由HMI端替换
$defaultPrompt = <<<'PROMPT'
你是{station_id}光束线的AI助手。当前时间是{time}。请根据用户{user}的问题,提供准确的光束线操作指导、实验支持和技术咨询。你可以回答关于光束线参数、实验流程、设备状态、安全规范等方面的问题。
你是{station_name}光束线的AI助手(终端: {terminal_name} / {terminal_code}。当前时间是{time}。请根据用户{user}的问题,提供准确的光束线操作指导、实验支持和技术咨询。你可以回答关于光束线参数、实验流程、设备状态、安全规范等方面的问题。
PROMPT;
// 为每条光束线创建智慧屏终端
$this->command->info('创建光束线智慧屏终端...');
// 创建通用知识库(全局,不关联线站)
$this->command->info('创建通用知识库...');
KnowledgeBase::create(['name' => '通用知识库', 'description' => '全站通用的规章制度和管理文档', 'status' => 'active']);
foreach ($beamlines as $beamline => $ipAddress) {
// 创建线站
$station = Station::firstOrCreate(['name' => $beamline]);
// 创建线站专属知识库并关联
$kb = KnowledgeBase::create([
'name' => "{$beamline} 知识库",
'description' => "{$beamline} 光束线专用文档",
'status' => 'active',
]);
$station->knowledgeBases()->attach($kb);
// 创建终端
$terminal = Terminal::create([
'name' => "{$beamline} 智慧屏",
'code' => "SCREEN-{$beamline}",
'ip_address' => $ipAddress,
'station_id' => $beamline,
'station_id' => $station->id,
'diagram_url' => 'https://ssrf.9z.work/scada/demo.html',
'is_online' => in_array($beamline, ['BL02U1', 'BL07U', 'BL08U', 'BL13U', 'BL15U']),
'last_online_at' => in_array($beamline, ['BL02U1', 'BL07U', 'BL08U', 'BL13U', 'BL15U'])
@@ -49,7 +60,6 @@ PROMPT;
: now()->subHours(rand(1, 24)),
]);
// 为每个终端创建提示词
TerminalPrompt::create([
'terminal_id' => $terminal->id,
'prompt_template' => $defaultPrompt,
@@ -57,12 +67,9 @@ PROMPT;
]);
}
$this->command->info('终端数据创建完成!');
$this->command->newLine();
$this->command->info('=== 生成的终端摘要 ===');
$this->command->info('总终端数量: ' . Terminal::count());
$this->command->info(' - 在线终端: ' . Terminal::where('is_online', true)->count());
$this->command->info(' - 离线终端: ' . Terminal::where('is_online', false)->count());
$this->command->info(' - 配置提示词的终端: ' . TerminalPrompt::count());
$this->command->info('线站/知识库/终端创建完成!');
$this->command->info(' - 线站: ' . Station::count());
$this->command->info(' - 知识库: ' . KnowledgeBase::count());
$this->command->info(' - 终端: ' . Terminal::count());
}
}