cmbYouku_Api/app/model/BaseModel.php

145 lines
3.9 KiB
PHP
Raw Permalink Normal View History

2024-07-01 15:57:07 +08:00
<?php
namespace app\model;
use app\exception\BusinessException;
use app\util\SessionUtil;
use think\facade\Request;
use think\Model;
use think\model\concern\SoftDelete;
class BaseModel extends Model
{
const STATE_ENABLED = 1; //业务状态 -正常
const STATE_DISABLED = 2;//业务状态 -禁用
const STATE_DESC = [
self::STATE_ENABLED => '启用',
self::STATE_DISABLED => '禁用',
];
public static function findById($id): object
{
return self::findOrEmpty($id);
}
public static function getInfoAndCheck(array $where)
{
$info = self::getInfoByWhere($where);
if (empty($info)) {
throw new BusinessException("无权限操作此数据.");
}
return $info;
}
public static function onAfterRead(Model $model): void
{
$model->hidden(['delete_time']);
if (!empty($model->status)) {
$model->statusDesc = self::STATE_DESC[$model->status];
}
if (!empty($model->state)) {
$model->statusDesc = self::STATE_DESC[$model->state];
}
}
public static function getListPage($where, $page, $order = ''): array
{
$model = self::where($where);
if ($order) {
$model->order($order);
}
return $model->paginate($page)->toArray();
}
public static function insertOne($data)
{
$data['create_time'] = date("Y-m-d H:i:s");
$data['update_time'] = date("Y-m-d H:i:s");
return self::create($data)->getKey();
}
public static function updateOne($where, $data)
{
$data['update_time'] = date("Y-m-d H:i:s");
self::update($data, $where);
}
public static function getOne($id): array
{
return self::findOrEmpty($id)->toArray();
}
public static function getInfoByWhere($where, $order = '', $field = '*'): array
{
$model = self::where($where);
if ($order) {
$model->order($order);
}
return $model->field($field)->findOrEmpty()->toArray();
}
public static function getList($where, $field = '*', $order = ''): array
{
$model = self::where($where)->field($field);
if ($order) {
$model->order($order);
}
return $model->select()->toArray();
}
public static function getCounts($where, $field = 'create_time', $times = [])
{
$model = self::where($where);
if (!empty($times)) {
$model->whereBetweenTime($field, $times[0], $times[1]);
}
return $model->count('id');
}
public static function getPageInfo(): array
{
return [
'list_rows' => Request::param("pageSize", 10),
'var_page' => 'page',
'page' => Request::param("page", 1),
];
}
public static function orderBy()
{
return Request::param("orderBy", "id desc");
}
public function searchPages($search = [], $data = [], $field = '*'): \think\Paginator
{
$model = self::where(self::appendBaseWhere([]));
if (!empty($search)) {
$model = $model->withSearch($search, $data);
}
return $model->order(self::orderBy())->field($field)->paginate(self::getPageInfo());
}
public function detailById($id, $field = ['*']): array
{
return self::field($field)->findOrEmpty($id)->toArray();
}
public function deleteById($id)
{
self::destroy($id);
}
public function getLists($where = [], $field = [], $order = [])
{
return (new static)->where($where)->field($field)->order($order)->select();
}
private static function appendBaseWhere($where = [])
{
if (!isset($where['site_id']) && !empty(self::getModel()->autoSiteId) && self::getModel()->autoSiteId) {
$where['site_id'] = SessionUtil::getSiteId();
}
return $where;
}
}