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\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();
}

View File

@ -149,6 +149,7 @@ class PostPushBoxModel extends BaseModel
$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);
});
@ -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']);
if ($oFollowList->isEmpty()) return null;
$aFollowList = $oFollowList->toArray();
$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);
}
}

View File

@ -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);