You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
virt/web/app/Services/Ticket/TicketThreadService.php

150 lines
5.7 KiB
PHP

<?php
namespace App\Services\Ticket;
use App\Models\CloudInstance;
use App\Models\Ticket;
use App\Services\TicketAttachmentService;
use voku\helper\AntiXSS;
/**
* Owns the complete ticket thread mutation contract.
*
* The HTTP adapters only translate requests and responses. Validation,
* sanitization, ownership, row locking, status transitions, attachment rows
* and rollback cleanup remain atomic behind this interface.
*/
final class TicketThreadService
{
public function open(OpenTicketCommand $command): TicketWriteResult
{
$title = $this->cleanRequired($command->title, '请填全工单主题和内容');
$content = $this->cleanRequired($command->content, '请填全工单主题和内容');
$type = in_array($command->type, ['pre_sales', 'after_sales', 'technical', 'finance'], true)
? $command->type : 'technical';
$priority = in_array($command->priority, ['high', 'medium', 'low'], true)
? $command->priority : 'medium';
$instanceId = $this->ownedInstanceId($command->actorId, $command->instanceId);
$connection = (new Ticket())->getConnection();
$createdPaths = [];
try {
return $connection->transaction(function () use (
$command,
$title,
$content,
$type,
$priority,
$instanceId,
&$createdPaths
): TicketWriteResult {
$ticket = new Ticket();
$ticket->title = $title;
$ticket->content = $content;
$ticket->rootid = 0;
$ticket->userid = $command->actorId;
$ticket->datetime = time();
$ticket->status = 1;
$ticket->ticket_type = $type;
$ticket->priority = $priority;
$ticket->instance_id = $instanceId;
if (!$ticket->save()) {
throw new \RuntimeException('工单保存失败');
}
$attachments = TicketAttachmentService::store(
$command->request,
(int) $ticket->id,
(int) $ticket->id,
$command->actorId,
$createdPaths
);
return new TicketWriteResult($ticket, $ticket, '新工单', $attachments);
}, 1);
} catch (\Throwable $exception) {
TicketAttachmentService::discardPaths($createdPaths);
throw $exception;
}
}
public function reply(ReplyTicketCommand $command): ?TicketWriteResult
{
if (!in_array($command->status, [0, 1], true)) {
throw new \InvalidArgumentException('工单状态无效');
}
$content = $this->cleanRequired($command->content, '请填写回复内容');
$connection = (new Ticket())->getConnection();
$createdPaths = [];
try {
return $connection->transaction(function () use (
$command,
$content,
&$createdPaths
): ?TicketWriteResult {
$query = Ticket::where('id', $command->ticketId)->where('rootid', 0);
if ($command->ownerId !== null) {
$query->where('userid', $command->ownerId);
}
$root = $query->lockForUpdate()->first();
if ($root === null) {
return null;
}
$event = $command->status === 1 && (int) $root->status !== 1
? '工单重新开启'
: ($command->actorIsAdmin ? '管理员回复工单' : '用户回复工单');
$reply = new Ticket();
$reply->title = (string) $root->title;
$reply->content = $content;
$reply->rootid = (int) $root->id;
$reply->userid = $command->actorId;
$reply->datetime = time();
$reply->status = $command->status;
$reply->ticket_type = (string) $root->ticket_type;
$reply->priority = (string) $root->priority;
$reply->instance_id = $root->instance_id;
$root->status = $command->status;
if (!$root->save() || !$reply->save()) {
throw new \RuntimeException('工单回复保存失败');
}
$attachments = TicketAttachmentService::store(
$command->request,
(int) $root->id,
(int) $reply->id,
$command->actorId,
$createdPaths
);
return new TicketWriteResult($root, $reply, $event, $attachments);
}, 1);
} catch (\Throwable $exception) {
TicketAttachmentService::discardPaths($createdPaths);
throw $exception;
}
}
private function cleanRequired(string $value, string $message): string
{
if (trim($value) === '') {
throw new \InvalidArgumentException($message);
}
$clean = trim((string) (new AntiXSS())->xss_clean($value));
if ($clean === '') {
throw new \InvalidArgumentException($message);
}
return $clean;
}
private function ownedInstanceId(int $userId, ?int $instanceId): ?int
{
if ($instanceId === null) {
return null;
}
$instance = CloudInstance::where('id', $instanceId)->where('user_id', $userId)
->where('status', '<>', 'deleted')->first();
if ($instance === null) {
throw new \InvalidArgumentException('关联实例不存在或无权访问');
}
return (int) $instance->id;
}
}