|
...
|
...
|
@@ -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,12 @@ 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);
|
|
|
|
$query = $this->formatQuery($map);
|
|
|
|
$lists = $query->select($fields)->orderBy($order)->paginate($row, ['*'], 'page', $p);
|
|
|
|
if (empty($lists)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$lists = $lists->toArray();
|
|
|
|
$this->allCount = $this->where($map)->count();
|
|
|
|
return $lists;
|
|
|
|
}
|
|
|
|
|
|
...
|
...
|
@@ -57,7 +44,8 @@ class Base extends Model |
|
|
|
* @method
|
|
|
|
*/
|
|
|
|
public function list($map,$order = 'id',$fields = ['*']){
|
|
|
|
$lists = $this->select($fields)->where($map)->orderBy($order)->get();
|
|
|
|
$query = $this->formatQuery($map);
|
|
|
|
$lists = $query->select($fields)->orderBy($order)->get();
|
|
|
|
if (empty($lists)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
...
|
...
|
@@ -73,7 +61,8 @@ class Base extends Model |
|
|
|
*/
|
|
|
|
public function read($condition,$files = ['*'])
|
|
|
|
{
|
|
|
|
$info = $this->select($files)->where($condition)->first();
|
|
|
|
$query = $this->formatQuery($condition);
|
|
|
|
$info = $query->select($files)->first();
|
|
|
|
if (empty($info)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
...
|
...
|
@@ -101,8 +90,10 @@ class Base extends Model |
|
|
|
* @method post
|
|
|
|
*/
|
|
|
|
public function edit($data,$condition){
|
|
|
|
$query = $this->formatQuery($condition);
|
|
|
|
$data['updated_at'] = date('Y-m-d H:i:s');
|
|
|
|
return $this->where($condition)->update($data);
|
|
|
|
$rs = $query->update($data);
|
|
|
|
return $rs;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
...
|
...
|
@@ -112,6 +103,65 @@ class Base extends Model |
|
|
|
* @method
|
|
|
|
*/
|
|
|
|
public function del($condition){
|
|
|
|
return $this->where($condition)->delete();
|
|
|
|
$query = $this->formatQuery($condition);
|
|
|
|
return $query->delete();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $map = ['$k'=>['like',$v],$k1]
|
|
|
|
* @param $val
|
|
|
|
* @name :参数处理查询
|
|
|
|
* @return Base
|
|
|
|
* @author :liyuhang
|
|
|
|
* @method
|
|
|
|
*/
|
|
|
|
public function formatQuery($map = [],$query = ''){
|
|
|
|
$query = $this;
|
|
|
|
$query->where(function ($query) use ($map){
|
|
|
|
foreach ($map as $k => $v){
|
|
|
|
if(is_array($v)){
|
|
|
|
//拼接数据
|
|
|
|
foreach ($v as $k1 => $v1){
|
|
|
|
switch ($k1){
|
|
|
|
case 'like':
|
|
|
|
// like查询 ['name|title'=> ['like','%a%']]
|
|
|
|
if (strpos($k, '|') !== false) {
|
|
|
|
$query->where(function ($query) use ($k,$v1) {
|
|
|
|
$item = explode('|', $k);
|
|
|
|
foreach ($item as $vo) {
|
|
|
|
$query->orWhere($vo, $v1[0], $v1[1]);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
$query->where($k,$v1[0], $v1[1]);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'in':
|
|
|
|
// in查询 ['id'=>['in'=>[1,2,3]]]
|
|
|
|
$query->whereIn($k, $v1[1]);
|
|
|
|
break;
|
|
|
|
case 'no in':
|
|
|
|
// in查询 ['id'=>['not in'=>[1,2,3]]]
|
|
|
|
$query->whereNotIn($k, $v1[1]);
|
|
|
|
break;
|
|
|
|
case 'between':
|
|
|
|
// in查询 ['id'=>['between'=>[create1,create2]]]
|
|
|
|
$query->whereBetween($k, $v1[1]);
|
|
|
|
case 'not between':
|
|
|
|
// not between查询 ['created_at'=>['not between'=>['xxx', 'xxx]]]
|
|
|
|
$query->whereNotBetween($k, $v1[1]);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
$query->where($k,$k1,$v1[1]);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}else{
|
|
|
|
$query->where($k,$v);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return $query;
|
|
|
|
}
|
|
|
|
} |
...
|
...
|
|