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