Class: EducodeSales::IdeasController

Inherits:
ApplicationController show all
Defined in:
app/controllers/educode_sales/ideas_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

#add_adviseObject



340
341
342
343
344
# File 'app/controllers/educode_sales/ideas_controller.rb', line 340

def add_advise
  followup = IdeaFollow.find(params[:id])
  followup.update(advise: params[:content])
  render_success
end

#assignObject



8
9
10
11
12
13
# File 'app/controllers/educode_sales/ideas_controller.rb', line 8

def assign
  selected_staff_ids = @idea.assist_staff_ids.map(&:to_i)
  staffs = Staff.where.not(role_id: 11).includes(:user)
  gon.ideas_staffs = staffs.map { |d| { name: d.user.real_name, value: d.id, selected: selected_staff_ids.include?(d.id) } }
  render layout: false
end

#assign_sale_staffObject



22
23
24
25
26
27
# File 'app/controllers/educode_sales/ideas_controller.rb', line 22

def assign_sale_staff
  selected_staff_ids = Array(@idea.staff_id)
  staffs = Staff.where.not(role_id: 11).includes(:user)
  gon.sales_staffs = staffs.map { |d| { name: d.user.real_name, value: d.id, selected: selected_staff_ids.include?(d.id) } }
  render layout: false
end

#assign_staffObject

指派协作人 assist_staff_ids协作人



16
17
18
19
20
# File 'app/controllers/educode_sales/ideas_controller.rb', line 16

def assign_staff
  @idea.assist_staff_ids = params[:to_id].to_s.split(",")
  @idea.save
  render_success
end

#createObject



143
144
145
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
# File 'app/controllers/educode_sales/ideas_controller.rb', line 143

def create
  idea = Idea.new(idea_params)
  # idea.school_id = Department.find_by_id(idea.department_id)&.school_id
  idea.creator = current_user
  assist_staff_ids = Array(params[:assist_staff_ids].to_s.split(","))
  attachment_ids = Array(params[:attachment_ids].to_s.split(","))
  other_staff_ids = Array(params[:other_staff_ids].to_s.split(","))
  if params[:business_id].present?
    idea.business_id = params[:business_id]
    idea.school_id = idea.business&.department&.school_id
  elsif params[:school_id].present?
    idea.name = params[:project_name]
  end
  if idea.school_id.blank?
    idea.school_id = params[:school_id]
  end
  idea.attachment_ids = attachment_ids

  # idea.assist_staff_ids = assist_staff_ids
  # idea.other_staff_ids = other_staff_ids
  idea.save
  attachment_ids.each do |a|
    attachment = Attachment.find_by_id(a)
    attachment.container_id = idea.id
    attachment.container_type = 'EducodeSales::Idea'
    attachment.save
  end
  render_success
end

#destroyObject



204
205
206
207
# File 'app/controllers/educode_sales/ideas_controller.rb', line 204

def destroy
  @idea.soft_destroy(current_user.id)
  render_success
end

#detailObject



173
174
175
# File 'app/controllers/educode_sales/ideas_controller.rb', line 173

def detail
  render layout: false
end

#editObject



209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'app/controllers/educode_sales/ideas_controller.rb', line 209

def edit
  staffs = Staff.where.not(role_id: 11).includes(:user)
  gon.staffs = staffs.map { |d| { name: d.user.real_name, value: d.id } }
  gon.staff_value = [{ name: @idea.staff&.user&.real_name, value: @idea.staff_id }]
  gon.sale_staff_value = [{ name: @idea.sale_staff&.user&.real_name, value: @idea.sale_staff_id }]
  gon.assist_staff_value = @idea.assist_staffs.map { |d| { name: d&.user&.real_name, value: d.id } }
  gon.attachment_list = @idea.attachments.map { |d| { name: d.filename, value: d.id } }
  gon.department = { value: @idea&.department_id, name: "#{@idea&.department&.school&.name}-#{@idea&.department&.name}" }
  gon.value = @idea.department_id
  gon.department_list = @idea.department.present? ? [{ name: @idea.department.name, value: @idea.department_id }] : []
  gon.school_list = @idea.school.present? ? [{ name: @idea.school.name, value: @idea.school_id }] : []
  gon.business_list = @idea.business.present? ? [{ name: @idea.business.name, value: @idea.business_id }] : []
  gon.other_staff_value = @idea.other_staffs.map { |d| { name: d&.user&.real_name, value: d.id } }
  render layout: false
end

#edit_follow_recordObject



281
282
283
284
285
286
287
288
# File 'app/controllers/educode_sales/ideas_controller.rb', line 281

def edit_follow_record
  @idea_follow = IdeaFollow.find(params[:id])
  @idea = @idea_follow.idea
  staffs = Staff.where.not(role_id: 11).includes(:user)
  gon.sale_staffs = staffs.map { |d| { name: d.user.real_name, value: d.id, selected: d.id == @idea_follow.sale_staff_id } }
  gon.idea_staffs = staffs.map { |d| { name: d.user.real_name, value: d.id, selected: d.id == @idea_follow.idea_staff_id } }
  render layout: false
end

#filesObject



346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
# File 'app/controllers/educode_sales/ideas_controller.rb', line 346

def files
  respond_to do |format|
    format.html do
      render layout: false
    end
    format.json do
      idea = Idea.find(params[:id])
      @files = idea.files
      if params[:sort].present? && params[:sort][:field]
        @files = @files.order("#{params[:sort][:field]} #{params[:sort][:order]}")
      else
        @files = @files.order("created_on desc")
      end
      @files = @files.page(params[:page]).per(params[:limit])
    end
  end
end

#follow_listObject



267
268
269
270
271
272
273
274
275
276
277
278
279
# File 'app/controllers/educode_sales/ideas_controller.rb', line 267

def follow_list
  @idea = Idea.find(params[:id])
  respond_to do |format|
    format.html do
      render layout: false
    end
    format.json do
      @follow_ups = @idea.idea_follows.includes(:staff, :sale_staff, :idea_staff).order(id: :desc)
      @latest = @follow_ups.order(id: :desc).first
      @follow_ups = @follow_ups.page(params[:page]).per(params[:limit])
    end
  end
end

#follow_upObject



290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
# File 'app/controllers/educode_sales/ideas_controller.rb', line 290

def follow_up
  idea = Idea.find(params[:id])
  follow_up = IdeaFollow.new(idea_id: idea.id, staff_id: @current_admin.id, money: params[:money], content: params[:content], status: params[:status], advise: params[:advise], sale_staff_id: params[:sale_staff_id], idea_staff_id: params[:idea_staff_id])
  if follow_up.save
    idea.last_idea_follow_id = follow_up.id
    idea.save
    attachment_ids = Array(params[:attachment_ids].to_s.split(","))
    attachment_ids.each do |a|
      attachment = Attachment.find_by_id(a)
      attachment.container_id = idea.id
      attachment.container_type = 'EducodeSales::Idea'
      attachment.description = follow_up.id.to_s
      attachment.save
    end
    render_success
  else
    render_failure follow_up
  end

end

#follow_upsObject



311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
# File 'app/controllers/educode_sales/ideas_controller.rb', line 311

def follow_ups
  @data = IdeaFollow
  if params[:q] && params[:q][:search_name].present?
    @data = @data.left_joins(idea: :business).where("educode_sales_businesses.name like ?", "%#{params[:q][:search_name]}%")
                 .or(@data.left_joins(idea: :business).where("educode_sales_ideas.name like ?", "%#{params[:q][:search_name]}%"))
  end
  if params[:q] && params[:q][:search_school].present?
    @data = @data.joins(idea: [department: :school]).where("schools.name like ?", "%#{params[:q][:search_school]}%")
  end
  if params[:q] && params[:q][:search_sale_staff_id].present?
    @data = @data.where(sale_staff_id: params[:q][:search_sale_staff_id])
  end
  if params[:q] && params[:q][:search_idea_staff].present?
    @data = @data.where(idea_staff_id: params[:q][:search_idea_staff])
  end
  if params[:q] && params[:q][:search_created_at].present?
    @data = @data.where("educode_sales_idea_follows.created_at >= ? and educode_sales_idea_follows.created_at <= ?", params[:q][:search_created_at].split(" - ")[0], params[:q][:search_created_at].split(" - ")[1])
  end
  if params[:q] && params[:q][:search_content].present?
    @data = @data.where("educode_sales_idea_follows.content like ?", "%#{params[:q][:search_content]}%")
  end
  # if params[:sort].present? && params[:sort][:field]
  #   @data = @data.order("#{params[:sort][:field]} #{params[:sort][:order]}")
  # else
  #   @data = @data.order("educode_sales_idea_follows.created_at desc")
  # end
  @data = @data.distinct.page(params[:page]).per(params[:limit])
end

#historyObject



177
178
179
# File 'app/controllers/educode_sales/ideas_controller.rb', line 177

def history
  render layout: false
end

#indexObject



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

def index
  respond_to do |format|
    format.html do
      staff_ids = EducodeSales::Idea.all.pluck(:staff_id)
      creator_ids = EducodeSales::Idea.all.pluck(:creator_id)
      sale_staff_ids = EducodeSales::Idea.all.pluck(:sale_staff_id)
      @creator_arr = EducodeSales::Staff.joins(:user).where(id: creator_ids).pluck("concat(users.lastname,users.firstname)", :id)
      @staff_arr = EducodeSales::Staff.joins(:user).where(id: staff_ids).pluck("concat(users.lastname,users.firstname)", :id)
      @sale_staff_arr = EducodeSales::Staff.joins(:user).where(id: sale_staff_ids).pluck("concat(users.lastname,users.firstname)", :id)
      filter = Filter.find_or_create_by(staff_id: @current_admin.id, clazz: "ideas")
      gon.filter = filter.extras || {}

      #协助人
      @assist_staffs = Staff.where.not(role_id: 11).includes(:user).map{ |d| {name: d.user.real_name, value: d.id}}
    end
    format.json do
      @ideas = params[:is_deleted].to_s == "true" ? Idea.deleted : Idea.not_deleted

      if @current_admin.is_admin?
        @ideas = @ideas
      else
        level = @current_admin.role.role_areas.find_by(clazz: '方案管理').try(:level)
        case level
        when '自己'
          idea_ids = @ideas.select { |d| Array(d.other_staff_ids).include?(@current_admin.id.to_s) || d.sale_staff_id == @current_admin.id || Array(d.assist_staff_ids).include?(@current_admin.id.to_s) || d.staff_id == @current_admin.id }.map { |d| d.id }
          @ideas = @ideas.where("educode_sales_ideas.creator_id = ? OR educode_sales_ideas.id in (?)", @current_admin.id, idea_ids)
        when '区域'
          # 查看区域商机,需要排除掉其它人员手上的监管学校
          other_staff_school_id = EducodeSales::StaffSchool.where.not(staff_id: @current_admin.id).where("school_id IN (SELECT school_id FROM educode_sales_staff_schools WHERE staff_id = #{@current_admin.id}) IS NOT TRUE").distinct.pluck :school_id

          school_ids = School.where(province: @current_admin.areas.pluck(:name)).pluck(:id) + StaffSchool.where(staff_id: @current_admin.id).pluck(:school_id) - other_staff_school_id
          idea_ids = @ideas.select { |d| Array(d.other_staff_ids).include?(@current_admin.id.to_s) || d.sale_staff_id == @current_admin.id || Array(d.assist_staff_ids).include?(@current_admin.id.to_s) || d.staff_id == @current_admin.id }.map { |d| d.id }
          @ideas = @ideas.left_joins(department: :school).where("schools.id in (?) OR educode_sales_ideas.creator_id = ? OR educode_sales_ideas.id in (?)", school_ids, @current_admin.id, idea_ids)
        else
          @ideas = @ideas
        end
      end

      @is_deleted = params[:is_deleted].to_s == "true"
      if params[:q] && params[:q][:created_at].present?
        date = params[:q][:created_at].split(" - ")
        @ideas = @ideas.where("educode_sales_ideas.created_at >= ? AND educode_sales_ideas.created_at <= ?", date[0] + " 00:00:00", date[1] + " 23:59:59")
      end
      if params[:q].present? && params[:q][:name].present?
        @ideas = @ideas.joins(:business).where("educode_sales_ideas.name like ? or educode_sales_businesses.name like ?", "%#{params[:q][:name]}%", "%#{params[:q][:name]}%")
      end
      if params[:q].present? && params[:q][:school].present?
        @ideas = @ideas.left_joins(:school).where("schools.name like ?", "%#{params[:q][:school]}%")
      end
      if params[:q].present? && params[:q][:creator_id].present?
        @ideas = @ideas.where("educode_sales_ideas.creator_id = ?", params[:q][:creator_id].to_i)
      end
      if params[:q].present? && params[:q][:staff_id].present?
        @ideas = @ideas.where("educode_sales_ideas.staff_id = ?", params[:q][:staff_id].to_i)
      end
      if params[:q].present? && params[:q][:search_sale_staff_id].present?
        @ideas = @ideas.where("educode_sales_ideas.sale_staff_id = ?", params[:q][:search_sale_staff_id].to_i)
      end

      if params[:q].present? && params[:q][:status].present?
        @ideas = @ideas.where(status: params[:q][:status])
      end
      if params[:q].present? && params[:q][:types].present?
        @ideas = @ideas.where(types: params[:q][:types])
      end
      if params[:q].present? && params[:q][:model].present?
        @ideas = @ideas.where(model: params[:q][:model])
      end
      if params[:q].present? && params[:q][:history_type].present?
        if params[:q][:history_type].to_i == 0
          @ideas = @ideas.where("educode_sales_ideas.history_type=#{params[:q][:history_type]} or educode_sales_ideas.history_type is null ")
        else
          @ideas = @ideas.where(history_type: params[:q][:history_type])
        end
      end
      if params[:q].present? && params[:q][:level].present?
        @ideas = @ideas.where(level: params[:q][:level])
      end

      if params[:q].present? && params[:q][:assist_staffs].present?
        @ideas = @ideas.select{|d| d&.assist_staff_ids.include?(params[:q][:assist_staffs])}
      end
      @ideas_count = @ideas.count

      @ideas = paginate @ideas
    end
  end
end

#newObject



125
126
127
128
129
130
131
132
# File 'app/controllers/educode_sales/ideas_controller.rb', line 125

def new
  staffs = Staff.where.not(role_id: 11).includes(:user)
  gon.staffs = staffs.map { |d| { name: d.user.real_name, value: d.id } }
  gon.department = { value: '', name: '' }
  gon.value = ''
  gon.attachments = []
  render layout: false
end

#new_follow_upObject



245
246
247
248
249
250
251
# File 'app/controllers/educode_sales/ideas_controller.rb', line 245

def new_follow_up
  @idea = Idea.find(params[:id])
  staffs = Staff.where.not(role_id: 11).includes(:user)
  gon.sale_staffs = staffs.map { |d| { name: d.user.real_name, value: d.id, selected: d.id == @idea.sale_staff_id } }
  gon.idea_staffs = staffs.map { |d| { name: d.user.real_name, value: d.id, selected: d.id == @idea.staff_id } }
  render layout: false
end

#show_followObject



253
254
255
256
257
258
259
260
261
262
263
264
265
# File 'app/controllers/educode_sales/ideas_controller.rb', line 253

def show_follow
  @idea_follow = IdeaFollow.find(params[:id])
  staffs = Staff.where.not(role_id: 11).includes(:user)
  gon.sale_staffs = staffs.map { |d| { name: d.user.real_name, value: d.id, selected: d.id == @idea_follow.sale_staff_id } }
  gon.idea_staffs = staffs.map { |d| { name: d.user.real_name, value: d.id, selected: d.id == @idea_follow.idea_staff_id } }
  respond_to do |format|
    format.html do
      render layout: false
    end
    format.json do
    end
  end
end

#show_schoolsObject



233
234
235
236
237
238
239
240
241
242
243
# File 'app/controllers/educode_sales/ideas_controller.rb', line 233

def show_schools
  respond_to do |format|
    format.html do
      render layout: false
    end
    format.json do
      @data = EducodeSales::ActivityTeacher.joins(teacher: [department: :school]).where(idea_id: params[:id]).select("count(teacher_id) AS teachers_count, departments.name AS department, schools.name AS school, school_id").group("school_id")
      @data = @data.distinct.page(params[:page]).per(params[:limit])
    end
  end
end

#show_teachersObject



225
226
227
# File 'app/controllers/educode_sales/ideas_controller.rb', line 225

def show_teachers
  render layout: false
end

#updateObject



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'app/controllers/educode_sales/ideas_controller.rb', line 181

def update
  @idea.assign_attributes(idea_params)
  # @idea.school_id = Department.find_by_id(@idea.department_id)&.school_id
  assist_staff_ids = Array(params[:assist_staff_ids].to_s.split(","))
  # @idea.assist_staff_ids = assist_staff_ids
  attachment_ids = Array(params[:attachment_ids].to_s.split(","))
  @idea.attachment_ids = attachment_ids
  other_staff_ids = Array(params[:other_staff_ids].to_s.split(","))
  # @idea.other_staff_ids = other_staff_ids
  if params[:business_id].present?
    @idea.business_id = params[:business_id]
    @idea.school_id = @idea.business&.department&.school_id
  elsif params[:school_id].present?
    @idea.name = params[:project_name]
  end
  if @idea.school_id.blank?
    @idea.school_id = params[:school_id]
  end
  check_changes
  @idea.save
  render_success
end

#update_adviseObject



134
135
136
137
138
139
140
141
# File 'app/controllers/educode_sales/ideas_controller.rb', line 134

def update_advise
  # followup = FollowUp.find(params[:id])
  # followup.update(advise: params[:content])
  # render_success
  idea = Idea.find(params[:id])
  idea.update(advise: params[:content])
  render_success
end

#update_sale_staffObject

指派方案经理 staff_id



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

def update_sale_staff
  @idea.staff_id = params[:to_id]
  @idea.save
  render_success
end

#upload_fileObject



229
230
231
# File 'app/controllers/educode_sales/ideas_controller.rb', line 229

def upload_file
  render layout: false
end