VideoController.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. namespace App\Http\Controllers\Api\Course;
  3. use App\Http\Controllers\Controller;
  4. use App\Repositories\Enums\Course\VideoWatchEnum;
  5. use App\Repositories\Enums\ModelStatusEnum;
  6. use App\Repositories\Enums\ResponseCodeEnum;
  7. use App\Repositories\Models\Course\Subscribe;
  8. use App\Repositories\Models\Course\UserRecord;
  9. use App\Repositories\Models\Course\Video;
  10. use App\Repositories\Models\Course\WatchRecord;
  11. use App\Services\Course\VideoService;
  12. use Carbon\Carbon;
  13. use Illuminate\Http\Request;
  14. use Illuminate\Support\Facades\DB;
  15. use Jiannei\Response\Laravel\Support\Facades\Response;
  16. /**
  17. * 视频管理
  18. *
  19. * @package namespace App\Http\Controllers;
  20. */
  21. class VideoController extends Controller
  22. {
  23. protected $service;
  24. public function __construct(VideoService $service)
  25. {
  26. parent::__construct();
  27. $this->service = $service;
  28. }
  29. /**
  30. * 视频详情
  31. * @param $id
  32. * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\Resources\Json\JsonResource
  33. * Author: Mead
  34. */
  35. public function show(Request $request)
  36. {
  37. $this->validateData($request, [
  38. 'id' => 'required|integer',
  39. 'course_id' => 'required|integer',
  40. ], [
  41. 'course_id' => '课程 id'
  42. ]);
  43. $id = $request->get('id');
  44. $video = $this->service->handleProfile($id);
  45. return $this->response->success($video);
  46. }
  47. /**
  48. * 视频记录
  49. * @param Request $request
  50. * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\Resources\Json\JsonResource
  51. * @throws \Illuminate\Validation\ValidationException
  52. */
  53. public function lookRecord(Request $request)
  54. {
  55. $this->validateData($request, [
  56. 'course_id' => 'required',
  57. 'video_id' => 'required',
  58. 'watch_seconds' => 'required',
  59. // 'is_watched' => 'required',
  60. ]);
  61. $course_id = $request->get('course_id');
  62. $video_id = $request->get('video_id');
  63. $watch_seconds = $request->get('watch_seconds', 10);
  64. // $is_watched = $request->get('is_watched', VideoWatchEnum::NO);
  65. $is_watched = VideoWatchEnum::NO;
  66. DB::beginTransaction();
  67. try {
  68. $total_seconds = Video::query()->where('id', $video_id)->value('duration') ?? 0;
  69. if ($total_seconds <= 0) {
  70. return $this->response->ok('请求成功');
  71. }
  72. $progress = 0;
  73. $progress_index = 0;
  74. $sec = 10;
  75. $user_id = login_user_id();
  76. $progress_index = bcdiv($watch_seconds, $sec, 0);
  77. // $user_id = 10;
  78. //进度方式一
  79. // $progress = bcdiv($watch_seconds * 100, $total_seconds, 0);
  80. // if ($progress > 100) {
  81. // $progress = 100;
  82. // $watch_seconds = $total_seconds;
  83. // }
  84. //进度方式二
  85. $use_nums = WatchRecord::query()->where('video_id', $video_id)->where('user_id', $user_id)->distinct('progress_index')->count();
  86. $progress = bcdiv($use_nums * 100, $total_seconds / $sec, 0);
  87. if ($progress >= 98) {
  88. if ($sec) {
  89. $total_nums = bcdiv($total_seconds, $sec, 0);
  90. if ($total_nums - 3 > 0) {
  91. if ($use_nums >= ($total_nums - 5)) {
  92. $is_watched = VideoWatchEnum::OK;
  93. $progress = 100;
  94. }
  95. } else {
  96. $is_watched = VideoWatchEnum::OK;
  97. $progress = 100;
  98. }
  99. } else {
  100. $is_watched = VideoWatchEnum::OK;
  101. $progress = 100;
  102. }
  103. }
  104. if ($is_watched) {
  105. Video::query()->where('id', $video_id)->increment('view_num', 1);
  106. }
  107. WatchRecord::query()->updateOrCreate([
  108. 'video_id' => $video_id,
  109. 'course_id' => $course_id,
  110. 'user_id' => $user_id,
  111. 'progress_index' => $progress_index,
  112. ], [
  113. 'is_watched' => $is_watched,
  114. 'watch_seconds' => $watch_seconds,
  115. 'watched_at' => Carbon::now(),
  116. 'progress' => $progress,
  117. 'progress_index' => $progress_index,
  118. ]);
  119. if ($is_watched == VideoWatchEnum::OK) {
  120. $use_complete_nums = WatchRecord::query()->where('course_id', $course_id)->where('is_watched', VideoWatchEnum::OK)->distinct('video_id')->count();
  121. $total_complete_nums = Video::query()->where('course_id', $course_id)->where('status', ModelStatusEnum::OK)->count();
  122. $total_progress = bcdiv($use_complete_nums * 100, $total_complete_nums, 0);
  123. if ($total_progress > 100) {
  124. $total_progress = 100;
  125. }
  126. $is_watched = 0;
  127. if ($use_complete_nums == $total_complete_nums) {
  128. $is_watched = 1;
  129. }
  130. Subscribe::query()
  131. ->where('course_id', $course_id)
  132. ->where('user_id', $user_id)
  133. ->updateOrCreate([
  134. 'course_id' => $course_id,
  135. 'user_id' => $user_id,
  136. ], [
  137. 'total_nums' => $total_complete_nums,
  138. 'use_nums' => $use_complete_nums,
  139. 'progress' => $total_progress,
  140. 'is_watched' => $is_watched,
  141. 'watched_at' => Carbon::now()->toDateTimeString()
  142. ]);
  143. }
  144. DB::commit();
  145. } catch (\Exception $exception) {
  146. DB::rollBack();
  147. $this->exception($exception);
  148. }
  149. return $this->response->ok('请求成功');
  150. }
  151. }