推拉函数与queue
This commit is contained in:
@ -1,9 +1,11 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models\Post;
|
||||
|
||||
use App\Exceptions\ModelException;
|
||||
use App\Models\Base\BaseModel;
|
||||
use App\Tools\Tools;
|
||||
use Illuminate\Database\Eloquent\Casts\Attribute;
|
||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
|
||||
class PostModel extends BaseModel
|
||||
@ -15,7 +17,9 @@ class PostModel extends BaseModel
|
||||
protected $primaryKey = 'id';
|
||||
protected $fillable = [
|
||||
'id',
|
||||
'type',
|
||||
'uuid',
|
||||
'post_batch_sn',
|
||||
'mid',
|
||||
'uid',
|
||||
'media',
|
||||
@ -24,25 +28,65 @@ class PostModel extends BaseModel
|
||||
'deleted_at',
|
||||
];
|
||||
|
||||
const TYPE_POST = PostPushBoxModel::TYPE_POST;
|
||||
const TYPE_REPOST = PostPushBoxModel::TYPE_REPOST;
|
||||
|
||||
const TYPE = [
|
||||
self::TYPE_POST => '推文',
|
||||
self::TYPE_REPOST => '转发',
|
||||
];
|
||||
|
||||
protected function media(): Attribute
|
||||
{
|
||||
return Attribute::make(
|
||||
get: fn(string $value = '') => Tools::JonsDecode($value),
|
||||
set: fn(array $value = []) => Tools::JonsEncode($value),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws ModelException
|
||||
*/
|
||||
function addPost($uid, $content = null, $media = 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(!$content && !$media) throw new ModelException('addPost params error');
|
||||
if (!in_array($type, [self::TYPE_POST, self::TYPE_REPOST])) throw new ModelException('type params error');
|
||||
$uuid = Tools::genUuid();
|
||||
$bsn = 'BSN_' . ($sBachSn ?? Tools::genUuid());
|
||||
if (!$content && !$media) throw new ModelException('addPost params error');
|
||||
$aItem['type'] = $type;
|
||||
$aItem['uid'] = $uid;
|
||||
$aItem['uuid'] = Tools::genUuid();
|
||||
$aItem['uuid'] = $uuid;
|
||||
$aItem['post_batch_sn'] = $bsn;
|
||||
$aItem['mid'] = $mid;
|
||||
$aItem['media'] = $media;
|
||||
$aItem['content'] = $content;
|
||||
$sDateTime = date('Y-m-d H:i:s');
|
||||
$aItem['created_at'] = $sDateTime;
|
||||
$res = $this->addItem($aItem);
|
||||
if($res){
|
||||
|
||||
if ($res) {
|
||||
$this->pushToQueue(self::TYPE_POST, $res->id);
|
||||
}
|
||||
return $res;
|
||||
}
|
||||
|
||||
function addBatchPost(array $aPostList): void
|
||||
{
|
||||
$mid = null;
|
||||
$bsn = 'BSN_' . ($sBachSn ?? Tools::genUuid());
|
||||
foreach ($aPostList as $aPostItem) {
|
||||
$oPost = $this->addPost($aPostItem['uid'], $aPostItem['type'], $aPostItem['content'], $aPostItem['media'], $mid, $bsn);
|
||||
if ($oPost) {
|
||||
$mid = $oPost->id;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//发送到消息队列处理新增post
|
||||
function pushToQueue($type, $id): void
|
||||
{
|
||||
PostPushBoxModel::addPostQueueProducer(['type' => $type, 'id' => $id]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws ModelException
|
||||
*/
|
||||
@ -50,9 +94,9 @@ class PostModel extends BaseModel
|
||||
{
|
||||
$oPost = $this->findItem($id);
|
||||
$res = $this->delItem($id);
|
||||
if($res){
|
||||
if ($res) {
|
||||
$oPostHistoryModel = new PostHistoryModel();
|
||||
$oPostHistoryModel->addPostHistory(PostHistoryModel::METHOD_DEL,$oPost->toArray());
|
||||
$oPostHistoryModel->addPostHistory(PostHistoryModel::METHOD_DEL, $oPost->toArray());
|
||||
}
|
||||
return $res;
|
||||
}
|
||||
@ -62,18 +106,18 @@ class PostModel extends BaseModel
|
||||
*/
|
||||
function delPostByUuid($uuid)
|
||||
{
|
||||
$oPost = $this->findItemByWhere(['uuid'=>$uuid]);
|
||||
$oPost = $this->findItemByWhere(['uuid' => $uuid]);
|
||||
$res = $this->newQuery()->where('uuid', $uuid)->delete();
|
||||
if($res){
|
||||
if ($res) {
|
||||
$oPostHistoryModel = new PostHistoryModel();
|
||||
$oPostHistoryModel->addPostHistory(PostHistoryModel::METHOD_DEL,$oPost->toArray());
|
||||
$oPostHistoryModel->addPostHistory(PostHistoryModel::METHOD_DEL, $oPost->toArray());
|
||||
}
|
||||
return $res;
|
||||
}
|
||||
|
||||
function getPostListByUid($uid): \Illuminate\Database\Eloquent\Collection|array
|
||||
{
|
||||
return $this->getItemsByWhere(['uid'=>$uid]);
|
||||
return $this->getItemsByWhere(['uid' => $uid]);
|
||||
}
|
||||
|
||||
function getPostById($id): \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Collection|\Illuminate\Database\Eloquent\Builder|array|null
|
||||
@ -86,13 +130,13 @@ class PostModel extends BaseModel
|
||||
*/
|
||||
function updatePostById($aItem): bool|int
|
||||
{
|
||||
if(!isset($aItem['id'])) throw new ModelException('updatePostById params error');
|
||||
if(empty($aItem['id'])) throw new ModelException('updatePostById params error');
|
||||
if (!isset($aItem['id'])) throw new ModelException('updatePostById params error');
|
||||
if (empty($aItem['id'])) throw new ModelException('updatePostById params error');
|
||||
$oPost = $this->findItem($aItem['id']);
|
||||
$res = $this->updateItem($aItem);
|
||||
if($res){
|
||||
if ($res) {
|
||||
$oPostHistoryModel = new PostHistoryModel();
|
||||
$oPostHistoryModel->addPostHistory(PostHistoryModel::METHOD_EDIT,$oPost->toArray());
|
||||
$oPostHistoryModel->addPostHistory(PostHistoryModel::METHOD_EDIT, $oPost->toArray());
|
||||
}
|
||||
return $res;
|
||||
}
|
||||
@ -102,31 +146,32 @@ class PostModel extends BaseModel
|
||||
*/
|
||||
function updatePostByUuid($aItem): bool|int
|
||||
{
|
||||
if(!isset($aItem['uuid'])) throw new ModelException('updatePostByUuid params error');
|
||||
if(empty($aItem['uuid'])) throw new ModelException('updatePostByUuid params error');
|
||||
$oPost = $this->findItemByWhere(['uuid'=>$aItem['uuid']]);
|
||||
$res = $this->updateItem($aItem,'uuid');
|
||||
if($res){
|
||||
if (!isset($aItem['uuid'])) throw new ModelException('updatePostByUuid params error');
|
||||
if (empty($aItem['uuid'])) throw new ModelException('updatePostByUuid params error');
|
||||
$oPost = $this->findItemByWhere(['uuid' => $aItem['uuid']]);
|
||||
$res = $this->updateItem($aItem, 'uuid');
|
||||
if ($res) {
|
||||
$oPostHistoryModel = new PostHistoryModel();
|
||||
$oPostHistoryModel->addPostHistory(PostHistoryModel::METHOD_EDIT,$oPost->toArray());
|
||||
$oPostHistoryModel->addPostHistory(PostHistoryModel::METHOD_EDIT, $oPost->toArray());
|
||||
}
|
||||
return $res;
|
||||
}
|
||||
|
||||
function getPostListByMid($mid): \Illuminate\Database\Eloquent\Collection|array
|
||||
{
|
||||
return $this->getItemsByWhere(['mid'=>$mid]);
|
||||
return $this->getItemsByWhere(['mid' => $mid]);
|
||||
}
|
||||
|
||||
|
||||
function getPostListByUidMid($uid,$mid): \Illuminate\Database\Eloquent\Collection|array
|
||||
function getPostListByUidMid($uid, $mid): \Illuminate\Database\Eloquent\Collection|array
|
||||
{
|
||||
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): \Illuminate\Database\Eloquent\Collection|array
|
||||
{
|
||||
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();
|
||||
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();
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user