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 if @person&.teacher.present? gon.teacher = "t-#{@person.teacher_id}" gon.teachers = { value: "t-#{@person.teacher_id}", name: @person.name } else gon.teacher = "" gon.teachers = {} end 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 @areas = EducodeSales::Common.where(clazz: 'area').map{|d| {name: d.name, value: d.id}} common = Common.find_by(clazz: 'staff_type', name: '运营') role = EducodeSales::Role.find_by(name: '生态经理') if role @staff_manage = Staff.where(role_id: role.id).map { |d| [d.name, d.id]} else @staff_manage = Staff.none end @follow_upers = Staff.all.map { |d| [d.name, d.id]} @staffs = Staff.where(job_type: common.id).map { |d| [d.name, d.id]} @more = can?(:create, EducodeSales::OperationPlan) || can?(:update, EducodeSales::Teacher) || can?(:destroy, EducodeSales::Teacher) gon.menus = [] if can?(:create, EducodeSales::OperationPlan) gon.menus << { title: '添加周计划', event: 'week' } gon.menus << { title: '添加月计划', event: 'month' } end if can?(:update, EducodeSales::Teacher) gon.menus << { title: '编辑', event: 'edit' } end if can?(:destroy, EducodeSales::Teacher) gon.menus << { title: '删除', event: 'delete' } end filter = Filter.find_or_create_by(staff_id: @current_admin.id, clazz: "teachers") gon.filter = filter.extras || {} # if filter.extras.present? # # if filter.extras['area'].nil? # # filter.extras['area'] = 1 # # filter.extras['property'] = 1 # # filter.extras['sale_way'] = 1 # # end # # filter.save # gon.filter = filter.extras # else # gon.filter = {'area': 1, 'property': 1, 'sale_way': 1} # end end format.json do @staff_manages = {} @sales_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 sales_common = EducodeSales::Common.find_by(clazz: 'staff_type', name: '销售') EducodeSales::Common.joins(market_areas: :staff).includes(market_areas: :staff).where(clazz: '区域').where("educode_sales_staffs.job_type = ?", sales_common.id).each do |d| @sales_manages[d.name] = d.market_areas.map { |d| d.staff&.user&.real_name }.uniq.compact end if @current_admin.is_admin? @teachers = Teacher else level = @current_admin.role.role_areas.find_by(clazz: '教师运营').level case level when '自己' @teachers = Teacher @teachers = @teachers.where(staff_id: @current_admin.id) teacher_ids = EducodeSales::Teacher.where(staff_id: 0).pluck(:id) - EducodeSales::TeacherAssignFollow.all.pluck(:Teacher_id) + EducodeSales::TeacherAssignFollow.where(staff_id: @current_admin.id).pluck(:teacher_id) + @teachers.ids @teachers = Teacher.where(id: teacher_ids) when '区域' school_ids = School.where(province: @current_admin.areas.pluck(:name)).pluck(:id) + StaffSchool.where(staff_id: @current_admin.id).pluck(:school_id) @teachers = 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) teacher_ids = EducodeSales::Teacher.where(staff_id: 0).pluck(:id) - EducodeSales::TeacherAssignFollow.all.pluck(:Teacher_id) + EducodeSales::TeacherAssignFollow.where(staff_id: @current_admin.id).pluck(:teacher_id) + @teachers.ids @teachers = Teacher.where(id: teacher_ids) else @teachers = Teacher end end @teachers = @teachers.includes(department: [:department_majors, :school]) if @current_admin.job_type == Common.find_by(clazz: 'staff_type', name: '销售').id public_teacher_ids = EducodeSales::Teacher.where(staff_id: 0).pluck(:id) - EducodeSales::TeacherAssignFollow.where(staff_id: @current_admin.id).pluck(:teacher_id) @teachers = @teachers.where.not(id: public_teacher_ids) end if params[:q].present? && params[:q][:assist].present? staff_id = params[:q][:assist].to_i teacher_ids = EducodeSales::TeacherAssignFollow.where(staff_id: staff_id).pluck(:teacher_id) @teachers = @teachers.where(id: teacher_ids) 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][:realname].present? @teachers = @teachers.where("educode_sales_teachers.realname like ?", "%#{params[:q][:realname]}%") end if params[:q].present? && params[:q][:professional_title].present? @teachers = @teachers.where("educode_sales_teachers.professional_title = ?", "#{params[:q][:professional_title]}") end if params[:q].present? && params[:q][:job].present? @teachers = @teachers.where("educode_sales_teachers.job = ?", "#{params[:q][:job]}") 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][:regist_at].present? date = params[:q][:regist_at].split(" - ") @teachers = @teachers.where("educode_sales_teachers.regist_at >= ? AND educode_sales_teachers.regist_at <= ?",date[0] + " 00:00:00", date[1] + " 23:59:59") end if params[:q].present? && params[:q][:status].present? if params[:q][:status] == "1" @teachers = @teachers.where("educode_sales_teachers.user_id is not null") else @teachers = @teachers.where("educode_sales_teachers.user_id is null") end end if params[:q].present? && params[:q][:mobile].present? @teachers = @teachers.joins(:user).where("users.phone like ?", "%#{params[:q][:mobile]}%") end if params[:q].present? && params[:q][:attitude].present? # @teachers = @teachers.where("educode_sales_teachers.attitude_id = ?", "#{params[:q][:attitude]}") @teachers = @teachers.joins(" JOIN educode_sales_teacher_follows ON educode_sales_teachers.follow_up_id = educode_sales_teacher_follows.id ").where("educode_sales_teacher_follows.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 in (?)", params[:q][:area]).pluck(:name).uniq @teachers = @teachers.joins(" JOIN departments ON educode_sales_teachers.department_id = departments.id JOIN schools ON departments.school_id = schools.id ").where("province in (?)", p) end if params[:activity_id].present? if params[:school_id].present? @teachers = @teachers.joins(activity_teachers: [teacher: :department]).where("educode_sales_activity_teachers.activity_id = ? AND departments.school_id = ?", params[:activity_id], params[:school_id]) else @teachers = @teachers.joins(:activity_teachers).where("educode_sales_activity_teachers.activity_id = ?", params[:activity_id]) end end if params[:q].present? && params[:q][:date].present? date = params[:q][:date].split(" - ") @teachers = @teachers.where("educode_sales_teachers.created_at > ? AND educode_sales_teachers.created_at < ?", date[0], date[1]) end if params[:q].present? && params[:q][:staff_manage].present? staff = Staff.find(params[:q][:staff_manage]) school_ids = School.where(province: staff.areas.pluck(:name)).pluck(:id) assign_teacher_ids = EducodeSales::AssignStaff.where("educode_sales_assign_staffs.sourcable_type = 'EducodeSales::Teacher' and educode_sales_assign_staffs.staff_id = ?", params[:q][:staff_manage]).pluck(:sourcable_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) + assign_teacher_ids @teachers = @teachers.where(id: teacher_ids) end if params[:q].present? && params[:q][:staff_id].present? @teachers = @teachers.select(" educode_sales_teachers.*, users.created_on, last_follow.created_at as latest_time, users.grade, users.experience, users.last_login_on").joins(" JOIN educode_sales_teacher_follows AS last_follow ON last_follow.id = educode_sales_teachers.follow_up_id AND last_follow.deleted_at IS NULL LEFT JOIN users ON educode_sales_teachers.user_id = users.id ") @teachers = @teachers.where("last_follow.staff_id = ?", params[:q][:staff_id].to_i) else @teachers = @teachers.select(" educode_sales_teachers.*, users.created_on, last_follow.created_at as latest_time, users.grade, users.experience, users.last_login_on").joins(" LEFT JOIN users ON educode_sales_teachers.user_id = users.id LEFT JOIN educode_sales_teacher_follows AS last_follow ON last_follow.id = educode_sales_teachers.follow_up_id AND last_follow.deleted_at IS NULL ") end if params[:q].present? && params[:q][:is_contact].present? @teachers = @teachers.where("last_follow.is_contact = ?", params[:q][:is_contact].to_i) end if params[:sort].present? && params[:sort][:field] && params[:sort][:field] == 'created_at' @teachers = @teachers.order("educode_sales_teachers.created_at #{params[:sort][:order]}") elsif params[:sort].present? && params[:sort][:field] && params[:sort][:field] == 'last_login_on' @teachers = @teachers.order("users.last_login_on #{params[:sort][:order]}") elsif params[:sort].present? && params[:sort][:field] && params[:sort][:field] == 'created_on' @teachers = @teachers.order("users.created_on #{params[:sort][:order]}") elsif params[:sort].present? && params[:sort][:field] && params[:sort][:field] == 'latest_time' @teachers = @teachers.order("last_follow.created_at #{params[:sort][:order]}") elsif params[:sort].present? && params[:sort][:field] && params[:sort][:field] == 'experience' @teachers = @teachers.order("users.experience #{params[:sort][:order]}") elsif params[:sort].present? && params[:sort][:field] && params[:sort][:field] == 'grade' @teachers = @teachers.order("users.grade #{params[:sort][:order]}") elsif params[:sort].present? && params[:sort][:field] @teachers = @teachers.order("#{params[:sort][:field]} #{params[:sort][:order]}") else @teachers = @teachers.order("educode_sales_teachers.created_at desc") end @teachers = @teachers.includes(:teacher_assign_follows, :department, :user, :follow_up, :activity_teachers, [assign_staffs: :staff]) if params[:type].to_s == 'export' @teachers = @teachers.page(1).per(1000) else @teachers = @teachers.page(params[:page]).per(params[:limit]) end end end end def import render layout: false end def new render layout: false end def create department = Department.find(params[:department_id]) teacher = @current_admin.teachers.build(teacher_params) params[:teacher_assist].each do |d| teacher.teacher_assign_follows.build(staff_id: d) end 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 unless params[:activity_id].present? return render json: {msg: '该教师已存在', success: false } if EducodeSales::Teacher.find_by(department_id: params[:department_id],name: params[:name]) end if params[:activity_id].present? if params[:user_id].present? # 把老师添加到活动列表,提取判断下老师列表是否存在 find_teacher = Teacher.find_by(user_id: params[:user_id]) if find_teacher teacher = find_teacher end else _teacher = EducodeSales::Teacher.find_by(department_id: params[:department_id],name: params[:name]) teacher = _teacher if _teacher.present? end return render json: {msg: '该教师已在活动中', success: false } if teacher && teacher.activity_teachers.find_by(activity_id: params[:activity_id]).present? end if params[:activity_id].present? 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) teacher_assists = [] params[:teacher_assist].each do |d| teacher_assists << teacher.teacher_assign_follows.find_or_initialize_by(staff_id: d) end teacher.teacher_assign_follows = teacher_assists 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 load_teacher gon.teacher_assist = @teacher.teacher_assign_follows.map do |d| {value: d.staff_id, name: d.staff.user.full_name} end 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 subject_names = %w[程序设计语言 计算机基础 算法设计 计算机系统能力 大数据 人工智能 数据库 电子信息 Web技术 软件工程 网络与安全 区块链 数学与统计 物联网 云计算 虚拟仿真].map{|d| {value: d, name: d}} gon.subject_names = subject_names render layout: false end def create_course load_teacher names = params[:select_subject_names].split(",") begin names.each do |name| course_subject = @teacher.course_subjects.build(name: name, role_name: params[:role_name]) course_subject.save! end render_success rescue Exception => ex render_failure(ex.message) 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 @latest = @follow_ups.order(created_at: :desc).first 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]) end end end def show_class render layout: false end def new_follow_record @teacher = Teacher.find(params[:id]) render layout: false 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}).where("is_delete = 0 and role != 4 and excellent = 0 and is_hidden = 0").where("start_date is null or start_date <= '#{Date.today}'").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 def daily_paper # 累计完成教师 @total_teachers_num = @current_admin.operation_plans.where("created_at >= ?", Time.now.beginning_of_month).distinct.count("teacher_id") @today_teacher_num = @current_admin.operation_plans.where("created_at >= ?", Time.now.beginning_of_day).distinct.count("teacher_id") user_ids = @current_admin.operation_plans.joins(:teacher).where("educode_sales_teachers.user_id IS NOT NULL").pluck(:user_id) @today_course_num = Course.joins(:course_members).where(course_members: {user_id: user_ids}).where("is_delete = 0 and role != 4 and excellent = 0 and is_hidden = 0").where("courses.created_at > ?", Time.now.beginning_of_day).count my_area = @current_admin.areas.pluck(:name) @total_users_num = UserExtension.joins(department: :school).where(schools: {province: my_area}).where("user_extensions.created_at > ?", Time.now.beginning_of_month).count @today_users_num = UserExtension.joins(department: :school).where(schools: {province: my_area}).where("user_extensions.created_at > ?", Time.now.beginning_of_day).count render layout: false end #获取该教师所指派生态经理 def assign role = Role.find_by( name: '生态经理') staffs = Staff.joins(:user).where(role_id: role.id) teacher = Teacher.find(params[:id]) selected_staff_ids = teacher.assign_staffs.pluck(:staff_id) gon.staff_manages = staffs.map { |d| {name: d.user.real_name, value: d.id, selected: selected_staff_ids.include?(d.id)} } render layout: false end #指派生态经理 def assign_staff @teacher = EducodeSales::Teacher.find(params[:id]) assign_staffs = [] params[:to_id].split(",").each do |i| assign_staffs << AssignStaff.find_or_initialize_by(sourcable_id: @teacher.id, staff_id: i, sourcable_type: "EducodeSales::Teacher") end @teacher.assign_staffs = assign_staffs @teacher.save render_success end private def teacher_params params.permit(:name, :professional_title, :job, :source_id, :wechat, :mobile, :realname) end def load_teacher @teacher = Teacher.find(params[:id]) end end end