VideosCoursesService.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. namespace App\Services\Common;
  3. use App\Models\VideoCoursesModel;
  4. use App\Models\VideosCoursesModel;
  5. use App\Services\BaseService;
  6. use Illuminate\Support\Facades\DB;
  7. /**
  8. *
  9. * @author laravel开发员
  10. * @since 2020/11/11
  11. * Class VideosCoursesService
  12. * @package App\Services\Common
  13. */
  14. class VideosCoursesService extends BaseService
  15. {
  16. public static $instance = null;
  17. /**
  18. * 构造函数
  19. * @author laravel开发员
  20. * @since 2020/11/11
  21. * VideosCoursesService constructor.
  22. */
  23. public function __construct()
  24. {
  25. $this->model = new VideoCoursesModel();
  26. }
  27. /**
  28. * 静态入口
  29. */
  30. public static function make()
  31. {
  32. if (!self::$instance) {
  33. self::$instance = new static();
  34. }
  35. return self::$instance;
  36. }
  37. /**
  38. * 获取课程数据列表
  39. */
  40. public function getDataList($params, $pageSize = 15)
  41. {
  42. $where = ['a.mark' => 1]; // Filter for non-deleted records.
  43. // Status filtering
  44. $status = isset($params['status']) ? (int) $params['status'] : 0;
  45. if ($status > 0) {
  46. $where['a.status'] = $status;
  47. }
  48. // Query with JOIN for video details, using course name to filter videos
  49. $query = $this->model->from('videos_courses as a')
  50. ->leftJoin('videos as v', 'a.video_id', '=', 'v.id') // Join with lev_videos table
  51. ->where($where)
  52. ->where(function ($query) use ($params) {
  53. $keyword = isset($params['keyword']) ? trim($params['keyword']) : '';
  54. if ($keyword) {
  55. $query->where('a.course_name', 'like', "%{$keyword}%");
  56. }
  57. })
  58. ->where(function ($query) use ($params) {
  59. $videoId = isset($params['video_id']) ? (int) $params['video_id'] : 0;
  60. if ($videoId) {
  61. $query->where('a.video_id', $videoId);
  62. }
  63. })
  64. // Filtering based on video_name (course set name)
  65. ->where(function ($query) use ($params) {
  66. $videoName = isset($params['video_name']) ? trim($params['video_name']) : '';
  67. if ($videoName) {
  68. $query->where('v.video_name', 'like', "%{$videoName}%");
  69. }
  70. })
  71. ->select([
  72. 'a.id',
  73. 'a.course_name',
  74. 'a.course_url',
  75. 'a.video_id',
  76. 'a.fee',
  77. 'a.poster',
  78. 'a.description',
  79. 'a.sort',
  80. 'a.create_time',
  81. 'a.update_time',
  82. 'a.status',
  83. 'v.video_name as video_name', // Selecting video_name from lev_videos
  84. 'v.poster as video_poster' // Selecting video poster from lev_videos
  85. ])
  86. ->orderBy('a.sort', 'desc')
  87. ->orderBy('a.create_time', 'desc');
  88. // Paginate results
  89. $list = $query->paginate($pageSize > 0 ? $pageSize : 9999999)->toArray();
  90. // Format
  91. if (!empty($list['data'])) {
  92. foreach ($list['data'] as &$item) {
  93. $item['create_time'] = $item['create_time'] ? datetime($item['create_time'], 'Y-m-d H:i:s') : '';
  94. $item['update_time'] = $item['update_time'] ? datetime($item['update_time'], 'Y-m-d H:i:s') : '';
  95. $item['poster'] = $item['poster'] ? get_image_url($item['poster']) : '';
  96. $item['video_poster'] = $item['video_poster'] ? get_image_url($item['video_poster']) : ''; // Formatting video poster URL
  97. }
  98. }
  99. return [
  100. 'pageSize' => $pageSize,
  101. 'total' => $list['total'] ?? 0,
  102. 'list' => $list['data'] ?? []
  103. ];
  104. }
  105. /**
  106. * 编辑课程信息
  107. */
  108. public function edit()
  109. {
  110. $data = request()->all();
  111. // Handle video cover (poster)
  112. if (!empty($data['poster'])) {
  113. $data['poster'] = get_image_path(trim($data['poster']));
  114. }
  115. $id = $data['id'] ?? 0;
  116. // Check if course name already exists
  117. $courseName = trim($data['course_name'] ?? '');
  118. $checkId = $this->model->where(['course_name' => $courseName, 'mark' => 1])->value('id');
  119. if ($checkId && $id != $checkId) {
  120. return message('课程名称已存在', false);
  121. }
  122. // Default fields
  123. $data['update_time'] = time();
  124. if (!$id) {
  125. $data['create_time'] = time();
  126. }
  127. return parent::edit($data); // Call BaseService's edit method
  128. }
  129. /**
  130. * 获取课程详细信息
  131. */
  132. public function info()
  133. {
  134. // Record ID
  135. $id = request()->input("id", 0);
  136. $info = [];
  137. if ($id) {
  138. $info = $this->model->getInfo($id);
  139. }
  140. $info['poster'] = $info['poster'] ? get_image_url($info['poster']) : '';
  141. return message(MESSAGE_OK, true, $info);
  142. }
  143. }