init medical training project
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
from apps.user.models import Department
|
||||
from config.exceptions import AppError
|
||||
|
||||
|
||||
def resolve_department(department_name: str):
|
||||
"""按名称解析科室,返回 Department 实例。
|
||||
|
||||
- 空/None → 返回 None(不强制)
|
||||
- 精确匹配 1 条 → 返回该 Department
|
||||
- 匹配 0 条 → 400 CASE_DEPARTMENT_NOT_FOUND
|
||||
- 匹配多条 → 400 CASE_DEPARTMENT_AMBIGUOUS
|
||||
"""
|
||||
if not department_name:
|
||||
return None
|
||||
|
||||
qs = Department.objects.filter(name=department_name)
|
||||
count = qs.count()
|
||||
|
||||
if count == 0:
|
||||
raise AppError(
|
||||
'CASE_DEPARTMENT_NOT_FOUND',
|
||||
f'科室 "{department_name}" 不存在',
|
||||
status_code=400,
|
||||
)
|
||||
if count > 1:
|
||||
raise AppError(
|
||||
'CASE_DEPARTMENT_AMBIGUOUS',
|
||||
f'科室 "{department_name}" 匹配到多条记录,请精确指定',
|
||||
details={'matches': list(qs.values_list('name', flat=True))},
|
||||
status_code=400,
|
||||
)
|
||||
return qs.first()
|
||||
Reference in New Issue
Block a user