Files
cycle_api/app/Models/Post/PostModel.php
2024-02-26 00:41:25 +08:00

134 lines
4.0 KiB
PHP

<?php
namespace App\Models\Post;
use App\Exceptions\ModelException;
use App\Models\Base\BaseModel;
use App\Tools\Tools;
use Illuminate\Database\Eloquent\SoftDeletes;
class PostModel extends BaseModel
{
//软删除
use SoftDeletes;
protected $table = 'customer_post';
protected $primaryKey = 'id';
protected $fillable = [
'id',
'uuid',
'mid',
'uid',
'media',
'content',
'created_at',
'deleted_at',
];
/**
* @throws ModelException
*/
function addPost($uid, $content = null, $media = null): \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Builder|array|null
{
if(!$content && !$media) throw new ModelException('addPost params error');
$aItem['uid'] = $uid;
$aItem['uuid'] = Tools::genUuid();
$aItem['media'] = $media;
$aItem['content'] = $content;
$sDateTime = date('Y-m-d H:i:s');
$aItem['created_at'] = $sDateTime;
$res = $this->addItem($aItem);
if($res){
}
return $res;
}
/**
* @throws ModelException
*/
function delPostById($id)
{
$oPost = $this->findItem($id);
$res = $this->delItem($id);
if($res){
$oPostHistoryModel = new PostHistoryModel();
$oPostHistoryModel->addPostHistory(PostHistoryModel::METHOD_DEL,$oPost->toArray());
}
return $res;
}
/**
* @throws ModelException
*/
function delPostByUuid($uuid)
{
$oPost = $this->findItemByWhere(['uuid'=>$uuid]);
$res = $this->newQuery()->where('uuid', $uuid)->delete();
if($res){
$oPostHistoryModel = new PostHistoryModel();
$oPostHistoryModel->addPostHistory(PostHistoryModel::METHOD_DEL,$oPost->toArray());
}
return $res;
}
function getPostListByUid($uid): \Illuminate\Database\Eloquent\Collection|array
{
return $this->getItemsByWhere(['uid'=>$uid]);
}
function getPostById($id): \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Collection|\Illuminate\Database\Eloquent\Builder|array|null
{
return $this->findItem($id);
}
/**
* @throws ModelException
*/
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');
$oPost = $this->findItem($aItem['id']);
$res = $this->updateItem($aItem);
if($res){
$oPostHistoryModel = new PostHistoryModel();
$oPostHistoryModel->addPostHistory(PostHistoryModel::METHOD_EDIT,$oPost->toArray());
}
return $res;
}
/**
* @throws ModelException
*/
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){
$oPostHistoryModel = new PostHistoryModel();
$oPostHistoryModel->addPostHistory(PostHistoryModel::METHOD_EDIT,$oPost->toArray());
}
return $res;
}
function getPostListByMid($mid): \Illuminate\Database\Eloquent\Collection|array
{
return $this->getItemsByWhere(['mid'=>$mid]);
}
function getPostListByUidMid($uid,$mid): \Illuminate\Database\Eloquent\Collection|array
{
return $this->getItemsByWhere(['uid'=>$uid,'mid'=>$mid]);
}
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();
}
}