service.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. # -*- coding: utf-8 -*-
  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 ..auth.schema import AuthSchema
  6. from .crud import PositionCRUD
  7. from .schema import (
  8. PositionCreateSchema,
  9. PositionUpdateSchema,
  10. PositionOutSchema,
  11. PositionQueryParam
  12. )
  13. class PositionService:
  14. """岗位模块服务层"""
  15. @classmethod
  16. async def get_position_detail_service(cls, auth: AuthSchema, id: int) -> dict:
  17. """
  18. 获取岗位详情
  19. 参数:
  20. - auth (AuthSchema): 认证信息模型
  21. - id (int): 岗位ID
  22. 返回:
  23. - Dict: 岗位详情对象
  24. """
  25. position = await PositionCRUD(auth).get_by_id_crud(id=id)
  26. return PositionOutSchema.model_validate(position).model_dump()
  27. @classmethod
  28. async def get_position_list_service(cls, auth: AuthSchema, search: PositionQueryParam | None = None, order_by: list[dict] | None = None) -> list[dict]:
  29. """
  30. 获取岗位列表
  31. 参数:
  32. - auth (AuthSchema): 认证信息模型
  33. - search (PositionQueryParam | None): 查询参数对象
  34. - order_by (list[dict] | None): 排序参数列表
  35. 返回:
  36. - list[dict]: 岗位列表对象
  37. """
  38. position_list = await PositionCRUD(auth).get_list_crud(search=search.__dict__, order_by=order_by)
  39. return [PositionOutSchema.model_validate(position).model_dump() for position in position_list]
  40. @classmethod
  41. async def create_position_service(cls, auth: AuthSchema, data: PositionCreateSchema) -> dict:
  42. """
  43. 创建岗位
  44. 参数:
  45. - auth (AuthSchema): 认证信息模型
  46. - data (PositionCreateSchema): 岗位创建模型
  47. 返回:
  48. - Dict: 创建的岗位对象
  49. """
  50. position = await PositionCRUD(auth).get(name=data.name)
  51. if position:
  52. raise CustomException(msg='创建失败,该岗位已存在')
  53. new_position = await PositionCRUD(auth).create(data=data)
  54. return PositionOutSchema.model_validate(new_position).model_dump()
  55. @classmethod
  56. async def update_position_service(cls, auth: AuthSchema, id:int, data: PositionUpdateSchema) -> dict:
  57. """
  58. 更新岗位
  59. 参数:
  60. - auth (AuthSchema): 认证信息模型
  61. - id (int): 岗位ID
  62. - data (PositionUpdateSchema): 岗位更新模型
  63. 返回:
  64. - dict: 更新的岗位对象
  65. """
  66. position = await PositionCRUD(auth).get_by_id_crud(id=id)
  67. if not position:
  68. raise CustomException(msg='更新失败,该岗位不存在')
  69. exist_position = await PositionCRUD(auth).get(name=data.name)
  70. if exist_position and exist_position.id != id:
  71. raise CustomException(msg='更新失败,岗位名称重复')
  72. updated_position = await PositionCRUD(auth).update(id=id, data=data)
  73. return PositionOutSchema.model_validate(updated_position).model_dump()
  74. @classmethod
  75. async def delete_position_service(cls, auth: AuthSchema, ids: list[int]) -> None:
  76. """
  77. 删除岗位
  78. 参数:
  79. - auth (AuthSchema): 认证信息模型
  80. - ids (list[int]): 岗位ID列表
  81. 返回:
  82. - None
  83. """
  84. if len(ids) < 1:
  85. raise CustomException(msg='删除失败,删除对象不能为空')
  86. for id in ids:
  87. position = await PositionCRUD(auth).get_by_id_crud(id=id)
  88. if not position:
  89. raise CustomException(msg='删除失败,该岗位不存在')
  90. await PositionCRUD(auth).delete(ids=ids)
  91. @classmethod
  92. async def set_position_available_service(cls, auth: AuthSchema, data: BatchSetAvailable) -> None:
  93. """
  94. 设置岗位状态
  95. 参数:
  96. - auth (AuthSchema): 认证信息模型
  97. - data (BatchSetAvailable): 批量设置状态模型
  98. 返回:
  99. - None
  100. """
  101. await PositionCRUD(auth).set_available_crud(ids=data.ids, status=data.status)
  102. @classmethod
  103. async def export_position_list_service(cls, position_list: list[dict]) -> bytes:
  104. """
  105. 导出岗位列表
  106. 参数:
  107. - position_list (list[dict]): 岗位列表对象
  108. 返回:
  109. - bytes: 导出的Excel文件字节流
  110. """
  111. mapping_dict = {
  112. 'id': '编号',
  113. 'name': '岗位名称',
  114. 'order': '显示顺序',
  115. 'status': '状态',
  116. 'description': '备注',
  117. 'created_time': '创建时间',
  118. 'updated_time': '更新时间',
  119. 'created_id': '创建者ID',
  120. 'updated_id': '更新者ID',
  121. }
  122. # 复制数据并转换状态
  123. data = position_list.copy()
  124. for item in data:
  125. item['status'] = '启用' if item.get('status') == '0' else '停用'
  126. item['creator'] = item.get('creator', {}).get('name', '未知') if isinstance(item.get('creator'), dict) else '未知'
  127. return ExcelUtil.export_list2excel(list_data=data, mapping_dict=mapping_dict)