perf: 优化部门管理模块service层及异常处理
This commit is contained in:
@@ -1,9 +1,10 @@
|
||||
from sqlalchemy import select, update, delete, or_, func
|
||||
from sqlalchemy import select, update, delete, or_, func, bindparam
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
from sqlalchemy.util import immutabledict
|
||||
from module_admin.entity.do.dept_do import SysDept
|
||||
from module_admin.entity.do.user_do import SysUser
|
||||
from module_admin.entity.do.role_do import SysRoleDept
|
||||
from module_admin.entity.vo.dept_vo import *
|
||||
from utils.time_format_util import list_format_datetime
|
||||
|
||||
|
||||
class DeptDao:
|
||||
@@ -21,25 +22,7 @@ class DeptDao:
|
||||
"""
|
||||
dept_info = (await db.execute(
|
||||
select(SysDept)
|
||||
.where(SysDept.dept_id == dept_id,
|
||||
SysDept.status == '0',
|
||||
SysDept.del_flag == '0')
|
||||
)).scalars().first()
|
||||
|
||||
return dept_info
|
||||
|
||||
@classmethod
|
||||
async def get_dept_by_id_for_list(cls, db: AsyncSession, dept_id: int):
|
||||
"""
|
||||
用于获取部门列表的工具方法
|
||||
:param db: orm对象
|
||||
:param dept_id: 部门id
|
||||
:return: 部门id对应的信息对象
|
||||
"""
|
||||
dept_info = (await db.execute(
|
||||
select(SysDept)
|
||||
.where(SysDept.dept_id == dept_id,
|
||||
SysDept.del_flag == '0')
|
||||
.where(SysDept.dept_id == dept_id)
|
||||
)).scalars().first()
|
||||
|
||||
return dept_info
|
||||
@@ -98,7 +81,7 @@ class DeptDao:
|
||||
return dept_result
|
||||
|
||||
@classmethod
|
||||
async def get_children_dept(cls, db: AsyncSession, dept_id: int):
|
||||
async def get_children_dept_dao(cls, db: AsyncSession, dept_id: int):
|
||||
"""
|
||||
根据部门id查询当前部门的子部门列表信息
|
||||
:param db: orm对象
|
||||
@@ -107,25 +90,10 @@ class DeptDao:
|
||||
"""
|
||||
dept_result = (await db.execute(
|
||||
select(SysDept)
|
||||
.where(SysDept.parent_id == dept_id,
|
||||
SysDept.del_flag == '0')
|
||||
.where(func.find_in_set(dept_id, SysDept.ancestors))
|
||||
)).scalars().all()
|
||||
|
||||
return list_format_datetime(dept_result)
|
||||
|
||||
@classmethod
|
||||
async def get_dept_all_ancestors(cls, db: AsyncSession):
|
||||
"""
|
||||
获取所有部门的ancestors信息
|
||||
:param db: orm对象
|
||||
:return: ancestors信息列表
|
||||
"""
|
||||
ancestors = (await db.execute(
|
||||
select(SysDept.ancestors)
|
||||
.where(SysDept.del_flag == '0')
|
||||
)).scalars().all()
|
||||
|
||||
return ancestors
|
||||
return dept_result
|
||||
|
||||
@classmethod
|
||||
async def get_dept_list_for_tree(cls, db: AsyncSession, dept_info: DeptModel, data_scope_sql: str):
|
||||
@@ -196,6 +164,43 @@ class DeptDao:
|
||||
[dept]
|
||||
)
|
||||
|
||||
@classmethod
|
||||
async def update_dept_children_dao(cls, db: AsyncSession, update_dept: List):
|
||||
"""
|
||||
更新子部门信息
|
||||
:param db: orm对象
|
||||
:param update_dept: 需要更新的部门列表
|
||||
:return:
|
||||
"""
|
||||
await db.execute(
|
||||
update(SysDept)
|
||||
.where(SysDept.dept_id == bindparam('dept_id'))
|
||||
.values(
|
||||
{
|
||||
'dept_id': bindparam('dept_id'),
|
||||
'ancestors': bindparam('ancestors'),
|
||||
}
|
||||
),
|
||||
update_dept,
|
||||
execution_options=immutabledict(
|
||||
{"synchronize_session": None}
|
||||
)
|
||||
)
|
||||
|
||||
@classmethod
|
||||
async def update_dept_status_normal_dao(cls, db: AsyncSession, dept_id_list: List):
|
||||
"""
|
||||
批量更新部门状态为正常
|
||||
:param db: orm对象
|
||||
:param dept_id_list: 部门id列表
|
||||
:return:
|
||||
"""
|
||||
await db.execute(
|
||||
update(SysDept)
|
||||
.where(SysDept.dept_id.in_(dept_id_list))
|
||||
.values(status='0')
|
||||
)
|
||||
|
||||
@classmethod
|
||||
async def delete_dept_dao(cls, db: AsyncSession, dept: DeptModel):
|
||||
"""
|
||||
@@ -209,3 +214,57 @@ class DeptDao:
|
||||
.where(SysDept.dept_id == dept.dept_id)
|
||||
.values(del_flag='2', update_by=dept.update_by, update_time=dept.update_time)
|
||||
)
|
||||
|
||||
@classmethod
|
||||
async def count_normal_children_dept_dao(cls, db: AsyncSession, dept_id: int):
|
||||
"""
|
||||
根据部门id查询查询所有子部门(正常状态)的数量
|
||||
:param db: orm对象
|
||||
:param dept_id: 部门id
|
||||
:return: 所有子部门(正常状态)的数量
|
||||
"""
|
||||
normal_children_dept_count = (await db.execute(
|
||||
select(func.count('*'))
|
||||
.select_from(SysDept)
|
||||
.where(
|
||||
SysDept.status == '0',
|
||||
SysDept.del_flag == '0',
|
||||
func.find_in_set(dept_id, SysDept.ancestors)
|
||||
)
|
||||
)).scalar()
|
||||
|
||||
return normal_children_dept_count
|
||||
|
||||
@classmethod
|
||||
async def count_children_dept_dao(cls, db: AsyncSession, dept_id: int):
|
||||
"""
|
||||
根据部门id查询查询所有子部门(所有状态)的数量
|
||||
:param db: orm对象
|
||||
:param dept_id: 部门id
|
||||
:return: 所有子部门(所有状态)的数量
|
||||
"""
|
||||
children_dept_count = (await db.execute(
|
||||
select(func.count('*'))
|
||||
.select_from(SysDept)
|
||||
.where(SysDept.del_flag == '0',
|
||||
SysDept.parent_id == dept_id)
|
||||
.limit(1)
|
||||
)).scalar()
|
||||
|
||||
return children_dept_count
|
||||
|
||||
@classmethod
|
||||
async def count_dept_user_dao(cls, db: AsyncSession, dept_id: int):
|
||||
"""
|
||||
根据部门id查询查询部门下的用户数量
|
||||
:param db: orm对象
|
||||
:param dept_id: 部门id
|
||||
:return: 部门下的用户数量
|
||||
"""
|
||||
dept_user_count = (await db.execute(
|
||||
select(func.count('*'))
|
||||
.select_from(SysUser)
|
||||
.where(SysUser.dept_id == dept_id, SysUser.del_flag == '0')
|
||||
)).scalar()
|
||||
|
||||
return dept_user_count
|
||||
|
Reference in New Issue
Block a user