Merge branch 'feature/mzl_dev' into develop
# Conflicts: # app/admin/service/SysDictService.php
This commit is contained in:
@@ -5,6 +5,9 @@ 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;
|
||||
@@ -151,4 +154,89 @@ class SysDictService
|
||||
$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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user