diff --git a/app/models/watch_course_video.rb b/app/models/watch_course_video.rb index cf2d67028..49435da63 100644 --- a/app/models/watch_course_video.rb +++ b/app/models/watch_course_video.rb @@ -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 diff --git a/app/models/watch_course_video_detail.rb b/app/models/watch_course_video_detail.rb new file mode 100644 index 000000000..714c41f49 --- /dev/null +++ b/app/models/watch_course_video_detail.rb @@ -0,0 +1,6 @@ +class WatchCourseVideoDetail < ApplicationRecord + belongs_to :user + belongs_to :watch_course_video + + serialize :times, Array +end diff --git a/app/services/create_watch_video_service.rb b/app/services/create_watch_video_service.rb index 5a3813571..32dd60819 100644 --- a/app/services/create_watch_video_service.rb +++ b/app/services/create_watch_video_service.rb @@ -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 diff --git a/db/migrate/20200318041550_create_watch_course_video_details.rb b/db/migrate/20200318041550_create_watch_course_video_details.rb new file mode 100644 index 000000000..07984cb42 --- /dev/null +++ b/db/migrate/20200318041550_create_watch_course_video_details.rb @@ -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