2023-11-17 01:44:01 -05:00
|
|
|
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
|
|
|
|
|
2023-12-04 07:40:49 -05:00
|
|
|
use App\Exceptions\ApiException;
|
2023-11-17 01:44:01 -05: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;
|
|
|
|
|
|
|
|
class TicketService {
|
|
|
|
public function reply($ticket, $message, $userId)
|
|
|
|
{
|
2023-12-06 06:00:26 -05:00
|
|
|
try{
|
|
|
|
DB::beginTransaction();
|
|
|
|
$ticketMessage = TicketMessage::create([
|
|
|
|
'user_id' => $userId,
|
|
|
|
'ticket_id' => $ticket->id,
|
|
|
|
'message' => $message
|
|
|
|
]);
|
|
|
|
if ($userId !== $ticket->user_id) {
|
2024-04-09 12:51:03 -04:00
|
|
|
$ticket->reply_status = Ticket::STATUS_OPENING;
|
2023-12-06 06:00:26 -05:00
|
|
|
} else {
|
2024-04-09 12:51:03 -04:00
|
|
|
$ticket->reply_status = Ticket::STATUS_CLOSED;
|
2023-12-06 06:00:26 -05:00
|
|
|
}
|
|
|
|
if (!$ticketMessage || !$ticket->save()) {
|
|
|
|
throw new \Exception();
|
|
|
|
}
|
|
|
|
DB::commit();
|
|
|
|
return $ticketMessage;
|
|
|
|
}catch(\Exception $e){
|
2023-11-17 01:44:01 -05:00
|
|
|
DB::rollback();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function replyByAdmin($ticketId, $message, $userId):void
|
|
|
|
{
|
|
|
|
$ticket = Ticket::where('id', $ticketId)
|
|
|
|
->first();
|
|
|
|
if (!$ticket) {
|
2023-12-06 15:01:32 -05:00
|
|
|
throw new ApiException('工单不存在');
|
2023-11-17 01:44:01 -05:00
|
|
|
}
|
2024-04-09 12:51:03 -04:00
|
|
|
$ticket->status = Ticket::STATUS_OPENING;
|
2023-12-06 06:00:26 -05:00
|
|
|
try{
|
|
|
|
DB::beginTransaction();
|
|
|
|
$ticketMessage = TicketMessage::create([
|
|
|
|
'user_id' => $userId,
|
|
|
|
'ticket_id' => $ticket->id,
|
|
|
|
'message' => $message
|
|
|
|
]);
|
|
|
|
if ($userId !== $ticket->user_id) {
|
2024-04-09 12:51:03 -04:00
|
|
|
$ticket->reply_status = Ticket::STATUS_OPENING;
|
2023-12-06 06:00:26 -05:00
|
|
|
} else {
|
2024-04-09 12:51:03 -04:00
|
|
|
$ticket->reply_status = Ticket::STATUS_CLOSED;
|
2023-12-06 06:00:26 -05:00
|
|
|
}
|
|
|
|
if (!$ticketMessage || !$ticket->save()) {
|
2023-12-06 15:01:32 -05:00
|
|
|
throw new ApiException('工单回复失败');
|
2023-12-06 06:00:26 -05:00
|
|
|
}
|
|
|
|
DB::commit();
|
|
|
|
}catch(\Exception $e){
|
|
|
|
DB::rollBack();
|
|
|
|
throw $e;
|
2023-11-17 01:44:01 -05:00
|
|
|
}
|
|
|
|
$this->sendEmailNotify($ticket, $ticketMessage);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 半小时内不再重复通知
|
|
|
|
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}"
|
|
|
|
]
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|