TicketReplyObserver.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <?php
  2. namespace App\Observers;
  3. use App\Models\TicketReply;
  4. use App\Models\User;
  5. use App\Notifications\TicketReplied;
  6. use Illuminate\Database\Eloquent\Collection;
  7. use Notification;
  8. class TicketReplyObserver
  9. {
  10. public function created(TicketReply $reply): void
  11. {
  12. $ticket = $reply->ticket;
  13. if ($reply->user_id) {
  14. if ($ticket->status !== 0) {
  15. $ticket->update(['status' => 0]);
  16. }
  17. Notification::send($this->findAdmin($reply), new TicketReplied($reply, route('admin.ticket.edit', $ticket))); // 通知相关管理员
  18. }
  19. if ($reply->admin_id) {
  20. if ($ticket->status !== 1) {
  21. $ticket->update(['status' => 1]);
  22. }
  23. if (sysConfig('ticket_replied_notification')) { // 通知用户
  24. $ticket->user->notify(new TicketReplied($reply, route('ticket.edit', $ticket), true));
  25. }
  26. }
  27. }
  28. private function findAdmin(TicketReply $reply): Collection
  29. {
  30. $ticket = $reply->ticket;
  31. if ($ticket->admin_id) {
  32. return $ticket->admin()->get();
  33. }
  34. $admins = $ticket->reply()->whereNotNull('admin_id')->distinct()->pluck('admin_id');
  35. if ($admins) {
  36. return User::findMany($admins);
  37. }
  38. return User::role('Super Admin')->get();
  39. }
  40. }