refactor: 重构数据库orm为异步 #I9MRQS
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
from sqlalchemy.orm import Session
|
||||
from sqlalchemy import select, update, delete
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
from module_admin.entity.do.config_do import SysConfig
|
||||
from module_admin.entity.vo.config_vo import *
|
||||
from utils.page_util import PageUtil
|
||||
@@ -11,36 +12,38 @@ class ConfigDao:
|
||||
"""
|
||||
|
||||
@classmethod
|
||||
def get_config_detail_by_id(cls, db: Session, config_id: int):
|
||||
async def get_config_detail_by_id(cls, db: AsyncSession, config_id: int):
|
||||
"""
|
||||
根据参数配置id获取参数配置详细信息
|
||||
:param db: orm对象
|
||||
:param config_id: 参数配置id
|
||||
:return: 参数配置信息对象
|
||||
"""
|
||||
config_info = db.query(SysConfig) \
|
||||
.filter(SysConfig.config_id == config_id) \
|
||||
.first()
|
||||
config_info = (await db.execute(
|
||||
select(SysConfig)
|
||||
.where(SysConfig.config_id == config_id)
|
||||
)).scalars().first()
|
||||
|
||||
return config_info
|
||||
|
||||
@classmethod
|
||||
def get_config_detail_by_info(cls, db: Session, config: ConfigModel):
|
||||
async def get_config_detail_by_info(cls, db: AsyncSession, config: ConfigModel):
|
||||
"""
|
||||
根据参数配置参数获取参数配置信息
|
||||
:param db: orm对象
|
||||
:param config: 参数配置参数对象
|
||||
:return: 参数配置信息对象
|
||||
"""
|
||||
config_info = db.query(SysConfig) \
|
||||
.filter(SysConfig.config_key == config.config_key if config.config_key else True,
|
||||
SysConfig.config_value == config.config_value if config.config_value else True) \
|
||||
.first()
|
||||
config_info = (await db.execute(
|
||||
select(SysConfig)
|
||||
.where(SysConfig.config_key == config.config_key if config.config_key else True,
|
||||
SysConfig.config_value == config.config_value if config.config_value else True)
|
||||
)).scalars().first()
|
||||
|
||||
return config_info
|
||||
|
||||
@classmethod
|
||||
def get_config_list(cls, db: Session, query_object: ConfigPageQueryModel, is_page: bool = False):
|
||||
async def get_config_list(cls, db: AsyncSession, query_object: ConfigPageQueryModel, is_page: bool = False):
|
||||
"""
|
||||
根据查询参数获取参数配置列表信息
|
||||
:param db: orm对象
|
||||
@@ -48,22 +51,21 @@ class ConfigDao:
|
||||
:param is_page: 是否开启分页
|
||||
:return: 参数配置列表信息对象
|
||||
"""
|
||||
query = db.query(SysConfig) \
|
||||
.filter(SysConfig.config_name.like(f'%{query_object.config_name}%') if query_object.config_name else True,
|
||||
SysConfig.config_key.like(f'%{query_object.config_key}%') if query_object.config_key else True,
|
||||
SysConfig.config_type == query_object.config_type if query_object.config_type else True,
|
||||
SysConfig.create_time.between(
|
||||
datetime.combine(datetime.strptime(query_object.begin_time, '%Y-%m-%d'), time(00, 00, 00)),
|
||||
datetime.combine(datetime.strptime(query_object.end_time, '%Y-%m-%d'), time(23, 59, 59)))
|
||||
if query_object.begin_time and query_object.end_time else True
|
||||
) \
|
||||
query = select(SysConfig) \
|
||||
.where(SysConfig.config_name.like(f'%{query_object.config_name}%') if query_object.config_name else True,
|
||||
SysConfig.config_key.like(f'%{query_object.config_key}%') if query_object.config_key else True,
|
||||
SysConfig.config_type == query_object.config_type if query_object.config_type else True,
|
||||
SysConfig.create_time.between(
|
||||
datetime.combine(datetime.strptime(query_object.begin_time, '%Y-%m-%d'), time(00, 00, 00)),
|
||||
datetime.combine(datetime.strptime(query_object.end_time, '%Y-%m-%d'), time(23, 59, 59)))
|
||||
if query_object.begin_time and query_object.end_time else True) \
|
||||
.distinct()
|
||||
config_list = PageUtil.paginate(query, query_object.page_num, query_object.page_size, is_page)
|
||||
config_list = await PageUtil.paginate(db, query, query_object.page_num, query_object.page_size, is_page)
|
||||
|
||||
return config_list
|
||||
|
||||
@classmethod
|
||||
def add_config_dao(cls, db: Session, config: ConfigModel):
|
||||
async def add_config_dao(cls, db: AsyncSession, config: ConfigModel):
|
||||
"""
|
||||
新增参数配置数据库操作
|
||||
:param db: orm对象
|
||||
@@ -72,30 +74,32 @@ class ConfigDao:
|
||||
"""
|
||||
db_config = SysConfig(**config.model_dump())
|
||||
db.add(db_config)
|
||||
db.flush()
|
||||
await db.flush()
|
||||
|
||||
return db_config
|
||||
|
||||
@classmethod
|
||||
def edit_config_dao(cls, db: Session, config: dict):
|
||||
async def edit_config_dao(cls, db: AsyncSession, config: dict):
|
||||
"""
|
||||
编辑参数配置数据库操作
|
||||
:param db: orm对象
|
||||
:param config: 需要更新的参数配置字典
|
||||
:return:
|
||||
"""
|
||||
db.query(SysConfig) \
|
||||
.filter(SysConfig.config_id == config.get('config_id')) \
|
||||
.update(config)
|
||||
await db.execute(
|
||||
update(SysConfig),
|
||||
[config]
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def delete_config_dao(cls, db: Session, config: ConfigModel):
|
||||
async def delete_config_dao(cls, db: AsyncSession, config: ConfigModel):
|
||||
"""
|
||||
删除参数配置数据库操作
|
||||
:param db: orm对象
|
||||
:param config: 参数配置对象
|
||||
:return:
|
||||
"""
|
||||
db.query(SysConfig) \
|
||||
.filter(SysConfig.config_id == config.config_id) \
|
||||
.delete()
|
||||
await db.execute(
|
||||
delete(SysConfig)
|
||||
.where(SysConfig.config_id.in_([config.config_id]))
|
||||
)
|
||||
|
Reference in New Issue
Block a user