f5e93f2400
# Conflicts: # app/admin/service/SysDictService.php
242 lines
7.1 KiB
PHP
242 lines
7.1 KiB
PHP
<?php
|
|
|
|
namespace app\admin\service;
|
|
|
|
use app\admin\validate\SysDictValidate;
|
|
use app\common\traits\crud\CrudServiceTrait;
|
|
use app\common\model\SysDict;
|
|
use app\common\model\SysDictCategory;
|
|
use app\common\utils\translation\service\TranslationService;
|
|
use app\queue\utils\Translat;
|
|
use app\common\utils\translation\service\TranslationService;
|
|
use app\queue\utils\Translat;
|
|
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['category_id']) && !empty($p['category_id'])) {
|
|
$config['where']['category_id'] = $p['category_id'];
|
|
}
|
|
// 关键字
|
|
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';
|
|
// 决定查id还是code
|
|
$scopeType = 0;
|
|
if (isset($p['scope_type']) && $p['scope_type'] > 0) {
|
|
$scopeType = 1;
|
|
}
|
|
if (isset($p['scope']) && !empty($p['scope'])) {
|
|
if ($scopeType == 1) {
|
|
$config['where']['dict_code'] = ['in', explode(',', $p['scope'])];
|
|
} else {
|
|
$config['where']['id'] = ['in', explode(',', $p['scope'])];
|
|
}
|
|
}
|
|
$config['with']['sysDictItem'][] = 'dict_id';
|
|
$config['with']['sysDictItem'][] = 'item_label';
|
|
$config['with']['sysDictItem'][] = 'item_value';
|
|
break;
|
|
case 'group':
|
|
$config['order']['id'] = 'desc';
|
|
$config['where']['id'] = 1;
|
|
$config['with']['sysDictItem'][] = 'dict_id';
|
|
$config['with']['sysDictItem'][] = 'item_label';
|
|
$config['with']['sysDictItem'][] = 'item_value';
|
|
break;
|
|
}
|
|
|
|
return $config;
|
|
}
|
|
|
|
public function group(Request $request)
|
|
{
|
|
$config = $this->buildQueryConfig($request, 'group');
|
|
$result = $this->logicShow($config);
|
|
return $this->postProcess('group', $result);
|
|
}
|
|
|
|
|
|
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;
|
|
}
|
|
|
|
public $translatFields = ['dict_name', 'description'];
|
|
|
|
public function afterServiceStore($res)
|
|
{
|
|
$sourceLocale = locale();
|
|
$data = $res->toArray();
|
|
$traitsourceFields = Translat::extractSourceFields($data, $this->translatFields);
|
|
|
|
(new TranslationService())->queueTranslations(
|
|
'sys_dict',
|
|
$res['id'],
|
|
$sourceLocale,
|
|
$traitsourceFields
|
|
);
|
|
}
|
|
|
|
|
|
/*
|
|
|
|
示例用法:
|
|
|
|
// 1) 获取分类为“车辆类型”的所有字典
|
|
$list = $dictService->getDictsByCategoryName('车辆类型');
|
|
// => [['dict_code' => 'SUV', 'dict_name' => '越野车'], …]
|
|
|
|
// 2) 获取某分类下某个 code 的名称
|
|
$name = $dictService->getDictName('车辆类型', 'SUV');
|
|
// => "越野车"
|
|
|
|
// 3) 获取某分类下拉选项
|
|
$options = $dictService->getDictOptions('车辆类型');
|
|
// => ['SUV' => '越野车', 'SEDAN' => '轿车', …]
|
|
|
|
// 4) 通过 code 直接拿名称(不管分类)
|
|
$name = $dictService->getNameByCode('SUV');
|
|
// => "越野车"
|
|
|
|
*/
|
|
|
|
|
|
/**
|
|
* 根据分类名获取字典列表
|
|
*/
|
|
public function getDictsByCategoryName(string $categoryName): array
|
|
{
|
|
$category = SysDictCategory::where('category_name', $categoryName)
|
|
->where('status', 1)
|
|
->first();
|
|
|
|
if (!$category) {
|
|
return [];
|
|
}
|
|
|
|
return $category->dicts()->get(['dict_code', 'dict_name'])->toArray();
|
|
}
|
|
|
|
/**
|
|
* 根据分类名 + 字典code 获取字典项名称
|
|
*/
|
|
public function getDictName(string $categoryName, string $dictCode): ?string
|
|
{
|
|
$dict = SysDict::whereHas('category', function ($q) use ($categoryName) {
|
|
$q->where('category_name', $categoryName)->where('status', 1);
|
|
})
|
|
->where('dict_code', $dictCode)
|
|
->where('status', 1)
|
|
->whereNull('deleted_at')
|
|
->first();
|
|
|
|
return $dict?->dict_name;
|
|
}
|
|
|
|
/**
|
|
* 根据分类名获取下拉键值对
|
|
* 返回格式:['code1' => '名称1', 'code2' => '名称2']
|
|
*/
|
|
public function getDictOptions(string $categoryName): array
|
|
{
|
|
$dicts = $this->getDictsByCategoryName($categoryName);
|
|
|
|
$options = [];
|
|
foreach ($dicts as $item) {
|
|
$options[$item['dict_code']] = $item['dict_name'];
|
|
}
|
|
|
|
return $options;
|
|
}
|
|
|
|
/**
|
|
* 根据字典 code 获取对应的名称(不依赖分类)
|
|
*/
|
|
public function getNameByCode(string $dictCode): ?string
|
|
{
|
|
$dict = SysDict::where('dict_code', $dictCode)
|
|
->where('status', 1)
|
|
->whereNull('deleted_at')
|
|
->first();
|
|
|
|
return $dict?->dict_name;
|
|
}
|
|
} |