Class: EducodeSales::SubjectsController

Inherits:
ApplicationController show all
Defined in:
app/controllers/educode_sales/subjects_controller.rb

Overview

实践课程管理

Instance Method Summary collapse

Methods inherited from ApplicationController

#authenticate_admin, #authenticate_request, #current_user, #filter, #is_commissioner_above?, #paginate, #render_failure, #render_success, #subject_members, #subject_staffs, #subject_url

Methods included from ApplicationHelper

#add_businesses_score, #base_url, #collection_amount_score, #completion_rate, #current?, #disk_filename, #get_businesses_chart, #handled_data, #handled_time_data, #handled_time_data_accurate, #relative_path, #signed_amount_score, #storage_path, #url_to_avatar, #visits_score

Instance Method Details

#createObject



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'app/controllers/educode_sales/subjects_controller.rb', line 99

def create
  # 开启事务
  ActiveRecord::Base.transaction do
    subject = Subject.where.not(id: BusinessSubject.pluck(:subject_id) ).find_by(id: params[:subject_id])
    if subject.present?
      attr = {
        deliver_date: params[:deliver_date],
        school_id: params[:school_id],
        subject_id: subject.id
      }
      new_object = BusinessSubject.create(attr)

      new_object.add_subject_shixuns(params[:shixun_ids].split(',').map(&:to_i))


      BusinessSubjectStaff.bulk_insert(:staff_id, :container_id, :container_type, :created_at, :updated_at, :category) do |d|
        params[:manage_id].split(",").each do |m|
          d.add [m.to_i, new_object.id, BusinessSubjectStaff::CONTAINER_TYPES::SUBJECT, Time.now, Time.now, BusinessSubjectStaff::CATEGORY_TYPES::SUBJECT_MANAGE ]
        end
        params[:sale_id].split(",").each do |s|
          d.add [s.to_i, new_object.id, BusinessSubjectStaff::CONTAINER_TYPES::SUBJECT, Time.now, Time.now, BusinessSubjectStaff::CATEGORY_TYPES::SUBJECT_STAFF ]
        end
      end

      render_success
    else
      render_failure("操作失败")
    end
  end
end

#editObject



85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'app/controllers/educode_sales/subjects_controller.rb', line 85

def edit
  if params[:id]
    business_subject = BusinessSubject.find(params[:id])
    @item = business_subject.subject
    @select = @item.shixuns.map { |item| { value: item.id, name: item.name } }
    @id = business_subject.id
    @school_id = business_subject.school_id
    @default_select = BusinessSubjectShixun.where("business_subject_id = #{params[:id]}")
                                           .select(:shixun_id)
                                           .map { |item| item.shixun_id }
    render layout: false
  end
end

#have_permission?Boolean

查看cancancan Gem优化代码

Returns:

  • (Boolean)


12
13
14
15
16
# File 'app/controllers/educode_sales/subjects_controller.rb', line 12

def have_permission?
  have_permission = can?(:subject, EducodeSales::BusinessDeliverSubject) || can?(:all_subject, EducodeSales::BusinessDeliverSubject)

  have_permission || @current_admin.is_admin ? "" : authorize!(:subject, BusinessDeliverSubject)
end

#indexObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'app/controllers/educode_sales/subjects_controller.rb', line 19

def index
  respond_to do |format|
    format.html do
    end
    format.json do

      if is_commissioner_above?("subject")
        business_subject = BusinessSubject
      else
        deliver_staffs = BusinessSubject.joins(business_deliver_subject: [manages: :staff])
        business_subject_ids  = deliver_staffs.where("educode_sales_staffs.id = #{@current_admin.id} ").ids
        subject_staffs = BusinessSubject.where(id: BusinessSubject.ids - deliver_staffs.ids).joins(manages: :staff).where("educode_sales_staffs.id = #{@current_admin.id} ")
        business_subject = BusinessSubject.where(id: business_subject_ids + subject_staffs.ids )
      end


      business_subject = business_subject.joins(:subject).left_joins(business_deliver_subject: [:manages, business: [:last_follow_up]])
      if params[:q] && params[:q][:business_name].present?
        business_subject = business_subject.joins(:business).where("educode_sales_businesses.name like '%#{params[:q][:business_name]}%' ")
      end
      # 查询条件 实践课程名称
      if params[:q] && params[:q][:subjects_name].present?
        business_subject = business_subject.where("subjects.name like '%#{params[:q][:subjects_name]}%'")
      end
      # 查询条件 时间
      if params[:q] && params[:q][:time].present?
        start_time = params[:q][:time].split(" - ")[0]
        end_time = params[:q][:time].split(" - ")[-1]
        business_subject = business_subject.where("educode_sales_business_subjects.deliver_date BETWEEN '#{start_time}' AND '#{end_time}' OR
                                                   educode_sales_follow_ups.reception_at BETWEEN'#{start_time}' AND '#{end_time}'")
      end
      business_subject_schools = {}
      business_subject_manges = {}
      business_subject_staffs = {}
      BusinessSubject.includes(:school, :manages, :subject, :staffs, business_deliver_subject: [:manages, business: [:school, last_follow_up: :assign_follow_ups] ]).find_each do |d|
        b_d_s = d.business_deliver_subject

        b_d_s.present? ? business_subject_schools.merge!({ "#{d.id}": d.business.school.name }) : business_subject_schools.merge!({"#{d.id}": d.school_name })
        b_d_s.present? ? business_subject_manges.merge!({ "#{d.id}":  b_d_s.subject_manages}) : business_subject_manges.merge!({"#{d.id}": d.manges_name })
        b_d_s.present? ? business_subject_staffs.merge!({ "#{d.id}": d.business.staff_name }) : business_subject_staffs.merge!({"#{d.id}": d.staffs_name })

      end
      @business_subject_schools = business_subject_schools.stringify_keys
      @business_subject_staffs = business_subject_staffs.stringify_keys
      @business_subject_manges = business_subject_manges.stringify_keys
      business_subject = business_subject.select("subjects.name s_name,
                                                  subjects.identifier s_identifier,
                                                  subjects.status status,
                                                  subjects.public public,
                                                  educode_sales_business_subjects.id,
                                                  IFNULL(educode_sales_follow_ups.reception_at, educode_sales_business_subjects.deliver_date) deliver_date,
                                                  IFNULL(educode_sales_businesses.name, '--' ) b_name
                                                  ")
      @count = business_subject.count("educode_sales_business_subjects.id")
      @business_subject = business_subject.page(params[:page]).per(params[:limit])

    end
  end
end

#list_shixunsObject



188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'app/controllers/educode_sales/subjects_controller.rb', line 188

def list_shixuns
    @shixuns = BusinessSubjectShixun.left_joins(:last_dectect)
                                    .left_joins(:shixun)
                                    .where("business_subject_id = #{params[:id]}")

    if params[:q].present? && params[:q][:shixun_name].present?
      @shixuns = @shixuns.where("shixuns.name like '%#{params[:q][:shixun_name]}%'")
    end
    if params[:q].present? && params[:q][:level].present? && params[:q][:level].to_i != 0
      @shixuns = @shixuns.where(level: params[:q][:level])
    end
    # 项目状态 0 全部
    if params[:q].present? && params[:q][:status].present? && params[:q][:status].to_i != 0
      @shixuns = @shixuns.where(shixun_status: params[:q][:status])
    end
    if params[:q].present? && params[:q][:category].present? && params[:q][:category].to_i != 0
      @shixuns = @shixuns.where(category: params[:q][:category])
    end
    @shixuns = @shixuns.select("educode_sales_business_subject_shixuns.*,
                                shixuns.name as shixun_name,
                                shixuns.identifier,
                                educode_sales_shixun_dectects.created_at as shixun_dectects_time,
                                educode_sales_shixun_dectects.content as dectects_content")
                       .page(params[:page])
                       .per(params[:limit])
  render layout: false
end

#newObject



79
80
81
82
83
# File 'app/controllers/educode_sales/subjects_controller.rb', line 79

def new
  # staffs = Staff.where.not(role_id: 11).includes(:user)
  gon.staffs = @manages
  render layout: false
end

#search_subjectsObject



175
176
177
178
179
180
181
182
183
184
185
186
# File 'app/controllers/educode_sales/subjects_controller.rb', line 175

def search_subjects
  respond_to do |format|
    format.json do
      @subjects = Subject.unhidden.where.not(id: BusinessSubject.pluck(:subject_id)).where("name like '%#{params[:q]}%' ").page(params[:page]).per(10)
      render json: {
        data: @subjects.map { |item| { value: item[:id], name: item[:name] } },
        code: 0,
        count: @subjects.total_count / 10
      }
    end
  end
end

#shixun_listObject



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'app/controllers/educode_sales/subjects_controller.rb', line 157

def shixun_list
  respond_to do |format|
    format.json do
      @list = Shixun.where(hidden: 0).where.not(id: BusinessSubjectShixun.joins(:business_subject).pluck(:shixun_id) ) # 1先创建实践项目下,通过实践课程无法选该
      if params[:q].present?
        @list = @list.where("name like '%#{params[:q]}%'").page(params[:page]).per(10)
      end
      if params[:subject_id].present?
        @list = @list.joins(:stage_shixuns).where("subject_id = #{params[:subject_id]}")
      end
      render json: {
        data: @list.map { |item| { value: item[:id], name: item[:name] } },
        code: 0
      }
    end
  end
end

#updateObject



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'app/controllers/educode_sales/subjects_controller.rb', line 130

def update
  # 开启事务
  ActiveRecord::Base.transaction do
    business_subject = BusinessSubject.find_by(id: params[:id])

    if business_subject
      new_shixun_ids = params[:shixun_ids].split(',').map(&:to_i)
      old_shixun_ids = business_subject.business_subject_shixuns.pluck(:shixun_id)

      # 需要删除的科目id
      change_shixuns = old_shixun_ids - new_shixun_ids
      add_shixuns = new_shixun_ids - old_shixun_ids

      # 取消关联
      business_subject.business_subject_shixuns.where(shixun_id: change_shixuns).update_all(business_subject_id: nil)

      # 创建关联
      business_subject.add_subject_shixuns(add_shixuns)

      render_success
    else
      render_failure("操作失败")
    end

  end
end