require_dependency "educode_sales/application_controller" module EducodeSales class TeacherFollowsController < ApplicationController 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 def destroy follow_up = TeacherFollow.find(params[:id]) if follow_up.destroy render_success else render_failure follow_up end end def update follow_up = TeacherFollow.find(params[:id]) follow_up.assign_attributes(follow_up_params) if follow_up.save render_success else render_failure follow_up end end 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 if params[:q].present? && params[:q][:staff].present? @follow_ups = @follow_ups.joins(staff: :user).where("CONCAT(users.lastname,users.firstname) like ?", "%#{params[:q][:staff]}%") 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 private def load_teacher @teacher = Teacher.find(params[:teacher_id]) end def follow_up_params params.permit(:attitude_id, :follow_id, :course_plan_id, :course_build_id, :description, :advise, :is_contact) end end end