Files
fdesk/app/admin/service/SysDictService.php
T
2025-09-01 17:08:02 +08:00

109 lines
3.0 KiB
PHP

<?php
namespace app\admin\service;
use app\admin\validate\SysDictValidate;
use app\common\traits\crud\CrudServiceTrait;
use app\common\model\SysDict;
use support\Request;
class SysDictService
{
use CrudServiceTrait;
/**
* 指定当前要操作的模型
*/
protected string $modelClass = SysDict::class;
/**
* 验证器
*/
protected $validator = SysDictValidate::class;
/**
* 获取验证器
*/
public function getValidator()
{
return new $this->validator;
}
/**
* 构建查询条件
*/
protected function buildQueryConfig(Request $request, string $scene = 'index'): array
{
$p = $request->all();
$config = [];
$config['model'] = $this->modelClass;
switch ($scene) {
case 'index':
// ====== ====== 排序 ====== ======
$config['order']['id'] = 'desc';
// ====== ====== 筛选 ====== ======
// 状态
if (isset($p['status']) && in_array($p['status'], [0,1])) {
$config['where']['status'] = $p['status'];
}
// 关键字
if (isset($p['keyword']) && $p['keyword']) {
$k = '%' . $p['keyword'] . '%';
foreach (['dict_name','dict_code','description'] as $col) {
$config['orWhere'][] = [$col, 'like', $k];
}
}
// ====== ====== 关联 ====== ======
$config['with']['sysDictItem'][] = 'item_label';
$config['with']['sysDictItem'][] = 'item_value';
break;
case 'show':
$config['where']['id'] = $p['id'];
// ====== ====== 关联 ====== ======
$config['with']['sysDictItem'][] = 'item_label';
$config['with']['sysDictItem'][] = 'item_value';
break;
case 'update':
case 'destroy':
$config['where']['id'] = $p['id'];
break;
case 'store':
break;
case 'scope':
$config['order']['id'] = 'desc';
if (isset($p['scope']) && !empty($p['scope'])) {
$config['where']['id'] = ['in', explode(',', $p['scope'])];
}
$config['with']['sysDictItem'][] = 'dict_id';
$config['with']['sysDictItem'][] = 'item_label';
$config['with']['sysDictItem'][] = 'item_value';
break;
}
return $config;
}
public function serviceListAllScope(Request $request)
{
$config = $this->buildQueryConfig($request, 'scope');
$result = $this->logicListAll($config);
return $this->postProcess('ListAllScope', $result);
}
public function afterServiceListAllScope($data)
{
// $data->each(function (&$item) {
// });
return $data;
}
}