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.
		
		
		
		
			
				
					276 lines
				
				11 KiB
			
		
		
			
		
	
	
					276 lines
				
				11 KiB
			| 
											1 year ago
										 | from sqlalchemy.ext.asyncio import AsyncSession | ||
| 
											1 year ago
										 | from config.constant import CommonConstant | ||
| 
											1 year ago
										 | from exceptions.exception import ServiceException, ServiceWarning | ||
| 
											1 year ago
										 | from module_admin.dao.dept_dao import DeptDao | ||
|  | from module_admin.entity.vo.common_vo import CrudResponseModel | ||
|  | from module_admin.entity.vo.dept_vo import DeleteDeptModel, DeptModel | ||
| 
											2 years ago
										 | from utils.common_util import CamelCaseUtil | ||
|  | 
 | ||
|  | 
 | ||
|  | class DeptService: | ||
|  |     """
 | ||
|  |     部门管理模块服务层 | ||
|  |     """
 | ||
|  | 
 | ||
|  |     @classmethod | ||
| 
											1 year ago
										 |     async def get_dept_tree_services(cls, query_db: AsyncSession, page_object: DeptModel, data_scope_sql: str): | ||
| 
											2 years ago
										 |         """
 | ||
|  |         获取部门树信息service | ||
| 
											1 year ago
										 | 
 | ||
| 
											2 years ago
										 |         :param query_db: orm对象 | ||
|  |         :param page_object: 查询参数对象 | ||
|  |         :param data_scope_sql: 数据权限对应的查询sql语句 | ||
|  |         :return: 部门树信息对象 | ||
|  |         """
 | ||
| 
											1 year ago
										 |         dept_list_result = await DeptDao.get_dept_list_for_tree(query_db, page_object, data_scope_sql) | ||
| 
											2 years ago
										 |         dept_tree_result = cls.list_to_tree(dept_list_result) | ||
|  | 
 | ||
|  |         return dept_tree_result | ||
|  | 
 | ||
|  |     @classmethod | ||
| 
											1 year ago
										 |     async def get_dept_for_edit_option_services( | ||
|  |         cls, query_db: AsyncSession, page_object: DeptModel, data_scope_sql: str | ||
|  |     ): | ||
| 
											2 years ago
										 |         """
 | ||
|  |         获取部门编辑部门树信息service | ||
| 
											1 year ago
										 | 
 | ||
| 
											2 years ago
										 |         :param query_db: orm对象 | ||
|  |         :param page_object: 查询参数对象 | ||
|  |         :param data_scope_sql: 数据权限对应的查询sql语句 | ||
|  |         :return: 部门树信息对象 | ||
|  |         """
 | ||
| 
											1 year ago
										 |         dept_list_result = await DeptDao.get_dept_info_for_edit_option(query_db, page_object, data_scope_sql) | ||
| 
											2 years ago
										 | 
 | ||
|  |         return CamelCaseUtil.transform_result(dept_list_result) | ||
|  | 
 | ||
|  |     @classmethod | ||
| 
											1 year ago
										 |     async def get_dept_list_services(cls, query_db: AsyncSession, page_object: DeptModel, data_scope_sql: str): | ||
| 
											2 years ago
										 |         """
 | ||
|  |         获取部门列表信息service | ||
| 
											1 year ago
										 | 
 | ||
| 
											2 years ago
										 |         :param query_db: orm对象 | ||
|  |         :param page_object: 分页查询参数对象 | ||
|  |         :param data_scope_sql: 数据权限对应的查询sql语句 | ||
|  |         :return: 部门列表信息对象 | ||
|  |         """
 | ||
| 
											1 year ago
										 |         dept_list_result = await DeptDao.get_dept_list(query_db, page_object, data_scope_sql) | ||
| 
											2 years ago
										 | 
 | ||
|  |         return CamelCaseUtil.transform_result(dept_list_result) | ||
|  | 
 | ||
| 
											1 year ago
										 |     @classmethod | ||
|  |     async def check_dept_data_scope_services(cls, query_db: AsyncSession, dept_id: int, data_scope_sql: str): | ||
|  |         """
 | ||
|  |         校验部门是否有数据权限service | ||
| 
											1 year ago
										 | 
 | ||
| 
											1 year ago
										 |         :param query_db: orm对象 | ||
|  |         :param dept_id: 部门id | ||
|  |         :param data_scope_sql: 数据权限对应的查询sql语句 | ||
|  |         :return: 校验结果 | ||
|  |         """
 | ||
|  |         depts = await DeptDao.get_dept_list(query_db, DeptModel(deptId=dept_id), data_scope_sql) | ||
|  |         if depts: | ||
|  |             return CrudResponseModel(is_success=True, message='校验通过') | ||
|  |         else: | ||
|  |             raise ServiceException(message='没有权限访问部门数据') | ||
|  | 
 | ||
|  |     @classmethod | ||
|  |     async def check_dept_name_unique_services(cls, query_db: AsyncSession, page_object: DeptModel): | ||
|  |         """
 | ||
|  |         校验部门名称是否唯一service | ||
| 
											1 year ago
										 | 
 | ||
| 
											1 year ago
										 |         :param query_db: orm对象 | ||
|  |         :param page_object: 部门对象 | ||
|  |         :return: 校验结果 | ||
|  |         """
 | ||
|  |         dept_id = -1 if page_object.dept_id is None else page_object.dept_id | ||
| 
											1 year ago
										 |         dept = await DeptDao.get_dept_detail_by_info( | ||
|  |             query_db, DeptModel(deptName=page_object.dept_name, parentId=page_object.parent_id) | ||
|  |         ) | ||
| 
											1 year ago
										 |         if dept and dept.dept_id != dept_id: | ||
|  |             return CommonConstant.NOT_UNIQUE | ||
|  |         return CommonConstant.UNIQUE | ||
|  | 
 | ||
| 
											2 years ago
										 |     @classmethod | ||
| 
											1 year ago
										 |     async def add_dept_services(cls, query_db: AsyncSession, page_object: DeptModel): | ||
| 
											2 years ago
										 |         """
 | ||
|  |         新增部门信息service | ||
| 
											1 year ago
										 | 
 | ||
| 
											2 years ago
										 |         :param query_db: orm对象 | ||
|  |         :param page_object: 新增部门对象 | ||
|  |         :return: 新增部门校验结果 | ||
|  |         """
 | ||
| 
											1 year ago
										 |         if not await cls.check_dept_name_unique_services(query_db, page_object): | ||
|  |             raise ServiceException(message=f'新增部门{page_object.dept_name}失败,部门名称已存在') | ||
| 
											1 year ago
										 |         parent_info = await DeptDao.get_dept_by_id(query_db, page_object.parent_id) | ||
| 
											1 year ago
										 |         if parent_info.status != CommonConstant.DEPT_NORMAL: | ||
|  |             raise ServiceException(message=f'部门{parent_info.dept_name}停用,不允许新增') | ||
|  |         page_object.ancestors = f'{parent_info.ancestors},{page_object.parent_id}' | ||
|  |         try: | ||
|  |             await DeptDao.add_dept_dao(query_db, page_object) | ||
|  |             await query_db.commit() | ||
|  |             return CrudResponseModel(is_success=True, message='新增成功') | ||
|  |         except Exception as e: | ||
|  |             await query_db.rollback() | ||
|  |             raise e | ||
| 
											2 years ago
										 | 
 | ||
|  |     @classmethod | ||
| 
											1 year ago
										 |     async def edit_dept_services(cls, query_db: AsyncSession, page_object: DeptModel): | ||
| 
											2 years ago
										 |         """
 | ||
|  |         编辑部门信息service | ||
| 
											1 year ago
										 | 
 | ||
| 
											2 years ago
										 |         :param query_db: orm对象 | ||
|  |         :param page_object: 编辑部门对象 | ||
|  |         :return: 编辑部门校验结果 | ||
|  |         """
 | ||
| 
											1 year ago
										 |         if not await cls.check_dept_name_unique_services(query_db, page_object): | ||
|  |             raise ServiceException(message=f'修改部门{page_object.dept_name}失败,部门名称已存在') | ||
|  |         elif page_object.dept_id == page_object.parent_id: | ||
|  |             raise ServiceException(message=f'修改部门{page_object.dept_name}失败,上级部门不能是自己') | ||
| 
											1 year ago
										 |         elif ( | ||
|  |             page_object.status == CommonConstant.DEPT_DISABLE | ||
|  |             and (await DeptDao.count_normal_children_dept_dao(query_db, page_object.dept_id)) > 0 | ||
|  |         ): | ||
| 
											1 year ago
										 |             raise ServiceException(message=f'修改部门{page_object.dept_name}失败,该部门包含未停用的子部门') | ||
|  |         new_parent_dept = await DeptDao.get_dept_by_id(query_db, page_object.parent_id) | ||
|  |         old_dept = await DeptDao.get_dept_by_id(query_db, page_object.dept_id) | ||
|  |         try: | ||
|  |             if new_parent_dept and old_dept: | ||
|  |                 new_ancestors = f'{new_parent_dept.ancestors},{new_parent_dept.dept_id}' | ||
|  |                 old_ancestors = old_dept.ancestors | ||
|  |                 page_object.ancestors = new_ancestors | ||
|  |                 await cls.update_dept_children(query_db, page_object.dept_id, new_ancestors, old_ancestors) | ||
|  |             edit_dept = page_object.model_dump(exclude_unset=True) | ||
|  |             await DeptDao.edit_dept_dao(query_db, edit_dept) | ||
| 
											1 year ago
										 |             if ( | ||
|  |                 page_object.status == CommonConstant.DEPT_NORMAL | ||
|  |                 and page_object.ancestors | ||
|  |                 and page_object.ancestors != 0 | ||
|  |             ): | ||
| 
											1 year ago
										 |                 await cls.update_parent_dept_status_normal(query_db, page_object) | ||
|  |             await query_db.commit() | ||
|  |             return CrudResponseModel(is_success=True, message='更新成功') | ||
|  |         except Exception as e: | ||
|  |             await query_db.rollback() | ||
|  |             raise e | ||
| 
											2 years ago
										 | 
 | ||
|  |     @classmethod | ||
| 
											1 year ago
										 |     async def delete_dept_services(cls, query_db: AsyncSession, page_object: DeleteDeptModel): | ||
| 
											2 years ago
										 |         """
 | ||
|  |         删除部门信息service | ||
| 
											1 year ago
										 | 
 | ||
| 
											2 years ago
										 |         :param query_db: orm对象 | ||
|  |         :param page_object: 删除部门对象 | ||
|  |         :return: 删除部门校验结果 | ||
|  |         """
 | ||
| 
											1 year ago
										 |         if page_object.dept_ids: | ||
| 
											2 years ago
										 |             dept_id_list = page_object.dept_ids.split(',') | ||
|  |             try: | ||
|  |                 for dept_id in dept_id_list: | ||
| 
											1 year ago
										 |                     if (await DeptDao.count_children_dept_dao(query_db, int(dept_id))) > 0: | ||
| 
											1 year ago
										 |                         raise ServiceWarning(message='存在下级部门,不允许删除') | ||
| 
											1 year ago
										 |                     elif (await DeptDao.count_dept_user_dao(query_db, int(dept_id))) > 0: | ||
| 
											1 year ago
										 |                         raise ServiceWarning(message='部门存在用户,不允许删除') | ||
| 
											2 years ago
										 | 
 | ||
| 
											1 year ago
										 |                     await DeptDao.delete_dept_dao(query_db, DeptModel(deptId=dept_id)) | ||
|  |                 await query_db.commit() | ||
| 
											1 year ago
										 |                 return CrudResponseModel(is_success=True, message='删除成功') | ||
| 
											2 years ago
										 |             except Exception as e: | ||
| 
											1 year ago
										 |                 await query_db.rollback() | ||
| 
											2 years ago
										 |                 raise e | ||
|  |         else: | ||
| 
											1 year ago
										 |             raise ServiceException(message='传入部门id为空') | ||
| 
											2 years ago
										 | 
 | ||
|  |     @classmethod | ||
| 
											1 year ago
										 |     async def dept_detail_services(cls, query_db: AsyncSession, dept_id: int): | ||
| 
											2 years ago
										 |         """
 | ||
|  |         获取部门详细信息service | ||
| 
											1 year ago
										 | 
 | ||
| 
											2 years ago
										 |         :param query_db: orm对象 | ||
|  |         :param dept_id: 部门id | ||
|  |         :return: 部门id对应的信息 | ||
|  |         """
 | ||
| 
											1 year ago
										 |         dept = await DeptDao.get_dept_detail_by_id(query_db, dept_id=dept_id) | ||
| 
											1 year ago
										 |         if dept: | ||
|  |             result = DeptModel(**CamelCaseUtil.transform_result(dept)) | ||
|  |         else: | ||
|  |             result = DeptModel(**dict()) | ||
| 
											2 years ago
										 | 
 | ||
|  |         return result | ||
|  | 
 | ||
|  |     @classmethod | ||
|  |     def list_to_tree(cls, permission_list: list) -> list: | ||
|  |         """
 | ||
|  |         工具方法:根据部门列表信息生成树形嵌套数据 | ||
| 
											1 year ago
										 | 
 | ||
| 
											2 years ago
										 |         :param permission_list: 部门列表信息 | ||
|  |         :return: 部门树形嵌套数据 | ||
|  |         """
 | ||
| 
											1 year ago
										 |         permission_list = [ | ||
|  |             dict(id=item.dept_id, label=item.dept_name, parentId=item.parent_id) for item in permission_list | ||
|  |         ] | ||
| 
											2 years ago
										 |         # 转成id为key的字典 | ||
|  |         mapping: dict = dict(zip([i['id'] for i in permission_list], permission_list)) | ||
|  | 
 | ||
|  |         # 树容器 | ||
|  |         container: list = [] | ||
|  | 
 | ||
|  |         for d in permission_list: | ||
|  |             # 如果找不到父级项,则是根节点 | ||
|  |             parent: dict = mapping.get(d['parentId']) | ||
|  |             if parent is None: | ||
|  |                 container.append(d) | ||
|  |             else: | ||
|  |                 children: list = parent.get('children') | ||
|  |                 if not children: | ||
|  |                     children = [] | ||
|  |                 children.append(d) | ||
|  |                 parent.update({'children': children}) | ||
|  | 
 | ||
|  |         return container | ||
|  | 
 | ||
|  |     @classmethod | ||
| 
											1 year ago
										 |     async def replace_first(cls, original_str: str, old_str: str, new_str: str): | ||
|  |         """
 | ||
|  |         工具方法:替换字符串 | ||
| 
											1 year ago
										 | 
 | ||
| 
											1 year ago
										 |         :param original_str: 需要替换的原始字符串 | ||
|  |         :param old_str: 用于匹配的字符串 | ||
|  |         :param new_str: 替换的字符串 | ||
|  |         :return: 替换后的字符串 | ||
| 
											2 years ago
										 |         """
 | ||
| 
											1 year ago
										 |         if original_str.startswith(old_str): | ||
|  |             return original_str.replace(old_str, new_str, 1) | ||
|  |         else: | ||
|  |             return original_str | ||
|  | 
 | ||
|  |     @classmethod | ||
|  |     async def update_parent_dept_status_normal(cls, query_db: AsyncSession, dept: DeptModel): | ||
|  |         """
 | ||
|  |         更新父部门状态为正常 | ||
| 
											1 year ago
										 | 
 | ||
| 
											2 years ago
										 |         :param query_db: orm对象 | ||
| 
											1 year ago
										 |         :param dept: 部门对象 | ||
|  |         :return: | ||
|  |         """
 | ||
|  |         dept_id_list = dept.ancestors.split(',') | ||
| 
											1 year ago
										 |         await DeptDao.update_dept_status_normal_dao(query_db, list(map(int, dept_id_list))) | ||
| 
											1 year ago
										 | 
 | ||
|  |     @classmethod | ||
|  |     async def update_dept_children(cls, query_db: AsyncSession, dept_id: int, new_ancestors: str, old_ancestors: str): | ||
|  |         """
 | ||
|  |         更新子部门信息 | ||
| 
											1 year ago
										 | 
 | ||
| 
											1 year ago
										 |         :param query_db: orm对象 | ||
|  |         :param dept_id: 部门id | ||
|  |         :param new_ancestors: 新的祖先 | ||
|  |         :param old_ancestors: 旧的祖先 | ||
| 
											2 years ago
										 |         :return: | ||
|  |         """
 | ||
| 
											1 year ago
										 |         children = await DeptDao.get_children_dept_dao(query_db, dept_id) | ||
|  |         update_children = [] | ||
|  |         for child in children: | ||
|  |             child_ancestors = await cls.replace_first(child.ancestors, old_ancestors, new_ancestors) | ||
|  |             update_children.append({'dept_id': child.dept_id, 'ancestors': child_ancestors}) | ||
|  |         if children: | ||
|  |             await DeptDao.update_dept_children_dao(query_db, update_children) |