# authorization_time: 自动授权时间 # auto_users_trial: 自动授权 class School < ActiveRecord::Base attr_accessible :name, :province, :pinyin, :city, :address, :logo_link, :auto_users_trial, :shool_code, :authorization_time, :identifier, :is_online, :video_desc, :video_name,:course_link, :course_name has_many :courses has_many :departments, :dependent => :destroy has_many :shixun_schools, :dependent => :destroy has_many :shixuns, :through => :shixun_schools # 工程认证 has_many :users, :through => :ec_school_users has_many :ec_school_users, :dependent => :destroy has_many :ec_majors, :through => :ec_major_schools has_many :ec_major_schools, :dependent => :destroy # banner图片信息 has_many :school_images, :dependent => :destroy # 视频附件 acts_as_attachable scope :authorization, lambda{where(auto_users_trial: 1)} def to_s self.name.to_s end def teacher_count User.find_by_sql("SELECT COUNT(users.`id`) AS teacher_count FROM users LEFT JOIN user_extensions ON users.id=user_extensions.user_id WHERE user_extensions.`school_id` = #{self.id} AND user_extensions.`identity` = 0").first.try(:teacher_count) end def student_count User.find_by_sql("SELECT COUNT(users.`id`) AS student_count FROM users LEFT JOIN user_extensions ON users.id=user_extensions.user_id WHERE user_extensions.`school_id` = #{self.id} AND user_extensions.`identity` = 1").first.try(:student_count) end def course_count Course.find_by_sql("SELECT COUNT(courses.`id`) AS course_count FROM courses LEFT JOIN user_extensions ON courses.tea_id=user_extensions.user_id WHERE user_extensions.`school_id` = #{self.id} AND courses.id != 1309").first.try(:course_count) end def shixun_count Shixun.find_by_sql("select count(s.id) as shixun_count from users u right join shixuns s on u.id=s.user_id and s.status in (2, 3) inner join user_extensions ue on u.id=ue.user_id and ue.school_id=#{self.id}").first.try(:shixun_count) end def shixun_report_count StudentWork.find_by_sql("SELECT count(*) as sw_count FROM `student_works` where user_id in (SELECT users.id FROM users RIGHT JOIN user_extensions ON users.id=user_extensions.user_id WHERE user_extensions.`school_id`=#{self.id}) and work_status between 1 and 2 and myshixun_id !=0").first.try(:sw_count) end end