修改查询助手

This commit is contained in:
mzl
2025-07-22 19:30:27 +08:00
parent b53cfe6bf7
commit 283b21b1f8
3 changed files with 122 additions and 54 deletions
@@ -24,7 +24,7 @@ trait CrudControllerTrait
? [$this, 'indexFormat'] ? [$this, 'indexFormat']
: null); : null);
return $this->success($data); return $this->success($data);
} catch (\Exception $e) { } catch (\Throwable $e) {
return $this->error($e->getMessage()); return $this->error($e->getMessage());
} }
} }
@@ -44,7 +44,7 @@ trait CrudControllerTrait
? [$this, 'showFormat'] ? [$this, 'showFormat']
: null); : null);
return $this->success($data); return $this->success($data);
} catch (\Exception $e) { } catch (\Throwable $e) {
return $this->error($e->getMessage()); return $this->error($e->getMessage());
} }
} }
@@ -61,7 +61,7 @@ trait CrudControllerTrait
$data = $this->validate($this->service->getValidator(), $request->post(), 'store'); $data = $this->validate($this->service->getValidator(), $request->post(), 'store');
$res = $this->service->serviceStore($request, $data); $res = $this->service->serviceStore($request, $data);
return $this->success($res, '创建成功'); return $this->success($res, '创建成功');
} catch (\Exception $e) { } catch (\Throwable $e) {
return $this->error($e->getMessage()); return $this->error($e->getMessage());
} }
} }
@@ -78,7 +78,7 @@ trait CrudControllerTrait
$data = $this->validate($this->service->getValidator(), $request->post(), 'update'); $data = $this->validate($this->service->getValidator(), $request->post(), 'update');
$res = $this->service->serviceUpdate($request, $data); $res = $this->service->serviceUpdate($request, $data);
return $this->success([], '更新成功'); return $this->success([], '更新成功');
} catch (\Exception $e) { } catch (\Throwable $e) {
return $this->error($e->getMessage()); return $this->error($e->getMessage());
} }
} }
@@ -94,7 +94,7 @@ trait CrudControllerTrait
try { try {
$count = $this->service->serviceDestroy($request); $count = $this->service->serviceDestroy($request);
return $this->success([], "成功删除了 {$count} 条"); return $this->success([], "成功删除了 {$count} 条");
} catch (\Exception $e) { } catch (\Throwable $e) {
return $this->error($e->getMessage()); return $this->error($e->getMessage());
} }
} }
+99 -32
View File
@@ -24,6 +24,7 @@ use support\exception\BusinessException;
class CrudUtil class CrudUtil
{ {
/** /**
* 构建查询,支持: * 构建查询,支持:
* - 本表字段过滤 where * - 本表字段过滤 where
@@ -56,48 +57,62 @@ class CrudUtil
$model = new $config['model']; $model = new $config['model'];
$q = $model->newQuery(); $q = $model->newQuery();
// 1. eager load // eager-load:支持 ['rel','rel2'] 和 ['rel'=>fields[], ...]
if (!empty($config['with'])) { if (! empty($config['with'])) {
$q->with($config['with']); foreach ($config['with'] as $rel => $fields) {
} if (is_int($rel)) {
// 普通关联
// 2. 关联表字段选择 $q->with($fields);
if (!empty($config['withFields'])) { } else {
foreach ($config['withFields'] as $rel => $fields) { // 带字段筛选的关联
$q->with([$rel => function ($qr) use ($fields) { $q->with([$rel => function ($qr) use ($fields) {
$fk = $qr->getModel()->getForeignKey(); $key = $qr->getModel()->getKeyName();
$qr->select(array_unique(array_merge($fields, [$fk]))); if (! in_array($key, $fields, true)) {
}]); $fields[] = $key;
}
$qr->select($fields);
}]);
}
} }
} }
// 3. 本表字段选择 // 表字段
if (!empty($config['fields'])) { if (!empty($config['fields'])) {
$q->select($config['fields']); $q->select($config['fields']);
} }
// 4. where 条件(支持 dot-notation 关联过滤) // where
if (!empty($config['where'])) { if (!empty($config['where'])) {
foreach ($config['where'] as $col => $val) { foreach ($config['where'] as $col => $val) {
if (strpos($col, '.') !== false) { if (strpos($col, '.') !== false) {
[$rel, $relCol] = explode('.', $col, 2); // 多层支持
$q->whereHas($rel, function ($qr) use ($relCol, $val) { $segments = explode('.', $col);
self::applyWhere($qr, $relCol, $val); $leaf = array_pop($segments);
}); self::applyNestedWhere($q, $segments, $leaf, $val);
} else { } else {
self::applyWhere($q, $col, $val); self::applyWhere($q, $col, $val);
} }
} }
} }
// 5. 排序 // or where
if (!empty($config['orWhere'])) {
$q->where(function($q) use($config) {
foreach ($config['orWhere'] as $cond) {
[$col,$op,$val] = $cond;
$q->orWhere($col,$op,$val);
}
});
}
// 排序
if (!empty($config['order'])) { if (!empty($config['order'])) {
foreach ($config['order'] as $col => $dir) { foreach ($config['order'] as $col => $dir) {
$q->orderBy($col, strtolower($dir) === 'asc' ? 'asc' : 'desc'); $q->orderBy($col, strtolower($dir) === 'asc' ? 'asc' : 'desc');
} }
} }
// 6. 数据权限过滤 // 数据权限过滤
if (!empty($config['dataLimit'])) { if (!empty($config['dataLimit'])) {
self::applyDataLimit($q, $config['dataLimit']); self::applyDataLimit($q, $config['dataLimit']);
} }
@@ -261,8 +276,29 @@ class CrudUtil
//=================== Internal Methods ===================// //=================== Internal Methods ===================//
/** /**
* 应用单字段 where 过滤 * 支持多层的 whereHas:$relations=['roles','permissions'],$leaf='name'
*/
protected static function applyNestedWhere(Builder $q, array $relations, string $leaf, $val): void
{
$rel = array_shift($relations);
$q->whereHas($rel, function (Builder $qr) use ($relations, $leaf, $val) {
if ($relations) {
// 还有更深层,继续递归
self::applyNestedWhere($qr, $relations, $leaf, $val);
} else {
// 最后一层,使用普通 applyWhere
self::applyWhere($qr, $leaf, $val);
}
});
}
/**
* 单字段 where 支持:
* - 多条件数组 [ [op1,val1], [op2,val2], … ]
* - 单条件 ['op', val]
* - 直接值
* *
* @param Builder $q * @param Builder $q
* @param string $col * @param string $col
@@ -271,22 +307,53 @@ class CrudUtil
*/ */
protected static function applyWhere(Builder $q, string $col, $val): void protected static function applyWhere(Builder $q, string $col, $val): void
{ {
if (is_array($val) && count($val) === 2) { // 多条件数组
[$op, $v] = $val; if (is_array($val) && isset($val[0]) && is_array($val[0])) {
switch (strtolower($op)) { foreach ($val as $pair) {
case 'like': $q->where($col,'like',$v); break; if (is_array($pair) && count($pair) === 2) {
case 'in': $q->whereIn($col,(array)$v); break; self::applySingleWhere($q, $col, $pair[0], $pair[1]);
case 'not in': $q->whereNotIn($col,(array)$v); break; }
case 'null': $q->whereNull($col); break;
case 'not null': $q->whereNotNull($col); break;
case 'between': $q->whereBetween($col,(array)$v); break;
default: $q->where($col,$op,$v);
} }
} else { }
// 单条件
elseif (is_array($val) && count($val) === 2) {
self::applySingleWhere($q, $col, $val[0], $val[1]);
}
// 直接等值
else {
$q->where($col, $val); $q->where($col, $val);
} }
} }
/**
* 真正执行一个条件的 where
*/
protected static function applySingleWhere(Builder $q, string $col, string $op, $v): void
{
switch (strtolower($op)) {
case 'like':
$q->where($col, 'like', $v);
break;
case 'in':
$q->whereIn($col, (array)$v);
break;
case 'not in':
$q->whereNotIn($col, (array)$v);
break;
case 'null':
$q->whereNull($col);
break;
case 'not null':
$q->whereNotNull($col);
break;
case 'between':
$q->whereBetween($col, (array)$v);
break;
default:
$q->where($col, $op, $v);
}
}
/** /**
* 应用数据权限过滤到查询(buildQuery 场景) * 应用数据权限过滤到查询(buildQuery 场景)
*/ */
+18 -17
View File
@@ -38,12 +38,8 @@ class InquiryLogService
$config = []; $config = [];
$config['model'] = $this->modelClass; $config['model'] = $this->modelClass;
switch ($scene) { switch ($scene) {
case 'default':
$config['where']['status'] = 1;
$config['order']['sort'] = 'desc';
$config['order']['id'] = 'desc';
break;
case 'index': case 'index':
// 排序
$config['order']['id'] = 'desc'; $config['order']['id'] = 'desc';
// 渠道 // 渠道
@@ -58,10 +54,14 @@ class InquiryLogService
// 时间 // 时间
if (isset($p['time']) && $p['time']) { if (isset($p['time']) && $p['time']) {
$time = explode(' - ', $p['time']); // $time = explode(' - ', $p['time']);
// $config['where']['created_at'] = [
// ['>=', strtotime($time[0])],
// ['<=', strtotime($time[1])],
// ];
$config['where']['created_at'] = [ $config['where']['created_at'] = [
['>=', strtotime($time[0])], strtotime(explode(' - ', $p['time'])[0]),
['<=', strtotime($time[1])], strtotime(explode(' - ', $p['time'])[1]),
]; ];
} }
@@ -72,17 +72,18 @@ class InquiryLogService
// 关键字 // 关键字
if (isset($p['keyword']) && $p['keyword']) { if (isset($p['keyword']) && $p['keyword']) {
// $config['where']['order_no|email|tel|whatsapp|contacts'] = ['like', '%' . $p['keyword'] . '%']; $k = '%' . $p['keyword'] . '%';
foreach (['order_no','email','tel','whatsapp','contacts'] as $col) {
$config['orWhere'][] = [$col, 'like', $k];
}
} }
$config['with'][] = 'enter'; // 关联
$config['with'][] = 'recipient'; $config['with'][] = 'country';
$config['with']['enter'][] = 'id';
$config['withFields']['enter'][] = 'id'; $config['with']['enter'][] = 'name';
$config['withFields']['enter'][] = 'name'; $config['with']['recipient'][] = 'id';
$config['with']['recipient'][] = 'name';
$config['withFields']['recipient'][] = 'id';
$config['withFields']['recipient'][] = 'name';
break; break;
case 'show': case 'show':
$config['where']['id'] = $p['id']; $config['where']['id'] = $p['id'];