Browse Source

朋友圈列表查询逻辑优化

author
wanghongjun 9 months ago
parent
commit
5cce7ebc1b
  1. 37
      app/enterprise/controller/Posts.php

37
app/enterprise/controller/Posts.php

@ -27,36 +27,37 @@ class Posts extends BaseController
// 朋友圈列表 // 朋友圈列表
public function index() public function index()
{ {
$where = '(p.delete_time = 0 and p.privacy > 0) and '; $where = '(p.delete_time = 0 and p.privacy > 0) ';
$whereOr = $where . ' ( (p.user_id = ' . $this->uid; $where .= ' and ( p.user_id = ' . $this->uid;
$prefix = config('database.connections.mysql.prefix');
$friendIds = Friend::getFriendIds($this->uid); $friendIds = Friend::getFriendIds($this->uid);
if ($friendIds) { if ($friendIds) {
$friendIdStr = implode(',',$friendIds); $friendIdStr = implode(',',$friendIds);
$whereOr .= ' or ( p.privacy = 1 and p.user_id in (' . $friendIdStr . '))'; $where .= ' or ( p.privacy = 1 and p.user_id in (' . $friendIdStr . '))';
}
$prefix = config('database.connections.mysql.prefix'); // 可见
$where .= " or ( p.privacy = 2 and exists (
select 1 from {$prefix}posts_privacy_users pvu
where pvu.posts_id = p.id and pvu.user_id = p.user_id
and pvu.type = 1 and pvu.user_id in ({$friendIdStr})
))";
// 可见 }
$whereOr .= " or ( p.privacy = 2 and exists (
select 1 from {$prefix}posts_privacy_users pvu
where pvu.posts_id = p.id and pvu.user_id = p.user_id
and pvu.type = 1 and pvu.user_id = {$this->uid}
))";
// 不可见 // 不可见
$whereOr .= " or ( p.privacy = 4 and not exists ( $where .= " or ( p.privacy = 4 and not exists (
select 1 from {$prefix}posts_privacy_users peu select 1 from {$prefix}posts_privacy_users peu
where peu.posts_id = p.id and peu.user_id = p.user_id where peu.posts_id = p.id and peu.user_id = p.user_id
and peu.type = 2 and peu.user_id = {$this->uid} and peu.type = 2 and peu.user_id = {$this->uid}
)))"; ))";
$where .= ')';
$field = 'p.id,p.content,p.location,p.address as location_address,p.type,p.user_id,p.create_time'; $field = 'p.id,p.content,p.location,p.address as location_address,p.type,p.user_id,p.create_time';
$order = 'p.create_time desc'; $order = 'p.create_time desc';
$model = new PostsModel(); $model = new PostsModel();
$list = $this->paginate($model->alias('p')->where($whereOr)->field($field)->order($order)); $list = $this->paginate($model->alias('p')->where($where)->field($field)->order($order));
$data = []; $data = [];
if ($list) { if ($list) {
@ -319,7 +320,7 @@ class Posts extends BaseController
{ {
$existsWhere = ['user_id' => $this->uid, 'privacy' => 0, 'delete_time' => 0]; $existsWhere = ['user_id' => $this->uid, 'privacy' => 0, 'delete_time' => 0];
$field = 'id as posts_id,content,type,location,address as location_address'; $field = 'id as posts_id,content,type,location,address as location_address';
$data = PostsModel::where($existsWhere)->field($field)->find()->toArray(); $data = PostsModel::where($existsWhere)->field($field)->find();
if ($data) { if ($data) {
$fileWhere = [ $fileWhere = [
'posts_id' => $data['posts_id'], 'posts_id' => $data['posts_id'],

Loading…
Cancel
Save