service.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. from app.api.v1.module_system.auth.schema import AuthSchema
  2. from app.core.base_schema import BatchSetAvailable
  3. from app.core.exceptions import CustomException
  4. from app.utils.excel_util import ExcelUtil
  5. from .crud import PositionCRUD
  6. from .schema import (
  7. PositionCreateSchema,
  8. PositionOutSchema,
  9. PositionQueryParam,
  10. PositionUpdateSchema,
  11. )
  12. class PositionService:
  13. """岗位模块服务层"""
  14. @classmethod
  15. async def get_position_detail_service(cls, auth: AuthSchema, id: int) -> dict:
  16. """
  17. 获取岗位详情
  18. 参数:
  19. - auth (AuthSchema): 认证信息模型
  20. - id (int): 岗位ID
  21. 返回:
  22. - Dict: 岗位详情对象
  23. """
  24. position = await PositionCRUD(auth).get_by_id_crud(id=id)
  25. return PositionOutSchema.model_validate(position).model_dump()
  26. @classmethod
  27. async def get_position_list_service(
  28. cls,
  29. auth: AuthSchema,
  30. search: PositionQueryParam | None = None,
  31. order_by: list[dict] | None = None,
  32. ) -> list[dict]:
  33. """
  34. 获取岗位列表
  35. 参数:
  36. - auth (AuthSchema): 认证信息模型
  37. - search (PositionQueryParam | None): 查询参数对象
  38. - order_by (list[dict] | None): 排序参数列表
  39. 返回:
  40. - list[dict]: 岗位列表对象
  41. """
  42. position_list = await PositionCRUD(auth).get_list_crud(
  43. search=search.__dict__, order_by=order_by
  44. )
  45. return [
  46. PositionOutSchema.model_validate(position).model_dump() for position in position_list
  47. ]
  48. @classmethod
  49. async def get_position_page_service(
  50. cls,
  51. auth: AuthSchema,
  52. page_no: int,
  53. page_size: int,
  54. search: PositionQueryParam | None = None,
  55. order_by: list[dict[str, str]] | None = None,
  56. ) -> dict:
  57. """
  58. 分页查询岗位(数据库 OFFSET/LIMIT)。
  59. 参数:
  60. - auth (AuthSchema): 认证信息模型
  61. - page_no (int): 页码(从 1 开始)
  62. - page_size (int): 每页条数
  63. - search (PositionQueryParam | None): 查询条件
  64. - order_by (list[dict[str, str]] | None): 排序字段列表
  65. 返回:
  66. - dict: 分页结果(结构由 `CRUD.page` 返回约定)
  67. """
  68. offset = (page_no - 1) * page_size
  69. return await PositionCRUD(auth).page(
  70. offset=offset,
  71. limit=page_size,
  72. order_by=order_by or [{"id": "asc"}],
  73. search=search.__dict__ if search else {},
  74. out_schema=PositionOutSchema,
  75. )
  76. @classmethod
  77. async def create_position_service(cls, auth: AuthSchema, data: PositionCreateSchema) -> dict:
  78. """
  79. 创建岗位
  80. 参数:
  81. - auth (AuthSchema): 认证信息模型
  82. - data (PositionCreateSchema): 岗位创建模型
  83. 返回:
  84. - dict: 创建的岗位详情字典
  85. """
  86. position = await PositionCRUD(auth).get(name=data.name)
  87. if position:
  88. raise CustomException(msg="创建失败,该岗位已存在")
  89. new_position = await PositionCRUD(auth).create(data=data)
  90. return PositionOutSchema.model_validate(new_position).model_dump()
  91. @classmethod
  92. async def update_position_service(
  93. cls, auth: AuthSchema, id: int, data: PositionUpdateSchema
  94. ) -> dict:
  95. """
  96. 更新岗位
  97. 参数:
  98. - auth (AuthSchema): 认证信息模型
  99. - id (int): 岗位ID
  100. - data (PositionUpdateSchema): 岗位更新模型
  101. 返回:
  102. - dict: 更新的岗位对象
  103. """
  104. position = await PositionCRUD(auth).get_by_id_crud(id=id)
  105. if not position:
  106. raise CustomException(msg="更新失败,该岗位不存在")
  107. exist_position = await PositionCRUD(auth).get(name=data.name)
  108. if exist_position and exist_position.id != id:
  109. raise CustomException(msg="更新失败,岗位名称重复")
  110. updated_position = await PositionCRUD(auth).update(id=id, data=data)
  111. return PositionOutSchema.model_validate(updated_position).model_dump()
  112. @classmethod
  113. async def delete_position_service(cls, auth: AuthSchema, ids: list[int]) -> None:
  114. """
  115. 删除岗位
  116. 参数:
  117. - auth (AuthSchema): 认证信息模型
  118. - ids (list[int]): 岗位ID列表
  119. 返回:
  120. - None
  121. """
  122. if len(ids) < 1:
  123. raise CustomException(msg="删除失败,删除对象不能为空")
  124. for id in ids:
  125. position = await PositionCRUD(auth).get_by_id_crud(id=id)
  126. if not position:
  127. raise CustomException(msg="删除失败,该岗位不存在")
  128. await PositionCRUD(auth).delete(ids=ids)
  129. @classmethod
  130. async def set_position_available_service(
  131. cls, auth: AuthSchema, data: BatchSetAvailable
  132. ) -> None:
  133. """
  134. 设置岗位状态
  135. 参数:
  136. - auth (AuthSchema): 认证信息模型
  137. - data (BatchSetAvailable): 批量设置状态模型
  138. 返回:
  139. - None
  140. """
  141. await PositionCRUD(auth).set_available_crud(ids=data.ids, status=data.status)
  142. @classmethod
  143. async def export_position_list_service(cls, position_list: list[dict]) -> bytes:
  144. """
  145. 导出岗位列表
  146. 参数:
  147. - position_list (list[dict]): 岗位列表对象
  148. 返回:
  149. - bytes: 导出的Excel文件字节流
  150. """
  151. mapping_dict = {
  152. "id": "编号",
  153. "name": "岗位名称",
  154. "order": "显示顺序",
  155. "status": "状态",
  156. "description": "备注",
  157. "created_time": "创建时间",
  158. "updated_time": "更新时间",
  159. "created_id": "创建者ID",
  160. "updated_id": "更新者ID",
  161. }
  162. # 复制数据并转换状态
  163. data = position_list.copy()
  164. for item in data:
  165. item["status"] = "启用" if item.get("status") == "0" else "停用"
  166. item["creator"] = (
  167. item.get("creator", {}).get("name", "未知")
  168. if isinstance(item.get("creator"), dict)
  169. else "未知"
  170. )
  171. return ExcelUtil.export_list2excel(list_data=data, mapping_dict=mapping_dict)