From aa3063a931be08523b9661dc66250c03d93bd9ad Mon Sep 17 00:00:00 2001 From: cxt <853663049@qq.com> Date: Sat, 21 Sep 2019 10:57:26 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=91=E8=AF=BE=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/subjects_controller.rb | 14 ++++++++++++++ app/decorators/tiding_decorator.rb | 4 ++++ app/models/subject.rb | 8 ++++++++ app/models/subject_appointment.rb | 4 ++++ app/views/subjects/show.json.jbuilder | 5 +++++ config/locales/tidings/zh-CN.yml | 1 + config/routes.rb | 1 + ...20190921010411_add_course_member_to_subjects.rb | 6 ++++++ .../20190921015840_create_subject_appointments.rb | 10 ++++++++++ spec/models/subject_appointment_spec.rb | 5 +++++ 10 files changed, 58 insertions(+) create mode 100644 app/models/subject_appointment.rb create mode 100644 db/migrate/20190921010411_add_course_member_to_subjects.rb create mode 100644 db/migrate/20190921015840_create_subject_appointments.rb create mode 100644 spec/models/subject_appointment_spec.rb diff --git a/app/controllers/subjects_controller.rb b/app/controllers/subjects_controller.rb index 36f14a5d5..16f9361f9 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..a58074e6f 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 @@ -109,4 +111,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..51311c062 100644 --- a/app/views/subjects/show.json.jbuilder +++ b/app/views/subjects/show.json.jbuilder @@ -24,4 +24,9 @@ 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 + 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 ce6fd57b0..4d6579066 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 ba851b2f2..32799286f 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