feat(阶段四): 实现 SOP 模板状态管理功能

- 创建发布 Action(PublishSopTemplateAction)
- 创建归档 Action(ArchiveSopTemplateAction)
- 创建预览 Action(PreviewSopTemplateAction)
- 发布前验证模板是否有步骤
- 发布时自动创建版本快照
- 预览模式显示完整模板内容
This commit is contained in:
2026-03-09 13:24:14 +08:00
parent c4ab592fd5
commit 6102ec95d2
3 changed files with 225 additions and 0 deletions

View File

@@ -0,0 +1,50 @@
<?php
namespace App\Filament\Actions;
use App\Models\SopTemplate;
use Filament\Actions\Action;
use Filament\Notifications\Notification;
class ArchiveSopTemplateAction extends Action
{
public static function getDefaultName(): ?string
{
return 'archive';
}
protected function setUp(): void
{
parent::setUp();
$this->label('归档模板');
$this->icon('heroicon-o-archive-box');
$this->color('warning');
$this->requiresConfirmation();
$this->modalHeading('归档 SOP 模板');
$this->modalDescription('归档后,模板将不再对用户显示。确定要归档吗?');
$this->modalSubmitActionLabel('确认归档');
$this->visible(function ($record) {
return $record instanceof SopTemplate && $record->status === 'published';
});
$this->action(function (SopTemplate $record) {
$record->update([
'status' => 'archived',
]);
Notification::make()
->title('归档成功')
->body('SOP 模板已成功归档')
->success()
->send();
});
}
}

View File

@@ -0,0 +1,90 @@
<?php
namespace App\Filament\Actions;
use App\Models\SopTemplate;
use Filament\Actions\Action;
use Filament\Infolists;
use Filament\Infolists\Infolist;
class PreviewSopTemplateAction extends Action
{
public static function getDefaultName(): ?string
{
return 'preview';
}
protected function setUp(): void
{
parent::setUp();
$this->label('预览模板');
$this->icon('heroicon-o-eye');
$this->color('info');
$this->modalHeading('SOP 模板预览');
$this->modalWidth('7xl');
$this->modalSubmitAction(false);
$this->modalCancelActionLabel('关闭');
$this->infolist(function (SopTemplate $record): Infolist {
return Infolist::make()
->record($record)
->schema([
Infolists\Components\Section::make('模板信息')
->schema([
Infolists\Components\TextEntry::make('name')
->label('模板名称')
->size(Infolists\Components\TextEntry\TextEntrySize::Large)
->weight('bold'),
Infolists\Components\TextEntry::make('description')
->label('模板描述')
->columnSpanFull(),
Infolists\Components\TextEntry::make('category')
->label('分类')
->badge(),
Infolists\Components\TextEntry::make('version')
->label('版本'),
])
->columns(2),
Infolists\Components\Section::make('操作步骤')
->schema([
Infolists\Components\RepeatableEntry::make('steps')
->label('')
->schema([
Infolists\Components\TextEntry::make('step_number')
->label('步骤')
->badge()
->color('primary'),
Infolists\Components\TextEntry::make('title')
->label('标题')
->weight('bold'),
Infolists\Components\TextEntry::make('content')
->label('内容')
->html()
->columnSpanFull(),
Infolists\Components\TextEntry::make('is_required')
->label('必需')
->badge()
->formatStateUsing(fn (bool $state): string => $state ? '是' : '否')
->color(fn (bool $state): string => $state ? 'success' : 'gray'),
])
->columns(3)
->contained(false),
]),
]);
});
}
}

View File

@@ -0,0 +1,85 @@
<?php
namespace App\Filament\Actions;
use App\Models\SopTemplate;
use App\Models\SopTemplateVersion;
use Filament\Actions\Action;
use Filament\Forms;
use Filament\Notifications\Notification;
class PublishSopTemplateAction extends Action
{
public static function getDefaultName(): ?string
{
return 'publish';
}
protected function setUp(): void
{
parent::setUp();
$this->label('发布模板');
$this->icon('heroicon-o-check-circle');
$this->color('success');
$this->requiresConfirmation();
$this->modalHeading('发布 SOP 模板');
$this->modalDescription('发布后,模板将对所有用户可见。确定要发布吗?');
$this->modalSubmitActionLabel('确认发布');
$this->form([
Forms\Components\Textarea::make('change_log')
->label('变更说明')
->placeholder('请描述本次发布的主要变更内容...')
->rows(3),
]);
$this->visible(function ($record) {
return $record instanceof SopTemplate && $record->status === 'draft';
});
$this->action(function (SopTemplate $record, array $data) {
// 验证模板是否有步骤
if ($record->steps()->count() === 0) {
Notification::make()
->title('发布失败')
->body('模板至少需要包含一个步骤才能发布')
->danger()
->send();
return;
}
// 创建版本快照
SopTemplateVersion::create([
'sop_template_id' => $record->id,
'version' => $record->version,
'change_log' => $data['change_log'] ?? '首次发布',
'content_snapshot' => [
'template' => $record->toArray(),
'steps' => $record->steps->toArray(),
],
'created_by' => auth()->id(),
'created_at' => now(),
]);
// 更新模板状态
$record->update([
'status' => 'published',
'published_at' => now(),
]);
Notification::make()
->title('发布成功')
->body('SOP 模板已成功发布')
->success()
->send();
});
}
}