VideoService.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. namespace App\Services\Common;
  3. use App\Models\ActionLogModel;
  4. use App\Models\VideoModel;
  5. use App\Services\BaseService;
  6. class VideoService extends BaseService
  7. {
  8. public static $instance = null;
  9. public function __construct()
  10. {
  11. $this->model = new VideoModel();
  12. }
  13. public static function make()
  14. {
  15. if (!self::$instance) {
  16. self::$instance = new static();
  17. }
  18. return self::$instance;
  19. }
  20. /**
  21. * 获取视频集列表
  22. */
  23. public function getDataList($params, $pageSize = 15)
  24. {
  25. $where = ['a.mark' => 1];
  26. // 状态过滤
  27. $status = isset($params['status']) ? (int) $params['status'] : 0;
  28. if ($status > 0) {
  29. $where['a.status'] = $status;
  30. }
  31. // 查询
  32. $query = $this->model->from('videos as a')
  33. ->where($where)
  34. ->where(function ($query) use ($params) {
  35. $keyword = isset($params['keyword']) ? trim($params['keyword']) : '';
  36. if ($keyword) {
  37. $query->where('a.video_name', 'like', "%{$keyword}%");
  38. }
  39. })
  40. ->where(function ($query) use ($params) {
  41. $categoryId = isset($params['category_id']) ? (int) $params['category_id'] : 0;
  42. if ($categoryId) {
  43. $query->where('a.category_id', $categoryId);
  44. }
  45. $type = isset($params['type']) ? (int) $params['type'] : 0;
  46. if ($type) {
  47. $query->where('a.type', $type);
  48. }
  49. })
  50. ->select([
  51. 'a.id',
  52. 'a.video_name',
  53. 'a.category_id',
  54. 'a.poster',
  55. 'a.description',
  56. 'a.sort',
  57. 'a.type',
  58. 'a.create_time',
  59. 'a.update_time',
  60. 'a.status',
  61. 'a.is_recommend',
  62. ])
  63. ->orderBy('a.sort', 'desc')
  64. ->orderBy('a.create_time', 'desc');
  65. $list = $query->paginate($pageSize > 0 ? $pageSize : 9999999)->toArray();
  66. // 格式化数据
  67. if (!empty($list['data'])) {
  68. foreach ($list['data'] as &$item) {
  69. $item['create_time'] = $item['create_time'] ? datetime($item['create_time'], 'Y-m-d H:i:s') : '';
  70. $item['update_time'] = $item['update_time'] ? datetime($item['update_time'], 'Y-m-d H:i:s') : '';
  71. $item['poster'] = $item['poster'] ? get_image_url($item['poster']) : '';
  72. }
  73. }
  74. return [
  75. 'pageSize' => $pageSize,
  76. 'total' => $list['total'] ?? 0,
  77. 'list' => $list['data'] ?? []
  78. ];
  79. }
  80. /**
  81. * 编辑或新增视频集
  82. */
  83. public function edit()
  84. {
  85. $data = request()->all();
  86. // 视频封面处理
  87. if (!empty($data['poster'])) {
  88. $data['poster'] = get_image_path(trim($data['poster']));
  89. }
  90. $id = $data['id'] ?? 0;
  91. $type = $data['type'] ?? 0;
  92. // 校验视频集名称是否唯一
  93. $videoName = trim($data['video_name'] ?? '');
  94. $checkId = $this->model->where(['video_name' => $videoName,'type'=>$type, 'mark' => 1])->value('id');
  95. if ($checkId && $id != $checkId) {
  96. return message('视频集名称已存在', false);
  97. }
  98. // 默认字段处理
  99. $data['update_time'] = time();
  100. if (!$id) {
  101. $data['create_time'] = time();
  102. }
  103. return parent::edit($data); // 调用父类的 edit 方法
  104. }
  105. /**
  106. * 获取视频集详情
  107. */
  108. public function info()
  109. {
  110. $id = request()->input("id", 0);
  111. $info = [];
  112. if ($id) {
  113. $info = $this->model->getInfo($id);
  114. }
  115. $info['poster'] = $info['poster'] ? get_image_url($info['poster']) : '';
  116. return message(MESSAGE_OK, true, $info);
  117. }
  118. /**
  119. * 获取视频集选项
  120. */
  121. public function options()
  122. {
  123. // 获取查询参数
  124. $param = request()->all();
  125. $keyword = getter($param, "keyword");
  126. // 查询视频集名称或描述
  127. $datas = $this->model
  128. ->where(function ($query) use ($keyword) {
  129. if ($keyword) {
  130. $query->where('video_name', 'like', "%{$keyword}%")
  131. ->orWhere('description', 'like', "%{$keyword}%");
  132. }
  133. })
  134. ->where(['mark' => 1])
  135. ->select(['id', 'video_name'])
  136. ->get();
  137. return $datas ? $datas->toArray() : [];
  138. }
  139. /**
  140. * 删除七天之前标记软删除的数据
  141. */
  142. public function delete()
  143. {
  144. // 设置日志标题
  145. ActionLogModel::setRecord(session('userId'), ['type' => 1, 'title' => "删除视频信息", 'content' => json_encode(request()->post(), 256), 'module' => 'admin']);
  146. ActionLogModel::record();
  147. $this->model->where('mark', 0)->where('update_time', '<=', time() - 7 * 86400)->delete();
  148. return parent::delete();
  149. }
  150. }