Class: EducodeSales::StaffsController

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

Instance Method Summary collapse

Methods inherited from ApplicationController

#authenticate_admin, #authenticate_request, #current_user, #render_failure, #render_success

Instance Method Details

#createObject



35
36
37
38
39
40
41
42
43
# File 'app/controllers/educode_sales/staffs_controller.rb', line 35

def create
  user = User.find(params[:id])
  staff = Staff.new(user_id: user.id)
  if staff.save
    render_success
  else
    render_failure staff
  end
end

#destroyObject



61
62
63
64
65
66
67
# File 'app/controllers/educode_sales/staffs_controller.rb', line 61

def destroy
  staff = Staff.find(params[:id])
  staff.update(deleted_at: Time.now, expired_at: Time.now.days_ago(1))
  render_success
# rescue ActiveRecord::DeleteRestrictionError => e
#   render_failure '该用户已有数据产生,暂不能删除'
end

#disableObject



69
70
71
72
73
# File 'app/controllers/educode_sales/staffs_controller.rb', line 69

def disable
  staff = Staff.find(params[:id])
  staff.update(expired_at: Time.now.days_ago(1))
  render_success
end

#editObject



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

def edit
  @staff = Staff.find(params[:id])
  gon.area_ids = @staff.area_ids
  gon.areas = Common.where(clazz: 'area').map { |d| { value: d.id, title: d.name } } #.unshift({value: -1, title: '全国'})
  gon.school_ids = @staff.staff_schools.ids
  gon.schools = @staff.staff_schools.pluck(:school_id).map { |d| { value: d, name: "#{School.find(d)&.name}-#{School.find(d)&.province}" } } #.unshift({value: -1, title: '全国'})
  # gon.school_ids = School.all.ids
  # gon.schools = School.all.first(100).map { |d| { value: d.id, title: d.name } } #.unshift({value: -1, title: '全国'})
  @staff_types = Common.where(clazz: 'staff_type').pluck(:name, :id)
  render layout: false
end

#follow_up_departmentsObject



105
106
107
108
# File 'app/controllers/educode_sales/staffs_controller.rb', line 105

def follow_up_departments
  staff = Staff.find(params[:id])
  @schools = (EducodeSales::Business.joins(:follow_ups, [department: :school]).where("educode_sales_follow_ups.staff_id = #{staff.id}").select("departments.id, departments.name, schools.name AS school, educode_sales_follow_ups.updated_at") + EducodeSales::Teacher.joins(:follow_up, [department: :school]).where("educode_sales_teacher_follows.staff_id = #{staff.id}").select("departments.id, departments.name, schools.name AS school, educode_sales_teacher_follows.updated_at")).uniq{|s| s.id}
end

#follow_up_schoolsObject



99
100
101
102
103
# File 'app/controllers/educode_sales/staffs_controller.rb', line 99

def follow_up_schools
  staff = Staff.find(params[:id])
  @schools = EducodeSales::Teacher.joins(:follow_up, :department).where("educode_sales_teacher_follows.staff_id = #{staff.id}").group("departments.school_id").select("departments.school_id, max(educode_sales_teacher_follows.updated_at) AS updated_at") + EducodeSales::Business.joins(:follow_ups, :department).where("educode_sales_follow_ups.staff_id = #{staff.id}").group("departments.school_id").select("departments.school_id, max(educode_sales_follow_ups.updated_at) AS updated_at")
  @count =  EducodeSales::Business.joins(:follow_ups, :department).where("educode_sales_follow_ups.staff_id = #{staff.id}").group("departments.school_id").count
end

#indexObject



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'app/controllers/educode_sales/staffs_controller.rb', line 6

def index
  respond_to do |format|
    format.html do
    end
    format.json do
      @staffs = Staff.where(is_admin: false, deleted_at: nil).page(params[:page]).per(params[:limit])
      if params[:q].present? && params[:q][:name].present?
        @staffs = @staffs.joins(:user).where('CONCAT(lastname, firstname) LIKE :name', name: "%#{params[:q][:name]}%")
      end
      if params[:q].present? && params[:q][:role].present?
        @staffs = @staffs.where(role: params[:q][:role])
      end
      if params[:q].present? && params[:q][:staff_id].present?
        @staffs = @staffs.where(staff_id: params[:q][:staff_id])
      end
      if params[:q].present? && params[:q][:staff_type].present?
        @staffs = @staffs.where(job_type: params[:q][:staff_type])
      end
      if params[:q].present? && params[:q][:banned].present?
        if params[:q][:banned] == "0"
          @staffs = @staffs.where("educode_sales_staffs.expired_at is null or educode_sales_staffs.expired_at > ?", Time.now)
        else
          @staffs = @staffs.where("educode_sales_staffs.expired_at is null or educode_sales_staffs.expired_at < ?", Time.now)
        end
      end
    end
  end
end

#newObject



57
58
59
# File 'app/controllers/educode_sales/staffs_controller.rb', line 57

def new
  render layout: false
end

#updateObject



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'app/controllers/educode_sales/staffs_controller.rb', line 75

def update
  staff = Staff.find(params[:id])
  staff.assign_attributes(role_id: params[:role_id], job_type: params[:job_type], enabled_at: params[:enabled_at])
  staff.expired_at = staff.enabled_at.months_since(params[:month].to_i)
  commons = []
  params[:area_ids].each do |d|
    commons << Common.find(d)
  end
  staff.areas = commons
  if params[:school_ids].present?
    staff.staff_schools.destroy_all
    params[:school_ids].each do |d|
      staff.staff_schools.create(school_id: d)
    end
  else
    staff.staff_schools.destroy_all
  end
  if staff.save
    render_success
  else
    render_failure staff
  end
end