You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
150 lines
4.2 KiB
150 lines
4.2 KiB
|
2 months ago
|
"""
|
||
|
|
配置服务
|
||
|
|
"""
|
||
|
|
from typing import List, Optional
|
||
|
|
from sqlalchemy.orm import Session
|
||
|
|
from fastapi import HTTPException, status
|
||
|
|
|
||
|
|
from app.models.config import SDKConfig, SystemConfig
|
||
|
|
|
||
|
|
|
||
|
|
class ConfigService:
|
||
|
|
"""配置服务"""
|
||
|
|
|
||
|
|
@staticmethod
|
||
|
|
def get_sdk_configs(db: Session) -> List[SDKConfig]:
|
||
|
|
"""获取所有SDK配置"""
|
||
|
|
return db.query(SDKConfig).order_by(SDKConfig.created_at.desc()).all()
|
||
|
|
|
||
|
|
@staticmethod
|
||
|
|
def get_sdk_config(db: Session, config_id: int) -> Optional[SDKConfig]:
|
||
|
|
"""获取指定SDK配置"""
|
||
|
|
return db.query(SDKConfig).filter(SDKConfig.id == config_id).first()
|
||
|
|
|
||
|
|
@staticmethod
|
||
|
|
def get_default_sdk_config(db: Session) -> Optional[SDKConfig]:
|
||
|
|
"""获取默认SDK配置"""
|
||
|
|
return db.query(SDKConfig).filter(SDKConfig.is_default == True).first()
|
||
|
|
|
||
|
|
@staticmethod
|
||
|
|
def create_sdk_config(db: Session, config_data: dict) -> SDKConfig:
|
||
|
|
"""
|
||
|
|
创建SDK配置
|
||
|
|
|
||
|
|
Args:
|
||
|
|
db: 数据库会话
|
||
|
|
config_data: 配置数据
|
||
|
|
|
||
|
|
Returns:
|
||
|
|
新配置对象
|
||
|
|
"""
|
||
|
|
config = SDKConfig(**config_data)
|
||
|
|
db.add(config)
|
||
|
|
db.commit()
|
||
|
|
db.refresh(config)
|
||
|
|
return config
|
||
|
|
|
||
|
|
@staticmethod
|
||
|
|
def update_sdk_config(
|
||
|
|
db: Session,
|
||
|
|
config_id: int,
|
||
|
|
config_data: dict
|
||
|
|
) -> SDKConfig:
|
||
|
|
"""
|
||
|
|
更新SDK配置
|
||
|
|
|
||
|
|
Args:
|
||
|
|
db: 数据库会话
|
||
|
|
config_id: 配置ID
|
||
|
|
config_data: 更新数据
|
||
|
|
|
||
|
|
Returns:
|
||
|
|
更新后的配置对象
|
||
|
|
"""
|
||
|
|
config = db.query(SDKConfig).filter(SDKConfig.id == config_id).first()
|
||
|
|
|
||
|
|
if not config:
|
||
|
|
raise HTTPException(
|
||
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
||
|
|
detail="配置不存在"
|
||
|
|
)
|
||
|
|
|
||
|
|
# 更新字段
|
||
|
|
for key, value in config_data.items():
|
||
|
|
if value is not None and hasattr(config, key):
|
||
|
|
setattr(config, key, value)
|
||
|
|
|
||
|
|
db.commit()
|
||
|
|
db.refresh(config)
|
||
|
|
return config
|
||
|
|
|
||
|
|
@staticmethod
|
||
|
|
def delete_sdk_config(db: Session, config_id: int):
|
||
|
|
"""
|
||
|
|
删除SDK配置
|
||
|
|
|
||
|
|
Args:
|
||
|
|
db: 数据库会话
|
||
|
|
config_id: 配置ID
|
||
|
|
"""
|
||
|
|
config = db.query(SDKConfig).filter(SDKConfig.id == config_id).first()
|
||
|
|
|
||
|
|
if not config:
|
||
|
|
raise HTTPException(
|
||
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
||
|
|
detail="配置不存在"
|
||
|
|
)
|
||
|
|
|
||
|
|
db.delete(config)
|
||
|
|
db.commit()
|
||
|
|
|
||
|
|
@staticmethod
|
||
|
|
def set_default_config(db: Session, config_id: int):
|
||
|
|
"""
|
||
|
|
设置默认配置
|
||
|
|
|
||
|
|
Args:
|
||
|
|
db: 数据库会话
|
||
|
|
config_id: 配置ID
|
||
|
|
"""
|
||
|
|
config = db.query(SDKConfig).filter(SDKConfig.id == config_id).first()
|
||
|
|
|
||
|
|
if not config:
|
||
|
|
raise HTTPException(
|
||
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
||
|
|
detail="配置不存在"
|
||
|
|
)
|
||
|
|
|
||
|
|
# 取消其他配置的默认状态
|
||
|
|
db.query(SDKConfig).update({SDKConfig.is_default: False})
|
||
|
|
|
||
|
|
# 设置当前配置为默认
|
||
|
|
config.is_default = True
|
||
|
|
db.commit()
|
||
|
|
db.refresh(config)
|
||
|
|
|
||
|
|
@staticmethod
|
||
|
|
def get_system_config(db: Session, key: str) -> Optional[str]:
|
||
|
|
"""获取系统配置值"""
|
||
|
|
config = db.query(SystemConfig).filter(SystemConfig.config_key == key).first()
|
||
|
|
return config.config_value if config else None
|
||
|
|
|
||
|
|
@staticmethod
|
||
|
|
def set_system_config(db: Session, key: str, value: str, description: str = None):
|
||
|
|
"""设置系统配置"""
|
||
|
|
config = db.query(SystemConfig).filter(SystemConfig.config_key == key).first()
|
||
|
|
|
||
|
|
if config:
|
||
|
|
config.config_value = value
|
||
|
|
if description:
|
||
|
|
config.description = description
|
||
|
|
else:
|
||
|
|
config = SystemConfig(
|
||
|
|
config_key=key,
|
||
|
|
config_value=value,
|
||
|
|
description=description
|
||
|
|
)
|
||
|
|
db.add(config)
|
||
|
|
|
||
|
|
db.commit()
|