diff --git a/backend/app/api/v1/system_settings.py b/backend/app/api/v1/system_settings.py index 870771b..c5aad94 100644 --- a/backend/app/api/v1/system_settings.py +++ b/backend/app/api/v1/system_settings.py @@ -168,22 +168,41 @@ async def set_feature_switch(db: AsyncSession, tenant_id: int, feature_code: str ) default_row = result.fetchone() + # 定义功能开关默认值映射 + FEATURE_DEFAULTS = { + 'employee_sync': ('员工同步', 'system', '从钉钉同步员工信息'), + 'dingtalk_login': ('钉钉免密登录', 'system', '允许通过钉钉免密登录'), + 'ai_practice': ('AI 对练', 'feature', 'AI 对练功能'), + 'dual_practice': ('双人对练', 'feature', '双人对练功能'), + } + if default_row: - # 插入租户级配置 - await db.execute( - text(""" - INSERT INTO feature_switches (tenant_id, feature_code, feature_name, feature_group, is_enabled, description) - VALUES (:tenant_id, :feature_code, :feature_name, :feature_group, :is_enabled, :description) - """), - { - "tenant_id": tenant_id, - "feature_code": feature_code, - "feature_name": default_row[0], - "feature_group": default_row[1], - "is_enabled": 1 if is_enabled else 0, - "description": default_row[2] - } - ) + feature_name = default_row[0] + feature_group = default_row[1] + description = default_row[2] + elif feature_code in FEATURE_DEFAULTS: + feature_name, feature_group, description = FEATURE_DEFAULTS[feature_code] + else: + # 未知功能码,使用默认值 + feature_name = feature_code + feature_group = 'system' + description = f'{feature_code} 功能开关' + + # 插入租户级配置 + await db.execute( + text(""" + INSERT INTO feature_switches (tenant_id, feature_code, feature_name, feature_group, is_enabled, description) + VALUES (:tenant_id, :feature_code, :feature_name, :feature_group, :is_enabled, :description) + """), + { + "tenant_id": tenant_id, + "feature_code": feature_code, + "feature_name": feature_name, + "feature_group": feature_group, + "is_enabled": 1 if is_enabled else 0, + "description": description + } + ) # ============================================