VideoService.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. })
  46. ->select([
  47. 'a.id',
  48. 'a.video_name',
  49. 'a.category_id',
  50. 'a.poster',
  51. 'a.description',
  52. 'a.sort',
  53. 'a.create_time',
  54. 'a.update_time',
  55. 'a.status',
  56. 'a.is_recommend',
  57. ])
  58. ->orderBy('a.sort', 'desc')
  59. ->orderBy('a.create_time', 'desc');
  60. $list = $query->paginate($pageSize > 0 ? $pageSize : 9999999)->toArray();
  61. // 格式化数据
  62. if (!empty($list['data'])) {
  63. foreach ($list['data'] as &$item) {
  64. $item['create_time'] = $item['create_time'] ? datetime($item['create_time'], 'Y-m-d H:i:s') : '';
  65. $item['update_time'] = $item['update_time'] ? datetime($item['update_time'], 'Y-m-d H:i:s') : '';
  66. $item['poster'] = $item['poster'] ? get_image_url($item['poster']) : '';
  67. }
  68. }
  69. return [
  70. 'pageSize' => $pageSize,
  71. 'total' => $list['total'] ?? 0,
  72. 'list' => $list['data'] ?? []
  73. ];
  74. }
  75. /**
  76. * 编辑或新增视频集
  77. */
  78. public function edit()
  79. {
  80. $data = request()->all();
  81. // 视频封面处理
  82. if (!empty($data['poster'])) {
  83. $data['poster'] = get_image_path(trim($data['poster']));
  84. }
  85. $id = $data['id'] ?? 0;
  86. // 校验视频集名称是否唯一
  87. $videoName = trim($data['video_name'] ?? '');
  88. $checkId = $this->model->where(['video_name' => $videoName, 'mark' => 1])->value('id');
  89. if ($checkId && $id != $checkId) {
  90. return message('视频集名称已存在', false);
  91. }
  92. // 默认字段处理
  93. $data['update_time'] = time();
  94. if (!$id) {
  95. $data['create_time'] = time();
  96. }
  97. return parent::edit($data); // 调用父类的 edit 方法
  98. }
  99. /**
  100. * 获取视频集详情
  101. */
  102. public function info()
  103. {
  104. $id = request()->input("id", 0);
  105. $info = [];
  106. if ($id) {
  107. $info = $this->model->getInfo($id);
  108. }
  109. $info['poster'] = $info['poster'] ? get_image_url($info['poster']) : '';
  110. return message(MESSAGE_OK, true, $info);
  111. }
  112. /**
  113. * 获取视频集选项
  114. */
  115. public function options()
  116. {
  117. // 获取查询参数
  118. $param = request()->all();
  119. $keyword = getter($param, "keyword");
  120. // 查询视频集名称或描述
  121. $datas = $this->model
  122. ->where(function ($query) use ($keyword) {
  123. if ($keyword) {
  124. $query->where('video_name', 'like', "%{$keyword}%")
  125. ->orWhere('description', 'like', "%{$keyword}%");
  126. }
  127. })
  128. ->where(['mark' => 1])
  129. ->select(['id', 'video_name'])
  130. ->get();
  131. return $datas ? $datas->toArray() : [];
  132. }
  133. /**
  134. * 删除七天之前标记软删除的数据
  135. */
  136. public function delete()
  137. {
  138. // 设置日志标题
  139. ActionLogModel::setRecord(session('userId'), ['type' => 1, 'title' => "删除视频信息", 'content' => json_encode(request()->post(), 256), 'module' => 'admin']);
  140. ActionLogModel::record();
  141. $this->model->where('mark', 0)->where('update_time', '<=', time() - 7 * 86400)->delete();
  142. return parent::delete();
  143. }
  144. }