Class: EducodeSales::IdeaFollowsController

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



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'app/controllers/educode_sales/idea_follows_controller.rb', line 5

def create
  load_teacher
  follow_up = @teacher.teacher_follows.build(follow_up_params)
  follow_up.staff = @current_admin
  if @teacher.user_id.present?
    course_ids = CourseMember.joins(:course).where(user_id: @teacher.user_id, courses: { is_delete: 0 }).where.not(role: 4).pluck(:course_id)
    follow_up.course_shixuns_count = CourseMember.joins(course: :practice_homework_shixuns).where(course_id: course_ids).pluck(:shixun_id).uniq.count
    follow_up.shixuns_count = ShixunMember.where(user_id: @teacher.user_id).count
    follow_up.students_count = CourseMember.where(course_id: course_ids, role: 4).count
    follow_up.evaluates_count = Course.where(id: course_ids).inject(0) { |i, d| i += d.evaluate_count }
    follow_up.courses_count = course_ids.size
  end
  if follow_up.save
    @teacher.update(follow_up_id: follow_up.id, followup_at: Time.now)
    render_success
  else
    render_failure follow_up
  end
end

#destroyObject



25
26
27
28
29
30
31
32
# File 'app/controllers/educode_sales/idea_follows_controller.rb', line 25

def destroy
  follow_up = IdeaFollow.find(params[:id])
  if follow_up.destroy
    render_success
  else
    render_failure follow_up
  end
end

#indexObject



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
130
131
132
133
# File 'app/controllers/educode_sales/idea_follows_controller.rb', line 51

def index
  @staff_manages = {}
  role = EducodeSales::Role.find_by(name: '生态经理')
  EducodeSales::Common.joins(market_areas: :staff).includes(market_areas: :staff).where(clazz: '区域').where("educode_sales_staffs.role_id = #{role.id}").each do |d|
    @staff_manages[d.name] = d.market_areas.map { |d| d.staff&.name }.uniq.compact
  end if role

  if @current_admin.is_admin?
    @follow_ups = TeacherFollow.all
  else
    level = @current_admin.role.role_areas.find_by(clazz: '教师运营').level
    case level
    when '自己'
      @follow_ups = TeacherFollow.where(staff_id: @current_admin.id)
    when '区域'
      school_ids = School.where(province: @current_admin.areas.pluck(:name)).pluck(:id)
      teacher_ids = EducodeSales::Teacher.joins("JOIN departments ON educode_sales_teachers.department_id = departments.id").where("departments.school_id in (?) OR educode_sales_teachers.staff_id = #{@current_admin.id}", school_ids).pluck(:id)
      @follow_ups = TeacherFollow.where(teacher_id: teacher_ids)
    else
      @follow_ups = TeacherFollow.all
    end
  end
  if params[:q].present? && params[:q][:name].present?
    @follow_ups = @follow_ups.joins(:teacher).where("educode_sales_teachers.name LIKE ?", "%#{params[:q][:name]}%")
  end

  if params[:q].present? && params[:q][:staff_id].present?
    staff = Staff.find(params[:q][:staff_id])
    school_ids = School.where(province: staff.areas.pluck(:name)).pluck(:id)
    teacher_ids = EducodeSales::Teacher.joins("JOIN departments ON educode_sales_teachers.department_id = departments.id").where("departments.school_id in (?)", school_ids).pluck(:id)
    @follow_ups = @follow_ups.where(teacher_id: teacher_ids)
  end

  if params[:q].present? && params[:q][:department].present?
    departments_ids = Department.joins(:school).where("schools.name like ?", "%#{params[:q][:department]}%").pluck(:id)
    @follow_ups = @follow_ups.joins(:teacher).where("department_id in (?)", departments_ids)
  end

  if params[:q].present? && params[:q][:area].present?
    p = EducodeSales::Common.find(params[:q][:area]).name
    # if @current_admin.is_admin?
    @follow_ups = @follow_ups.joins(:teacher).joins("
        JOIN departments ON educode_sales_teachers.department_id = departments.id
        JOIN schools ON departments.school_id = schools.id
      ").where("province = ?", p)
    # else
    #   level = @current_admin.role.role_areas.find_by(clazz: '教师运营').level
    #   if level == "区域"
    #     school_ids = School.where(province: @current_admin.areas.pluck(:name)).pluck(:id)
    #     @follow_ups = @follow_ups.joins(:teacher).joins("
    #       JOIN departments ON educode_sales_teachers.department_id = departments.id
    #       JOIN schools ON departments.school_id = schools.id
    #     ").where("province = ? AND departments.school_id in (?)", p, school_ids)
    #   else
    #     @follow_ups = @follow_ups.joins(:teacher).joins("
    #       JOIN departments ON educode_sales_teachers.department_id = departments.id
    #       JOIN schools ON departments.school_id = schools.id
    #     ").where("province = ?", p)
    #   end
    # end
  end

  if params[:q].present? && params[:q][:attitude_id].present?
    @follow_ups = @follow_ups.where("educode_sales_teacher_follows.attitude_id = ?", "#{params[:q][:attitude_id]}")
  end

  if params[:q].present? && params[:q][:description].present?
    @follow_ups = @follow_ups.where("educode_sales_teacher_follows.description LIKE ?", "%#{params[:q][:description]}%")
  end

  if params[:q].present? && params[:q][:follows_date].present?
    date = params[:q][:follows_date].split(" - ")
    @follow_ups = @follow_ups.where("educode_sales_teacher_follows.created_at > ? AND educode_sales_teacher_follows.created_at < ?", date[0], date[1] + ' 23:59:59')
  end

  @follow_ups = @follow_ups.includes(:teacher, :attitude, :staff)
  if params[:sort].present? && params[:sort][:field]
    @follow_ups = @follow_ups.order("#{params[:sort][:field]} #{params[:sort][:order]}")
  else
    @follow_ups = @follow_ups.order("educode_sales_teacher_follows.created_at desc")
  end
  @follow_ups = @follow_ups.page(params[:page]).per(params[:limit])
end

#updateObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'app/controllers/educode_sales/idea_follows_controller.rb', line 34

def update
  # 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])

  follow_up = IdeaFollow.find(params[:id])
  follow_up.money = params[:money]
  follow_up.content = params[:content]
  follow_up.status = params[:status]
  follow_up.advise = params[:advise]
  follow_up.sale_staff_id = params[:sale_staff_id]
  follow_up.idea_staff_id = params[:idea_staff_id]
  if follow_up.save
    render_success
  else
    render_failure follow_up
  end
end