crud
This commit is contained in:
@@ -0,0 +1,122 @@
|
||||
<?php
|
||||
|
||||
namespace app\admin\service;
|
||||
|
||||
use app\common\traits\crud\CrudServiceTrait;
|
||||
use app\common\model\Corp;
|
||||
use support\Request;
|
||||
|
||||
class CorpService
|
||||
{
|
||||
use CrudServiceTrait;
|
||||
|
||||
/**
|
||||
* 指定当前要操作的模型
|
||||
*/
|
||||
protected string $modelClass = Corp::class;
|
||||
|
||||
|
||||
/**
|
||||
* 如果需要自定义,比如强制加一个 where['status']=1,
|
||||
* 只要在这里重写:
|
||||
*/
|
||||
// protected function buildQueryConfig(Request $request): array
|
||||
// {
|
||||
// $config = [];
|
||||
// $config['where']['id'] = 2;
|
||||
// return $config;
|
||||
// }
|
||||
|
||||
/**
|
||||
* 如果需要自定义“哪些字段可筛选”或额外联表,
|
||||
* 只需重写 buildQueryConfig:
|
||||
*/
|
||||
// protected function buildQueryConfig(Request $request): array
|
||||
// {
|
||||
// $config = parent::buildQueryConfig($request);
|
||||
// // 强制只看 status=1 的记录
|
||||
// $config['where']['status'] = 1;
|
||||
// // 自动 eager load company relation
|
||||
// $config['with'][] = 'company';
|
||||
// return $config;
|
||||
// }
|
||||
|
||||
/**
|
||||
* 复杂查询,覆盖 complexQuery 方法
|
||||
*/
|
||||
// protected function complexQuery(array $params): array
|
||||
// {
|
||||
// return $this->logicComplex(function($q) use ($params) {
|
||||
// // 自定义 join、where、select
|
||||
// return $q->where('status', 1)
|
||||
// ->orderBy('created_at','desc')
|
||||
// ->paginate($params['limit'] ?? 15);
|
||||
// })->toArray();
|
||||
// }
|
||||
|
||||
// /**
|
||||
// * 复杂查询示例:
|
||||
// * 只要在 URL 里添加 ?complex_query=1,
|
||||
// * 并传入你需要的任何参数,
|
||||
// * `serviceList()` 就会走到这里来:
|
||||
// */
|
||||
// protected function complexQuery(Request $request): array
|
||||
// {
|
||||
// // 假设前端传了 date_from、date_to
|
||||
// $from = $request->get('date_from');
|
||||
// $to = $request->get('date_to');
|
||||
|
||||
// // 交给 CrudUtil 执行一个自定义 query
|
||||
// $pager = $this->logicComplex(function($q) use ($from, $to) {
|
||||
// $q->where('status', 1);
|
||||
|
||||
// if ($from && $to) {
|
||||
// $q->whereBetween('created_at', [$from, $to]);
|
||||
// }
|
||||
|
||||
// // 再加个排序
|
||||
// $q->orderBy('created_at','desc');
|
||||
|
||||
// // 用 paginate 而不是 listAll
|
||||
// return $q->paginate(
|
||||
// /* perPage */ (int)request()->get('limit',15),
|
||||
// ['*'],
|
||||
// 'page',
|
||||
// (int)request()->get('page',1)
|
||||
// );
|
||||
// });
|
||||
|
||||
// // 把 Paginator 转成数组格式
|
||||
// return [
|
||||
// 'total' => $pager->total(),
|
||||
// 'data' => $pager->items(),
|
||||
// ];
|
||||
// }
|
||||
|
||||
|
||||
// protected function afterServiceList(array $res): array
|
||||
// {
|
||||
// // 对列表的 data 做处理
|
||||
// $res['data'] = array_map(fn($i)=>[...], $res['data']);
|
||||
// return $res;
|
||||
// }
|
||||
|
||||
// protected function afterServiceShow($item)
|
||||
// {
|
||||
// // 对详情对象处理
|
||||
// $item['extra']=123;
|
||||
// return $item;
|
||||
// }
|
||||
|
||||
// afterServiceStore/Update/Destroy 同理
|
||||
|
||||
|
||||
// protected function afterServiceList(mixed $result): array
|
||||
// {
|
||||
// if ($result['records']) {
|
||||
// $result['records'] = array_map(fn($i) => [...$i], $result['records']);
|
||||
// }
|
||||
|
||||
// return $result;
|
||||
// }
|
||||
}
|
||||
Reference in New Issue
Block a user