diff --git a/app/controllers/subjects_controller.rb b/app/controllers/subjects_controller.rb
index ba9b13f85..b7b208842 100644
--- a/app/controllers/subjects_controller.rb
+++ b/app/controllers/subjects_controller.rb
@@ -415,6 +415,20 @@ class SubjectsController < ApplicationController
order by ue_count desc limit 10")
end
+ # 预约报名
+ def appointment
+ tip_exception("还存在未结束的课堂") unless @subject.max_course_end_date.nil? || @subject.max_course_end_date < Date.today
+ tip_exception("无需重复报名") if @subject.subject_appointments.exists?(user_id: current_user.id)
+ ActiveRecord::Base.transaction do
+ @subject.subject_appointments << SubjectAppointment.new(user_id: current_user.id)
+ @subject.increment!(:participant_count)
+ if @subject.participant_count == @subject.student_count
+ @subject.start_course_notify
+ end
+ normal_status("预约成功")
+ end
+ end
+
private
def subject_params
tip_exception("实训路径名称不能为空") if params[:name].blank?
diff --git a/app/decorators/tiding_decorator.rb b/app/decorators/tiding_decorator.rb
index 851c3d075..b4f851e5f 100644
--- a/app/decorators/tiding_decorator.rb
+++ b/app/decorators/tiding_decorator.rb
@@ -384,4 +384,8 @@ module TidingDecorator
def public_course_start_content
I18n.t(locale_format) % [belong_container&.name, belong_container&.start_date&.strftime("%Y-%m-%d")]
end
+
+ def subject_start_course_content
+ I18n.t(locale_format) % belong_container&.name
+ end
end
diff --git a/app/models/subject.rb b/app/models/subject.rb
index d5ea3f433..d29a97d0c 100644
--- a/app/models/subject.rb
+++ b/app/models/subject.rb
@@ -13,6 +13,8 @@ class Subject < ApplicationRecord
has_many :stage_shixuns, dependent: :destroy
has_many :shixuns, through: :stage_shixuns
+ has_many :subject_appointments, dependent: :destroy
+
has_many :subject_members, ->{ order("subject_members.position asc")}, dependent: :destroy
has_many :users, through: :subject_members
has_many :tidings, as: :container, dependent: :destroy
@@ -44,6 +46,10 @@ class Subject < ApplicationRecord
courses.where("start_date <= '#{Date.today}' and end_date >= '#{Date.today}'").count > 0
end
+ def has_participate?
+ subject_appointments.exists?(user_id: User.current.id)
+ end
+
# 挑战过路径的成员数(金课统计去重后的报名人数)
def member_count
excellent ? CourseMember.where(role: 4, course_id: courses.pluck(:id)).pluck(:user_id).length : shixuns.pluck(:myshixuns_count).sum
@@ -109,4 +115,10 @@ class Subject < ApplicationRecord
def learning? user_id
Myshixun.where(user_id: user_id, shixun_id: shixuns).exists?
end
+
+ def start_course_notify
+ Tiding.create(user_id: user_id, trigger_user_id: 0, container_id: id,
+ container_type: 'SubjectStartCourse', belong_container_id: id,
+ belong_container_type: 'Subject', tiding_type: 'System')
+ end
end
\ No newline at end of file
diff --git a/app/models/subject_appointment.rb b/app/models/subject_appointment.rb
new file mode 100644
index 000000000..a4bc53b18
--- /dev/null
+++ b/app/models/subject_appointment.rb
@@ -0,0 +1,4 @@
+class SubjectAppointment < ApplicationRecord
+ belongs_to :subject
+ belongs_to :user
+end
diff --git a/app/views/subjects/show.json.jbuilder b/app/views/subjects/show.json.jbuilder
index 997e0c3d6..7febcd789 100644
--- a/app/views/subjects/show.json.jbuilder
+++ b/app/views/subjects/show.json.jbuilder
@@ -24,4 +24,10 @@ if @subject.excellent
json.course_identity @user.course_identity(course)
json.course_status subject_course_status course
end
+
+ if @subject.max_course_end_date.nil? || @subject.max_course_end_date < Date.today
+ json.student_count @subject.student_count
+ json.participant_count @subject.participant_count
+ json.has_participate @subject.has_participate?
+ end
end
\ No newline at end of file
diff --git a/config/locales/tidings/zh-CN.yml b/config/locales/tidings/zh-CN.yml
index d729eeabd..557f3f79b 100644
--- a/config/locales/tidings/zh-CN.yml
+++ b/config/locales/tidings/zh-CN.yml
@@ -227,3 +227,4 @@
1_end: "你提交的发布视频申请:%s,审核已通过"
2_end: "你提交的发布视频申请:%s,审核未通过
原因:%{reason}"
PublicCourseStart_end: "你报名参与的开放课程:%s,将于%s正式开课"
+ SubjectStartCourse_end: "您创建的开放课程:%s 已达到开课人数要求。您可以在24小时内自主开设新一期课程。如果超过24小时未开课,平台将自动开课并复制您上一期的课程内容。"
diff --git a/config/routes.rb b/config/routes.rb
index 2855fa36f..af7e0ee3b 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -284,6 +284,7 @@ Rails.application.routes.draw do
post :up_member_position
post :down_member_position
get :right_banner
+ post :appointment
end
collection do
diff --git a/db/migrate/20190921010411_add_course_member_to_subjects.rb b/db/migrate/20190921010411_add_course_member_to_subjects.rb
new file mode 100644
index 000000000..76df05604
--- /dev/null
+++ b/db/migrate/20190921010411_add_course_member_to_subjects.rb
@@ -0,0 +1,6 @@
+class AddCourseMemberToSubjects < ActiveRecord::Migration[5.2]
+ def change
+ add_column :subjects, :student_count, :integer, :default => 0
+ add_column :subjects, :participant_count, :integer, :default => 0
+ end
+end
diff --git a/db/migrate/20190921015840_create_subject_appointments.rb b/db/migrate/20190921015840_create_subject_appointments.rb
new file mode 100644
index 000000000..68c924de7
--- /dev/null
+++ b/db/migrate/20190921015840_create_subject_appointments.rb
@@ -0,0 +1,10 @@
+class CreateSubjectAppointments < ActiveRecord::Migration[5.2]
+ def change
+ create_table :subject_appointments do |t|
+ t.references :subject, index: true
+ t.references :user, index: true
+
+ t.timestamps
+ end
+ end
+end
diff --git a/spec/models/subject_appointment_spec.rb b/spec/models/subject_appointment_spec.rb
new file mode 100644
index 000000000..9b8d2e1bd
--- /dev/null
+++ b/spec/models/subject_appointment_spec.rb
@@ -0,0 +1,5 @@
+require 'rails_helper'
+
+RSpec.describe SubjectAppointment, type: :model do
+ pending "add some examples to (or delete) #{__FILE__}"
+end