Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions backend/plugin/config/api/v1/sys/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,21 @@
CreateConfigParam,
GetConfigDetail,
UpdateConfigParam,
UpdateConfigsParam,
)
from backend.plugin.config.service.config_service import config_service

router = APIRouter()


@router.get('/all', summary='获取所有参数配置', dependencies=[DependsJwtAuth])
async def get_all_configs(
type: Annotated[str | None, Query(description='参数配置类型')] = None,
) -> ResponseSchemaModel[list[GetConfigDetail]]:
configs = await config_service.get_all(type=type)
return response_base.success(data=configs)


@router.get('/{pk}', summary='获取参数配置详情', dependencies=[DependsJwtAuth])
async def get_config(pk: Annotated[int, Path(description='参数配置 ID')]) -> ResponseSchemaModel[GetConfigDetail]:
config = await config_service.get(pk=pk)
Expand Down Expand Up @@ -57,6 +66,14 @@ async def create_config(obj: CreateConfigParam) -> ResponseModel:
return response_base.success()


@router.put('', summary='批量更新参数配置', dependencies=[Depends(RequestPermission('sys.config.edits')), DependsRBAC])
async def bulk_update_config(objs: list[UpdateConfigsParam]) -> ResponseModel:
count = await config_service.bulk_update(objs=objs)
if count > 0:
return response_base.success()
return response_base.fail()


@router.put(
'/{pk}',
summary='更新参数配置',
Expand Down
16 changes: 14 additions & 2 deletions backend/plugin/config/crud/crud_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# -*- coding: utf-8 -*-
from typing import Sequence

from sqlalchemy import Select
from sqlalchemy import Select, update
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy_crud_plus import CRUDPlus

Expand All @@ -23,7 +23,7 @@ async def get(self, db: AsyncSession, pk: int) -> Config | None:
"""
return await self.select_model_by_column(db, id=pk)

async def get_by_type(self, db: AsyncSession, type: str) -> Sequence[Config | None]:
async def get_all(self, db: AsyncSession, type: str) -> Sequence[Config | None]:
"""
通过键名获取参数配置

Expand Down Expand Up @@ -81,6 +81,18 @@ async def update(self, db: AsyncSession, pk: int, obj: UpdateConfigParam) -> int
"""
return await self.update_model(db, pk, obj)

async def bulk_update(self, db: AsyncSession, objs: list[UpdateConfigParam]) -> int:
"""
批量更新参数配置

:param db: 数据库会话
:param objs: 批量更新参数配置参数
:return:
"""
params = [obj.model_dump(exclude_unset=True) for obj in objs]
await db.execute(update(self.model), params)
return len(params)

async def delete(self, db: AsyncSession, pks: list[int]) -> int:
"""
批量删除参数配置
Expand Down
4 changes: 2 additions & 2 deletions backend/plugin/config/plugin.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[plugin]
summary = '参数配置'
version = '0.0.1'
description = '通常用于前端工程数据展示'
version = '0.0.2'
description = '通常用于动态配置系统参数/前端工程数据展示'
author = 'wu-clan'

[app]
Expand Down
6 changes: 6 additions & 0 deletions backend/plugin/config/schema/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ class UpdateConfigParam(ConfigSchemaBase):
"""更新参数配置参数"""


class UpdateConfigsParam(UpdateConfigParam):
"""批量更新参数配置参数"""

id: int = Field(description='参数配置 ID')


class GetConfigDetail(ConfigSchemaBase):
"""参数配置详情"""

Expand Down
33 changes: 33 additions & 0 deletions backend/plugin/config/service/config_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from backend.plugin.config.schema.config import (
CreateConfigParam,
UpdateConfigParam,
UpdateConfigsParam,
)


Expand All @@ -30,6 +31,17 @@ async def get(*, pk: int) -> Config:
raise errors.NotFoundError(msg='参数配置不存在')
return config

@staticmethod
async def get_all(*, type: str | None):
"""
获取所有参数配置

:param type: 参数配置类型
:return:
"""
async with async_db_session() as db:
return await config_dao.get_all(db, type)

@staticmethod
async def get_select(*, name: str | None, type: str | None) -> Select:
"""
Expand Down Expand Up @@ -75,6 +87,27 @@ async def update(*, pk: int, obj: UpdateConfigParam) -> int:
count = await config_dao.update(db, pk, obj)
return count

@staticmethod
async def bulk_update(*, objs: list[UpdateConfigsParam]) -> int:
"""
批量更新参数配置

:param objs: 参数配置批量更新参数
:return:
"""
async with async_db_session.begin() as db:
for batch in range(0, len(objs), 1000):
for obj in objs:
config = await config_dao.get(db, obj.id)
if not config:
raise errors.NotFoundError(msg='参数配置不存在')
if config.key != obj.key:
config = await config_dao.get_by_key(db, obj.key)
if config:
raise errors.ConflictError(msg=f'参数配置 {obj.key} 已存在')
count = await config_dao.bulk_update(db, objs)
return count

@staticmethod
async def delete(*, pks: list[int]) -> int:
"""
Expand Down