service.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. # -*- coding: utf-8 -*-
  2. from typing import Any
  3. from app.core.base_schema import BatchSetAvailable
  4. from app.core.exceptions import CustomException
  5. from app.utils.excel_util import ExcelUtil
  6. from ..auth.schema import AuthSchema
  7. from .crud import RoleCRUD
  8. from .schema import (
  9. RoleCreateSchema,
  10. RoleUpdateSchema,
  11. RolePermissionSettingSchema,
  12. RoleOutSchema,
  13. RoleQueryParam
  14. )
  15. class RoleService:
  16. """角色模块服务层"""
  17. @classmethod
  18. async def get_role_detail_service(cls, auth: AuthSchema, id: int) -> dict:
  19. """
  20. 获取角色详情
  21. 参数:
  22. - auth (AuthSchema): 认证信息模型
  23. - id (int): 角色ID
  24. 返回:
  25. - dict: 角色详情字典
  26. """
  27. role = await RoleCRUD(auth).get_by_id_crud(id=id)
  28. return RoleOutSchema.model_validate(role).model_dump()
  29. @classmethod
  30. async def get_role_list_service(cls, auth: AuthSchema, search: RoleQueryParam | None = None, order_by: list[dict[str, str]] | None = None) -> list[dict]:
  31. """
  32. 获取角色列表
  33. 参数:
  34. - auth (AuthSchema): 认证信息模型
  35. - search (RoleQueryParam | None): 查询参数模型
  36. - order_by (list[dict[str, str]] | None): 排序参数列表
  37. 返回:
  38. - list[dict]: 角色详情字典列表
  39. """
  40. role_list = await RoleCRUD(auth).get_list_crud(search=search.__dict__, order_by=order_by)
  41. return [RoleOutSchema.model_validate(role).model_dump() for role in role_list]
  42. @classmethod
  43. async def create_role_service(cls, auth: AuthSchema, data: RoleCreateSchema) -> dict:
  44. """
  45. 创建角色
  46. 参数:
  47. - auth (AuthSchema): 认证信息模型
  48. - data (RoleCreateSchema): 创建角色模型
  49. 返回:
  50. - dict: 新创建的角色详情字典
  51. """
  52. role = await RoleCRUD(auth).get(name=data.name)
  53. if role:
  54. raise CustomException(msg='创建失败,该角色已存在')
  55. obj = await RoleCRUD(auth).get(code=data.code)
  56. if obj:
  57. raise CustomException(msg='创建失败,编码已存在')
  58. new_role = await RoleCRUD(auth).create(data=data)
  59. return RoleOutSchema.model_validate(new_role).model_dump()
  60. @classmethod
  61. async def update_role_service(cls, auth: AuthSchema, id: int, data: RoleUpdateSchema) -> dict:
  62. """
  63. 更新角色
  64. 参数:
  65. - auth (AuthSchema): 认证信息模型
  66. - id (int): 角色ID
  67. - data (RoleUpdateSchema): 更新角色模型
  68. 返回:
  69. - dict: 更新后的角色详情字典
  70. """
  71. role = await RoleCRUD(auth).get_by_id_crud(id=id)
  72. if not role:
  73. raise CustomException(msg='更新失败,该角色不存在')
  74. exist_role = await RoleCRUD(auth).get(name=data.name)
  75. if exist_role and exist_role.id != id:
  76. raise CustomException(msg='更新失败,角色名称重复')
  77. updated_role = await RoleCRUD(auth).update(id=id, data=data)
  78. return RoleOutSchema.model_validate(updated_role).model_dump()
  79. @classmethod
  80. async def delete_role_service(cls, auth: AuthSchema, ids: list[int]) -> None:
  81. """
  82. 删除角色
  83. 参数:
  84. - auth (AuthSchema): 认证信息模型
  85. - ids (list[int]): 角色ID列表
  86. 返回:
  87. - None
  88. """
  89. if len(ids) < 1:
  90. raise CustomException(msg='删除失败,删除对象不能为空')
  91. for id in ids:
  92. role = await RoleCRUD(auth).get_by_id_crud(id=id)
  93. if not role:
  94. raise CustomException(msg='删除失败,该角色不存在')
  95. await RoleCRUD(auth).delete(ids=ids)
  96. @classmethod
  97. async def set_role_permission_service(cls, auth: AuthSchema, data: RolePermissionSettingSchema) -> None:
  98. """
  99. 设置角色权限
  100. 参数:
  101. - auth (AuthSchema): 认证信息模型
  102. - data (RolePermissionSettingSchema): 角色权限设置模型
  103. 返回:
  104. - None
  105. """
  106. # 设置角色菜单权限
  107. await RoleCRUD(auth).set_role_menus_crud(role_ids=data.role_ids, menu_ids=data.menu_ids)
  108. # 设置数据权限范围
  109. await RoleCRUD(auth).set_role_data_scope_crud(role_ids=data.role_ids, data_scope=data.data_scope)
  110. # 设置自定义数据权限部门
  111. if data.data_scope == 5 and data.dept_ids:
  112. await RoleCRUD(auth).set_role_depts_crud(role_ids=data.role_ids, dept_ids=data.dept_ids)
  113. else:
  114. await RoleCRUD(auth).set_role_depts_crud(role_ids=data.role_ids, dept_ids=[])
  115. @classmethod
  116. async def set_role_available_service(cls, auth: AuthSchema, data: BatchSetAvailable) -> None:
  117. """
  118. 设置角色可用状态
  119. 参数:
  120. - auth (AuthSchema): 认证信息模型
  121. - data (BatchSetAvailable): 批量设置可用状态模型
  122. 返回:
  123. - None
  124. """
  125. await RoleCRUD(auth).set_available_crud(ids=data.ids, status=data.status)
  126. @classmethod
  127. async def export_role_list_service(cls, role_list: list[dict[str, Any]]) -> bytes:
  128. """
  129. 导出角色列表
  130. 参数:
  131. - role_list (list[dict[str, Any]]): 角色详情字典列表
  132. 返回:
  133. - bytes: Excel文件字节流
  134. """
  135. # 字段映射配置
  136. mapping_dict = {
  137. 'id': '角色编号',
  138. 'name': '角色名称',
  139. 'order': '显示顺序',
  140. 'data_scope': '数据权限',
  141. 'status': '状态',
  142. 'description': '备注',
  143. 'created_time': '创建时间',
  144. 'updated_time': '更新时间',
  145. 'created_id': '创建者ID',
  146. 'updated_id': '更新者ID',
  147. }
  148. # 数据权限映射
  149. data_scope_map = {
  150. 1: '仅本人数据权限',
  151. 2: '本部门数据权限',
  152. 3: '本部门及以下数据权限',
  153. 4: '全部数据权限',
  154. 5: '自定义数据权限'
  155. }
  156. # 处理数据
  157. data = role_list.copy()
  158. for item in data:
  159. item['status'] = '启用' if item.get('status') == '0' else '停用'
  160. item['data_scope'] = data_scope_map.get(item.get('data_scope', 1), '')
  161. item['creator'] = item.get('creator', {}).get('name', '未知') if isinstance(item.get('creator'), dict) else '未知'
  162. return ExcelUtil.export_list2excel(list_data=data, mapping_dict=mapping_dict)