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) 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) # follow_up.profit_amount = follow_up.actual_amount * (100-follow_up.divide_rate.to_i) * 0.01 if follow_up.actual_amount if follow_up.save render_success else render_failure follow_up end end # # def teachers # follow_up = @current_admin.follow_ups.find_by(id: params[:id]) # if follow_up.present? # @teachers = follow_up.key_person.includes(:teacher).page(params[:page]).per(params[:limit]) # else # @teachers = FollowUp.none # end # end # # def money_plans # follow_up = @current_admin.follow_ups.find_by(id: params[:id]) # @money_plans = follow_up.money_plans.page(params[:page]).per(params[:limit]) # end # # def add_money # follow_up = @current_admin.follow_ups.find(params[:id]) # money_plan = follow_up.money_plans.build(date_at: params[:date_at], amount: params[:amount], clazz: params[:clazz]) # money_plan.staff = @current_admin # if money_plan.save # render_success # else # render_failure money_plan # end # end # # def update_money # follow_up = @current_admin.follow_ups.find_by(id: params[:id]) # money_plan = follow_up.money_plans.find(params[:plan_id]) # if money_plan.update(date_at: params[:date_at], amount: params[:amount], clazz: params[:clazz]) # render_success # else # render_failure money_plan # end # end # # def delete_money # follow_up = @current_admin.follow_ups.find_by(id: params[:id]) # money_plan = follow_up.money_plans.find(params[:plan_id]) # if money_plan.destroy # render_success # else # render_failure money_plan # end # end # # def add_keys # follow_up = @current_admin.follow_ups.find(params[:id]) # if params[:name].blank? # return render_failure '请从平台里选择教师' # end # # names = params[:name].split("-") # if names[0] == 't' # # 从教师列表里选择的用户 # teacher = Teacher.find(names[1]) # key_peprson = KeyPerson.new(teacher_params) # key_peprson.teacher = teacher # else # # 从头歌平台选择的用户 # user = User.find(names[1]) # teacher = Teacher.new(staff: @current_admin, # professional_title: params[:professional_title], # job: params[:job], # user_id: user.id, # is_key: true, # department_id: user.department_id, # name: user.real_name # ) # teacher.save # key_peprson = KeyPerson.new(teacher_params) # key_peprson.teacher = teacher # end # # follow_up.key_person << key_peprson # if follow_up.save! # render_success # else # render_failure follow_up # end # rescue ActiveRecord::RecordInvalid => e # if e.message.include?("Teacher已经被使用") || e.message.include?('Key person是无效的') # render_failure '该老师已在关键人列表中' # else # render_failure e.message # end # 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) end # # def teacher_params # params.permit(:professional_title, :job, :attitude_id, :sex, :birth_date, :remark) # end end end