消息的部分优化

This commit is contained in:
SylorHuang 2019-11-07 09:04:33 +08:00
parent 5a061f1ca2
commit 82956b37dd
4 changed files with 56 additions and 69 deletions

View File

@ -11,20 +11,19 @@ class PrivateMessagesController < ApplicationController
User.current.onclick_time.update_attribute(:onclick_time, Time.now)
jours = PrivateMessage.find_by_sql("SELECT ui.* FROM (SELECT * FROM private_messages WHERE STATUS != 2 AND user_id = #{@user.id} ORDER BY id DESC) ui GROUP BY ui.target_id ORDER BY ui.send_time DESC")
@unred_all_messages = @user.count_new_message("all")
@unred_private_messages = @user.unread_private_messages
# jours = PrivateMessage.without_deleted.where("user_id = ?", @user.id).order("created_at desc")
@limit = 20
@jours_count = jours.count
@jours_count = jours.size
@jours_all_pages = Paginator.new @jours_count, @limit, params['page'] || 1
@offset ||= @jours_all_pages.offset
@jours_alls = paginateHelper jours, @limit
@unred_all_messages = @user.count_new_message("all")
@unred_private_messages = @user.unread_private_messages
render :layout=>'base_user_message'
else
render_403
end
end
def new

View File

@ -506,41 +506,41 @@ class UsersController < ApplicationController
return render_403
end
# 初始化/更新 点击按钮时间, 24小时内显示系统消息
user_all_messages = MessageAll.where(:user_id => @user.id).includes(:message)
user_all_messages = @user.get_user_messages
# 全部设为已读
case @msg_type
when "all"
@message_alls = user_all_messages
when "course"
@message_alls = user_all_messages.where(message_type: "CourseMessage")
@message_alls = user_all_messages.where(message_type: "CourseMessage").order("created_at desc")
when "project"
@message_alls = user_all_messages.where(message_type: "ForgeMessage")
@message_alls = user_all_messages.where(message_type: "ForgeMessage").order("created_at desc")
when "at"
@message_alls = user_all_messages.where(message_type: %w(UserFeedbackMessage MemoMessage AtMessage BlogMessage))
@message_alls = user_all_messages.where(message_type: %w(UserFeedbackMessage MemoMessage AtMessage BlogMessage)).order("created_at desc")
when "apply"
@message_alls = user_all_messages.where(message_type: "AppliedMessage")
@message_alls = user_all_messages.where(message_type: "AppliedMessage").order("created_at desc")
else
@message_alls = user_all_messages.where(message_type: %w(SystemMessage ContestMessage OrgMessage))
@message_alls = user_all_messages.where(message_type: %w(SystemMessage ContestMessage OrgMessage)).order("created_at desc")
end
@unred_all_messages = @user.count_new_message(@msg_type)
# @unred_private_messages = 0
@unred_private_messages = @user.unread_private_messages
@message_alls = @message_alls.order("created_at desc")
# @unred_all_messages = 0
#点击进入tab后全部更新为已读全部页面除外
# @message_alls = message_alls
@limit = 20
@message_count = @message_alls.count
@message_count = @message_alls.size
@message_all_pages = Paginator.new @message_count, @limit, params['page'] || 1
@offset ||= @message_all_pages.offset
@message_alls = paginateHelper @message_alls, @limit
# @message_alls = message_alls.order("created_at desc")
@unred_all_messages = @user.count_new_message(@msg_type)
# @unred_private_messages = 0
@unred_private_messages = @user.unread_private_messages
respond_to do |format|
format.js
format.html{render layout: "base_user_message"}
end
end
# 初始化/更新 点击按钮时间

View File

@ -178,6 +178,7 @@ class User < Principal
has_one :onclick_time
has_many :system_messages
has_many :at_messages
has_many :message_alls
# 虚拟转换
has_many :new_jours, :as => :jour, :class_name => 'JournalsForMessage', :conditions => "status=1"
@ -502,64 +503,50 @@ class User < Principal
# 新消息统计,
def count_new_message(type)
message_new_time = OnclickTime.where("user_id =?", User.current).first
message_new_time = OnclickTime.where(user_id: id).first
if message_new_time.nil?
message_new_time = OnclickTime.new
message_new_time.user_id = User.current.id
message_new_time.user_id = id
# 第一次初始化点击铃铛时间
message_new_time.onclick_time = User.current.last_login_on.nil? ? Time.now : User.current.last_login_on
message_new_time.onclick_time = last_login_on.nil? ? Time.now : last_login_on
message_new_time.save
end
user = User.current
onclick_time = user.onclick_time.onclick_time
delete_courses = Course.where(:is_delete => 1).blank? ? "(-1)" : "(" + Course.where(:is_delete => 1).map(&:id).join(",") + ")"
course_messages = CourseMessage.where("user_id =? and viewed =? and created_at >? and course_id not in #{delete_courses}", user.id, 0, onclick_time)
course_count = course_messages.size
contest_messages = ContestMessage.where("user_id =? and viewed =? and created_at >?", user.id, 0, onclick_time)
contest_count = contest_messages.count
forge_messages = ForgeMessage.where("user_id =? and viewed =? and created_at >?", user.id, 0, onclick_time)
forge_count = forge_messages.count
user_feedbacks = UserFeedbackMessage.where("user_id =? and viewed =? and created_at >?", user.id, 0, onclick_time)
user_feedback_count = user_feedbacks.count
user_memos = MemoMessage.where("user_id =? and viewed =? and created_at >?", user.id, 0, onclick_time)
user_memo_count = user_memos.count
system_messages = SystemMessage.where("created_at >?", onclick_time)
system_messages_count = system_messages.count
at_messages = AtMessage.where("user_id =? and viewed =? and created_at >?", user.id, 0, onclick_time)
at_count = at_messages.count
org_messages = OrgMessage.where("user_id=? and viewed =? and created_at >?", user.id,0, onclick_time)
org_count = org_messages.count
applied_messages = AppliedMessage.where("user_id=? and viewed =? and created_at >?", user.id, 0, onclick_time)
applied_count = applied_messages.count
blog_messages = BlogMessage.where("user_id=? and viewed=? and created_at >?", user.id, 0, onclick_time)
blog_message_count = blog_messages.count
user_private_messages = PrivateMessage.where("user_id=? and status=?",user.id, 0)
private_messages_count = user_private_messages.size
if type != "all"
if type == "all"
onclick_time = user.onclick_time.onclick_time
delete_courses = Course.where(:is_delete => 1).blank? ? "(-1)" : "(" + Course.where(:is_delete => 1).map(&:id).join(",") + ")"
course_count = CourseMessage.where("user_id =? and viewed =? and created_at >? and course_id not in #{delete_courses}", user.id, 0, onclick_time).size
contest_count = ContestMessage.where("user_id =? and viewed =? and created_at >?", user.id, 0, onclick_time).size
forge_count = ForgeMessage.where("user_id =? and viewed =? and created_at >?", user.id, 0, onclick_time).size
user_feedback_count = UserFeedbackMessage.where("user_id =? and viewed =? and created_at >?", user.id, 0, onclick_time).size
user_memo_count = MemoMessage.where("user_id =? and viewed =? and created_at >?", user.id, 0, onclick_time).size
system_messages_count = SystemMessage.where("created_at >?", onclick_time).size
at_count = AtMessage.where("user_id =? and viewed =? and created_at >?", user.id, 0, onclick_time).size
org_count = OrgMessage.where("user_id=? and viewed =? and created_at >?", user.id,0, onclick_time).size
applied_count = AppliedMessage.where("user_id=? and viewed =? and created_at >?", user.id, 0, onclick_time).size
blog_message_count = BlogMessage.where("user_id=? and viewed=? and created_at >?", user.id, 0, onclick_time).size
# private_messages_count = PrivateMessage.where("user_id=? and status=?",user.id, 0).size
messages_count = course_count + contest_count + forge_count + user_feedback_count + user_memo_count + system_messages_count + at_count + org_count + applied_count + blog_message_count
else
Rails.logger.info("######________type__________##########{type}")
message_new_time.update_attribute(:onclick_time, Time.now)
course_messages.update_all(:viewed => true) unless course_messages.nil?
forge_messages.update_all(:viewed => true) unless forge_messages.nil?
user_feedbacks.update_all(:viewed => true) unless user_feedbacks.nil?
user_memos.update_all(:viewed => true) unless user_memos.nil?
org_messages.update_all(:viewed => true) unless org_messages.nil?
at_messages.update_all(:viewed => true) unless at_messages.nil?
contest_messages.update_all(:viewed => true) unless contest_messages.nil?
blog_messages.update_all(:viewed => true) unless blog_messages.nil?
applied_messages.update_all(:viewed => true) unless applied_messages.nil?
CourseMessage.where(user_id: user.id)&.update_all(:viewed => true)
ForgeMessage.where(user_id: user.id)&.update_all(:viewed => true)
UserFeedbackMessage.where(user_id: user.id)&.update_all(:viewed => true)
MemoMessage.where(user_id: user.id)&.update_all(:viewed => true)
OrgMessage.where(user_id: user.id)&.update_all(:viewed => true)
AtMessage.where(user_id: user.id)&.update_all(:viewed => true)
ContestMessage.where(user_id: user.id)&.update_all(:viewed => true)
BlogMessage.where(user_id: user.id)&.update_all(:viewed => true)
AppliedMessage.where(user_id: user.id)&.update_all(:viewed => true)
messages_count = 0
end
messages_count = course_count + contest_count + forge_count + user_feedback_count + user_memo_count + system_messages_count + at_count + org_count + applied_count + blog_message_count + private_messages_count
messages_count
end
def get_user_messages
message_alls.includes(:message)
end
#未读私信数量

View File

@ -88,7 +88,8 @@
<div class="navHomepageNews" id="user_messages">
<a href="<%= user_message_path(User.current,click_user_message: true) %>" class="fl"><i class="iconfont icon-xiaoxilingdang fontGrey4 f22 fl mt7"></i></a>
<% new_message_count = User.current.count_new_message("all").to_i %>
<% if new_message_count > 0 %>
<% new_private_message_count = User.current.unread_private_messages %>
<% if new_message_count > 0 || new_private_message_count > 0 %>
<%#= link_to new_message_count, user_path(User.current, :click_user_message => true), :class => "newsActive", :target => "_Blank" %>
<span class="haveNews"></span>
<% end %>