feat(阶段四): 创建 SOP 模板服务类
- 实现 exportToJson 方法(导出为 JSON) - 实现 importFromJson 方法(从 JSON 导入) - 实现 publish 方法(发布模板) - 实现 createVersion 方法(创建版本快照) - 实现 archive 方法(归档模板) - 实现 duplicate 方法(复制模板) - 导入时验证数据结构和必需字段 - 复制时包含所有步骤和交互任务
This commit is contained in:
222
app/Services/SopTemplateService.php
Normal file
222
app/Services/SopTemplateService.php
Normal file
@@ -0,0 +1,222 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use App\Models\SopTemplate;
|
||||
use App\Models\SopTemplateVersion;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
|
||||
class SopTemplateService
|
||||
{
|
||||
/**
|
||||
* 导出模板为 JSON 格式
|
||||
*
|
||||
* @param SopTemplate $template
|
||||
* @return string
|
||||
*/
|
||||
public function exportToJson(SopTemplate $template): string
|
||||
{
|
||||
$data = [
|
||||
'template' => [
|
||||
'name' => $template->name,
|
||||
'description' => $template->description,
|
||||
'category' => $template->category,
|
||||
'tags' => $template->tags,
|
||||
'version' => $template->version,
|
||||
'applicable_departments' => $template->applicable_departments,
|
||||
'applicable_positions' => $template->applicable_positions,
|
||||
],
|
||||
'steps' => $template->steps->map(function ($step) {
|
||||
return [
|
||||
'step_number' => $step->step_number,
|
||||
'title' => $step->title,
|
||||
'content' => $step->content,
|
||||
'sort_order' => $step->sort_order,
|
||||
'is_required' => $step->is_required,
|
||||
'interactive_tasks' => $step->interactiveTasks->map(function ($task) {
|
||||
return [
|
||||
'task_type' => $task->task_type,
|
||||
'task_config' => $task->task_config,
|
||||
'validation_rules' => $task->validation_rules,
|
||||
'timeout_seconds' => $task->timeout_seconds,
|
||||
'is_required' => $task->is_required,
|
||||
];
|
||||
})->toArray(),
|
||||
];
|
||||
})->toArray(),
|
||||
'exported_at' => now()->toIso8601String(),
|
||||
'exported_by' => auth()->user()?->name,
|
||||
];
|
||||
|
||||
return json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
|
||||
}
|
||||
|
||||
/**
|
||||
* 从 JSON 导入模板
|
||||
*
|
||||
* @param string $json
|
||||
* @return SopTemplate
|
||||
* @throws ValidationException
|
||||
*/
|
||||
public function importFromJson(string $json): SopTemplate
|
||||
{
|
||||
$data = json_decode($json, true);
|
||||
|
||||
if (json_last_error() !== JSON_ERROR_NONE) {
|
||||
throw ValidationException::withMessages([
|
||||
'file' => ['无效的 JSON 格式'],
|
||||
]);
|
||||
}
|
||||
|
||||
// 验证数据结构
|
||||
$validator = Validator::make($data, [
|
||||
'template' => 'required|array',
|
||||
'template.name' => 'required|string|max:255',
|
||||
'template.version' => 'required|string|max:50',
|
||||
'steps' => 'required|array|min:1',
|
||||
'steps.*.step_number' => 'required|integer',
|
||||
'steps.*.title' => 'required|string|max:255',
|
||||
]);
|
||||
|
||||
if ($validator->fails()) {
|
||||
throw new ValidationException($validator);
|
||||
}
|
||||
|
||||
return DB::transaction(function () use ($data) {
|
||||
// 创建模板
|
||||
$template = SopTemplate::create([
|
||||
'name' => $data['template']['name'],
|
||||
'description' => $data['template']['description'] ?? null,
|
||||
'category' => $data['template']['category'] ?? null,
|
||||
'tags' => $data['template']['tags'] ?? [],
|
||||
'version' => $data['template']['version'],
|
||||
'status' => 'draft',
|
||||
'applicable_departments' => $data['template']['applicable_departments'] ?? [],
|
||||
'applicable_positions' => $data['template']['applicable_positions'] ?? [],
|
||||
'created_by' => auth()->id(),
|
||||
]);
|
||||
|
||||
// 创建步骤
|
||||
foreach ($data['steps'] as $stepData) {
|
||||
$step = $template->steps()->create([
|
||||
'step_number' => $stepData['step_number'],
|
||||
'title' => $stepData['title'],
|
||||
'content' => $stepData['content'] ?? null,
|
||||
'sort_order' => $stepData['sort_order'] ?? $stepData['step_number'],
|
||||
'is_required' => $stepData['is_required'] ?? true,
|
||||
]);
|
||||
|
||||
// 创建交互任务
|
||||
if (!empty($stepData['interactive_tasks'])) {
|
||||
foreach ($stepData['interactive_tasks'] as $taskData) {
|
||||
$step->interactiveTasks()->create([
|
||||
'task_type' => $taskData['task_type'],
|
||||
'task_config' => $taskData['task_config'] ?? [],
|
||||
'validation_rules' => $taskData['validation_rules'] ?? [],
|
||||
'timeout_seconds' => $taskData['timeout_seconds'] ?? null,
|
||||
'is_required' => $taskData['is_required'] ?? true,
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $template;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 发布模板
|
||||
*
|
||||
* @param SopTemplate $template
|
||||
* @param string|null $changeLog
|
||||
* @return void
|
||||
*/
|
||||
public function publish(SopTemplate $template, ?string $changeLog = null): void
|
||||
{
|
||||
// 创建版本快照
|
||||
$this->createVersion($template, $changeLog);
|
||||
|
||||
// 更新状态
|
||||
$template->update([
|
||||
'status' => 'published',
|
||||
'published_at' => now(),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建版本快照
|
||||
*
|
||||
* @param SopTemplate $template
|
||||
* @param string|null $changeLog
|
||||
* @return SopTemplateVersion
|
||||
*/
|
||||
public function createVersion(SopTemplate $template, ?string $changeLog = null): SopTemplateVersion
|
||||
{
|
||||
return SopTemplateVersion::create([
|
||||
'sop_template_id' => $template->id,
|
||||
'version' => $template->version,
|
||||
'change_log' => $changeLog ?? '版本快照',
|
||||
'content_snapshot' => [
|
||||
'template' => $template->toArray(),
|
||||
'steps' => $template->steps->map(function ($step) {
|
||||
return array_merge($step->toArray(), [
|
||||
'interactive_tasks' => $step->interactiveTasks->toArray(),
|
||||
]);
|
||||
})->toArray(),
|
||||
],
|
||||
'created_by' => auth()->id(),
|
||||
'created_at' => now(),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 归档模板
|
||||
*
|
||||
* @param SopTemplate $template
|
||||
* @return void
|
||||
*/
|
||||
public function archive(SopTemplate $template): void
|
||||
{
|
||||
$template->update([
|
||||
'status' => 'archived',
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 复制模板
|
||||
*
|
||||
* @param SopTemplate $template
|
||||
* @param string $newName
|
||||
* @return SopTemplate
|
||||
*/
|
||||
public function duplicate(SopTemplate $template, string $newName): SopTemplate
|
||||
{
|
||||
return DB::transaction(function () use ($template, $newName) {
|
||||
// 复制模板
|
||||
$newTemplate = $template->replicate();
|
||||
$newTemplate->name = $newName;
|
||||
$newTemplate->status = 'draft';
|
||||
$newTemplate->published_at = null;
|
||||
$newTemplate->created_by = auth()->id();
|
||||
$newTemplate->save();
|
||||
|
||||
// 复制步骤
|
||||
foreach ($template->steps as $step) {
|
||||
$newStep = $step->replicate();
|
||||
$newStep->sop_template_id = $newTemplate->id;
|
||||
$newStep->save();
|
||||
|
||||
// 复制交互任务
|
||||
foreach ($step->interactiveTasks as $task) {
|
||||
$newTask = $task->replicate();
|
||||
$newTask->sop_step_id = $newStep->id;
|
||||
$newTask->save();
|
||||
}
|
||||
}
|
||||
|
||||
return $newTemplate;
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user