fix: ticket reply_status semantics, N+1 query, and admin reply auto-reopen
This commit is contained in:
@@ -40,7 +40,7 @@ class CheckTicket extends Command
|
||||
{
|
||||
Ticket::where('status', 0)
|
||||
->where('updated_at', '<=', time() - 24 * 3600)
|
||||
->where('reply_status', 0)
|
||||
->where('reply_status', Ticket::REPLY_STATUS_REPLIED)
|
||||
->lazyById(200)
|
||||
->each(function ($ticket) {
|
||||
if ($ticket->user_id === $ticket->last_reply_user_id) return;
|
||||
|
||||
@@ -55,6 +55,7 @@ class TicketController extends Controller
|
||||
if (!$ticket) {
|
||||
return $this->fail([400202, '工单不存在']);
|
||||
}
|
||||
$ticket->messages->each(fn($msg) => $msg->setRelation('ticket', $ticket));
|
||||
$result = $ticket->toArray();
|
||||
$result['user'] = UserController::transformUserData($ticket->user);
|
||||
|
||||
@@ -144,11 +145,12 @@ class TicketController extends Controller
|
||||
$ticket = Ticket::with([
|
||||
'user',
|
||||
'messages' => function ($query) {
|
||||
$query->with(['user']); // 如果需要用户信息
|
||||
$query->with(['user']);
|
||||
}
|
||||
])->findOrFail($ticketId);
|
||||
|
||||
// 自动包含 is_me 属性
|
||||
$ticket->messages->each(fn($msg) => $msg->setRelation('ticket', $ticket));
|
||||
|
||||
return response()->json([
|
||||
'data' => $ticket
|
||||
]);
|
||||
|
||||
@@ -39,6 +39,9 @@ class Ticket extends Model
|
||||
self::STATUS_CLOSED => '关闭'
|
||||
];
|
||||
|
||||
const REPLY_STATUS_WAITING = 0;
|
||||
const REPLY_STATUS_REPLIED = 1;
|
||||
|
||||
public function user(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(User::class, 'user_id', 'id');
|
||||
|
||||
@@ -29,6 +29,7 @@ class TicketMessage extends Model
|
||||
];
|
||||
|
||||
protected $appends = ['is_from_user', 'is_from_admin'];
|
||||
protected $hidden = ['ticket'];
|
||||
|
||||
/**
|
||||
* 关联的工单
|
||||
@@ -43,7 +44,7 @@ class TicketMessage extends Model
|
||||
*/
|
||||
public function getIsFromUserAttribute(): bool
|
||||
{
|
||||
return $this->ticket->user_id === $this->user_id;
|
||||
return $this->ticket && $this->ticket->user_id === $this->user_id;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -51,6 +52,6 @@ class TicketMessage extends Model
|
||||
*/
|
||||
public function getIsFromAdminAttribute(): bool
|
||||
{
|
||||
return $this->ticket->user_id !== $this->user_id;
|
||||
return $this->ticket && $this->ticket->user_id !== $this->user_id;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,11 +22,11 @@ class TicketService
|
||||
'ticket_id' => $ticket->id,
|
||||
'message' => $message
|
||||
]);
|
||||
if ($userId !== $ticket->user_id) {
|
||||
$ticket->reply_status = Ticket::STATUS_OPENING;
|
||||
} else {
|
||||
$ticket->reply_status = Ticket::STATUS_CLOSED;
|
||||
}
|
||||
$isAdmin = $userId !== $ticket->user_id;
|
||||
$ticket->reply_status = $isAdmin
|
||||
? Ticket::REPLY_STATUS_REPLIED
|
||||
: Ticket::REPLY_STATUS_WAITING;
|
||||
$ticket->last_reply_user_id = $userId;
|
||||
if (!$ticketMessage || !$ticket->save()) {
|
||||
throw new \Exception();
|
||||
}
|
||||
@@ -40,33 +40,15 @@ class TicketService
|
||||
|
||||
public function replyByAdmin($ticketId, $message, $userId): void
|
||||
{
|
||||
$ticket = Ticket::where('id', $ticketId)
|
||||
->first();
|
||||
$ticket = Ticket::where('id', $ticketId)->first();
|
||||
if (!$ticket) {
|
||||
throw new ApiException('工单不存在');
|
||||
}
|
||||
$ticket->status = Ticket::STATUS_OPENING;
|
||||
try {
|
||||
DB::beginTransaction();
|
||||
$ticketMessage = TicketMessage::create([
|
||||
'user_id' => $userId,
|
||||
'ticket_id' => $ticket->id,
|
||||
'message' => $message
|
||||
]);
|
||||
if ($userId !== $ticket->user_id) {
|
||||
$ticket->reply_status = Ticket::STATUS_OPENING;
|
||||
} else {
|
||||
$ticket->reply_status = Ticket::STATUS_CLOSED;
|
||||
}
|
||||
if (!$ticketMessage || !$ticket->save()) {
|
||||
throw new ApiException('工单回复失败');
|
||||
}
|
||||
DB::commit();
|
||||
HookManager::call('ticket.reply.admin.after', [$ticket, $ticketMessage]);
|
||||
} catch (\Exception $e) {
|
||||
DB::rollBack();
|
||||
throw $e;
|
||||
$ticketMessage = $this->reply($ticket, $message, $userId);
|
||||
if (!$ticketMessage) {
|
||||
throw new ApiException('工单回复失败');
|
||||
}
|
||||
HookManager::call('ticket.reply.admin.after', [$ticket, $ticketMessage]);
|
||||
$this->sendEmailNotify($ticket, $ticketMessage);
|
||||
}
|
||||
|
||||
@@ -81,7 +63,9 @@ class TicketService
|
||||
$ticket = Ticket::create([
|
||||
'user_id' => $userId,
|
||||
'subject' => $subject,
|
||||
'level' => $level
|
||||
'level' => $level,
|
||||
'reply_status' => Ticket::REPLY_STATUS_WAITING,
|
||||
'last_reply_user_id' => $userId,
|
||||
]);
|
||||
if (!$ticket) {
|
||||
throw new ApiException('工单创建失败');
|
||||
|
||||
Reference in New Issue
Block a user