Files

110 lines
3.6 KiB
PHP
Raw Permalink Normal View History

2023-11-17 14:44:01 +08:00
<?php
namespace App\Services;
2023-12-04 20:40:49 +08:00
use App\Exceptions\ApiException;
2023-11-17 14:44:01 +08:00
use App\Jobs\SendEmailJob;
use App\Models\Ticket;
use App\Models\TicketMessage;
use App\Models\User;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use App\Services\Plugin\HookManager;
2023-11-17 14:44:01 +08:00
2025-07-26 18:49:58 +08:00
class TicketService
{
2023-11-17 14:44:01 +08:00
public function reply($ticket, $message, $userId)
{
2025-07-26 18:49:58 +08:00
try {
DB::beginTransaction();
$ticketMessage = TicketMessage::create([
'user_id' => $userId,
'ticket_id' => $ticket->id,
'message' => $message
]);
$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();
}
DB::commit();
return $ticketMessage;
2025-07-26 18:49:58 +08:00
} catch (\Exception $e) {
2023-11-17 14:44:01 +08:00
DB::rollback();
return false;
}
}
2025-07-26 18:49:58 +08:00
public function replyByAdmin($ticketId, $message, $userId): void
2023-11-17 14:44:01 +08:00
{
$ticket = Ticket::where('id', $ticketId)->first();
2023-11-17 14:44:01 +08:00
if (!$ticket) {
throw new ApiException('工单不存在');
2023-11-17 14:44:01 +08:00
}
$ticketMessage = $this->reply($ticket, $message, $userId);
if (!$ticketMessage) {
throw new ApiException('工单回复失败');
2023-11-17 14:44:01 +08:00
}
HookManager::call('ticket.reply.admin.after', [$ticket, $ticketMessage]);
2023-11-17 14:44:01 +08:00
$this->sendEmailNotify($ticket, $ticketMessage);
}
2025-07-26 18:49:58 +08:00
public function createTicket($userId, $subject, $level, $message)
{
try {
DB::beginTransaction();
if (Ticket::where('status', 0)->where('user_id', $userId)->lockForUpdate()->first()) {
DB::rollBack();
throw new ApiException('存在未关闭的工单');
}
$ticket = Ticket::create([
'user_id' => $userId,
'subject' => $subject,
'level' => $level,
'reply_status' => Ticket::REPLY_STATUS_WAITING,
'last_reply_user_id' => $userId,
2025-07-26 18:49:58 +08:00
]);
if (!$ticket) {
throw new ApiException('工单创建失败');
}
$ticketMessage = TicketMessage::create([
'user_id' => $userId,
'ticket_id' => $ticket->id,
'message' => $message
]);
if (!$ticketMessage) {
DB::rollBack();
throw new ApiException('工单消息创建失败');
}
DB::commit();
return $ticket;
} catch (\Exception $e) {
DB::rollBack();
throw $e;
}
}
2023-11-17 14:44:01 +08:00
// 半小时内不再重复通知
private function sendEmailNotify(Ticket $ticket, TicketMessage $ticketMessage)
{
$user = User::find($ticket->user_id);
$cacheKey = 'ticket_sendEmailNotify_' . $ticket->user_id;
if (!Cache::get($cacheKey)) {
Cache::put($cacheKey, 1, 1800);
SendEmailJob::dispatch([
'email' => $user->email,
'subject' => '您在' . admin_setting('app_name', 'XBoard') . '的工单得到了回复',
'template_name' => 'notify',
'template_value' => [
'name' => admin_setting('app_name', 'XBoard'),
'url' => admin_setting('app_url'),
'content' => "主题:{$ticket->subject}\r\n回复内容:{$ticketMessage->message}"
]
]);
}
}
}