You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
pgfqe6ch8/app/models/school.rb

91 lines
3.9 KiB

6 years ago
# 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,
6 years ago
:identifier, :is_online, :video_desc, :video_name,:course_link,
:course_name, :partner_id, :customer_id
6 years ago
has_many :courses
has_many :active_courses, class_name: 'Course', conditions: 'is_end = false'
6 years ago
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
has_one :ec_template_major, conditions: 'template_major = 1', class_name: 'EcMajorSchool'
6 years ago
has_many :partners, :dependent => :destroy
has_many :customers, :dependent => :destroy
6 years ago
# banner图片信息
has_many :school_images, :dependent => :destroy
# 报表信息
has_many :school_daily_reports
has_one :school_report
has_many :teacher_extensions, conditions: "identity = #{User::TEACHER}", class_name: 'UserExtensions'
has_many :student_extensions, conditions: "identity = #{User::STUDENT}", class_name: 'UserExtensions'
6 years ago
# 视频附件
acts_as_attachable
scope :authorization, lambda{where(auto_users_trial: 1)}
def to_s
self.name.to_s
end
6 years ago
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
def statistic_url
dep = departments.where("identifier is not null").first
6 years ago
url = dep.present? && !dep.identifier.blank? ? "/colleges/#{dep.identifier}/statistics" : "/colleges/#{self.id}/statistics"
end
def course_act_time
CourseActivity.find_by_sql("SELECT max(ca.updated_at) as max_update FROM course_activities ca left join courses on ca.course_id =
courses.id LEFT JOIN user_extensions ON courses.tea_id=user_extensions.user_id WHERE
user_extensions.`school_id` = #{self.id}").first.try(:max_update)
end
def self.provinces
Rails.cache.fetch('china_province_cache', expires_in: 1.days) do
School.pluck('distinct province').select(&:present?)
end
end
6 years ago
end