记录视频播放片段

courseware
anke1460 5 years ago
parent 8a410b569b
commit 4ecd32c6be

@ -3,7 +3,7 @@ class WatchCourseVideo < ApplicationRecord
belongs_to :user
has_many :watch_video_histories
has_one :watch_course_video_detail
validates :course_video_id, uniqueness: {scope: :user_id}
end

@ -0,0 +1,6 @@
class WatchCourseVideoDetail < ApplicationRecord
belongs_to :user
belongs_to :watch_course_video
serialize :times, Array
end

@ -26,6 +26,7 @@ class CreateWatchVideoService < ApplicationService
watch_video_history.total_duration = params[:total_duration]
watch_video_history.watch_duration = params[:watch_duration] > watch_video_history.duration ? watch_video_history.duration : params[:watch_duration]
watch_video_history.is_finished = params[:ed].present?
watch_video_history.last_point = params[:point].to_i
watch_video_history.save!
watch_course_video = watch_video_history.watch_course_video
@ -33,6 +34,18 @@ class CreateWatchVideoService < ApplicationService
if watch_course_video.present?
watch_course_video.total_duration = watch_course_video.watch_video_histories.sum(:total_duration)
watch_course_video.end_at = current_time
if params[:point].to_i > watch_course_video.last_point
detail = WatchCourseVideoDetail.find_or_initialize_by(watch_course_video_id: watch_course_video.id, user_id: user.id) do |d|
d.times = [[ watch_course_video.last_point, params[:point].to_i]]
end
if detail.persisted?
detail.times << [watch_course_video.last_point, params[:point].to_i]
end
detail.save
end
watch_course_video.last_point = params[:point].to_i
if !watch_course_video.is_finished
# 更新课程视频的时长及是否看完状态
watch_course_video.watch_duration = params[:watch_duration] if watch_course_video.watch_duration < params[:watch_duration]
@ -50,8 +63,8 @@ class CreateWatchVideoService < ApplicationService
d.start_at = current_time
d.duration = params[:duration]
end
watch_course_video.save! unless watch_course_video.persisted?
watch_course_video.last_point = params[:point].to_i
watch_course_video.save!
watch_video_history = build_video_log(current_time, course_video.video_id, watch_course_video.id)
watch_video_history.save!
else

@ -0,0 +1,14 @@
class CreateWatchCourseVideoDetails < ActiveRecord::Migration[5.2]
def change
create_table :watch_course_video_details do |t|
t.references :user, index: true
t.text :times
t.references :watch_course_video, index: true
t.timestamps
end
add_column :watch_course_videos, :last_point, :integer, default: 0
add_column :watch_video_histories, :last_point, :integer, default: 0
end
end
Loading…
Cancel
Save