2020-08-03 16:10:56 +08:00
|
|
|
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
|
|
|
|
|
|
|
|
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 {
|
2022-04-28 00:58:37 +08:00
|
|
|
public function reply($ticket, $message, $userId)
|
|
|
|
{
|
|
|
|
DB::beginTransaction();
|
|
|
|
$ticketMessage = TicketMessage::create([
|
|
|
|
'user_id' => $userId,
|
|
|
|
'ticket_id' => $ticket->id,
|
|
|
|
'message' => $message
|
|
|
|
]);
|
|
|
|
if ($userId !== $ticket->user_id) {
|
|
|
|
$ticket->reply_status = 0;
|
|
|
|
} else {
|
|
|
|
$ticket->reply_status = 1;
|
|
|
|
}
|
|
|
|
if (!$ticketMessage || !$ticket->save()) {
|
|
|
|
DB::rollback();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
DB::commit();
|
|
|
|
return $ticketMessage;
|
|
|
|
}
|
|
|
|
|
2020-08-03 16:17:03 +08:00
|
|
|
public function replyByAdmin($ticketId, $message, $userId):void
|
2020-08-03 16:10:56 +08:00
|
|
|
{
|
|
|
|
$ticket = Ticket::where('id', $ticketId)
|
|
|
|
->first();
|
|
|
|
if (!$ticket) {
|
|
|
|
abort(500, '工单不存在');
|
|
|
|
}
|
2022-01-22 01:52:02 +08:00
|
|
|
$ticket->status = 0;
|
2020-08-03 16:10:56 +08:00
|
|
|
DB::beginTransaction();
|
|
|
|
$ticketMessage = TicketMessage::create([
|
|
|
|
'user_id' => $userId,
|
|
|
|
'ticket_id' => $ticket->id,
|
|
|
|
'message' => $message
|
|
|
|
]);
|
2022-04-28 00:58:37 +08:00
|
|
|
if ($userId !== $ticket->user_id) {
|
|
|
|
$ticket->reply_status = 0;
|
|
|
|
} else {
|
|
|
|
$ticket->reply_status = 1;
|
|
|
|
}
|
2020-08-03 16:10:56 +08:00
|
|
|
if (!$ticketMessage || !$ticket->save()) {
|
|
|
|
DB::rollback();
|
|
|
|
abort(500, '工单回复失败');
|
|
|
|
}
|
|
|
|
DB::commit();
|
|
|
|
$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' => '您在' . config('v2board.app_name', 'V2Board') . '的工单得到了回复',
|
|
|
|
'template_name' => 'notify',
|
|
|
|
'template_value' => [
|
|
|
|
'name' => config('v2board.app_name', 'V2Board'),
|
|
|
|
'url' => config('v2board.app_url'),
|
|
|
|
'content' => "主题:{$ticket->subject}\r\n回复内容:{$ticketMessage->message}"
|
|
|
|
]
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|