require_dependency "educode_sales/application_controller" module EducodeSales class TeachersController < ApplicationController def add_keys follow_up = FollowUp.find(params[:follow_up_id]) if follow_up.key_people_count == 0 last_follow = follow_up.business.follow_ups.where.not(id: follow_up.id).order("id desc").first if last_follow.present? && last_follow.key_people_count > 0 @person = last_follow.key_person.last gon.teacher = "t-#{@person.teacher_id}" gon.teachers = { value: "t-#{@person.teacher_id}", name: @person.teacher.name } end end @attitudes = Common.where(clazz: 'key_attitude').order("position").pluck(:name, :id) gon.attitudes = @attitudes render layout: false end def index respond_to do |format| format.html do end format.json do if @current_admin.is_admin? @teachers = Teacher.where(is_key: false) else level = @current_admin.role.role_areas.find_by(clazz: '教师运营').level case level when '自己' @teachers = Teacher.where(is_key: false) @teachers = @teachers.where(staff_id: @current_admin.id) when '区域' school_ids = School.where(province: @current_admin.areas.pluck(:name)).pluck(:id) @teachers = Teacher.joins("JOIN departments ON educode_sales_teachers.department_id = departments.id").where(is_key: false).where("departments.school_id in (?) OR educode_sales_teachers.staff_id = #{@current_admin.id}", school_ids) else @teachers = Teacher.where(is_key: false) end end if params[:q].present? && params[:q][:name].present? @teachers = @teachers.where("educode_sales_teachers.name like ?", "%#{params[:q][:name]}%") end if params[:q].present? && params[:q][:professional_title].present? @teachers = @teachers.where("educode_sales_teachers.professional_title like ?", "%#{params[:q][:professional_title]}%") end if params[:q].present? && params[:q][:regist_at].present? @teachers = @teachers.where("educode_sales_teachers.regist_at like ?", "%#{params[:q][:regist_at]}%") end if params[:q].present? && params[:q][:attitude].present? @teachers = @teachers.where("educode_sales_teachers.attitude_id = ?", "#{params[:q][:attitude]}") end if params[:q].present? && params[:q][:teacher_source].present? @teachers = @teachers.where("educode_sales_teachers.source_id = ?", "#{params[:q][:teacher_source]}") end if params[:q].present? && params[:q][:school].present? departments_ids = Department.joins(:school).where("schools.name like ?", "%#{params[:q][:school]}%").pluck(:id) @teachers = @teachers.joins(:department).where("departments.id in (?)", departments_ids) end if params[:q].present? && params[:q][:area].present? p = EducodeSales::Common.where(id: params[:q][:area]).pluck(:name)[0] @teachers = @teachers.joins(" JOIN departments ON educode_sales_teachers.department_id = departments.id JOIN schools ON departments.school_id = schools.id ").where("province = ?", p) end if params[:activity_id].present? @teachers = @teachers.joins(:activity_teachers).where("educode_sales_activity_teachers.activity_id = ?", params[:activity_id]) end if params[:q].present? && params[:q][:date].present? date = params[:q][:date].split(" - ") @teachers = @teachers.where("created_at > ? AND created_at < ?", date[0], date[1]) end @teachers = @teachers.order("created_at desc").page(params[:page]).per(params[:limit]) end end end def new render layout: false end def create department = Department.find(params[:department_id]) teacher = @current_admin.teachers.build(teacher_params) teacher.department_id = department.id if params[:user_id].present? user = User.find(params[:user_id]) teacher.user_id = user.id teacher.regist_at = user.created_on else teacher.regist_at = Time.now end if params[:activity_id].present? && params[:user_id].present? # 把老师添加到活动列表,提取判断下老师列表是否存在 find_teacher = Teacher.find_by(user_id: params[:user_id]) if find_teacher teacher = find_teacher end teacher.activity_teachers.build(activity_id: params[:activity_id]) end if teacher.save render_success else render_failure teacher end end def update department = Department.find(params[:department_id]) teacher = Teacher.find(params[:id]) teacher.department_id = department.id teacher.assign_attributes(teacher_params) if params[:user_id].present? user = User.find(params[:user_id]) teacher.user_id = user.id end if teacher.save render_success else render_failure teacher end end def destroy if params[:activity_id].present? activity_teacher = ActivityTeacher.find_by(activity_id: params[:activity_id], teacher_id: params[:id]) activity_teacher.destroy else teacher = Teacher.find(params[:id]) teacher.destroy end render_success end def edit @teacher = Teacher.find(params[:id]) gon.department = { value: @teacher.department_id, name: "#{@teacher.department&.school&.name}-#{@teacher.department&.name}" } gon.value = @teacher.department_id render layout: false end def unfinish_plans load_teacher @plans = @teacher.operation_plans.where.not(finish_rate: 100).page(params[:page]).per(params[:limit]) end def course_subject load_teacher @courses = @teacher.course_subjects.all end def search_new render layout: false end def add_courses load_teacher render layout: false end def create_course load_teacher course_subject = @teacher.course_subjects.build(name: params[:name], role_name: params[:role_name],) # course_subject = CourseSubject.new(teacher_id: params[:teacher_id], name: params[:name], role_name: params[:role_name],) if course_subject.save render_success else render_failure course_subject end end def update_course course_subject = CourseSubject.find(params[:id]) if course_subject.update(name: params[:name], role_name: params[:role_name]) render_success else render_failure course_subject end end def delete_course course_subject = CourseSubject.find(params[:id]) course_subject.destroy render_success end def add_event render layout: false end def show_follow @teacher = Teacher.find(params[:id]) respond_to do |format| format.html do render layout: false end format.json do @follow_ups = @teacher.teacher_follows if params[:field] @follow_ups = @follow_ups.order("#{params[:field]} #{params[:order]}") else @follow_ups = @follow_ups.order("created_at desc") end @follow_ups = @follow_ups.page(params[:page]).per(params[:limit]) @latest = @follow_ups.order(created_at: :desc).first.created_at.to_s end end end def show_class render layout: false end def new_follow_record @teacher = Teacher.find(params[:id]) render layout: false end # def create_follow_record # # end # # def update_follow_record # # end # def delete_follow_record # # end def show_follow_record @follow_up = TeacherFollow.find(params[:id]) render layout: false end def edit_follow_record @follow_up = TeacherFollow.find(params[:id]) render layout: false end def course_list user = User.find_by(id: params[:user_id]) if user @courses = Course.joins(:course_members).where(course_members: {user_id: user.id}).distinct("course_id").page(params[:page]).per(params[:limit]) else @courses = Course.none end end def activities teacher = Teacher.find(params[:id]) @activities = teacher.activities.page(params[:page]).per(params[:limit]) end private def teacher_params params.permit(:name, :professional_title, :job, :source_id) end def load_teacher @teacher = Teacher.find(params[:id]) end end end