LectureService.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. namespace App\Services;
  3. use App\Models\UserLectureRelation;
  4. use App\Models\WorkstationLectureRelation;
  5. use App\Models\Lecture; // 确保引入 Lecture 模型
  6. use App\Traits\PageTrait;
  7. class LectureService
  8. {
  9. use PageTrait;
  10. public function getList($condition, $sort = 'id', $sortable = 'desc')
  11. {
  12. $query = Lecture::query();
  13. $query->select(
  14. 'id',
  15. 'title',
  16. 'start_time',
  17. 'end_time',
  18. 'status',
  19. 'master_id'
  20. );
  21. // 使用 whereIn 方法筛选 lecture_id 在 $lectureIdArr 数组中的记录
  22. // 搜索
  23. if (isset($condition['keywords']) && $condition['keywords']) {
  24. $query->where('title', 'like', '%' . $condition['keywords'] . '%');
  25. }
  26. // 排序
  27. $query->orderBy($sort, $sortable);
  28. // 执行查询并返回结果
  29. return self::JsonPage($query);
  30. }
  31. public function getListForExpert($condition, $sort = 'id', $sortable = 'desc')
  32. {
  33. $userId = auth('expert')->user()->id;
  34. $userLectureIdArr = UserLectureRelation::query()->where('user_id', $userId)->pluck('lecture_id')->toArray();
  35. if(empty($userLectureIdArr)){
  36. return [
  37. 'list' => [],
  38. 'count' => 0,
  39. 'current_page' => 1,
  40. 'page_size' => 15,
  41. ];
  42. }
  43. $query = Lecture::query();
  44. $query->select(
  45. 'id',
  46. 'title',
  47. 'start_time',
  48. 'end_time',
  49. 'status'
  50. );
  51. // 使用 whereIn 方法筛选 lecture_id 在 $lectureIdArr 数组中的记录
  52. $query->whereIn('id', $userLectureIdArr);
  53. // 搜索
  54. if (isset($condition['keywords']) && $condition['keywords']) {
  55. $query->where('title', 'like', '%' . $condition['keywords'] . '%');
  56. }
  57. if (isset($condition['status']) && $condition['status'] !== NULL) {
  58. $query->where('status', $condition['status']);
  59. }
  60. // 排序
  61. $query->orderBy($sort, $sortable);
  62. // 执行查询并返回结果
  63. return self::JsonPage($query);
  64. }
  65. public function getListForGrider($condition, $sort = 'id', $sortable = 'desc')
  66. {
  67. $userId = auth('grider')->user()->id;
  68. $userLectureIdArr = UserLectureRelation::query()->where('user_id', $userId)->pluck('lecture_id')->toArray();
  69. $workstationLectureIdArr = WorkstationLectureRelation::query()->where('workstation_id', 'abc')->pluck('lecture_id')->toArray();
  70. $lectureIdArr = array_merge($userLectureIdArr, $workstationLectureIdArr);
  71. if(empty($lectureIdArr)){
  72. return [
  73. 'list' => [],
  74. 'count' => 0,
  75. 'current_page' => 1,
  76. 'page_size' => 15,
  77. ];
  78. }
  79. $query = Lecture::query();
  80. $query->select(
  81. 'id',
  82. 'title',
  83. 'start_time',
  84. 'end_time',
  85. 'status'
  86. );
  87. // 使用 whereIn 方法筛选 lecture_id 在 $lectureIdArr 数组中的记录
  88. $query->whereIn('id', $lectureIdArr);
  89. // 搜索
  90. if (isset($condition['keywords']) && $condition['keywords']) {
  91. $query->where('title', 'like', '%' . $condition['keywords'] . '%');
  92. }
  93. if (isset($condition['status']) && $condition['status'] !== NULL) {
  94. $query->where('status', $condition['status']);
  95. }
  96. // 排序
  97. $query->orderBy($sort, $sortable);
  98. // 执行查询并返回结果
  99. return self::JsonPage($query);
  100. }
  101. }