Class: EducodeSales::CustomersController

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

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

#batch_update_school_tagsObject



417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
# File 'app/controllers/educode_sales/customers_controller.rb', line 417

def batch_update_school_tags
  xlsx = Roo::Spreadsheet.open(params[:file])
  ods = xlsx.sheet(0)
  rows = ods.last_row - 1
  i = 0
  school_tag_hash = SchoolTag.where(for_missions: true).pluck(:name, :id).to_h
  rows.times do |r|  #行数
    i += 1
    next unless ods.row(r+1)[0]
    school = School.find_by(name: ods.row(r+1)[0].to_s.strip)
    school_list = EducodeSales::CustomerAdd::SCHOOL_LIST.invert
    if school
      property_names = ods.row(r+1)[2].to_s.strip
      tags = []
      if property_names
        property_names.gsub("", " ").gsub(",", " ").split(" ").each do |tag|
          if school_tag_hash[tag]
            tags << SchoolTagMiddle.find_or_initialize_by(school_id: school.id, school_tag_id: school_tag_hash[tag])
          end
        end
      end
      school.school_tag_middles = tags
    end
  end

  SchoolTagMiddle.where(school_id: nil).delete_all

  render json: { succcess: true}
end

#createObject

def create

ActiveRecord::Base.transaction do
  property = SchoolProperty.find_or_create_by!(project_985: params[:project_985].present? ? 1 : 0,
                                               project_211: params[:project_211].present? ? 1 : 0,
                                               regular_college: params[:regular_college].present? ? 1 : 0,
                                               junior_college: params[:junior_college].present? ? 1 : 0,
                                               secondary_school: params[:secondary_school].present? ? 1 : 0,
                                               military_school: params[:military_school].present? ? 1 : 0,
                                               enterprise: params[:enterprise].present? ? 1 : 0)
  @school = School.new
  @school.attributes = school_params
  @school.school_property = property
  @school.save!
  EducodeSales::CustomerExtension.create(customer_staff_id: @current_admin.id, school_id: @school.id)
end
render_success

end



392
393
394
395
396
397
# File 'app/controllers/educode_sales/customers_controller.rb', line 392

def create
  params[:school_ids].each do |d|
    CustomerAdd.create(school_id: d)
  end
  render_success
end

#create_departmentObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'app/controllers/educode_sales/customers_controller.rb', line 10

def create_department
  department_name = params[:department_name].to_s.strip
  school = School.find(params[:school_id])

  return render_failure('部门名称重复') if school.departments.exists?(name: department_name)

  ActiveRecord::Base.transaction do
    department = school.departments.create!(name: department_name, is_auth: 1)
    ApplyAddDepartment.create!(school_id: school.id, status: 1, name: department.name,
                               department_id: department.id, user_id: current_user.id)
  end

  render_success
end

#delete_majorObject



293
294
295
296
297
298
299
300
301
# File 'app/controllers/educode_sales/customers_controller.rb', line 293

def delete_major
  department = Department.find(params[:id])
  major = department.department_majors.find(params[:major_id])
  if major.destroy
    render_success
  else
    render_failure major
  end
end

#editObject



399
400
401
402
403
# File 'app/controllers/educode_sales/customers_controller.rb', line 399

def edit
  @school = School.find(params[:id])
  @school_property = @school.school_property
  render layout: false
end

#edit_departmentObject



25
26
27
28
# File 'app/controllers/educode_sales/customers_controller.rb', line 25

def edit_department
  @department = Department.find(params[:id])
  render layout: false
end

#edit_follow_recordObject



365
366
367
368
369
# File 'app/controllers/educode_sales/customers_controller.rb', line 365

def edit_follow_record
  @follow_up = EducodeSales::CustomerFollow.find(params[:id])
  @school = School.find(@follow_up.school_id)
  render layout: false
end

#edit_majorObject



405
406
407
408
409
410
411
412
413
414
415
# File 'app/controllers/educode_sales/customers_controller.rb', line 405

def edit_major
  @school = School.find(params[:id])
  @school_property = @school.school_property
  if params[:major].present?
    @major = DepartmentMajor.find_by(id: params[:major])
  end
  if params[:department_id].present?
    @department = Department.find_by(id: params[:department_id])
  end
  render layout: false
end

#giveObject



303
304
305
306
307
# File 'app/controllers/educode_sales/customers_controller.rb', line 303

def give
  common = Common.find_by(clazz: 'staff_type', name: '销售')
  @staffs = Staff.joins(:user).where(job_type: common.id).map { |d| [d.user.real_name, d.id] }
  render layout: false
end

#indexObject



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
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
156
157
158
159
160
161
162
# File 'app/controllers/educode_sales/customers_controller.rb', line 60

def index
  authorize! :read, Customer
  respond_to do |format|
    format.html do
      common = Common.find_by(clazz: 'staff_type', name: '销售')
      @staffs = Staff.joins(:user).where(job_type: common.id).map { |d| [d.user.real_name, d.id] }
      gon.school_tags = SchoolTag.where(for_missions: true).map { |d| {value: d.id, name: d.name } }
    end
    format.json do
      if @current_admin.is_admin?
        @customers = School.all
      else
        level = @current_admin.role.role_areas.find_by(clazz: '客户管理').level
        case level
        when '自己'
          school_ids = CustomerExtension.where(customer_staff_id: @current_admin.id).pluck(:school_id)
          @customers = School.where(id: school_ids)
        when '区域'
          a_school_ids = School.where(province: @current_admin.areas.pluck(:name)).ids
          b_school_ids = CustomerExtension.where(customer_staff_id: @current_admin.id).pluck(:school_id)
          school_ids = a_school_ids + b_school_ids + StaffSchool.where(staff_id: @current_admin.id).pluck(:school_id)
          @customers = School.where(id: school_ids)
        else
          @customers = School.all
        end
      end
      part_a_ids = CustomerFollow.all.pluck(:school_id)
      part_b_ids = Business.where(id: EducodeSales::FollowUp.pluck(:business_id)).pluck(:school_id)
      ids = part_a_ids + part_b_ids + CustomerAdd.all.pluck(:school_id)
      @customers = @customers.where(id: ids)
      if params[:q].present? && params[:q][:name].present?
        @customers = @customers.where("schools.name like ?", "%#{params[:q][:name]}%")
      end
      if params[:q].present? && params[:q][:area].present?
        @customers = @customers.where("schools.province = ?", "#{params[:q][:area]}")
      end
      if params[:q].present? && params[:q][:staff_id].present?
        school_ids = EducodeSales::CustomerExtension.where(customer_staff_id: params[:q][:staff_id]).pluck(:school_id)
        @customers = @customers.where(id: school_ids)
      end
      if params[:q].present? && params[:q][:property].present?
        property = params[:q][:property].split(",").map(&:to_i)
        # property.each do |p|
        #   case p
        #   when 0
        #     school_property_ids += SchoolProperty.where(project_985: true).ids
        #   when 1
        #     school_property_ids += SchoolProperty.where(project_211: true).ids
        #   when 2
        #     school_property_ids += SchoolProperty.where(regular_college: true).ids
        #   when 3
        #     school_property_ids += SchoolProperty.where(junior_college: true).ids
        #   when 4
        #     school_property_ids += SchoolProperty.where(secondary_school: true).ids
        #   when 5
        #     school_property_ids += SchoolProperty.where(military_school: true).ids
        #   when 6
        #     school_property_ids += SchoolProperty.where(enterprise: true).ids
        #   when 7
        #     school_property_ids += SchoolProperty.where(mid_school: true).ids
        #   when 8
        #     school_property_ids += SchoolProperty.where(ele_school: true).ids
        #   when 9
        #     school_property_ids += SchoolProperty.where(other: true).ids
        #   else
        #   end
        # end
        @customers = @customers.joins(:school_tags).where("school_tags.id in (?)", property).distinct
        # @customers = @customers.where(school_property_id: school_property_ids)
      end

      if params[:q].present? && params[:q][:date].present?
        ids = EducodeSales::CustomerFollow.all.pluck(:school_id)
        @customers = @customers.where(id: ids)
        school_ids = []
        date = params[:q][:date].split(" - ")
        @customers.each do |d|
          business_ids = EducodeSales::Business.where(school_id: d.id).ids
          follow_ups = EducodeSales::FollowUp.where(business_id: business_ids)
          customer_follows = EducodeSales::CustomerFollow.where(school_id: d.id)
          next if follow_ups.blank? && customer_follows.blank?
          a_last_follow_time = follow_ups.last&.created_at&.to_s
          b_last_follow_time = customer_follows.last&.created_at&.to_s
          if a_last_follow_time.present? && b_last_follow_time.present?
            last_follow_time = ((a_last_follow_time < b_last_follow_time) ? b_last_follow_time : a_last_follow_time)
          else
            last_follow_time = (a_last_follow_time || b_last_follow_time)
          end
          if last_follow_time.present? && last_follow_time > date[0] && last_follow_time < date[1]
            school_ids << d.id
          end
        end
        @customers = @customers.where(id: school_ids)
      end

      if params[:page].present?
        @customers = @customers.order(id: :desc).page(params[:page]).per(params[:limit])
      else
        @customers = @customers.order(id: :desc)
      end
    end
  end
end

#listObject



164
165
166
167
168
169
170
171
172
173
174
175
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
208
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
# File 'app/controllers/educode_sales/customers_controller.rb', line 164

def list
  authorize! :read, Customer
  respond_to do |format|
    format.html do
      common = Common.find_by(clazz: 'staff_type', name: '销售')
      @staffs = Staff.includes(:user).where(job_type: common.id).map { |d| [d.user.real_name, d.id] }
      gon.school_tags = SchoolTag.where(for_missions: true).map { |d| {value: d.id, name: d.name } }
    end
    format.json do
      @o = EducodeSales::Common.find_by(extras: EducodeSales::Common::OTYPE)&.id
      @customers = School.from("
      (
        SELECT m.*, sc.school_business_count, de.department_business_count
        FROM
        (
          SELECT schools.id, departments.id AS department_id, COUNT(department_majors.id) AS major_count
          FROM schools
          LEFT JOIN departments ON schools.id = departments.school_id
          LEFT JOIN department_majors ON department_majors.department_id = departments.id
          GROUP BY schools.id, departments.id
        ) AS m
        LEFT JOIN  (
          SELECT schools.id, COUNT(educode_sales_businesses.id) AS school_business_count
          FROM schools
          LEFT JOIN educode_sales_businesses ON educode_sales_businesses.school_id = schools.id AND educode_sales_businesses.deleted_at is NULL
          GROUP BY schools.id
        ) AS sc ON m.id = sc.id
        LEFT JOIN (
          SELECT schools.id, departments.id AS department_id, COUNT(d.id) AS department_business_count
          FROM schools
          LEFT JOIN departments ON schools.id = departments.school_id
          LEFT JOIN educode_sales_businesses AS d ON d.department_id = departments.id AND d.deleted_at is NULL
          GROUP BY schools.id, departments.id
        ) AS de ON m.id = de.id AND m.department_id = de.department_id
      ) AS s")
      if @current_admin.is_admin?
        # @customers = School.all
      else
        level = @current_admin.role.role_areas.find_by(clazz: '客户管理').level
        case level
        when '自己'
          school_ids = CustomerExtension.where(customer_staff_id: @current_admin.id).pluck(:school_id)
          @customers = @customers.where(id: school_ids)
        when '区域'
          a_school_ids = School.where(province: @current_admin.areas.pluck(:name)).ids
          b_school_ids = CustomerExtension.where(customer_staff_id: @current_admin.id).pluck(:school_id)
          school_ids = a_school_ids + b_school_ids + StaffSchool.where(staff_id: @current_admin.id).pluck(:school_id)
          @customers = @customers.where(id: school_ids)
        else
          # @customers = School.all
        end
      end

      @customers = @customers.select("
        schools.*,
        s.school_business_count,
        s.department_business_count,
        s.major_count,
        s.department_id,
        departments.name AS department_name
      ").joins("
        JOIN schools ON s.id = schools.id
        LEFT JOIN departments ON schools.id = departments.school_id AND s.department_id = departments.id
      ")

      # part_a_ids = CustomerFollow.all.pluck(:school_id)
      # part_b_ids = Business.pluck(:school_id)
      # ids = part_a_ids + part_b_ids + CustomerAdd.all.pluck(:school_id)
      # @customers = @customers.where(id: ids.uniq)
      if params[:q].present? && params[:q][:name].present?
        @customers = @customers.where("schools.name like ?", "%#{params[:q][:name]}%")
      end
      if params[:q].present? && params[:q][:area].present?
        @customers = @customers.where("schools.province = ?", "#{params[:q][:area]}")
      end
      if params[:q].present? && params[:q][:staff_id].present?
        school_ids = EducodeSales::CustomerExtension.where(customer_staff_id: params[:q][:staff_id]).pluck(:school_id)
        @customers = @customers.where(id: school_ids)
      end
      if params[:q].present? && params[:q][:property].present?
        property = params[:q][:property].split(",").map(&:to_i)
        @customers = @customers.joins("JOIN school_tag_middles ON school_tag_middles.school_id = s.id INNER JOIN school_tags ON school_tags.id = school_tag_middles.school_tag_id").where("school_tags.id in (?)", property).distinct
      end

      if params[:q].present? && params[:q][:date].present?
        ids = EducodeSales::CustomerFollow.all.pluck(:school_id)
        @customers = @customers.where(id: ids)
        school_ids = []
        date = params[:q][:date].split(" - ")
        @customers.each do |d|
          business_ids = EducodeSales::Business.where(school_id: d.id).ids
          follow_ups = EducodeSales::FollowUp.where(business_id: business_ids)
          customer_follows = EducodeSales::CustomerFollow.where(school_id: d.id)
          next if follow_ups.blank? && customer_follows.blank?
          a_last_follow_time = follow_ups.last&.created_at&.to_s
          b_last_follow_time = customer_follows.last&.created_at&.to_s
          if a_last_follow_time.present? && b_last_follow_time.present?
            last_follow_time = ((a_last_follow_time < b_last_follow_time) ? b_last_follow_time : a_last_follow_time)
          else
            last_follow_time = (a_last_follow_time || b_last_follow_time)
          end
          if last_follow_time.present? && last_follow_time > date[0] && last_follow_time < date[1]
            school_ids << d.id
          end
        end
        @customers = @customers.where(id: school_ids)
      end

      @count = School.joins("LEFT JOIN departments ON schools.id = departments.school_id").count

      if params[:page].present?
        @customers = @customers.order(id: :desc).page(params[:page]).per(params[:limit])
      else
        @customers = @customers.order(id: :desc)
      end
    end
  end
end

#majorObject



46
47
48
49
50
51
52
53
54
# File 'app/controllers/educode_sales/customers_controller.rb', line 46

def major
  department = Department.find(params[:id])
  major = department.department_majors.new(name: params[:name])
  if major.save
    render_success
  else
    render_failure major
  end
end

#majorsObject



41
42
43
44
# File 'app/controllers/educode_sales/customers_controller.rb', line 41

def majors
  department = Department.find(params[:id])
  @majors = department.department_majors
end

#newObject



371
372
373
# File 'app/controllers/educode_sales/customers_controller.rb', line 371

def new
  render layout: false
end

#new_departmentObject



6
7
8
# File 'app/controllers/educode_sales/customers_controller.rb', line 6

def new_department
  render layout: false
end

#new_follow_recordObject



354
355
356
357
# File 'app/controllers/educode_sales/customers_controller.rb', line 354

def new_follow_record
  @school = School.find(params[:id])
  render layout: false
end

#new_majorObject



56
57
58
# File 'app/controllers/educode_sales/customers_controller.rb', line 56

def new_major
  render layout: false
end

#show_customer_followObject



326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
# File 'app/controllers/educode_sales/customers_controller.rb', line 326

def show_customer_follow
  respond_to do |format|
    format.html do
      @school_name = School.find(params[:id]).name
      @area = School.find(params[:id]).province
      @staff = EducodeSales::CustomerExtension.where(school_id: params[:id]).map { |d| d.customer_staff&.user&.real_name}.join("")
      render layout: false
    end
    format.json do
      if params[:department_id].present?
        @follow_ups = EducodeSales::CustomerFollow.where(department_id: params[:department_id])
      else
        @follow_ups = EducodeSales::CustomerFollow.where(school_id: params[:id])
      end

      @latest = @follow_ups.order(created_at: :desc).first
      @follow_ups = @follow_ups.order("created_at desc")
      @follow_ups = @follow_ups.page(params[:page]).per(params[:limit])
    end
  end
end

#show_departmentObject



348
349
350
351
352
# File 'app/controllers/educode_sales/customers_controller.rb', line 348

def show_department
  @departments = School.find(params[:id]).departments
  @departments = @departments.order("created_at desc")
  @departments = @departments.page(params[:page]).per(params[:limit])
end

#show_followObject



309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
# File 'app/controllers/educode_sales/customers_controller.rb', line 309

def show_follow
  respond_to do |format|
    format.html do
      @school_name = School.find(params[:id]).name
      @area = School.find(params[:id]).province
      @staff = EducodeSales::CustomerExtension.find_by(school_id: params[:id])&.customer_staff&.user&.real_name
      render layout: false
    end
    format.json do
      @follow_ups = EducodeSales::CustomerFollow.where(school_id: params[:id])
      @latest = @follow_ups.order(created_at: :desc).first
      @follow_ups = @follow_ups.order("created_at desc")
      @follow_ups = @follow_ups.page(params[:page]).per(params[:limit])
    end
  end
end

#show_follow_recordObject



359
360
361
362
363
# File 'app/controllers/educode_sales/customers_controller.rb', line 359

def show_follow_record
  @follow_up = EducodeSales::CustomerFollow.find(params[:id])
  @school = School.find(@follow_up.school_id)
  render layout: false
end

#show_majorsObject



30
31
32
33
# File 'app/controllers/educode_sales/customers_controller.rb', line 30

def show_majors
  @department = Department.find(params[:id])
  render layout: false
end

#updateObject



447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
# File 'app/controllers/educode_sales/customers_controller.rb', line 447

def update
  @school = School.find(params[:id])
  if params[:department_id].present?
    department = @school.departments.find_by(id: params[:department_id])
    if department
      department.update(name: params[:department])
      department.department_majors.find_by(id: params[:major_id])&.update(name: params[:major]) if params[:major_id].present?
    end
  end


  ActiveRecord::Base.transaction do
    property = SchoolProperty.find_or_create_by!(project_985: params[:project_985].present? ? 1 : 0,
                                                 project_211: params[:project_211].present? ? 1 : 0,
                                                 regular_college: params[:regular_college].present? ? 1 : 0,
                                                 junior_college: params[:junior_college].present? ? 1 : 0,
                                                 secondary_school: params[:secondary_school].present? ? 1 : 0,
                                                 military_school: params[:military_school].present? ? 1 : 0,
                                                 other: params[:other].present? ? 1 : 0,
                                                 mid_school: params[:mid_school].present? ? 1 : 0,
                                                 ele_school: params[:ele_school].present? ? 1 : 0,
                                                 enterprise: params[:enterprise].present? ? 1 : 0)
    @school.attributes = school_params
    @school.school_property = property
    @school.save!

    #先删除全部的标签,再添加标签
    missions_school_tags = SchoolTag.where(for_missions: true)
    SchoolTagMiddle.where(school_id: @school.id, school_tag_id: missions_school_tags.ids).destroy_all
    #保存学校标签

    keys = %w[regular_college junior_college secondary_school military_school other mid_school ele_school enterprise]
    names = EducodeSales::CustomerAdd::SCHOOL_LIST
    property.attributes.each do |key, value|
      next unless key.in?(keys)
      next unless value.to_s == "true"
      school_tag = SchoolTag.find_by(name: names[key], for_missions: true)
      if school_tag.present?
        SchoolTagMiddle.create(school_id: @school.id, school_tag_id: school_tag.id)
      end
    end

  end
  render_success
end

#update_departmentObject



35
36
37
38
39
# File 'app/controllers/educode_sales/customers_controller.rb', line 35

def update_department
  department = Department.find(params[:id])
  department.update(name: params[:department_name])
  render_success
end

#update_majorObject



283
284
285
286
287
288
289
290
291
# File 'app/controllers/educode_sales/customers_controller.rb', line 283

def update_major
  department = Department.find(params[:id])
  major = department.department_majors.find(params[:major_id])
  if major.update(name: params[:name])
    render_success
  else
    render_failure major
  end
end