Add admin settings UI and provider deletes

This commit is contained in:
2026-04-27 18:04:00 +08:00
parent 5eb32b32a6
commit 4a8974e387
6 changed files with 714 additions and 54 deletions

View File

@@ -1,7 +1,13 @@
from sqlalchemy import delete, func, or_, select
from sqlalchemy.orm import Session
from app.common.errors.app_error import NotFoundAppError
from app.models.entities import ProviderAccount, ProviderModel
from app.common.errors.app_error import BusinessAppError, NotFoundAppError
from app.models.entities import (
ProviderAccount,
ProviderModel,
VideoGenerationTask,
VideoModelSupplierBinding,
)
from app.modules.providers.repository import ProvidersRepository
@@ -50,6 +56,43 @@ class ProvidersService:
self.db.commit()
return self.serialize_account(item)
def delete_account(self, account_id: int) -> dict:
item = self.repository.get_account(account_id)
if not item:
raise NotFoundAppError("provider account not found", code=60001)
model_ids = list(
self.db.scalars(
select(ProviderModel.id).where(ProviderModel.provider_account_id == account_id)
)
)
task_conditions = [VideoGenerationTask.provider_account_id == account_id]
if model_ids:
task_conditions.append(VideoGenerationTask.provider_model_id.in_(model_ids))
task_count = self.db.scalar(
select(func.count())
.select_from(VideoGenerationTask)
.where(or_(*task_conditions))
)
if task_count:
raise BusinessAppError(
"该供应商已有任务记录,不能直接删除,请先停用。",
code=60011,
)
if model_ids:
self.db.execute(
delete(VideoModelSupplierBinding).where(
VideoModelSupplierBinding.provider_model_id.in_(model_ids)
)
)
self.db.execute(
delete(ProviderModel).where(ProviderModel.provider_account_id == account_id)
)
self.db.delete(item)
self.db.commit()
return {"id": account_id, "deleted": True}
def list_models(self) -> list[dict]:
accounts = {item.id: item for item in self.repository.list_accounts().all()}
return [self.serialize_model(item, accounts) for item in self.repository.list_models().all()]
@@ -72,6 +115,31 @@ class ProvidersService:
account = self.repository.get_account(item.provider_account_id)
return self.serialize_model(item, {account.id: account} if account else {})
def delete_model(self, model_id: int) -> dict:
item = self.repository.get_model(model_id)
if not item:
raise NotFoundAppError("provider model not found", code=60002)
task_count = self.db.scalar(
select(func.count())
.select_from(VideoGenerationTask)
.where(VideoGenerationTask.provider_model_id == model_id)
)
if task_count:
raise BusinessAppError(
"该模型已有任务记录,不能直接删除,请先停用。",
code=60012,
)
self.db.execute(
delete(VideoModelSupplierBinding).where(
VideoModelSupplierBinding.provider_model_id == model_id
)
)
self.db.delete(item)
self.db.commit()
return {"id": model_id, "deleted": True}
@staticmethod
def serialize_account(item: ProviderAccount) -> dict:
return {
@@ -109,4 +177,3 @@ class ProvidersService:
"defaultResolution": item.default_resolution,
"status": item.status,
}