536 lines
18 KiB
Ruby
536 lines
18 KiB
Ruby
#班级相关的缓存
|
|
class CourseCache
|
|
class << self
|
|
include CoursesHelper
|
|
#-----------------------------数据统计 课程老师、学生数、动态数、通知数
|
|
def course_count_cache(course_id)
|
|
key = "course_count:"+course_id.to_s
|
|
data = Rails.cache.read(key)
|
|
if data.nil?
|
|
course = Course.find(course_id)
|
|
teacher_num = TeacherAndAssistantCount(course)
|
|
student_num = studentCount(course)
|
|
act_count = course.course_activities.count
|
|
news_count = course.news.count
|
|
|
|
data = {
|
|
:teacher_num=>teacher_num,
|
|
:student_num=>student_num,
|
|
:act_count=>act_count,
|
|
:news_count=>news_count
|
|
}
|
|
|
|
Rails.cache.write(key,data)
|
|
end
|
|
data
|
|
end
|
|
|
|
#更新
|
|
def update_course_count_cache(course_id,opt={})
|
|
key = "course_count:"+course_id.to_s
|
|
data = Rails.cache.read(key)
|
|
if data.nil?
|
|
# course = Course.find(course_id)
|
|
# teacher_num = TeacherAndAssistantCount(course)
|
|
# student_num = studentCount(course)
|
|
# act_count = course.course_activities.count
|
|
# news_count = course.news.count
|
|
#
|
|
# data = {
|
|
# :teacher_num=>teacher_num,
|
|
# :student_num=>student_num,
|
|
# :act_count=>act_count,
|
|
# :news_count=>news_count
|
|
# }
|
|
#
|
|
# Rails.cache.write(key,data)
|
|
else
|
|
data = data.merge(opt)
|
|
Rails.cache.write(key,data)
|
|
end
|
|
end
|
|
|
|
#----------------------作业列表------------------------------
|
|
def course_homeworks_cache(course_id)
|
|
key = 'course_homeworks:'+course_id.to_s
|
|
data = Rails.cache.read(key)
|
|
if data.nil?
|
|
data = []
|
|
course = Course.find(course_id)
|
|
course.homework_commons.order("created_at desc").each do |homework|
|
|
data << {
|
|
:id=>homework.id,
|
|
:name=>homework.name,
|
|
:user_id=>homework.user_id,
|
|
:publish_time=>homework.publish_time,
|
|
:updated_at=>homework.created_at,
|
|
}
|
|
end
|
|
Rails.cache.write(key,data)
|
|
end
|
|
data
|
|
end
|
|
|
|
#增加删除 type
|
|
def update_course_homeworks_cache(course_id,homework_id,type,opt={})
|
|
key = 'course_homeworks:'+course_id.to_s
|
|
data = Rails.cache.read(key)
|
|
if data.nil?
|
|
# data = []
|
|
# course = Course.find(course_id)
|
|
# course.homework_commons.order("created_at desc").each do |homework|
|
|
# data << {
|
|
# :id=>homework.id,
|
|
# :name=>homework.name,
|
|
# :user_id=>homework.user_id,
|
|
# :publish_time=>homework.publish_time,
|
|
# :updated_at=>homework.created_at,
|
|
# }
|
|
# end
|
|
# Rails.cache.write(key,data)
|
|
else
|
|
if type == 0
|
|
#增加
|
|
homework = HomeworkCommon.find(homework_id)
|
|
#增加在第一个
|
|
opt = {
|
|
:id=>homework.id,
|
|
:name=>homework.name,
|
|
:user_id=>homework.user_id,
|
|
:publish_time=>homework.publish_time,
|
|
:updated_at=>homework.created_at,
|
|
}
|
|
data.insert(0,opt)
|
|
elsif type == 1
|
|
#删除
|
|
data.each do |homework|
|
|
if homework[:id] == homework_id
|
|
data.delete(homework)
|
|
break
|
|
end
|
|
end
|
|
else
|
|
#修改
|
|
data.each_with_index do |homework,index|
|
|
if homework[:id] == homework_id
|
|
data[index] = data[index].merge(opt)
|
|
break
|
|
end
|
|
end
|
|
end
|
|
Rails.cache.write(key,data)
|
|
end
|
|
end
|
|
|
|
#--------------------班级活跃度列表---------------------
|
|
def course_contributor_score_cache(course_id)
|
|
key = 'course_contributor_score:'+course_id.to_s
|
|
data = Rails.cache.read(key)
|
|
if data.nil?
|
|
data = []
|
|
|
|
# course = Course.find(course_id)
|
|
# course.course_contributor_scores.each do |contributor_score|
|
|
# data << {
|
|
# :id=>contributor_score.id,
|
|
# :user_id=>contributor_score.user_id,
|
|
# :resource_num=>contributor_score.resource_num,
|
|
# :message_num=>contributor_score.message_num,
|
|
# :message_reply_num=>contributor_score.message_reply_num,
|
|
# :journal_num=>contributor_score.journal_num,
|
|
# :homework_journal_num=>contributor_score.homework_journal_num,
|
|
# :news_reply_num=>contributor_score.news_reply_num,
|
|
# :news_num=>contributor_score.news_num,
|
|
# :total_score=>contributor_score.resource_num.to_i * 5 + contributor_score.message_num.to_i * 2 +
|
|
# contributor_score.message_reply_num.to_i * 1 + contributor_score.journal_num.to_i * 1 +
|
|
# + contributor_score.homework_journal_num.to_i * 1 + contributor_score.news_reply_num.to_i * 1 +
|
|
# contributor_score.news_num.to_i * 1
|
|
# }
|
|
# end
|
|
|
|
ccs = CourseContributorScore.find_by_sql("SELECT ccs.*, (message_num*2 + message_reply_num + news_reply_num + news_num +
|
|
resource_num*5 + journal_num + homework_journal_num ) as con_score FROM `course_contributor_scores` ccs JOIN students_for_courses sfs
|
|
ON sfs.student_id = ccs.user_id AND sfs.course_id = ccs.course_id where ccs.course_id = #{course_id} order by
|
|
con_score desc;")
|
|
|
|
ccs.each do |contributor_score|
|
|
data << {
|
|
:id=>contributor_score.id,
|
|
:user_id=>contributor_score.user_id,
|
|
:resource_num=>contributor_score.resource_num,
|
|
:message_num=>contributor_score.message_num,
|
|
:message_reply_num=>contributor_score.message_reply_num,
|
|
:journal_num=>contributor_score.journal_num,
|
|
:homework_journal_num=>contributor_score.homework_journal_num,
|
|
:news_reply_num=>contributor_score.news_reply_num,
|
|
:news_num=>contributor_score.news_num,
|
|
:total_score=>contributor_score.con_score
|
|
}
|
|
end
|
|
|
|
Rails.cache.write(key,data)
|
|
end
|
|
data
|
|
end
|
|
|
|
def update_course_contributor_score_cache(course_id,user_id,opt={})
|
|
key = 'course_contributor_score:'+course_id.to_s
|
|
data = Rails.cache.read(key)
|
|
if data.nil?
|
|
# data = []
|
|
# ccs = CourseContributorScore.find_by_sql("SELECT ccs.*, (message_num*2 + message_reply_num + news_reply_num + news_num +
|
|
# resource_num*5 + journal_num + homework_journal_num ) as con_score FROM `course_contributor_scores` ccs JOIN students_for_courses sfs
|
|
# ON sfs.student_id = ccs.user_id AND sfs.course_id = ccs.course_id where ccs.course_id = #{course_id} order by
|
|
# con_score desc;")
|
|
#
|
|
# ccs.each do |contributor_score|
|
|
# data << {
|
|
# :id=>contributor_score.id,
|
|
# :user_id=>contributor_score.user_id,
|
|
# :resource_num=>contributor_score.resource_num,
|
|
# :message_num=>contributor_score.message_num,
|
|
# :message_reply_num=>contributor_score.message_reply_num,
|
|
# :journal_num=>contributor_score.journal_num,
|
|
# :homework_journal_num=>contributor_score.homework_journal_num,
|
|
# :news_reply_num=>contributor_score.news_reply_num,
|
|
# :news_num=>contributor_score.news_num,
|
|
# :total_score=>contributor_score.con_score
|
|
# }
|
|
# end
|
|
#
|
|
# Rails.cache.write(key,data)
|
|
else
|
|
has_record = false
|
|
data.each_with_index do |contributor_score,index|
|
|
if contributor_score[:user_id] == user_id
|
|
data[index] = data[index].merge(opt)
|
|
has_record = true
|
|
break
|
|
end
|
|
end
|
|
|
|
if !has_record
|
|
data << opt
|
|
end
|
|
|
|
Rails.cache.write(key,data)
|
|
end
|
|
end
|
|
|
|
#--------------------班级英雄榜列表---------------------
|
|
def course_heroes_cache(course_id)
|
|
key = 'course_heroes:'+course_id.to_s
|
|
data = Rails.cache.read(key)
|
|
if data.nil?
|
|
data = []
|
|
course = Course.find(course_id)
|
|
|
|
sql_select = "SELECT members.*,(
|
|
SELECT SUM(work_score)
|
|
FROM student_works,homework_commons
|
|
WHERE student_works.homework_common_id = homework_commons.id
|
|
AND homework_commons.course_id = #{course_id}
|
|
AND student_works.user_id = members.user_id
|
|
) AS score
|
|
FROM members
|
|
JOIN students_for_courses
|
|
ON students_for_courses.student_id = members.user_id AND students_for_courses.course_id = members.course_id
|
|
WHERE members.course_id = #{course_id} "
|
|
homework_scores = Member.find_by_sql(sql_select)
|
|
|
|
homework_scores.each do |hero|
|
|
data << {
|
|
:score=>hero.score,
|
|
:user_id=>hero.user_id
|
|
}
|
|
end
|
|
Rails.cache.write(key,data)
|
|
end
|
|
data
|
|
end
|
|
|
|
def update_course_heroes_cache(course_id,user_id,type,opt={})
|
|
key = 'course_heroes:'+course_id.to_s
|
|
data = Rails.cache.read(key)
|
|
if data.nil?
|
|
# data = []
|
|
# course = Course.find(course_id)
|
|
# sql_select = "SELECT members.*,(
|
|
# SELECT SUM(work_score)
|
|
# FROM student_works,homework_commons
|
|
# WHERE student_works.homework_common_id = homework_commons.id
|
|
# AND homework_commons.course_id = #{course_id}
|
|
# AND student_works.user_id = members.user_id
|
|
# ) AS score
|
|
# FROM members
|
|
# JOIN students_for_courses
|
|
# ON students_for_courses.student_id = members.user_id AND students_for_courses.course_id = members.course_id
|
|
# WHERE members.course_id = #{course_id} ORDER BY score desc"
|
|
# homework_scores = Member.find_by_sql(sql_select)
|
|
#
|
|
# homework_scores.each do |hero|
|
|
# data << {
|
|
# :score=>hero.score,
|
|
# :user_id=>hero.user_id
|
|
# }
|
|
# end
|
|
# Rails.cache.write(key,data)
|
|
else
|
|
if type == 0
|
|
#不管存不存在都更新
|
|
sql_select = "SELECT members.*,(
|
|
SELECT SUM(work_score)
|
|
FROM student_works,homework_commons
|
|
WHERE student_works.homework_common_id = homework_commons.id
|
|
AND homework_commons.course_id = #{course_id}
|
|
AND student_works.user_id = #{user_id}
|
|
) AS score
|
|
FROM members
|
|
JOIN students_for_courses
|
|
ON students_for_courses.student_id = members.user_id AND students_for_courses.course_id = members.course_id
|
|
WHERE members.course_id = #{course_id} and members.user_id = #{user_id}"
|
|
|
|
result = Member.find_by_sql(sql_select).first
|
|
if result
|
|
score = result.score
|
|
opt={
|
|
:score=>score,
|
|
:user_id=>user_id
|
|
}
|
|
has_record = false
|
|
data.each_with_index do |hero,index|
|
|
if hero[:user_id] == user_id
|
|
data[index] = data[index].merge(opt)
|
|
has_record = true
|
|
break
|
|
end
|
|
end
|
|
if !has_record
|
|
data << opt
|
|
end
|
|
end
|
|
else
|
|
has_record = false
|
|
data.each_with_index do |hero,index|
|
|
if hero[:user_id] == user_id
|
|
data[index] = data[index].merge(opt)
|
|
has_record = true
|
|
break
|
|
end
|
|
end
|
|
if !has_record
|
|
data << opt
|
|
end
|
|
end
|
|
Rails.cache.write(key,data)
|
|
end
|
|
end
|
|
|
|
#--------------------班级动态列表---------------------
|
|
def course_acts_cache(course_id)
|
|
key = 'course_acts:'+course_id.to_s
|
|
data = Rails.cache.read(key)
|
|
if data.nil?
|
|
# data = []
|
|
course = Course.find(course_id)
|
|
data = course.course_activities
|
|
# course.course_activities.each do |act|
|
|
# data << {
|
|
# :id=>act.id,
|
|
# :user_id=>act.user_id,
|
|
# :course_act_id=>act.course_act_id,
|
|
# :course_act_type=>act.course_act_type,
|
|
# :updated_at=>act.updated_at
|
|
# }
|
|
# end
|
|
|
|
Rails.cache.write(key,data)
|
|
end
|
|
data
|
|
end
|
|
|
|
#更新内容
|
|
def update_course_acts_cache(course_id,course_act_id,course_act_type,opt={})
|
|
key = 'course_acts:'+course_id.to_s
|
|
data = Rails.cache.read(key)
|
|
if data.nil?
|
|
# data = []
|
|
# course = Course.find(course_id)
|
|
# course.course_activities.each do |act|
|
|
# data << {
|
|
# :id=>act.id,
|
|
# :user_id=>act.user_id,
|
|
# :course_act_id=>act.course_act_id,
|
|
# :course_act_type=>act.course_act_type,
|
|
# :updated_at=>act.updated_at
|
|
# }
|
|
# end
|
|
# Rails.cache.write(key,data)
|
|
else
|
|
data.each_with_index do |act,index|
|
|
if act[:course_act_id] == course_act_id && act[:course_act_type] == course_act_type
|
|
data[index] = data[index].merge(opt)
|
|
break
|
|
end
|
|
end
|
|
Rails.cache.write(key,data)
|
|
end
|
|
end
|
|
|
|
#更新条数
|
|
def update_course_acts_nums_cache(course_id,course_act,type)
|
|
key = 'course_acts:'+course_id.to_s
|
|
data = Rails.cache.read(key)
|
|
if data.nil?
|
|
# data = []
|
|
# course = Course.find(course_id)
|
|
# course.course_activities.each do |act|
|
|
# data << {
|
|
# :id=>act.id,
|
|
# :user_id=>act.user_id,
|
|
# :course_act_id=>act.course_act_id,
|
|
# :course_act_type=>act.course_act_type,
|
|
# :updated_at=>act.updated_at
|
|
# }
|
|
# end
|
|
# Rails.cache.write(key,data)
|
|
else
|
|
if type == 1
|
|
#删除
|
|
data.each do |act|
|
|
if act[:id] == course_act.id
|
|
data.delete(act)
|
|
break
|
|
end
|
|
end
|
|
else
|
|
#增加
|
|
data << {
|
|
:id=>course_act.id,
|
|
:user_id=>course_act.user_id,
|
|
:course_act_id=>course_act.course_act_id,
|
|
:course_act_type=>course_act.course_act_type,
|
|
:updated_at=>course_act.updated_at
|
|
}
|
|
update_excelletn_courses_cache(course_id,3,{:updated_at=>course_act.updated_at})
|
|
end
|
|
Rails.cache.write(key,data)
|
|
end
|
|
course_count = course_count_cache(course_id)
|
|
if type == 1
|
|
update_course_count_cache(course_id,{:act_count=>course_count[:act_count]-1})
|
|
else
|
|
update_course_count_cache(course_id,{:act_count=>course_count[:act_count]+1})
|
|
end
|
|
end
|
|
|
|
#--------------------精品课程---------------------
|
|
def excelletn_courses_cache()
|
|
key = 'excelletn_courses'
|
|
data = Rails.cache.read(key)
|
|
if data.nil?
|
|
data = []
|
|
|
|
sql = "SELECT c.id,max(cs.updated_at) updated_at FROM course_activities cs, courses c where cs.course_id = c.id
|
|
and (c.is_excellent =1 or c.excellent_option =1) and c.is_public = 1 group by id order by updated_at desc ;"
|
|
|
|
Course.find_by_sql(sql).each do |ec|
|
|
data << {
|
|
:id=>ec.id,
|
|
:tags=>ec.tags,
|
|
:updated_at=>ec.updated_at
|
|
}
|
|
end
|
|
|
|
Rails.cache.write(key,data)
|
|
end
|
|
data
|
|
end
|
|
|
|
#更新内容
|
|
def update_excelletn_courses_cache(course_id,type,opt={})
|
|
key = 'excelletn_courses'
|
|
data = Rails.cache.read(key)
|
|
if data.nil?
|
|
# data = []
|
|
#
|
|
# sql = "SELECT c.id,max(cs.updated_at) updated_at FROM course_activities cs, courses c where cs.course_id = c.id
|
|
# and (c.is_excellent =1 or c.excellent_option =1) and c.is_public = 1 group by id order by updated_at desc ;"
|
|
#
|
|
# Course.find_by_sql(sql).each do |ec|
|
|
# data << {
|
|
# :id=>ec.id,
|
|
# :tags=>ec.tags,
|
|
# :updated_at=>ec.updated_at
|
|
# }
|
|
# end
|
|
# Rails.cache.write(key,data)
|
|
else
|
|
if type == 1
|
|
#增加
|
|
course = Course.find(course_id)
|
|
if course.is_public == 1
|
|
sql = "SELECT max(updated_at) updated_at FROM course_activities where course_id =#{course_id}"
|
|
info = Course.find_by_sql(sql).first
|
|
data << {
|
|
:id=>course_id,
|
|
:tags=>course.tags,
|
|
:updated_at=>info.updated_at
|
|
}
|
|
end
|
|
elsif type == 2
|
|
#删除
|
|
data.each do |ec|
|
|
if ec[:id] == course_id
|
|
data.delete(ec)
|
|
break
|
|
end
|
|
end
|
|
else
|
|
#修改
|
|
data.each_with_index do |ex,index|
|
|
if ex[:id] == course_id
|
|
data[index] = data[index].merge(opt)
|
|
break
|
|
end
|
|
end
|
|
end
|
|
|
|
Rails.cache.write(key,data)
|
|
end
|
|
end
|
|
|
|
#-----------------------------班级最新动态时间--------------
|
|
def course_act_updated_at_cache(course_id)
|
|
key = "course_act_updated_at"
|
|
data = Rails.cache.read(key) || []
|
|
if data[course_id].nil?
|
|
sql = "SELECT max(updated_at) updated_at FROM course_activities where course_id =#{course_id}"
|
|
updated_at = Course.find_by_sql(sql).first.updated_at
|
|
data[course_id] = updated_at
|
|
Rails.cache.write(key,data)
|
|
end
|
|
data[course_id]
|
|
end
|
|
|
|
#更新
|
|
def update_course_act_updated_at_cache(course_id)
|
|
key = "course_act_updated_at"
|
|
data = Rails.cache.read(key) || []
|
|
sql = "SELECT max(updated_at) updated_at FROM course_activities where course_id =#{course_id}"
|
|
updated_at = Course.find_by_sql(sql).first.updated_at
|
|
data[course_id] = updated_at
|
|
Rails.cache.write(key,data)
|
|
end
|
|
|
|
#更新
|
|
def update_course_act_updated_at_to_0_cache(course_id)
|
|
key = "course_act_updated_at"
|
|
data = Rails.cache.read(key) || []
|
|
data[course_id] = 0
|
|
Rails.cache.write(key,data)
|
|
end
|
|
|
|
end
|
|
end
|