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

Instance Method Details

#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



199
200
201
202
203
204
# File 'app/controllers/educode_sales/customers_controller.rb', line 199

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

#create_departmentObject



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

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

#editObject



206
207
208
209
210
# File 'app/controllers/educode_sales/customers_controller.rb', line 206

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

#edit_departmentObject



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

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

#edit_follow_recordObject



172
173
174
175
176
# File 'app/controllers/educode_sales/customers_controller.rb', line 172

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

#giveObject



131
132
133
134
135
# File 'app/controllers/educode_sales/customers_controller.rb', line 131

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



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

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]}
    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?
        p "--------------------------------------------------------------------------------------"
        p params[:q][:staff_id]
        p "--------------------------------------------------------------------------------------"
        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?
        school_property_ids = []
        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
          else
          end
        end
        @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

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

#newObject



178
179
180
# File 'app/controllers/educode_sales/customers_controller.rb', line 178

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



161
162
163
164
# File 'app/controllers/educode_sales/customers_controller.rb', line 161

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

#show_departmentObject



154
155
156
157
158
# File 'app/controllers/educode_sales/customers_controller.rb', line 154

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



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'app/controllers/educode_sales/customers_controller.rb', line 137

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



166
167
168
169
170
# File 'app/controllers/educode_sales/customers_controller.rb', line 166

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

#updateObject



212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'app/controllers/educode_sales/customers_controller.rb', line 212

def update
  @school = School.find(params[:id])
  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.attributes = school_params
    @school.school_property = property
    @school.save!
  end
  render_success
end

#update_departmentObject



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

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