Files
cycle_api/app/Models/Api/Post/PostHistoryModel.php
2024-03-11 02:21:52 +08:00

59 lines
1.6 KiB
PHP

<?php
namespace App\Models\Api\Post;
use App\Exceptions\ModelException;
use App\Models\Api\Base\ApiBaseModel;
class PostHistoryModel extends ApiBaseModel
{
protected $table = 'customer_post_history';
protected $primaryKey = 'id';
protected $fillable = [
'id',
'method',
'oid',
'uuid',
'mid',
'uid',
'media',
'content',
'created_at',
];
const METHOD_ADD = 1;
const METHOD_DEL = 2;
const METHOD_EDIT = 4;
const METHOD = [
self::METHOD_ADD => '新增',
self::METHOD_DEL => '删除',
self::METHOD_EDIT => '编辑',
];
/**
* @throws ModelException
*/
function addPostHistory($method, $aItem): \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Builder|array|null
{
if(!in_array($method,self::METHOD)) throw new ModelException('addPostHistory method error');
if(!isset($aItem['uuid'])) throw new ModelException('addPostHistory params error');
$aItem['oid'] = $aItem['id'];
unset($aItem['id']);
$sDateTime = date('Y-m-d H:i:s');
$aItem['created_at'] = $sDateTime;
$aItem['method'] = $method;
return $this->addItem($aItem);
}
//获取最新的历史记录(=当前)
function findLastPostId($post_id,$col = ['*']): \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Builder|null
{
return $this->newQuery()
->where('oid',$post_id)
->where('method','!=',self::METHOD_DEL)
->orderBy('created_at','desc')
->first($col);
}
}