| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180 |
- <?php
- namespace App\Services\Common;
- use App\Models\VideoCoursesModel;
- use App\Models\VideosCoursesModel;
- use App\Models\ActionLogModel;
- use App\Services\BaseService;
- use Illuminate\Support\Facades\DB;
- /**
- *
- * @author laravel开发员
- * @since 2020/11/11
- * Class VideosCoursesService
- * @package App\Services\Common
- */
- class VideosCoursesService extends BaseService
- {
- public static $instance = null;
- /**
- * 构造函数
- * @author laravel开发员
- * @since 2020/11/11
- * VideosCoursesService constructor.
- */
- public function __construct()
- {
- $this->model = new VideoCoursesModel();
- }
- /**
- * 静态入口
- */
- public static function make()
- {
- if (!self::$instance) {
- self::$instance = new static();
- }
- return self::$instance;
- }
- /**
- * 获取课程数据列表
- */
- public function getDataList($params, $pageSize = 15)
- {
- $where = ['a.mark' => 1]; // Filter for non-deleted records.
- // Status filtering
- $status = isset($params['status']) ? (int) $params['status'] : 0;
- if ($status > 0) {
- $where['a.status'] = $status;
- }
- // Fee filtering
- $fee = isset($params['fee']) ? (int) $params['fee'] : -1;
- if ($fee >= 0) {
- $where['a.fee'] = $fee;
- }
- // Query with JOIN for video details, using course name to filter videos
- $query = $this->model->from('videos_courses as a')
- ->leftJoin('videos as v', 'a.video_id', '=', 'v.id') // Join with lev_videos table
- ->where($where)
- ->where(function ($query) use ($params) {
- $keyword = isset($params['keyword']) ? trim($params['keyword']) : '';
- if ($keyword) {
- $query->where('a.course_name', 'like', "%{$keyword}%");
- }
- })
- ->where(function ($query) use ($params) {
- $videoId = isset($params['video_id']) ? (int) $params['video_id'] : 0;
- if ($videoId) {
- $query->where('a.video_id', $videoId);
- }
- })
- // Filtering based on video_name (course set name)
- ->where(function ($query) use ($params) {
- $videoName = isset($params['video_name']) ? trim($params['video_name']) : '';
- if ($videoName) {
- $query->where('v.video_name', 'like', "%{$videoName}%");
- }
- })
- ->select([
- 'a.id',
- 'a.course_name',
- 'a.course_url',
- 'a.video_id',
- 'a.fee',
- 'a.poster',
- 'a.description',
- 'a.sort',
- 'a.create_time',
- 'a.update_time',
- 'a.status',
- 'v.video_name as video_name', // Selecting video_name from lev_videos
- 'v.poster as video_poster' // Selecting video poster from lev_videos
- ])
- ->orderBy('a.sort', 'desc')
- ->orderBy('a.create_time', 'desc');
- // Paginate results
- $list = $query->paginate($pageSize > 0 ? $pageSize : 9999999)->toArray();
- // Format
- if (!empty($list['data'])) {
- foreach ($list['data'] as &$item) {
- $item['create_time'] = $item['create_time'] ? datetime($item['create_time'], 'Y-m-d H:i:s') : '';
- $item['update_time'] = $item['update_time'] ? datetime($item['update_time'], 'Y-m-d H:i:s') : '';
- $item['poster'] = $item['poster'] ? get_image_url($item['poster']) : '';
- $item['video_poster'] = $item['video_poster'] ? get_image_url($item['video_poster']) : ''; // Formatting video poster URL
- }
- }
- return [
- 'pageSize' => $pageSize,
- 'total' => $list['total'] ?? 0,
- 'list' => $list['data'] ?? []
- ];
- }
- /**
- * 编辑课程信息
- */
- public function edit()
- {
- $data = request()->all();
- // Handle video cover (poster)
- if (!empty($data['poster'])) {
- $data['poster'] = get_image_path(trim($data['poster']));
- }
- $id = $data['id'] ?? 0;
- // Check if course name already exists
- $courseName = trim($data['course_name'] ?? '');
- $checkId = $this->model->where(['course_name' => $courseName, 'mark' => 1])->value('id');
- if ($checkId && $id != $checkId) {
- return message('课程名称已存在', false);
- }
- // Default fields
- $data['update_time'] = time();
- if (!$id) {
- $data['create_time'] = time();
- }
- return parent::edit($data); // Call BaseService's edit method
- }
- /**
- * 获取课程详细信息
- */
- public function info()
- {
- // Record ID
- $id = request()->input("id", 0);
- $info = [];
- if ($id) {
- $info = $this->model->getInfo($id);
- }
- $info['poster'] = $info['poster'] ? get_image_url($info['poster']) : '';
- return message(MESSAGE_OK, true, $info);
- }
- /**
- * 删除七天之前标记软删除的数据
- */
- public function delete()
- {
- // 设置日志标题
- ActionLogModel::setRecord(session('userId'), ['type' => 1, 'title' => "删除视频课程信息", 'content' => json_encode(request()->post(), 256), 'module' => 'admin']);
- ActionLogModel::record();
- $this->model->where('mark', 0)->where('update_time', '<=', time() - 7 * 86400)->delete();
- return parent::delete();
- }
- }
|