Base.php
6.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
class Base extends Model
{
protected $table = '';
//自动维护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',
];
/**
* @name 列表数据
* @return void
* @author :liyuhang
* @method
*/
public function lists($map, $p, $row, $order = 'id', $fields = ['*']){
//TODO::where(['id'=>'','name'=>''])
$lists = $this->select($fields)->wheres($map)->orderBy($order)->paginate($row, ['*'], 'page', $p);
if (empty($lists)) {
return false;
}
$lists = $lists->toArray();
return $lists;
}
/**
* @param $map
* @param $order
* @param $fields
* @name :无分页列表
* @return mixed
* @author :liyuhang
* @method
*/
public function list($map,$order = 'id',$fields = ['*']){
$lists = $this->select($fields)->wheres($map)->orderBy($order)->get();
if (empty($lists)) {
return false;
}
$lists = $lists->toArray();
return $lists;
}
/**
* @param array:$condition
* @name :获取单条数据详情
* @return mixed
* @author :liyuhang
* @method get
*/
public function read($condition,$files = ['*'])
{
$info = $this->select($files)->wheres($condition)->first();
if (empty($info)) {
return false;
}
$info = $info->toArray();
return $info;
}
/**
* @name :新增
* @return void
* @author :liyuhang
* @method post
*/
public function add($data){
$data['created_at'] = date('Y-m-d H:i:s');
$data['updated_at'] = date('Y-m-d H:i:s');
return $this->insert($data);
}
/**
* @name :编辑
* @return void
* @author :liyuhang
* @method post
*/
public function edit($data,$condition){
$data['updated_at'] = date('Y-m-d H:i:s');
return $this->wheres($condition)->update($data);
}
/**
* @name : 删除数据
* @return void
* @author :liyuhang
* @method
*/
public function del($condition){
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;
}
}