feat: update cms case api
This commit is contained in:
+10
-196
@@ -23,28 +23,14 @@ from .serializers import (
|
||||
)
|
||||
from .services import case_importer, scoring_rule_generator
|
||||
from .services.department_resolver import resolve_department
|
||||
from .services.exam_items import (
|
||||
normalize_exam_items, assert_no_duplicate_exam_items, EXAM_ITEM_FIELDS,
|
||||
from .services.case_writer import (
|
||||
create_case_full, build_full_response as _build_full_response,
|
||||
validate_scoring_rules as _validate_scoring_rules,
|
||||
CASE_BASE_FIELDS, TRADITIONAL_FIELDS, TEACHING_FIELDS, SCORING_RULE_FIELDS,
|
||||
)
|
||||
|
||||
audit = logging.getLogger('audit')
|
||||
|
||||
TRADITIONAL_FIELDS = {'standard_diagnosis', 'standard_treatment', 'guideline_reference'}
|
||||
TEACHING_FIELDS = {'teaching_goal', 'discussion_questions', 'teacher_guide', 'scoring_focus'}
|
||||
SCORING_RULE_FIELDS = {
|
||||
'dimension', 'competency_dimension', 'score_weight',
|
||||
'ai_auto_score', 'osce_dimension', 'scoring_standard', 'rubric_json',
|
||||
}
|
||||
|
||||
CASE_BASE_FIELDS = {
|
||||
'title', 'case_type', 'difficulty', 'difficulty_score',
|
||||
'chief_complaint', 'description', 'patient_age', 'patient_gender',
|
||||
'tags', 'symptom_tags', 'disease_tags', 'competency_tags',
|
||||
'guideline_tags', 'knowledge_points', 'icd_codes',
|
||||
'estimated_minutes', 'osce_enabled', 'rag_enabled',
|
||||
'ai_prompt_template', 'multimodal_assets',
|
||||
}
|
||||
|
||||
|
||||
class CaseBaseViewSet(viewsets.ModelViewSet):
|
||||
"""病例管理"""
|
||||
@@ -154,65 +140,12 @@ class CaseBaseViewSet(viewsets.ModelViewSet):
|
||||
)
|
||||
def full_create(self, request):
|
||||
"""C3: 创建病例(主表+子表+评分规则同一事务)"""
|
||||
data = request.data
|
||||
case_type = data.get('case_type', '')
|
||||
|
||||
if case_type not in ('traditional', 'teaching'):
|
||||
raise AppError('CASE_TYPE_NOT_SUPPORTED', f'case_type 不支持: {case_type}', status_code=400)
|
||||
|
||||
if 'stages' in data:
|
||||
raise AppError('CASE_FIELD_NOT_ALLOWED', '本接口不接收 stages 字段', status_code=400)
|
||||
|
||||
sub_data = data.get(case_type)
|
||||
other_type = 'teaching' if case_type == 'traditional' else 'traditional'
|
||||
if not sub_data:
|
||||
raise AppError('CASE_SUBTYPE_REQUIRED', f'{case_type} 子表数据缺失', status_code=400)
|
||||
if data.get(other_type):
|
||||
raise AppError('CASE_SUBTYPE_CONFLICT', '不允许同时传入两种子表数据', status_code=400)
|
||||
|
||||
scoring_rules_data = data.get('scoring_rules', [])
|
||||
if not scoring_rules_data:
|
||||
raise AppError('CASE_VALIDATION_ERROR', 'scoring_rules 必填且至少 1 条', status_code=400)
|
||||
_validate_scoring_rules(scoring_rules_data)
|
||||
|
||||
exam_items_data = normalize_exam_items(data.get('exam_items') or [])
|
||||
assert_no_duplicate_exam_items(exam_items_data)
|
||||
|
||||
department = resolve_department(data.get('department_name', ''))
|
||||
|
||||
with transaction.atomic():
|
||||
case_kwargs = {k: data[k] for k in CASE_BASE_FIELDS if k in data}
|
||||
case_kwargs['department'] = department
|
||||
case_kwargs['created_by'] = request.user
|
||||
case_kwargs['status'] = 1
|
||||
case_kwargs['vector_status'] = 0
|
||||
case_kwargs['publish_status'] = 1 if data.get('auto_publish') else 0
|
||||
case = CaseBase.objects.create(**case_kwargs)
|
||||
|
||||
sub_model = TraditionalCase if case_type == 'traditional' else TeachingCase
|
||||
allowed_sub = TRADITIONAL_FIELDS if case_type == 'traditional' else TEACHING_FIELDS
|
||||
sub_model.objects.create(case=case, **{k: v for k, v in sub_data.items() if k in allowed_sub})
|
||||
|
||||
rule_objs = [
|
||||
ScoringRule(case=case, **{k: v for k, v in rule.items() if k in SCORING_RULE_FIELDS})
|
||||
for rule in scoring_rules_data
|
||||
]
|
||||
ScoringRule.objects.bulk_create(rule_objs)
|
||||
|
||||
if exam_items_data:
|
||||
exam_objs = [
|
||||
CaseExamItem(
|
||||
case=case,
|
||||
**{k: v for k, v in item.items() if k in EXAM_ITEM_FIELDS},
|
||||
)
|
||||
for item in exam_items_data
|
||||
]
|
||||
CaseExamItem.objects.bulk_create(exam_objs)
|
||||
case, n_rules, n_items = create_case_full(request.data, request.user)
|
||||
|
||||
audit.info(
|
||||
'CASE_CREATE case_id=%s from=%s by=%s scoring_rules=%d exam_items=%d',
|
||||
case.id, data.get('parse_id', 'form'), request.user.id,
|
||||
len(rule_objs), len(exam_items_data),
|
||||
case.id, request.data.get('parse_id', 'form'), request.user.id,
|
||||
n_rules, n_items,
|
||||
)
|
||||
|
||||
return Response(_build_full_response(case), status=status.HTTP_201_CREATED)
|
||||
@@ -240,7 +173,7 @@ class CaseBaseViewSet(viewsets.ModelViewSet):
|
||||
if not case:
|
||||
raise AppError('NOT_FOUND', '病例不存在', status_code=404)
|
||||
|
||||
if case.publish_status != 1:
|
||||
if case.publish_status != 2: # 仅「已发布」可公开查看;草稿/正常需作者或管理员
|
||||
if not request.user.is_authenticated:
|
||||
raise AppError('AUTH_UNAUTHORIZED', '请先登录', status_code=401)
|
||||
if not (request.user.id == case.created_by_id
|
||||
@@ -336,15 +269,14 @@ class CaseBaseViewSet(viewsets.ModelViewSet):
|
||||
@action(detail=True, methods=['post'])
|
||||
def publish(self, request, pk=None):
|
||||
case = self.get_object()
|
||||
case.publish_status = 1
|
||||
case.publish_status = 2 # 已发布
|
||||
case.save()
|
||||
return Response({'message': '病例已发布'})
|
||||
|
||||
@action(detail=True, methods=['post'])
|
||||
def unpublish(self, request, pk=None):
|
||||
case = self.get_object()
|
||||
case.publish_status = 2
|
||||
case.save()
|
||||
case.delete() # 下架 = 软删除(is_deleted=1)
|
||||
return Response({'message': '病例已下架'})
|
||||
|
||||
|
||||
@@ -384,121 +316,3 @@ class ScoringRuleViewSet(viewsets.ModelViewSet):
|
||||
filterset_fields = ['case', 'dimension', 'ai_auto_score', 'osce_dimension']
|
||||
|
||||
|
||||
# ── helpers ──────────────────────────────────────────────────────────────
|
||||
|
||||
def _validate_scoring_rules(rules):
|
||||
if not isinstance(rules, list):
|
||||
raise AppError('CASE_VALIDATION_ERROR', 'scoring_rules 必须为数组', status_code=400)
|
||||
for i, rule in enumerate(rules):
|
||||
if not isinstance(rule, dict):
|
||||
raise AppError('CASE_VALIDATION_ERROR', f'scoring_rules[{i}] 必须为对象', status_code=400)
|
||||
if not rule.get('dimension'):
|
||||
raise AppError('CASE_VALIDATION_ERROR', f'scoring_rules[{i}].dimension 必填', status_code=400)
|
||||
weight = rule.get('score_weight')
|
||||
if weight is not None:
|
||||
try:
|
||||
weight = float(weight)
|
||||
except (TypeError, ValueError):
|
||||
raise AppError('CASE_VALIDATION_ERROR', f'scoring_rules[{i}].score_weight 须为数字', status_code=400)
|
||||
if weight <= 0 or weight > 1:
|
||||
raise AppError('CASE_VALIDATION_ERROR', f'scoring_rules[{i}].score_weight 须在 (0, 1]', status_code=400)
|
||||
|
||||
|
||||
def _build_full_response(case):
|
||||
if hasattr(case, '_prefetched_objects_cache') and 'exam_items' in getattr(
|
||||
case, '_prefetched_objects_cache', {}
|
||||
):
|
||||
exam_qs = case.exam_items.all()
|
||||
else:
|
||||
exam_qs = CaseExamItem.objects.filter(case_id=case.id).order_by('display_order', 'id')
|
||||
|
||||
result = {
|
||||
'case': {
|
||||
'id': case.id,
|
||||
'title': case.title,
|
||||
'case_type': case.case_type,
|
||||
'difficulty': case.difficulty,
|
||||
'difficulty_score': case.difficulty_score,
|
||||
'department': case.department_id,
|
||||
'department_name': case.department.name if case.department else None,
|
||||
'chief_complaint': case.chief_complaint,
|
||||
'description': case.description,
|
||||
'patient_age': case.patient_age,
|
||||
'patient_gender': case.patient_gender,
|
||||
'tags': case.tags,
|
||||
'symptom_tags': case.symptom_tags,
|
||||
'disease_tags': case.disease_tags,
|
||||
'competency_tags': case.competency_tags,
|
||||
'guideline_tags': case.guideline_tags,
|
||||
'knowledge_points': case.knowledge_points,
|
||||
'icd_codes': case.icd_codes,
|
||||
'estimated_minutes': case.estimated_minutes,
|
||||
'osce_enabled': case.osce_enabled,
|
||||
'rag_enabled': case.rag_enabled,
|
||||
'ai_prompt_template': case.ai_prompt_template,
|
||||
'multimodal_assets': case.multimodal_assets,
|
||||
'vector_status': case.vector_status,
|
||||
'publish_status': case.publish_status,
|
||||
'status': case.status,
|
||||
'created_by': case.created_by_id,
|
||||
'created_by_name': case.created_by.real_name if case.created_by else None,
|
||||
'created_at': case.created_at.isoformat() if case.created_at else None,
|
||||
'updated_at': case.updated_at.isoformat() if case.updated_at else None,
|
||||
},
|
||||
}
|
||||
|
||||
if case.case_type == 'traditional':
|
||||
try:
|
||||
tc = case.traditionalcase
|
||||
result['traditional'] = {
|
||||
'standard_diagnosis': tc.standard_diagnosis,
|
||||
'standard_treatment': tc.standard_treatment,
|
||||
'guideline_reference': tc.guideline_reference,
|
||||
}
|
||||
except TraditionalCase.DoesNotExist:
|
||||
result['traditional'] = None
|
||||
elif case.case_type == 'teaching':
|
||||
try:
|
||||
tc = case.teachingcase
|
||||
result['teaching'] = {
|
||||
'teaching_goal': tc.teaching_goal,
|
||||
'discussion_questions': tc.discussion_questions,
|
||||
'teacher_guide': tc.teacher_guide,
|
||||
'scoring_focus': tc.scoring_focus,
|
||||
}
|
||||
except TeachingCase.DoesNotExist:
|
||||
result['teaching'] = None
|
||||
|
||||
rules = case.scoring_rules.all().order_by('id')
|
||||
result['scoring_rules'] = [
|
||||
{
|
||||
'id': r.id,
|
||||
'dimension': r.dimension,
|
||||
'competency_dimension': r.competency_dimension,
|
||||
'score_weight': float(r.score_weight),
|
||||
'ai_auto_score': r.ai_auto_score,
|
||||
'osce_dimension': r.osce_dimension,
|
||||
'scoring_standard': r.scoring_standard,
|
||||
'rubric_json': r.rubric_json,
|
||||
}
|
||||
for r in rules
|
||||
]
|
||||
|
||||
result['exam_items'] = [
|
||||
{
|
||||
'id': e.id,
|
||||
'item_code': e.item_code,
|
||||
'item_name': e.item_name,
|
||||
'item_type': e.item_type,
|
||||
'category': e.category,
|
||||
'result_text': e.result_text,
|
||||
'result_structured': e.result_structured,
|
||||
'is_key': e.is_key,
|
||||
'is_abnormal': e.is_abnormal,
|
||||
'score_weight': float(e.score_weight),
|
||||
'display_order': e.display_order,
|
||||
}
|
||||
for e in exam_qs
|
||||
]
|
||||
|
||||
return result
|
||||
|
||||
Reference in New Issue
Block a user