Class: EducodeSales::BusinessCoursesController

Inherits:
ApplicationController show all
Includes:
BusinessCoursesHelper, SubjectHelper
Defined in:
app/controllers/educode_sales/business_courses_controller.rb

Overview

商机交付课程

Instance Method Summary collapse

Methods included from SubjectHelper

#manges, #school_name, #staffs, #status

Methods included from BusinessCoursesHelper

#real_shixun_category, #real_shixun_level, #real_shixun_status

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

#business_paramsObject



260
261
262
# File 'app/controllers/educode_sales/business_courses_controller.rb', line 260

def business_params
  params.require(:business).permit(:status, :business_id)
end

#createObject



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'app/controllers/educode_sales/business_courses_controller.rb', line 126

def create
  # 开启事务
  ActiveRecord::Base.transaction do
    item = BusinessDeliverSubject.find_by(business_id: params[:business][:business_id])
    # 判断是否存在 不存在进入下一步

    if item.blank?
      add_item = BusinessDeliverSubject.new(business_params)

      add_item.add_subjects(params[:business][:subject_ids].split(",")) if add_item.save! && !params[:business][:subject_ids].blank?

      BusinessSubjectStaff.bulk_insert(:staff_id, :container_id, :container_type, :created_at, :updated_at, :category) do |d|
        params[:business][:manage_id].split(",").each do |m|
          d.add [m.to_i, add_item.id, BusinessSubjectStaff::CONTAINER_TYPES::BUSSINESS, Time.now, Time.now, BusinessSubjectStaff::CATEGORY_TYPES::BUSINESS_MANAGE]
        end
      end

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

#editObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'app/controllers/educode_sales/business_courses_controller.rb', line 61

def edit
  if params[:id]
    # 查询商机信息 和交付状态等
    @item = BusinessDeliverSubject.joins(:business)
                                  .select("educode_sales_businesses.name,
                                   educode_sales_business_deliver_subjects.status,
                                   educode_sales_business_deliver_subjects.id")
                                  .find(params[:id])
    # 查询课程是否已经有实践课程
    @subjects = @item.business_subjects.pluck(:subject_id)
    @subjects = Subject.where(id: @subjects).map { |item| { value: item[:id], name: item[:name] } }
    gon.staffs = @manages

    @value_list = BusinessSubjectStaff.where(container_type: BusinessSubjectStaff::CONTAINER_TYPES::BUSSINESS,
                                             container_id: params[:id])
                                      .select(:staff_id)
                                      .map { |item| item[:staff_id] }

    render layout: false
  end
end

#have_permission?Boolean

查看cancancan Gem优化代码

Returns:

  • (Boolean)


13
14
15
16
17
# File 'app/controllers/educode_sales/business_courses_controller.rb', line 13

def have_permission?
  have_permission = can?(:business, EducodeSales::BusinessDeliverSubject) || can?(:all_business, EducodeSales::BusinessDeliverSubject)

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

#indexObject



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
# File 'app/controllers/educode_sales/business_courses_controller.rb', line 21

def index
  respond_to do |format|
    format.html do
    end
    format.json do
      @business_deliver = BusinessDeliverSubject.all

      unless is_commissioner_above?
        business_ids = @business_deliver.pluck(:business_id)
        last_follow_staff_business = Business.where(id: business_ids).joins(last_follow_up: [assign_follow_ups: [staff: :user]]).where("CONCAT(users.lastname, users.firstname) = '#{@current_admin.user.real_name}' ")

        last_follow_staff_business_ids = last_follow_staff_business.pluck("educode_sales_businesses.id")
        staff_business = Business.where(id: business_ids - last_follow_staff_business_ids).joins(staff: :user).where("CONCAT(users.lastname, users.firstname) = '#{@current_admin.user.real_name}' ")
        @business_deliver = BusinessDeliverSubject.where(business_id: last_follow_staff_business_ids + staff_business.ids)
      end

      @business_deliver = @business_deliver.joins(business: [:school, :staff, :last_follow_up])

      # 查询条件 交付状态 state == 0 代表全部
      if params[:q] && params[:q][:state].present? && params[:q][:state] != '0'
        @business_deliver = @business_deliver.where("educode_sales_business_deliver_subjects.status = :status", status: params[:q][:state])
      end
      # 查询条件 商机id
      if params[:q] && params[:q][:name].present?
        @business_deliver = @business_deliver.where("educode_sales_businesses.name like '%#{params[:q][:name]}%'")
      end
      # 查询条件 时间
      if params[:q] && params[:q][:time].present?
        start_time = params[:q][:time].split(" - ")[0]
        end_time = params[:q][:time].split(" - ")[-1]
        @business_deliver = @business_deliver.where("educode_sales_follow_ups.reception_at BETWEEN '#{start_time}' AND '#{end_time}' ")
      end
      # 分页查询
      @business_deliver = @business_deliver.includes(business: [:school, :staff], business_subjects: :subject) \
                                           .page(params[:page])
                                           .per(params[:limit])
    end
  end
end

#list_shixunsObject



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'app/controllers/educode_sales/business_courses_controller.rb', line 176

def list_shixuns
  respond_to do |format|
    format.html do
      render layout: false
    end
    format.json do
      shixuns = BusinessSubjectShixun
      if params[:id].present?
        business_deliver = BusinessDeliverSubject.find_by(id: params[:id])
        shixuns = BusinessSubjectShixun.joins(:shixun, business_subject: [:business_deliver_subject, :subject]).left_joins(:last_dectect).where("educode_sales_business_deliver_subjects.id = #{business_deliver.id }")
      elsif params[:subject_id].present?
        shixuns = BusinessSubjectShixun.joins(:shixun, business_subject: [:business_deliver_subject, :subject]).left_joins(:last_dectect).where("educode_sales_business_subjects.id = #{params[:subject_id] }")
      end

      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][:subject_name].present?
        shixuns = shixuns.where("subjects.name like '%#{params[:q][:subject_name]}%'")
      end
      shixuns = shixuns.search_category(params[:q][:category]) if params[:q].present?
      shixuns = shixuns.search_status(params[:q][:status]) if params[:q].present?
      shixuns = shixuns.search_level(params[:q][:level]) if params[:q].present?
      @count = shixuns.count

      @shixuns = shixuns.select("educode_sales_business_subject_shixuns.*, shixuns.name  s_name, shixuns.identifier s_identifier,
                                 subjects.name as subjects_name,subjects.identifier as subjects_identifier,
                                 educode_sales_shixun_dectects.content dectect_content, educode_sales_shixun_dectects.date dectect_time ")
                        .page(params[:page]).per(params[:limit])
    end
  end
end

#list_subjectsObject



209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'app/controllers/educode_sales/business_courses_controller.rb', line 209

def list_subjects
  respond_to do |format|
    format.html do
      render layout: false
    end
    format.json do
      # 商机交付课程id
      bussiness_deliver_id = params[:id].to_i

      # subject.status =>  :0 编辑中  1 审核中  2 发布
      # public: 2: 公开
      subjects = BusinessSubject.joins("INNER JOIN subjects bs ON bs.id = educode_sales_business_subjects.subject_id
                                        INNER JOIN educode_sales_business_deliver_subjects e_s_b_d_s ON educode_sales_business_subjects.business_deliver_subject_id = e_s_b_d_s.id and e_s_b_d_s.id = #{bussiness_deliver_id}
                                        INNER JOIN educode_sales_businesses esb ON esb.id = educode_sales_business_subjects.business_id
                                        LEFT JOIN educode_sales_follow_ups fu ON fu.id = esb.last_follow_up_id
                                       ")

      # 搜索条件
      if params[:q].present? && params[:q][:name].present?
        subjects = subjects.where("bs.name like '%#{params[:q][:name]}%'")
      end
      if params[:q].present? && params[:q][:p_state].present?
        status = params[:q][:p_state].to_i
        if status == 1
          subjects = subjects.where("bs.public = #{status}")
        elsif [0, 2].include? status
          subjects = subjects.where("bs.status = #{status} and public not in (1,2) ")
        else
          subjects = subjects.where("bs.public = 2")
        end
      end
      @subjects = subjects.includes(business_subject_shixuns: :shixun).select("educode_sales_business_subjects.*, bs.identifier, bs.status status, bs.name name, bs.public, IFNULL(fu.reception_at, '--') reception_at").page(params[:page]).per(params[:limit])

    end
  end

end

#newObject



117
118
119
120
121
122
123
124
# File 'app/controllers/educode_sales/business_courses_controller.rb', line 117

def new
  staffs = Staff.where.not(role_id: 11).includes(:user)
  @businesses = Business
  # 要求只能是已签单的商机
  gon.stage_id = Common.where(clazz: '商机阶段', name: ['已签单']).pluck(:id)[0]
  gon.staffs = @manages
  render layout: false
end

#select_businessObject



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'app/controllers/educode_sales/business_courses_controller.rb', line 150

def select_business
  respond_to do |format|
    format.json do
      stage_id = Common.where(clazz: '商机阶段', name: ['已签单']).take.id.to_i
      @businesses = Business.joins(:last_follow_up, :school)
                            .where("educode_sales_businesses.name LIKE '%#{params[:q]}%' and educode_sales_follow_ups.stage_id = #{stage_id} ")
                            .where("educode_sales_businesses.id not in (?) ", BusinessDeliverSubject.pluck(:business_id).map(&:to_i)) # todo 数组中包含nil会导致查询失败,无法找到数据


      common = Common.find_by(clazz: 'staff_type', name: '销售')
      @staffs = Staff.find(current_user.id)
      if @staffs.present? && common.id.to_i == @staffs.job_type.to_i
        @businesses = @businesses.where(staff_id:current_user.id)
      end
      @count = @businesses.count("educode_sales_businesses.id")
      @businesses = @businesses.select("educode_sales_businesses.*, schools.name as s_name, schools.id as s_id")
                               .page(params[:page]).per(10)
      render json: {
        data: @businesses,
        code: 0
      }
    end
  end

end

#subjectsObject



247
248
249
250
251
252
253
254
255
256
257
258
# File 'app/controllers/educode_sales/business_courses_controller.rb', line 247

def subjects
  respond_to do |format|
    format.json do
      @subjects = Subject.unhidden.where.not(id: BusinessSubject.joins(:business).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

#updateObject



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'app/controllers/educode_sales/business_courses_controller.rb', line 83

def update
  # 开启事务
  ActiveRecord::Base.transaction do
    @business = BusinessDeliverSubject.find_by(id: params[:id])
    # 修改状态
    if !@business.blank?
      @business.update(params.require(:business).permit(:status))

      old_subject_ids = @business.business_subjects.pluck(:subject_id)
      new_subject_ids = params[:business][:subject_ids].split(',').map(&:to_i)

      del_arr = old_subject_ids - new_subject_ids
      # 需要新增的科目id
      add_arr = new_subject_ids - old_subject_ids

      @business.business_subjects.where(subject_id: del_arr).update_all(business_id: nil, business_deliver_subject_id: nil)

      @business.add_subjects(add_arr)

      @business.manages.destroy_all

      BusinessSubjectStaff.bulk_insert(:staff_id, :container_id, :container_type, :created_at, :updated_at, :category) do |d|
        params[:business][:manage_id].split(",").each do |m|
          d.add [m.to_i, @business.id, BusinessSubjectStaff::CONTAINER_TYPES::BUSSINESS, Time.now, Time.now, BusinessSubjectStaff::CATEGORY_TYPES::BUSINESS_MANAGE]
        end
      end

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