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, #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



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

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

#destroyObject



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

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

#disableObject



76
77
78
79
80
81
82
# File 'app/controllers/educode_sales/staffs_controller.rb', line 76

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

#editObject



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'app/controllers/educode_sales/staffs_controller.rb', line 46

def edit
  @staff = Staff.find(params[:id])
  @permissions = @staff.permissions.pluck(: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)
  tag_ids = @staff.staff_school_tags.pluck(:school_tag_id)
  names = %w[本科院校 军事院校 高职院校 中职院校 中学 小学 企业 其他]
  gon.school_properties = SchoolTag.where(for_missions: true).sort_by { |tag| names.index(tag.name) }.map { |d| { value: d.id, name: d.name, selected: tag_ids.include?(d.id) } }
  render layout: false
end

#follow_up_departmentsObject



135
136
137
138
# File 'app/controllers/educode_sales/staffs_controller.rb', line 135

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



129
130
131
132
133
# File 'app/controllers/educode_sales/staffs_controller.rb', line 129

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



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

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

#listObject



140
141
142
143
144
# File 'app/controllers/educode_sales/staffs_controller.rb', line 140

def list
  @staffs = EducodeSales::Staff.where.not(id: params[:id]).map { |d| [d.user.real_name, d.id ]}
  gon.trans_staffs = @staffs.to_h.invert
  render layout: false
end

#newObject



62
63
64
# File 'app/controllers/educode_sales/staffs_controller.rb', line 62

def new
  render layout: false
end

#transferObject



146
147
148
149
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/staffs_controller.rb', line 146

def transfer
  if @current_admin.is_admin?
    staff = Staff.find(params[:id])
    to = Staff.find(params[:to_id])
    staff.businesses.update_all(staff_id: to.id)
    # 商机
    staff.follow_ups.update_all(staff_id: to.id)
    # 销售动态
    staff.sale_plans.update_all(staff_id: to.id)
    # 销售计划
    staff.places.update_all(staff_id: to.id)
    # 渠道
    staff.activities.update_all(staff_id: to.id)
    # 活动
    staff.teachers.update_all(staff_id: to.id)
    # 教师
    staff.teacher_follows.update_all(staff_id: to.id)
    # 教师动态
    staff.operation_plans.update_all(staff_id: to.id)
    # 活动计划

    EducodeSales::Activity.where(sales_id: staff.id).update_all(sales_id: to.id)
    CustomerExtension.where(staff_id: staff.id).update_all(staff_id: to.id)
    # 回款计划
    MoneyPlan.where(staff_id: staff.id).update_all(staff_id: to.id)
    # 客户
  end
  render_success
end

#updateObject



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

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], name: params[:name])
  staff.expired_at = staff.enabled_at.months_since(params[:month].to_i)
  commons = []
  params[:area_ids].to_s.split(",").each do |d|
    commons << Common.find(d)
  end
  staff.areas = commons
  if params[:school_ids].present?
    schools = []
    params[:school_ids].to_s.split(",").each do |d|
      schools << EducodeSales::StaffSchool.find_or_initialize_by(staff_id: staff.id, school_id: d)
    end
    staff.staff_schools = schools
  else
    staff.staff_schools.destroy_all
  end
  tags = []
  params[:school_tag_ids].to_s.split(",").each do |tag_id|
    tags << EducodeSales::StaffSchoolTag.find_or_initialize_by(staff_id: staff.id, school_tag_id: tag_id)
  end
  #人员额外权限
  permissions = []
  if params[:permission].present?
    params[:permission].each do |d|
      d[1].each do |c|
        permissions << Permission.find_by(subject: d[0], action: c[1])
      end
    end
  end
  staff.staff_permissions.delete_all
  permissions.each do |permission|
    staff.staff_permissions.create(staff_id: staff.id, permission_id: permission.id)
  end
  staff.staff_school_tags = tags
  params[:current_admin] = @current_admin.id
  impressionist(staff, params)
  if staff.save
    render_success
  else
    render_failure staff
  end
end