|
|
|
@ -10,19 +10,21 @@ class CreateWatchVideoService < ApplicationService
|
|
|
|
|
def call
|
|
|
|
|
ActiveRecord::Base.transaction do
|
|
|
|
|
current_time = Time.now
|
|
|
|
|
params[:watch_duration] = params[:watch_duration].to_f.round(2)
|
|
|
|
|
params[:total_duration] = params[:total_duration].to_f.round(2)
|
|
|
|
|
params[:duration] = params[:duration].to_f.round(2)
|
|
|
|
|
|
|
|
|
|
if params[:log_id].present?
|
|
|
|
|
if params[:total_duration].to_f < params[:watch_duration].to_f || params[:watch_duration].to_f < 0
|
|
|
|
|
raise Error, '观看时长错误'
|
|
|
|
|
watch_video_history = user.watch_video_histories.find(params[:log_id])
|
|
|
|
|
if params[:total_duration] < params[:watch_duration]
|
|
|
|
|
return watch_video_history
|
|
|
|
|
end
|
|
|
|
|
# 更新观看时长
|
|
|
|
|
watch_video_history = user.watch_video_histories.find(params[:log_id])
|
|
|
|
|
|
|
|
|
|
if watch_video_history.present? && watch_video_history.watch_duration <= params[:watch_duration].to_f && params[:total_duration].to_f > watch_video_history.total_duration
|
|
|
|
|
if watch_video_history.present? && !watch_video_history.is_finished && watch_video_history.watch_duration <= params[:watch_duration] && watch_video_history.total_duration <= params[:total_duration]
|
|
|
|
|
# 如果观看总时长没变,说明视频没有播放,无需再去记录
|
|
|
|
|
|
|
|
|
|
watch_video_history.end_at = current_time
|
|
|
|
|
watch_video_history.total_duration = params[:total_duration]
|
|
|
|
|
watch_video_history.watch_duration = params[:watch_duration].to_f > watch_video_history.duration ? watch_video_history.duration : params[:watch_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.save!
|
|
|
|
|
|
|
|
|
@ -31,15 +33,14 @@ 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
|
|
|
|
|
watch_course_video.save!
|
|
|
|
|
if !watch_course_video.is_finished && watch_course_video.watch_duration < params[:watch_duration].to_f
|
|
|
|
|
if !watch_course_video.is_finished && watch_course_video.watch_duration < params[:watch_duration]
|
|
|
|
|
# 更新课程视频的时长及是否看完状态
|
|
|
|
|
watch_course_video.watch_duration = params[:watch_duration]
|
|
|
|
|
if params[:ed].present?
|
|
|
|
|
watch_course_video.is_finished = watch_course_video.total_duration >= watch_course_video.duration
|
|
|
|
|
end
|
|
|
|
|
watch_course_video.save!
|
|
|
|
|
end
|
|
|
|
|
watch_course_video.save!
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
else
|
|
|
|
|