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

Instance Method Details

#add_adviseObject



240
241
242
243
244
# File 'app/controllers/educode_sales/ideas_controller.rb', line 240

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

#createObject



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 107

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 idea.business_id.present?
    idea.school_id = idea.business&.department&.school_id
    idea.department_id = idea.business&.department_id
  end
  idea.attachment_ids = attachment_ids
  idea.assist_staff_ids = assist_staff_ids
  idea.other_staff_ids = other_staff_ids
  idea.save
  render_success
end

#destroyObject



151
152
153
154
# File 'app/controllers/educode_sales/ideas_controller.rb', line 151

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

#detailObject



125
126
127
# File 'app/controllers/educode_sales/ideas_controller.rb', line 125

def detail
  render layout: false
end

#editObject



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'app/controllers/educode_sales/ideas_controller.rb', line 156

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

#filesObject



246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
# File 'app/controllers/educode_sales/ideas_controller.rb', line 246

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_upObject



198
199
200
201
202
203
204
205
206
207
208
209
# File 'app/controllers/educode_sales/ideas_controller.rb', line 198

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
    render_success
  else
    render_failure follow_up
  end

end

#follow_upsObject



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

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



129
130
131
# File 'app/controllers/educode_sales/ideas_controller.rb', line 129

def history
  render layout: false
end

#indexObject



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

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)
    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 '自己'
          # business_ids = Business.joins(last_follow_up: :assign_follow_ups).where("educode_sales_assign_follow_ups.staff_id = ?", @current_admin.id).pluck(:id)
          # @businesses = Business.where("educode_sales_businesses.staff_id = ? OR educode_sales_businesses.id in (?)", @current_admin.id, business_ids)
          idea_ids = Idea.all.pluck(:other_staff_ids).flatten.uniq
          @ideas = Idea.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 = Idea.all.pluck(:other_staff_ids).flatten.uniq
          @ideas = @ideas.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.where("educode_sales_ideas.name like ?", "%#{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

      @ideas = @ideas.page(params[:page]).per(params[:limit])
    end
  end
end

#newObject



89
90
91
92
93
94
95
96
# File 'app/controllers/educode_sales/ideas_controller.rb', line 89

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



192
193
194
195
196
# File 'app/controllers/educode_sales/ideas_controller.rb', line 192

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

#show_schoolsObject



180
181
182
183
184
185
186
187
188
189
190
# File 'app/controllers/educode_sales/ideas_controller.rb', line 180

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



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

def show_teachers
  render layout: false
end

#updateObject



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'app/controllers/educode_sales/ideas_controller.rb', line 133

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 @idea.business_id.present?
    @idea.school_id = @idea.business&.department&.school_id
    @idea.department_id = @idea.business&.department_id
  end
  check_changes
  @idea.save
  render_success
end

#update_adviseObject



98
99
100
101
102
103
104
105
# File 'app/controllers/educode_sales/ideas_controller.rb', line 98

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

#upload_fileObject



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

def upload_file
  render layout: false
end