123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114 |
- <?php
- namespace App\Services;
- use App\Models\UserLectureRelation;
- use App\Models\WorkstationLectureRelation;
- use App\Models\Lecture; // 确保引入 Lecture 模型
- use App\Traits\PageTrait;
- class LectureService
- {
- use PageTrait;
- public function getList($condition, $sort = 'id', $sortable = 'desc')
- {
- $query = Lecture::query();
- $query->select(
- 'id',
- 'title',
- 'start_time',
- 'end_time',
- 'status',
- 'master_id'
- );
- // 使用 whereIn 方法筛选 lecture_id 在 $lectureIdArr 数组中的记录
- // 搜索
- if (isset($condition['keywords']) && $condition['keywords']) {
- $query->where('title', 'like', '%' . $condition['keywords'] . '%');
- }
- // 排序
- $query->orderBy($sort, $sortable);
- // 执行查询并返回结果
- return self::JsonPage($query);
- }
- public function getListForExpert($condition, $sort = 'id', $sortable = 'desc')
- {
- $userId = auth('expert')->user()->id;
- $userLectureIdArr = UserLectureRelation::query()->where('user_id', $userId)->pluck('lecture_id')->toArray();
- if(empty($userLectureIdArr)){
- return [
- 'list' => [],
- 'count' => 0,
- 'current_page' => 1,
- 'page_size' => 15,
- ];
- }
- $query = Lecture::query();
- $query->select(
- 'id',
- 'title',
- 'start_time',
- 'end_time',
- 'status'
- );
- // 使用 whereIn 方法筛选 lecture_id 在 $lectureIdArr 数组中的记录
- $query->whereIn('id', $userLectureIdArr);
- // 搜索
- if (isset($condition['keywords']) && $condition['keywords']) {
- $query->where('title', 'like', '%' . $condition['keywords'] . '%');
- }
- if (isset($condition['status']) && $condition['status'] !== NULL) {
- $query->where('status', $condition['status']);
- }
- // 排序
- $query->orderBy($sort, $sortable);
- // 执行查询并返回结果
- return self::JsonPage($query);
- }
- public function getListForGrider($condition, $sort = 'id', $sortable = 'desc')
- {
- $userId = auth('grider')->user()->id;
- $userLectureIdArr = UserLectureRelation::query()->where('user_id', $userId)->pluck('lecture_id')->toArray();
- $workstationLectureIdArr = WorkstationLectureRelation::query()->where('workstation_id', 'abc')->pluck('lecture_id')->toArray();
- $lectureIdArr = array_merge($userLectureIdArr, $workstationLectureIdArr);
- if(empty($lectureIdArr)){
- return [
- 'list' => [],
- 'count' => 0,
- 'current_page' => 1,
- 'page_size' => 15,
- ];
- }
- $query = Lecture::query();
- $query->select(
- 'id',
- 'title',
- 'start_time',
- 'end_time',
- 'status'
- );
- // 使用 whereIn 方法筛选 lecture_id 在 $lectureIdArr 数组中的记录
- $query->whereIn('id', $lectureIdArr);
- // 搜索
- if (isset($condition['keywords']) && $condition['keywords']) {
- $query->where('title', 'like', '%' . $condition['keywords'] . '%');
- }
- if (isset($condition['status']) && $condition['status'] !== NULL) {
- $query->where('status', $condition['status']);
- }
- // 排序
- $query->orderBy($sort, $sortable);
- // 执行查询并返回结果
- return self::JsonPage($query);
- }
- }
|