From f4f61a5f4c39962170a1a7009db05cc0a9806d07 Mon Sep 17 00:00:00 2001 From: cano Date: Sun, 3 Mar 2024 10:15:50 +0800 Subject: [PATCH] fix some problems --- app/Models/Post/PostModel.php | 35 ++++++++++++++++-- app/Models/Post/PostPushBoxModel.php | 53 +++++++++++++++------------- app/Tools/CollectOffsetLimit.php | 4 +-- 3 files changed, 64 insertions(+), 28 deletions(-) diff --git a/app/Models/Post/PostModel.php b/app/Models/Post/PostModel.php index 3fade4e..5cf00d7 100644 --- a/app/Models/Post/PostModel.php +++ b/app/Models/Post/PostModel.php @@ -4,6 +4,7 @@ namespace App\Models\Post; use App\Exceptions\ModelException; use App\Models\Base\BaseModel; +use App\Models\Post\Structs\PostParamsStruct; use App\Tools\Tools; use Illuminate\Database\Eloquent\Casts\Attribute; use Illuminate\Database\Eloquent\SoftDeletes; @@ -24,6 +25,7 @@ class PostModel extends BaseModel 'uid', 'media', 'content', + 'post_params', 'created_at', 'deleted_at', ]; @@ -44,12 +46,27 @@ class PostModel extends BaseModel ); } + protected function postParams(): Attribute + { + return Attribute::make( + get: fn(string $value = '') => Tools::JonsDecode($value), + set: fn(array $value = []) => Tools::JonsEncode($value), + ); + } + /** * @throws ModelException */ function addPost($uid, $type = self::TYPE_POST, $content = null, $media = null, $mid = null, $sBachSn = null): \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Builder|array|null { if (!in_array($type, [self::TYPE_POST, self::TYPE_REPOST])) throw new ModelException('type params error'); + $aPostParams = []; + if($type == self::TYPE_REPOST){ + $oRePostModel = $this->findItem($mid); + $aPostParams[PostParamsStruct::REPOST_ORG_USER_ID] = $oRePostModel->uid; + $aPostParams[PostParamsStruct::REPOST_ORG_POST_ID] = $oRePostModel->mid; + } + $uuid = Tools::genUuid(); $bsn = 'BSN_' . ($sBachSn ?? Tools::genUuid()); if (!$content && !$media) throw new ModelException('addPost params error'); @@ -60,6 +77,7 @@ class PostModel extends BaseModel $aItem['mid'] = $mid; $aItem['media'] = $media; $aItem['content'] = $content; + $aItem['post_params'] = $aPostParams; $sDateTime = date('Y-m-d H:i:s'); $aItem['created_at'] = $sDateTime; $res = $this->addItem($aItem); @@ -168,10 +186,23 @@ class PostModel extends BaseModel return $this->getItemsByWhere(['uid' => $uid, 'mid' => $mid]); } - function getPostListByUids($uids, $sDateLimit = null): \Illuminate\Database\Eloquent\Collection|array + function getPostListByUids($uids, $sDateLimit = null,$cols = ['*'],$offset = null, $limit = null): \Illuminate\Database\Eloquent\Collection|array + { + $oModel = $this->newQuery(); + if ($offset) $oModel->offset($offset); + if ($limit) $oModel->limit($offset); + if ($sDateLimit == null) $sDateLimit = date('Y-m-d H:i:s', strtotime('-3 day')); + return $oModel + ->where('created_at', $sDateLimit) + ->whereIn('uid', $uids) + ->orderBy('created_at') + ->get($cols); + } + + function CountPostListByUids($uids, $sDateLimit = null): int { if ($sDateLimit == null) $sDateLimit = date('Y-m-d H:i:s', strtotime('-3 day')); - return $this->newQuery()->where('created_at', $sDateLimit)->whereIn('uid', $uids)->get(); + return $this->newQuery()->where('created_at', $sDateLimit)->whereIn('uid', $uids)->orderBy('created_at')->count(); } diff --git a/app/Models/Post/PostPushBoxModel.php b/app/Models/Post/PostPushBoxModel.php index eb33ea1..5d5ac94 100644 --- a/app/Models/Post/PostPushBoxModel.php +++ b/app/Models/Post/PostPushBoxModel.php @@ -77,8 +77,8 @@ class PostPushBoxModel extends BaseModel protected function postParams(): Attribute { return Attribute::make( - get: fn (string $value = '') => Tools::JonsDecode($value), - set: fn (array $value = []) => Tools::JonsEncode($value), + get: fn(string $value = '') => Tools::JonsDecode($value), + set: fn(array $value = []) => Tools::JonsEncode($value), ); } @@ -93,14 +93,14 @@ class PostPushBoxModel extends BaseModel */ function addPostQueueConsumer(array $params) { - if(empty($params)) return false; - if(isset($params['id'])) return false; - if(isset($params['type'])) return false; + if (empty($params)) return false; + if (isset($params['id'])) return false; + if (isset($params['type'])) return false; $id = $params['id']; $type = $params['type']; - if(!in_array($type,[self::TYPE_POST,self::TYPE_REPOST,self::TYPE_COMMENT])) return false; - if(empty($id)) return false; + if (!in_array($type, [self::TYPE_POST, self::TYPE_REPOST, self::TYPE_COMMENT])) return false; + if (empty($id)) return false; $oPostModel = new PostModel(); $oPost = null; @@ -112,13 +112,13 @@ class PostPushBoxModel extends BaseModel } elseif ($type == self::TYPE_REPOST) { $postId = $id; $oRePostModel = $oPostModel->findItem($id); - if(!$oRePostModel) return false; + if (!$oRePostModel) return false; $aPostParams[PostParamsStruct::REPOST_ORG_USER_ID] = $oRePostModel->uid; $aPostParams[PostParamsStruct::REPOST_ORG_POST_ID] = $oRePostModel->mid; } elseif ($type == self::TYPE_COMMENT) { $oPostCommentModel = new PostCommentModel(); $oPostCommentModel = $oPostCommentModel->findItem($id); - if(!$oPostCommentModel) return false; + if (!$oPostCommentModel) return false; $postId = $oPostCommentModel->pid; $aPostParams[PostParamsStruct::COMMENT_ID] = $oPostCommentModel->id; $aPostParams[PostParamsStruct::COMMENT_USER_ID] = $oPostCommentModel->uid; @@ -143,12 +143,13 @@ class PostPushBoxModel extends BaseModel $iTotalCount = $this->countSendFans($bSendMode, $oPost->uid); //计算发送总数 $aPost = $oPost->toArray(); - if(empty($aPost)) return false; + if (empty($aPost)) return false; $aPost['post_params'] = $aPostParams; //分批发送 $oCollectOffsetLimit = new CollectOffsetLimit(); $oCollectOffsetLimit->setITotalCount($iTotalCount)->runWhile(function ($offset, $limit) use ($aPost, $bSendMode) { $oFollowList = $this->getFansListWithPage($bSendMode, $aPost['uid'], $offset, $limit); + if(empty($oFollowList)) return; $this->sendPostToBox($aPost, $oFollowList); }); @@ -201,7 +202,7 @@ class PostPushBoxModel extends BaseModel * @param $limit * @return \Illuminate\Database\Eloquent\Collection|array */ - function getPushBoxList($uid, $last_id = 0, $limit = 20,$cols = ['*']): \Illuminate\Database\Eloquent\Collection|array + function getPushBoxList($uid, $last_id = 0, $limit = 20, $cols = ['*']): \Illuminate\Database\Eloquent\Collection|array { //活跃用户直接拉取未读消息 return $this->newQuery() @@ -218,33 +219,37 @@ class PostPushBoxModel extends BaseModel //放在用户状态更新时调用 function pullBigFanMasterPostConsumer($uid) { - //@@获取文章和转发文章和评论文章 + //获取文章和转发文章 //获取大v定义粉丝数,获取大于该粉丝数的大v文章 $iConfigFansPushLimit = intval(env('CONFIG_FANS_PUSH_LIMIT', 2000)); - //@@此处需针对大量数据进行分批获取 $oFollowModel = new FollowModel(); - $oFollowList = $oFollowModel->getFollowListWithFansLimit($uid,$iConfigFansPushLimit,['a.follow_uid']); + $oFollowList = $oFollowModel->getFollowListWithFansLimit($uid, $iConfigFansPushLimit, ['a.follow_uid']); if ($oFollowList->isEmpty()) return null; $aFollowList = $oFollowList->toArray(); - $aFollowUid = array_column($aFollowList,'follow_uid'); + $aFollowUid = array_column($aFollowList, 'follow_uid'); $oPostModel = new PostModel(); - $oPostList = $oPostModel->getPostListByUids($aFollowUid); - - //将推文发送到信箱 - self::sendPostToBoxByUid($oPostList,$uid); + //分批发送到信箱 + //@@大v评论拉取到自己信箱 + $iTotalCount = $oPostModel->CountPostListByUids($aFollowUid); + $oCollectOffsetLimit = new CollectOffsetLimit(); + $oCollectOffsetLimit->setITotalCount($iTotalCount)->runWhile(function ($offset, $limit) use ($oPostModel,$aFollowUid,$uid) { + $oPostList = $oPostModel->getPostListByUids($aFollowUid,null,['*'],$offset, $limit); + if(empty($oPostList)) return; + $this->sendPostToBoxByUid($oPostList->toArray(), $uid); + }); } - function sendPostToBoxByUid($oPostList, $uid): void + function sendPostToBoxByUid(array $oPostList, $uid): void { - $date = date('Y-m-d H:i:s'); foreach ($oPostList as $oPost) { + $aItem['type'] = $oPost->type; $aItem['uid'] = $uid; $aItem['pid'] = $oPost->id; $aItem['puuid'] = $oPost->uuid; - $aItem['created_at'] = $oPost->created_at; - $aItem['created_box_at'] = $date; - $this->addItem($aItem); + $aItem['post_params'] = $oPost->post_params; + $aItem['post_created_at'] = $oPost->created_at; + $this->addItemWithCreateTime($aItem); } } diff --git a/app/Tools/CollectOffsetLimit.php b/app/Tools/CollectOffsetLimit.php index e5dc47b..9d8a5c6 100644 --- a/app/Tools/CollectOffsetLimit.php +++ b/app/Tools/CollectOffsetLimit.php @@ -5,7 +5,7 @@ namespace App\Tools; class CollectOffsetLimit { private $offset = 0; - private $limit = 2000; + private $limit = 1000; private $page = 0; private $iTotalCount = null; @@ -34,7 +34,7 @@ class CollectOffsetLimit { $this->countPage(); if($this->iTotalCount === null) $this->iTotalCount = $this->fCountFun; - if($this->iTotalCount === 0 | $this->page === 0) return []; + if($this->iTotalCount === 0 || $this->page === 0) return []; for( $iPage = 1; $iPage <= $this->page; $iPage++){ $this->offset = ($iPage - 1) * $this->limit; $fun($this->offset,$this->limit);