Files
cycle_api/app/Models/Post/PostPushBoxModel.php
2024-03-02 16:53:43 +08:00

182 lines
6.3 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace App\Models\Post;
use App\Exceptions\ModelException;
use App\Models\Base\BaseModel;
use App\Models\Customer\CustomerUserExtendModel;
use App\Models\Follow\FollowModel;
use App\Tools\CollectOffsetLimit;
class PostPushBoxModel extends BaseModel
{
protected $table = 'customer_post_push_box';
protected $primaryKey = 'id';
protected $fillable = [
'id',
'uid',
'pid',
'puuid',
'is_like',
'is_repost',
'is_bookmark',
'is_read',
'created_at',
'created_box_at',
'deleted_at',
];
const IS_LIKE_DEFAULT = 1;
const IS_LIKE_YES = 2;
const IS_LIKE_NO = 3;
const IS_LIKE = [
self::IS_LIKE_DEFAULT => '默认',
self::IS_LIKE_YES => '喜欢',
self::IS_LIKE_NO => '不喜欢',
];
const IS_REPOST_DEFAULT = 1;
const IS_REPOST_YES = 2;
const IS_REPOST = [
self::IS_REPOST_DEFAULT => '默认',
self::IS_REPOST_YES => '已转发',
];
const IS_BOOKMARK_DEFAULT = 1;
const IS_BOOKMARK_YES = 2;
const IS_BOOKMARK = [
self::IS_BOOKMARK_DEFAULT => '默认',
self::IS_BOOKMARK_YES => '已收藏',
];
const IS_READ_DEFAULT = 1;
const IS_READ_YES = 2;
const IS_READ = [
self::IS_READ_DEFAULT => '默认',
self::IS_READ_YES => '已收藏',
];
/**
* 提交后调用事件
* @throws ModelException
*/
function newPostPushTask($id = null, $uuid = null)
{
$oPostModel = new PostModel();
$oPost = null;
if ($id) $oPost = $oPostModel->findItem($id);
if ($uuid) $oPost = $oPostModel->findItemByWhere(['uuid' => $uuid]);
if (!$oPost) throw new ModelException('post not found');
$iConfigFansPushLimit = intval(env('CONFIG_FANS_PUSH_LIMIT', 2000));
//查询粉丝数
$CustomerUserExtendModel = new CustomerUserExtendModel();
$CustomerUserExtend = $CustomerUserExtendModel->findItem($oPost->uid);
if (!$CustomerUserExtend) throw new ModelException('user extend not found');
//粉丝数少于$iConfigFansPushLimit的用户走写扩散流程所有粉丝信箱插入一条
$bSendMode = $CustomerUserExtend->fans_num < $iConfigFansPushLimit;
$iTotalCount = $this->countSendFans($bSendMode, $oPost->uid); //计算发送总数
//分批发送
$oCollectOffsetLimit = new CollectOffsetLimit();
$oCollectOffsetLimit->setITotalCount($iTotalCount)->runWhile(function ($offset, $limit) use ($oPost, $bSendMode) {
$oFollowList = $this->getFansListWithPage($bSendMode, $oPost->uid, $offset, $limit);
$this->sendPostToBox($oPost, $oFollowList);
});
}
function getFansListWithPage($bAllFans, $uid, $offset = 0, $limit = 2000): \Illuminate\Database\Eloquent\Collection|array|\Illuminate\Support\Collection
{
$oFollowModel = new FollowModel();
//粉丝数少于$iConfigFansPushLimit的用户走写扩散流程所有粉丝信箱插入一条
if ($bAllFans) {
$oFollowList = $oFollowModel->getFansList($uid, ['uid'], $offset, $limit); //获取所有粉丝列表
} else { //读扩散流程(只针对活跃粉丝信箱插入一条)
//获取活跃粉丝列表
//@活跃粉丝数量过大需要分批处理
$oFollowList = $oFollowModel->getActiveFansUidList($uid, ['a.uid'], $offset, $limit);
}
return $oFollowList;
}
function countSendFans($bAllFans, $uid): int
{
$oFollowModel = new FollowModel();
if ($bAllFans) {
$iCount = $oFollowModel->getFansCount($uid); //获取所有粉丝列表
} else { //读扩散流程(只针对活跃粉丝信箱插入一条)
//获取活跃粉丝列表
$iCount = $oFollowModel->getActiveFansUidListCount($uid);
}
return $iCount;
}
//发送到推送信箱
function sendPostToBox($oPost, $oFollowList): void
{
if (!$oFollowList) return;
$date = date('Y-m-d H:i:s');
foreach ($oFollowList as $oFollow) {
$aItem['uid'] = $oFollow->uid;
$aItem['pid'] = $oPost->id;
$aItem['puuid'] = $oPost->uuid;
$aItem['created_at'] = $oPost->created_at;
$aItem['created_box_at'] = $date;
$this->addItem($aItem);
}
}
/**
* 拉取推送信箱列表
* @param $uid
* @param $last_id //上次最后一条id
* @param $limit
* @return \Illuminate\Database\Eloquent\Collection|array
*/
function getPushBoxList($uid, $last_id = 0, $limit = 20): \Illuminate\Database\Eloquent\Collection|array
{
//活跃用户直接拉取未读消息
return $this->newQuery()->where('uid', $uid)->where('id', '>', $last_id)->where('is_read', self::IS_READ_DEFAULT)->orderBy('created_at', 'desc')->limit($limit)->get();
//非活跃用户拉取大v消息在用户状态更新时已经调用过此处不用在做处理
}
//非活跃拉取已跟随大v最新文章。
//放在用户状态更新时调用
function pullBigFanMasterPost($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);
//将推文发送到信箱
$this->sendPostToBoxByUid($oPostList,$uid);
}
function sendPostToBoxByUid($oPostList, $uid): void
{
$date = date('Y-m-d H:i:s');
foreach ($oPostList as $oPost) {
$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);
}
}
}