refactor(ticket): centralize thread mutations
parent
281c36e990
commit
f97e935fc0
@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Ticket;
|
||||
|
||||
final readonly class OpenTicketCommand
|
||||
{
|
||||
public function __construct(
|
||||
public int $actorId,
|
||||
public string $title,
|
||||
public string $content,
|
||||
public string $type,
|
||||
public string $priority,
|
||||
public ?int $instanceId,
|
||||
public mixed $request
|
||||
) {
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Ticket;
|
||||
|
||||
final readonly class ReplyTicketCommand
|
||||
{
|
||||
public function __construct(
|
||||
public int $ticketId,
|
||||
public int $actorId,
|
||||
public string $content,
|
||||
public int $status,
|
||||
public mixed $request,
|
||||
public ?int $ownerId = null,
|
||||
public bool $actorIsAdmin = false
|
||||
) {
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,149 @@
|
||||
<?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;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Ticket;
|
||||
|
||||
use App\Models\Ticket;
|
||||
|
||||
final readonly class TicketWriteResult
|
||||
{
|
||||
public function __construct(
|
||||
public Ticket $root,
|
||||
public Ticket $message,
|
||||
public string $event,
|
||||
public array $attachments,
|
||||
public bool $wasCreated = true
|
||||
) {
|
||||
}
|
||||
}
|
||||
@ -1,103 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use App\Models\Ticket;
|
||||
|
||||
/**
|
||||
* Atomically append a message to an existing ticket thread.
|
||||
*
|
||||
* A ticket thread has one root row whose status is the source of truth. The
|
||||
* root row is therefore selected with a row lock inside the same transaction
|
||||
* that writes the reply and its attachment rows. Filesystem paths created by
|
||||
* the attachment writer are explicitly discarded if the transaction cannot
|
||||
* commit; a database rollback alone cannot remove those files.
|
||||
*/
|
||||
final class TicketReplyService
|
||||
{
|
||||
/**
|
||||
* @param mixed $request PSR-7 request, including uploaded files
|
||||
* @param int|null $ownerId Restrict the root to this user for user replies
|
||||
* @return array{root: Ticket, reply: Ticket, event: string, attachments: array}|null
|
||||
*/
|
||||
public static function create(
|
||||
int $ticketId,
|
||||
int $actorId,
|
||||
string $content,
|
||||
int $status,
|
||||
$request,
|
||||
?int $ownerId = null
|
||||
): ?array {
|
||||
if (!in_array($status, [0, 1], true)) {
|
||||
throw new \InvalidArgumentException('工单状态无效');
|
||||
}
|
||||
|
||||
$connection = (new Ticket())->getConnection();
|
||||
$createdPaths = [];
|
||||
try {
|
||||
return $connection->transaction(function () use (
|
||||
$ticketId,
|
||||
$actorId,
|
||||
$content,
|
||||
$status,
|
||||
$request,
|
||||
$ownerId,
|
||||
&$createdPaths
|
||||
) {
|
||||
// This query must be inside the transaction. A lock acquired
|
||||
// in autocommit mode would be released before the reply save.
|
||||
$query = Ticket::where('id', $ticketId)->where('rootid', 0);
|
||||
if ($ownerId !== null) {
|
||||
$query->where('userid', $ownerId);
|
||||
}
|
||||
$root = $query->lockForUpdate()->first();
|
||||
if ($root === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$event = $status === 1 && (int) $root->status !== 1
|
||||
? '工单重新开启'
|
||||
: '用户回复工单';
|
||||
|
||||
$reply = new Ticket();
|
||||
$reply->title = (string) $root->title;
|
||||
$reply->content = $content;
|
||||
$reply->rootid = (int) $root->id;
|
||||
$reply->userid = $actorId;
|
||||
$reply->datetime = time();
|
||||
$reply->status = $status;
|
||||
$reply->ticket_type = (string) $root->ticket_type;
|
||||
$reply->priority = (string) $root->priority;
|
||||
$reply->instance_id = $root->instance_id;
|
||||
|
||||
$root->status = $status;
|
||||
if (!$root->save() || !$reply->save()) {
|
||||
throw new \RuntimeException('工单回复保存失败');
|
||||
}
|
||||
|
||||
// Keep attachment rows in this transaction. The service also
|
||||
// records every newly-created path for commit-failure cleanup.
|
||||
$attachments = TicketAttachmentService::store(
|
||||
$request,
|
||||
(int) $root->id,
|
||||
(int) $reply->id,
|
||||
$actorId,
|
||||
$createdPaths
|
||||
);
|
||||
|
||||
return [
|
||||
'root' => $root,
|
||||
'reply' => $reply,
|
||||
'event' => $event,
|
||||
'attachments' => $attachments,
|
||||
];
|
||||
}, 1);
|
||||
} catch (\Throwable $exception) {
|
||||
// The DB transaction has already rolled back (or failed to
|
||||
// commit) when control reaches here. Only remove files created by
|
||||
// this request; never delete pre-existing ticket attachments.
|
||||
TicketAttachmentService::discardPaths($createdPaths);
|
||||
throw $exception;
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue