From 4072011dca07b222f3896af1e9d64fb8b56add68 Mon Sep 17 00:00:00 2001 From: daiao <358551898@qq.com> Date: Wed, 5 Jun 2019 15:24:33 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=9E=E8=AE=AD=E8=AF=84=E8=AE=BA=E7=9A=84?= =?UTF-8?q?=E5=9B=9E=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/services/shixuns_service.rb | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/app/services/shixuns_service.rb b/app/services/shixuns_service.rb index b7834ea7..887f07c4 100644 --- a/app/services/shixuns_service.rb +++ b/app/services/shixuns_service.rb @@ -69,8 +69,20 @@ class ShixunsService dis = Shixun.select([:id, :user_id]).find(dis_id) dis_type = params[:container_type] # 如:"Shixun" # 总数,分页使用 - disscuss_count = Discuss.where(:dis_id => dis_id, :dis_type => dis_type, :root_id => nil).count - discusses = Discuss.limit(LIMIT).where(:dis_id => dis_id, :dis_type => dis_type, :root_id => nil).includes(:user, :praise_tread).offset(offset) + if current_user.admin? + disscuss_count = Discuss.where(:dis_id => dis_id, :dis_type => dis_type, :root_id => nil).count + discusses = Discuss.limit(LIMIT).where(:dis_id => dis_id, :dis_type => dis_type, + :root_id => nil).includes(:user, :praise_tread).offset(offset) + else + disscusses = Discuss.where("dis_id = :dis_id and dis_type = :dis_type and root_id = :root_id and + (hidden = :hidden or user_id = :user_id)", + {dis_id: dis_id, dis_type: dis_type, root_id: nil, hidden: false, + user_id: current_user.id}) + + disscuss_count = disscusses.count + discusses = disscusses.limit(LIMIT).includes(:user, :praise_tread).offset(offset) + end + base_data discusses, dis, current_user return {:children_list => @children_list, :disscuss_count => disscuss_count} @@ -119,9 +131,6 @@ class ShixunsService @children_list = [] # 目前只取十个,不多N+1问题不大 # 需要彻底解决则需要改数据路结构,比如Nested算法可以解决 - unless current_user.admin? - discusses = discusses.where(:hidden => false) - end if discusses.present? discusses.each do |d| # 总点赞数 @@ -145,7 +154,13 @@ class ShixunsService :user_praise => user_praise, :admin => current_user.admin?} # 现在没有二级回复,所以查询的时候直接从root_id取 - children = Discuss.where(:root_id => d.id).includes(:user).reorder("created_at asc") + children = + if current_user.admin? + Discuss.where(root_id: d.id).includes(:user).reorder("created_at asc") + else + Discuss.where("root_id = :root_id and (hidden = :hidden or user_id = :user_id", + {root_id: d.id, hidden: false, user_id: current_user.id}).includes(:user).reorder("created_at asc") + end @children_list << parents.merge({:children => (children.map{|child| [:content => child.content, :time => time_from_now(child.created_at), :position => child.position , :reward => child.reward,:hidden => child.hidden, :image_url => url_to_avatar(child.user), :username => child.username, :user_id => child.user_id, :user_login => child.user.try(:login),