From 763791f93694ae7fae03a8169189c071a66c91fa Mon Sep 17 00:00:00 2001
From: anke1460 <zuosjob@gmail.com>
Date: Fri, 20 Mar 2020 18:20:15 +0800
Subject: [PATCH] =?UTF-8?q?=E8=AF=BE=E5=A0=82=E8=A7=86=E9=A2=91=E5=8F=91?=
 =?UTF-8?q?=E5=B8=83=E5=90=8E=E7=BB=99=E5=AD=A6=E7=94=9F=E5=8F=91=E9=80=81?=
 =?UTF-8?q?=E6=B6=88=E6=81=AF?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 app/controllers/tidings_controller.rb         |  4 ++-
 app/decorators/tiding_decorator.rb            |  3 +++
 app/jobs/course_video_uploaded_job.rb         | 25 +++++++++++++++++++
 app/services/videos/batch_publish_service.rb  |  1 +
 .../videos/dispatch_callback_service.rb       |  1 +
 config/locales/tidings/zh-CN.yml              |  1 +
 6 files changed, 34 insertions(+), 1 deletion(-)
 create mode 100644 app/jobs/course_video_uploaded_job.rb

diff --git a/app/controllers/tidings_controller.rb b/app/controllers/tidings_controller.rb
index 91d6c095d..91836a12a 100644
--- a/app/controllers/tidings_controller.rb
+++ b/app/controllers/tidings_controller.rb
@@ -12,7 +12,7 @@ class TidingsController < ApplicationController
       case params[:type]
       when 'notice'      then 'System'
       when 'apply'       then 'Apply'
-      when 'course'      then %w(HomeworkCommon Exercise Poll GraduationTask GraduationTopic)
+      when 'course'      then %w(HomeworkCommon Exercise Poll GraduationTask GraduationTopic PublishCourseVideo)
       when 'project'     then 'Project'
       when 'interaction' then %w(Comment Mentioned Praise Fan)
       when 'project_package' then %w(Created Destroyed Bidding BiddingEnd BiddingWon BiddingLost)
@@ -25,6 +25,8 @@ class TidingsController < ApplicationController
 
     tidings = tidings.where(container_type: 'ProjectPackage') if params[:type] == 'project_package'
 
+   
+
     @count   = tidings.count
     @tidings = paginate(tidings.order(created_at: :desc), per_page: 10)
   end
diff --git a/app/decorators/tiding_decorator.rb b/app/decorators/tiding_decorator.rb
index 8d266b7df..4e640a28b 100644
--- a/app/decorators/tiding_decorator.rb
+++ b/app/decorators/tiding_decorator.rb
@@ -403,6 +403,8 @@ module TidingDecorator
   def video_content
     if tiding_type == 'System'
       I18n.t(locale_format(tiding_type, status), reason: extra) % container.try(:title)
+    elsif tiding_type == 'PublishCourseVideo'
+      I18n.t(locale_format(tiding_type)) % [belong_container&.name]
     else
       I18n.t(locale_format(tiding_type)) % [container.try(:title) || extra]
     end
@@ -419,4 +421,5 @@ module TidingDecorator
   def hack_content
     I18n.t(locale_format(parent_container_type)) % (container&.name || extra)
   end
+  
 end
diff --git a/app/jobs/course_video_uploaded_job.rb b/app/jobs/course_video_uploaded_job.rb
new file mode 100644
index 000000000..71dfaa600
--- /dev/null
+++ b/app/jobs/course_video_uploaded_job.rb
@@ -0,0 +1,25 @@
+class CourseVideoUploadedJob < ApplicationJob
+  queue_as :notify
+
+  def perform(video_id)
+    video = Video.select("id, user_id").find_by(id: video_id)
+    course_ids = video&.course_videos&.pluck(:course_id)
+
+    return unless course_ids.present?
+
+    course_members = CourseMember.where(course_id: course_ids, role: 'STUDENT').select("user_id, course_id")
+    Tiding.bulk_insert do |worker|
+      course_members.find_each do |m|
+        worker.add(
+          user_id:               m.user_id,
+          tiding_type:           'PublishCourseVideo',
+          trigger_user_id:       video.user_id,
+          container_id:          video.id,
+          container_type:        'Video',
+          belong_container_type: 'Course',
+          belong_container_id:   m.course_id)
+      end
+    end
+  end
+
+end
diff --git a/app/services/videos/batch_publish_service.rb b/app/services/videos/batch_publish_service.rb
index 3ddc54967..543497e9e 100644
--- a/app/services/videos/batch_publish_service.rb
+++ b/app/services/videos/batch_publish_service.rb
@@ -52,6 +52,7 @@ class Videos::BatchPublishService < ApplicationService
 
         if param[:course_id].present?
           video.video_applies.create!(status: "agreed")
+          CourseVideoUploadedJob.perform_later(video.id) if video.transcoded
         else
           video.video_applies.create!
         end
diff --git a/app/services/videos/dispatch_callback_service.rb b/app/services/videos/dispatch_callback_service.rb
index 4ff94a1c4..49b1d5935 100644
--- a/app/services/videos/dispatch_callback_service.rb
+++ b/app/services/videos/dispatch_callback_service.rb
@@ -22,6 +22,7 @@ class Videos::DispatchCallbackService < ApplicationService
     when 'StreamTranscodeComplete' then # 转码完成
       #return if video.play_url.present?
       video.update!(play_url: params['FileUrl'], transcoded: true)
+      CourseVideoUploadedJob.perform_later(video.id) if video.video_applies.exists?(status: 'agreed') #通过的情况基本上是课堂视频
     when 'DeleteMediaComplete' #完成云端视频删除
       video.update_column(:delete_state, Video::FINISH_DELETE)
     end
diff --git a/config/locales/tidings/zh-CN.yml b/config/locales/tidings/zh-CN.yml
index cda146780..5eb6ee759 100644
--- a/config/locales/tidings/zh-CN.yml
+++ b/config/locales/tidings/zh-CN.yml
@@ -236,6 +236,7 @@
       System:
         1_end: "你提交的发布视频申请:%s,审核已通过"
         2_end: "你提交的发布视频申请:%s,审核未通过<br/><span>原因:%{reason}</span>"
+      PublishCourseVideo_end: "在课堂 %s 发布了视频,观看视频可以增加平时分哦~"
     PublicCourseStart_end: "你报名参与的开放课程:%s,将于%s正式开课"
     SubjectStartCourse_end: "您创建的开放课程:%s 已达到开课人数要求。您可以在24小时内自主开设新一期课程。如果超过24小时未开课,平台将自动开课并复制您上一期的课程内容。"
     LiveLink_end: "%s 直播将于30分钟后开始"