fix some problems

This commit is contained in:
cano
2024-03-03 10:15:50 +08:00
parent 1c555934d1
commit f4f61a5f4c
3 changed files with 64 additions and 28 deletions

View File

@ -4,6 +4,7 @@ namespace App\Models\Post;
use App\Exceptions\ModelException; use App\Exceptions\ModelException;
use App\Models\Base\BaseModel; use App\Models\Base\BaseModel;
use App\Models\Post\Structs\PostParamsStruct;
use App\Tools\Tools; use App\Tools\Tools;
use Illuminate\Database\Eloquent\Casts\Attribute; use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\SoftDeletes; use Illuminate\Database\Eloquent\SoftDeletes;
@ -24,6 +25,7 @@ class PostModel extends BaseModel
'uid', 'uid',
'media', 'media',
'content', 'content',
'post_params',
'created_at', 'created_at',
'deleted_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 * @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 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'); 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(); $uuid = Tools::genUuid();
$bsn = 'BSN_' . ($sBachSn ?? Tools::genUuid()); $bsn = 'BSN_' . ($sBachSn ?? Tools::genUuid());
if (!$content && !$media) throw new ModelException('addPost params error'); if (!$content && !$media) throw new ModelException('addPost params error');
@ -60,6 +77,7 @@ class PostModel extends BaseModel
$aItem['mid'] = $mid; $aItem['mid'] = $mid;
$aItem['media'] = $media; $aItem['media'] = $media;
$aItem['content'] = $content; $aItem['content'] = $content;
$aItem['post_params'] = $aPostParams;
$sDateTime = date('Y-m-d H:i:s'); $sDateTime = date('Y-m-d H:i:s');
$aItem['created_at'] = $sDateTime; $aItem['created_at'] = $sDateTime;
$res = $this->addItem($aItem); $res = $this->addItem($aItem);
@ -168,10 +186,23 @@ class PostModel extends BaseModel
return $this->getItemsByWhere(['uid' => $uid, 'mid' => $mid]); 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')); 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();
} }

View File

@ -77,8 +77,8 @@ class PostPushBoxModel extends BaseModel
protected function postParams(): Attribute protected function postParams(): Attribute
{ {
return Attribute::make( return Attribute::make(
get: fn (string $value = '') => Tools::JonsDecode($value), get: fn(string $value = '') => Tools::JonsDecode($value),
set: fn (array $value = []) => Tools::JonsEncode($value), set: fn(array $value = []) => Tools::JonsEncode($value),
); );
} }
@ -93,14 +93,14 @@ class PostPushBoxModel extends BaseModel
*/ */
function addPostQueueConsumer(array $params) function addPostQueueConsumer(array $params)
{ {
if(empty($params)) return false; if (empty($params)) return false;
if(isset($params['id'])) return false; if (isset($params['id'])) return false;
if(isset($params['type'])) return false; if (isset($params['type'])) return false;
$id = $params['id']; $id = $params['id'];
$type = $params['type']; $type = $params['type'];
if(!in_array($type,[self::TYPE_POST,self::TYPE_REPOST,self::TYPE_COMMENT])) return false; if (!in_array($type, [self::TYPE_POST, self::TYPE_REPOST, self::TYPE_COMMENT])) return false;
if(empty($id)) return false; if (empty($id)) return false;
$oPostModel = new PostModel(); $oPostModel = new PostModel();
$oPost = null; $oPost = null;
@ -112,13 +112,13 @@ class PostPushBoxModel extends BaseModel
} elseif ($type == self::TYPE_REPOST) { } elseif ($type == self::TYPE_REPOST) {
$postId = $id; $postId = $id;
$oRePostModel = $oPostModel->findItem($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_USER_ID] = $oRePostModel->uid;
$aPostParams[PostParamsStruct::REPOST_ORG_POST_ID] = $oRePostModel->mid; $aPostParams[PostParamsStruct::REPOST_ORG_POST_ID] = $oRePostModel->mid;
} elseif ($type == self::TYPE_COMMENT) { } elseif ($type == self::TYPE_COMMENT) {
$oPostCommentModel = new PostCommentModel(); $oPostCommentModel = new PostCommentModel();
$oPostCommentModel = $oPostCommentModel->findItem($id); $oPostCommentModel = $oPostCommentModel->findItem($id);
if(!$oPostCommentModel) return false; if (!$oPostCommentModel) return false;
$postId = $oPostCommentModel->pid; $postId = $oPostCommentModel->pid;
$aPostParams[PostParamsStruct::COMMENT_ID] = $oPostCommentModel->id; $aPostParams[PostParamsStruct::COMMENT_ID] = $oPostCommentModel->id;
$aPostParams[PostParamsStruct::COMMENT_USER_ID] = $oPostCommentModel->uid; $aPostParams[PostParamsStruct::COMMENT_USER_ID] = $oPostCommentModel->uid;
@ -143,12 +143,13 @@ class PostPushBoxModel extends BaseModel
$iTotalCount = $this->countSendFans($bSendMode, $oPost->uid); //计算发送总数 $iTotalCount = $this->countSendFans($bSendMode, $oPost->uid); //计算发送总数
$aPost = $oPost->toArray(); $aPost = $oPost->toArray();
if(empty($aPost)) return false; if (empty($aPost)) return false;
$aPost['post_params'] = $aPostParams; $aPost['post_params'] = $aPostParams;
//分批发送 //分批发送
$oCollectOffsetLimit = new CollectOffsetLimit(); $oCollectOffsetLimit = new CollectOffsetLimit();
$oCollectOffsetLimit->setITotalCount($iTotalCount)->runWhile(function ($offset, $limit) use ($aPost, $bSendMode) { $oCollectOffsetLimit->setITotalCount($iTotalCount)->runWhile(function ($offset, $limit) use ($aPost, $bSendMode) {
$oFollowList = $this->getFansListWithPage($bSendMode, $aPost['uid'], $offset, $limit); $oFollowList = $this->getFansListWithPage($bSendMode, $aPost['uid'], $offset, $limit);
if(empty($oFollowList)) return;
$this->sendPostToBox($aPost, $oFollowList); $this->sendPostToBox($aPost, $oFollowList);
}); });
@ -201,7 +202,7 @@ class PostPushBoxModel extends BaseModel
* @param $limit * @param $limit
* @return \Illuminate\Database\Eloquent\Collection|array * @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() return $this->newQuery()
@ -218,33 +219,37 @@ class PostPushBoxModel extends BaseModel
//放在用户状态更新时调用 //放在用户状态更新时调用
function pullBigFanMasterPostConsumer($uid) function pullBigFanMasterPostConsumer($uid)
{ {
//@@获取文章和转发文章和评论文章 //获取文章和转发文章
//获取大v定义粉丝数,获取大于该粉丝数的大v文章 //获取大v定义粉丝数,获取大于该粉丝数的大v文章
$iConfigFansPushLimit = intval(env('CONFIG_FANS_PUSH_LIMIT', 2000)); $iConfigFansPushLimit = intval(env('CONFIG_FANS_PUSH_LIMIT', 2000));
//@@此处需针对大量数据进行分批获取
$oFollowModel = new FollowModel(); $oFollowModel = new FollowModel();
$oFollowList = $oFollowModel->getFollowListWithFansLimit($uid,$iConfigFansPushLimit,['a.follow_uid']); $oFollowList = $oFollowModel->getFollowListWithFansLimit($uid, $iConfigFansPushLimit, ['a.follow_uid']);
if ($oFollowList->isEmpty()) return null; if ($oFollowList->isEmpty()) return null;
$aFollowList = $oFollowList->toArray(); $aFollowList = $oFollowList->toArray();
$aFollowUid = array_column($aFollowList,'follow_uid'); $aFollowUid = array_column($aFollowList, 'follow_uid');
$oPostModel = new PostModel(); $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) { foreach ($oPostList as $oPost) {
$aItem['type'] = $oPost->type;
$aItem['uid'] = $uid; $aItem['uid'] = $uid;
$aItem['pid'] = $oPost->id; $aItem['pid'] = $oPost->id;
$aItem['puuid'] = $oPost->uuid; $aItem['puuid'] = $oPost->uuid;
$aItem['created_at'] = $oPost->created_at; $aItem['post_params'] = $oPost->post_params;
$aItem['created_box_at'] = $date; $aItem['post_created_at'] = $oPost->created_at;
$this->addItem($aItem); $this->addItemWithCreateTime($aItem);
} }
} }

View File

@ -5,7 +5,7 @@ namespace App\Tools;
class CollectOffsetLimit class CollectOffsetLimit
{ {
private $offset = 0; private $offset = 0;
private $limit = 2000; private $limit = 1000;
private $page = 0; private $page = 0;
private $iTotalCount = null; private $iTotalCount = null;
@ -34,7 +34,7 @@ class CollectOffsetLimit
{ {
$this->countPage(); $this->countPage();
if($this->iTotalCount === null) $this->iTotalCount = $this->fCountFun; 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++){ for( $iPage = 1; $iPage <= $this->page; $iPage++){
$this->offset = ($iPage - 1) * $this->limit; $this->offset = ($iPage - 1) * $this->limit;
$fun($this->offset,$this->limit); $fun($this->offset,$this->limit);