CommentService.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. namespace App\Services\Client;
  3. use App\Models\OrderComment;
  4. use App\Models\CoachUser;
  5. use App\Models\MemberUser;
  6. use Illuminate\Support\Facades\Auth;
  7. class CommentService
  8. {
  9. /**
  10. * 创建评价
  11. */
  12. public function createComment(int $coachId, string $content, int $rating)
  13. {
  14. // 获取当前用户
  15. $userId = Auth::id();
  16. $user = MemberUser::findOrFail($userId);
  17. // 检查用户状态
  18. if ($user->state !== 'enable') {
  19. throw new \Exception('用户状态异常');
  20. }
  21. // 检查技师是否存在
  22. $coach = CoachUser::findOrFail($coachId);
  23. // 创建评价
  24. $comment = new OrderComment();
  25. $comment->user_id = $userId;
  26. $comment->coach_id = $coachId;
  27. $comment->content = $content;
  28. $comment->rating = $rating;
  29. $comment->state = 'enable';
  30. $comment->save();
  31. return ['message' => '评价成功'];
  32. }
  33. /**
  34. * 获取评价列表
  35. */
  36. public function getCommentList(int $coachId)
  37. {
  38. // 获取当前用户
  39. $userId = Auth::id();
  40. $user = MemberUser::findOrFail($userId);
  41. // 检查用户状态
  42. if ($user->state !== 'enable') {
  43. throw new \Exception('用户状态异常');
  44. }
  45. // 获取评价列表
  46. $comments = OrderComment::where('coach_id', $coachId)
  47. ->where('state', 'enable')
  48. ->orderBy('created_at', 'desc')
  49. ->with(['user:id,nickname,avatar'])
  50. ->paginate(10);
  51. return $comments;
  52. }
  53. }