|
...
|
...
|
@@ -8,28 +8,15 @@ use Illuminate\Support\Facades\DB; |
|
|
|
class Base extends Model
|
|
|
|
{
|
|
|
|
protected $table = '';
|
|
|
|
public $allCount = 0;
|
|
|
|
//自动维护create_at创建时间 updated_at修改时间
|
|
|
|
public $timestamps = true;
|
|
|
|
//统一设置
|
|
|
|
//统一设置返回时间格式
|
|
|
|
protected $casts = [
|
|
|
|
'created_at' => 'datetime:Y-m-d H:i:s',
|
|
|
|
'updated_at' => 'datetime:Y-m-d H:i:s',
|
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 日期序列化
|
|
|
|
* @param \DateTimeInterface $date
|
|
|
|
* @return string
|
|
|
|
* @author zbj
|
|
|
|
* @date 2023/4/13
|
|
|
|
*/
|
|
|
|
protected function serializeDate(\DateTimeInterface $date): string
|
|
|
|
{
|
|
|
|
return $date->format('Y-m-d H:i:s');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @name 列表数据
|
|
|
|
* @return void
|
|
|
|
* @author :liyuhang
|
|
...
|
...
|
@@ -37,12 +24,11 @@ class Base extends Model |
|
|
|
*/
|
|
|
|
public function lists($map, $p, $row, $order = 'id', $fields = ['*']){
|
|
|
|
//TODO::where(['id'=>'','name'=>''])
|
|
|
|
$lists = DB::table($this->table)->select($fields)->where($map)->orderBy($order)->paginate($row, ['*'], 'page', $p);
|
|
|
|
$lists = $this->select($fields)->wheres($map)->orderBy($order)->paginate($row, ['*'], 'page', $p);
|
|
|
|
if (empty($lists)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$lists = $lists->toArray();
|
|
|
|
$this->allCount = $this->where($map)->count();
|
|
|
|
return $lists;
|
|
|
|
}
|
|
|
|
|
|
...
|
...
|
@@ -57,7 +43,7 @@ class Base extends Model |
|
|
|
* @method
|
|
|
|
*/
|
|
|
|
public function list($map,$order = 'id',$fields = ['*']){
|
|
|
|
$lists = $this->select($fields)->where($map)->orderBy($order)->get();
|
|
|
|
$lists = $this->select($fields)->wheres($map)->orderBy($order)->get();
|
|
|
|
if (empty($lists)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
...
|
...
|
@@ -73,7 +59,7 @@ class Base extends Model |
|
|
|
*/
|
|
|
|
public function read($condition,$files = ['*'])
|
|
|
|
{
|
|
|
|
$info = $this->select($files)->where($condition)->first();
|
|
|
|
$info = $this->select($files)->wheres($condition)->first();
|
|
|
|
if (empty($info)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
...
|
...
|
@@ -102,7 +88,7 @@ class Base extends Model |
|
|
|
*/
|
|
|
|
public function edit($data,$condition){
|
|
|
|
$data['updated_at'] = date('Y-m-d H:i:s');
|
|
|
|
return $this->where($condition)->update($data);
|
|
|
|
return $this->wheres($condition)->update($data);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
...
|
...
|
@@ -112,6 +98,102 @@ class Base extends Model |
|
|
|
* @method
|
|
|
|
*/
|
|
|
|
public function del($condition){
|
|
|
|
return $this->where($condition)->delete();
|
|
|
|
return $this->wheres($condition)->delete();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function wheres($map,$val = ''){
|
|
|
|
$query = $this;
|
|
|
|
$query->where(function ($query) use ($map,$val){
|
|
|
|
//拼接数据
|
|
|
|
if(is_array($map)){
|
|
|
|
foreach ($map as $v){
|
|
|
|
switch ($v){
|
|
|
|
case 'like':
|
|
|
|
// like查询 ['name|title', 'like', '%a%']
|
|
|
|
if (strpos($v[0], '|') !== false) {
|
|
|
|
$query->where(function ($query) use ($v) {
|
|
|
|
$item = explode('|', $v[0]);
|
|
|
|
foreach ($item as $vo) {
|
|
|
|
$query->orWhere($vo, $v[1], $v[2]);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
$query->where($v[0], $v[1], $v[2]);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'in':
|
|
|
|
// in查询 ['id', 'in', [1,2,3]]
|
|
|
|
if (!is_array($v[2])) {
|
|
|
|
$v[2] = explode(',', $v[2]);
|
|
|
|
}
|
|
|
|
$query->whereIn($v[0], $v[2]);
|
|
|
|
break;
|
|
|
|
case 'not in':
|
|
|
|
// not in查询 ['id', 'not in', [1,2,3]]
|
|
|
|
if (!is_array($v[2])) {
|
|
|
|
$v[2] = explode(',', $v[2]);
|
|
|
|
}
|
|
|
|
$query->whereNotIn($v[0], $v[2]);
|
|
|
|
break;
|
|
|
|
case 'between':
|
|
|
|
// between查询 ['created_at', 'between', ['xxx', 'xxx]]
|
|
|
|
if (!is_array($v[2])) {
|
|
|
|
$v[2] = explode(',', $v[2]);
|
|
|
|
}
|
|
|
|
$query->whereBetween($v[0], $v[2]);
|
|
|
|
break;
|
|
|
|
case 'not between':
|
|
|
|
// not between查询 ['created_at', 'not between', ['xxx', 'xxx]]
|
|
|
|
if (!is_array($v[2])) {
|
|
|
|
$v[2] = explode(',', $v[2]);
|
|
|
|
}
|
|
|
|
$query->whereNotBetween($v[0], $v[2]);
|
|
|
|
break;
|
|
|
|
case 'null':
|
|
|
|
// null查询 ['deleted_at', 'null']
|
|
|
|
$query->whereNull($v[0]);
|
|
|
|
break;
|
|
|
|
case "not null":
|
|
|
|
// not null查询 ['deleted_at', 'not null']
|
|
|
|
$query->whereNotNull($v[0]);
|
|
|
|
break;
|
|
|
|
case "or":
|
|
|
|
// or查询 [[['status'=>1],['status'=>2]], 'or'];
|
|
|
|
//格式:or (status=1 and status=2)
|
|
|
|
$where = $v[0];
|
|
|
|
$query->orWhere(function ($query) use ($where) {
|
|
|
|
// 递归解析查询条件
|
|
|
|
$this->formatQuery($where, $query);
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
case 'xor':
|
|
|
|
// xor查询 [[['status'=>1],['status'=>2]], 'xor'];
|
|
|
|
// 格式:and (status=1 or status=2)
|
|
|
|
$where = $v[0];
|
|
|
|
$query->where(function ($query) use ($where) {
|
|
|
|
foreach ($where as $w) {
|
|
|
|
$query->orWhere(function ($query) use ($w) {
|
|
|
|
// 递归解析查询条件
|
|
|
|
$this->formatQuery([$w], $query);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// 常规查询
|
|
|
|
if (count($v) == 2) {
|
|
|
|
$query->where($v[0], '=', $v[1]);
|
|
|
|
} else {
|
|
|
|
$query->where($v[0], $v[1], $v[2]);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}else{
|
|
|
|
$query->where($map,$val);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return $query;
|
|
|
|
}
|
|
|
|
} |
...
|
...
|
|