Base.php
10.9 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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
<?php
namespace app\api\model;
use app\api\library\Exception\DataMissException;
use think\Exception;
use think\Model;
class Base extends Model {
/**
* 是否开启自动写入功能
* @var bool
*/
protected $autoWriteTimestamp = 'int';
protected $createTime = 'createtime';
protected $updateTime = 'updatetime';
/**
* 构造函数
* @date 2020-07-16
* @param $data
*/
public function __construct($data = []) {
parent::__construct($data);
}
/**
* 构造关联用户查询
*/
public function userInfo(){
return $this->hasOne("User","id","user_id")->field("id,username");
}
/**
* 构造关联用户查询
*/
public function userScoreLog(){
return $this->hasMany("UserScoreLog","user_id","user_id")->field("user_id,score");
}
/**
* 构造关联社区文章查询
*/
public function social(){
return $this->hasMany("Social","sort_id","id");
}
/**
* 构造关联社区文章查询
*/
public function store(){
return $this->hasOne("Store","id","store_id")->field("id,store_name,store_icon,industry_id,images");
}
/**
* 单条查询
* @param array $where 查询条件
* @param string $field 查询field
* @param string $order 查询排序
* @param boolean $isFail 是否直接返回错误
* @return array|false|mixed|\PDOStatement|string|Model
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
public function findOrFail($where = [],$isFail = true ,$field = '*',$order = "") {
if (empty($order)) {
if ($this->getPk() != null) {
$order = $this->getPk() . ' desc';
} else {
$order = 'id desc';
}
}
$array = explode(',', $field);
$sql = $this->connect_where($where);
if($isFail){//如果直接返回错误
$sql->failException(true);
}
try{
if(isset($where['hasWhere'])){
$field = $this->changeField($field);
$order = $this->changeOrder($order);
}
if ($array[0] != '*' && count($array) <= 1) {
return $sql->value($field);
} else {
if($isFail){
return $sql->field($field)->order($order)->find()->toArray();
}else{
$data = $sql->field($field)->order($order)->find();
if($data){
$data = $data->toArray();
}
return $data;
}
}
}catch (Exception $exception){
throw new DataMissException();
}
}
/**
* 获取列表信息(无分页)
* @param array $where 查询条件
* @param string $field 查询字段
* @param string $order 排序方式\
* @param boolean $isFail 是否直接返回错误
* @return array|false|\PDOStatement|string|\think\Collection
*/
public function selectOrFail($where, $isFail = true,$field = '*', $order ='') {
if (empty($order)) {
if ($this->getPk() != null) {
$order = $this->getPk() . ' desc';
} else {
$order = 'id desc';
}
}
try{
$sql = $this->connect_where($where);
if($isFail){
$sql->failException(true);
}
if(isset($where['hasWhere'])){
$field = $this->changeField($field);
$order = $this->changeOrder($order);
}
return collection($sql->order($order)->field($field)->select())->toArray();
}catch (Exception $exception){
throw new DataMissException();
}
}
/**
* 获取列表信息 (分页)
* @param $page
* @param $where array 查询条件
* @param string $field string 查询字段
* @param string $order string 查询排序
* @param string $per_page_number int 每页显示数量
*/
public function pageSelect($page,$where,$field = '*',$order='',$per_page_number = 10){
if (empty($order)) {
if ($this->getPk() != null) {
$order = $this->getPk() . ' desc';
} else {
$order = 'id desc';
}
}
try{
$sql = $this->connect_where($where);
if(isset($where['hasWhere'])){
$field = $this->changeField($field);
$order = $this->changeOrder($order);
}
return $sql->field($field)->order($order)->paginate($per_page_number, false, [
'page' => $page,
]);
}catch (Exception $exception){
throw new DataMissException();
}
}
/**
* 修改/新增数据
* @param $data
* @return bool|mixed
*/
public function dataUpdate($data) {
if (empty($data)) {
return false;
} else {
if (array_key_exists($this->getPk(), $data) && !empty($data[$this->getPk()])) { // 数据修改
$result = $this->allowField(true)->edit($data);
} else { // 数据插入
$result = $this->allowField(true)->add($data);
}
return $result;
}
}
/**
* 新增数据
* @param $data
* @return bool|mixed
*/
public function add(array $data = []) {
if (!empty($data)) {
$res = $this->save($data);
if ($res > 0) {
return $res;
} else {
return false;
}
} else {
return false;
}
}
/**
* 修改数据
* @param $data
* @return bool|mixed
*/
public function edit(array $data = []) {
if (!empty($data)) {
$res = $this->save($data,[$this->getPk()=>$data[$this->getPk()]]);
if ($res) {
return $data[$this->getPk()];
} else {
return false;
}
} else {
return false;
}
}
/**
* 获取无限级数据 2020-07-16
* @param $where 条件
* @param string $order 排序
* @param int $limit_level 查询几级结束
* @param string $relation_id 关联上下级ID字段名
* @param string $keyword 返回数组中当期级别的名称
* @return array
*/
public function getInfinite($where, $order = '', $limit_level = 0, $relation_id = 'pid', $keyword = 'level') {
return $this->getInfiniteData($where, $order, $limit_level, $relation_id, $keyword, 0, []);
}
/**
* 无限级数据查询方法 2020-07-16
* @param $where
* @param string $order
* @param int $limit_level
* @param string $relation_id
* @param string $keyword
* @param int $now_level
* @param array $return_data
* @return array
*/
public function getInfiniteData($where, $order = '', $limit_level = 0, $relation_id = 'pid', $keyword = 'level', $now_level = 0, $return_data = []) {
if (!empty($limit_level)) {
if ($limit_level <= $now_level) {
return $return_data;
}
}
if ($now_level == 0) {
$where[$relation_id] = ['elt', 0];
}
if (empty($order)) {
$order = $this->getPk() . ' desc';
}
$array = $this->where($where)->order($order)->select();
if (empty($array)) {
return $return_data;
}
foreach ($array as $key => $value) {
$where[$relation_id] = $value[$this->getPk()];
$value[$keyword] = $now_level;
$return_data[] = $value;
$return_data = $this->getInfiniteData($where, $order, $limit_level, $relation_id, $keyword, $now_level + 1, $return_data);
}
return $return_data;
}
/**
* 查询符合条件的个数
* @param $where array 查询条件
* @return int|string
*/
public function getCount($where) {
return $this->where($where)->count($this->getPk());
}
/**
* 查询符合条件的字段总和
* @param $where array 查询条件
* @param $field string 求和字段
* @return int|string
*/
public function getSum($where,$field = 'num') {
return $this->where($where)->sum($field);
}
/**
* 生成订单编号 - 不同的表订单编号带不同的大写字母以区分
* @author changsheng
* @date 2020-07-16
* @param string $char 订单编号所带的大写字母
* @param int $length 订单编号后字符串长度
* @return int|string
*/
public function createOrderNumber($char = "", $length = 4) {
$count = 0;
do {
$order_number = $char . date('YmdHis');
for ($i = 0; $i < $length; $i++) {
$order_number .= rand(0, 9);
}
$count = $this->getCount(['order_number' => $order_number]); //保证生成的订单编号不会重复
} while ($count>0);
return $order_number;
}
/**
* 组合where 条件
* @param $where_array array 链式二维数组
*/
public function connect_where($where_array){
$query = $this;
if(isset($where_array['hasWhere'])){
if(!isset($where_array['hasWhere']['name'])||!isset($where_array['hasWhere']['where'])){
unset($where_array['hasWhere']);
}
$query = $query->hasWhere($where_array['hasWhere']['name'],$where_array['hasWhere']['where']);
}
if(isset($where_array['with'])){
$query = $query->with($where_array['with']);
}
if(isset($where_array['where'])){
if(isset($where_array['hasWhere'])){
//反转数组处理表头
$where_array['where']=array_flip($where_array['where']);
foreach ($where_array['where'] as &$value){
$value = $this->getModel()->name.".".$value;
}
$where_array['where']=array_flip($where_array['where']);
$query = $query->where($where_array['where']);
}else{
$query = $query->where($where_array['where']);
}
}
return $query;
}
/**
* 修改field
* @param $field
*/
protected function changeField($field){
$field = explode(",",$field);
foreach ($field as &$value){
$value = $this->getTable().".".$value;
}
$field = implode(",",$field);
return $field;
}
/**
* 修改排序
* @param string $order 排序
*/
protected function changeOrder($order){
$order = explode(",",$order);
foreach ($order as &$value){
$value = $this->getTable().".".$value;
}
$order = implode(",",$order);
return $order;
}
}