VideosCoursesService.php 5.6 KB

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