module EducodeSales class Teacher < ApplicationRecord has_many :activity_teachers, dependent: :destroy has_many :activities, through: :activity_teachers has_many :teacher_follows, dependent: :destroy has_many :operation_plans, dependent: :destroy has_many :course_subjects, dependent: :destroy has_many :key_person, dependent: :destroy belongs_to :department, optional: true belongs_to :staff belongs_to :user, optional: true # belongs_to :follow_up, counter_cache: true, optional: true belongs_to :follow_up, class_name: 'TeacherFollow', optional: true validates :user_id, uniqueness: { allow_blank: true, message: '已存在老师列表'} def cal_courses_count if self.user_id.present? Course.joins(:course_members).where(course_members: {user_id: self.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).count else 0 end end def teacher_used_of_department user_ids = User.joins(:user_extension).where(user_extensions: {department_id: self.department_id, identity: 0}).pluck(:id) course_ids = CourseMember.where(role: "CREATOR", user_id: user_ids).pluck(:course_id) course_members = CourseMember.joins(course: :practice_homework_shixuns).where(course_id: course_ids).group("course_id").having("COUNT(homework_commons_shixuns.id) > 100 AND COUNT(course_members.user_id) > 20").select("COUNT(case when course_members.role !=4 then 1 END) AS count") course_members.present? ? course_members[0]['count'] : 0 end def cal_students_count if user_id.present? course_ids = 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").pluck(:course_id) CourseMember.where(course_id: course_ids, role: "STUDENT").count else 0 end end end end