审批&备货单

This commit is contained in:
mzl
2025-09-09 13:42:00 +08:00
parent 5dd2925687
commit 918bac19a5
23 changed files with 1928 additions and 1427 deletions
+87
View File
@@ -5,6 +5,7 @@ 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 support\Request;
class SysDictService
@@ -133,4 +134,90 @@ class SysDictService
return $data;
}
/*
示例用法:
// 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;
}
}