controller.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. from typing import Annotated, Optional
  2. from fastapi import APIRouter, Depends, Path, Query
  3. from fastapi.responses import JSONResponse
  4. from app.api.v1.module_system.auth.schema import AuthSchema
  5. from app.common.response import ResponseSchema, SuccessResponse
  6. from app.core.dependencies import AuthPermission, get_current_user
  7. from app.core.exceptions import CustomException
  8. from app.core.logger import log
  9. from app.core.router_class import OperationLogRoute
  10. from .crud import EmployeeCRUD
  11. from .schema import (
  12. EmployeeCreateOrUpdateSchema,
  13. EmployeeListOutSchema,
  14. EmployeeOperationOutSchema,
  15. EmployeeInviteQuerySchema,
  16. EmployeeInviteQueryOutSchema,
  17. )
  18. from .service import EmployeeService
  19. EmployeeRouter = APIRouter(
  20. route_class=OperationLogRoute,
  21. prefix="/employee",
  22. tags=["员工管理"],
  23. )
  24. @EmployeeRouter.get(
  25. "/info",
  26. summary="查询员工详情",
  27. description="查询员工详情",
  28. )
  29. async def info_employee_controller(
  30. auth: Annotated[AuthSchema, Depends(get_current_user)],
  31. enterprise_id: Annotated[Optional[str], Query(description="企业ID")] = None,
  32. employee_id: Annotated[Optional[str], Query(description="员工ID")] = None,
  33. employee_email: Annotated[Optional[str], Query(description="员工邮箱")] = None,
  34. employee_mobile: Annotated[Optional[str], Query(description="员工手机号")] = None,
  35. ) -> JSONResponse:
  36. """查询员工详情"""
  37. # 如果没传 enterprise_id,从当前用户推断
  38. if not enterprise_id:
  39. if not employee_mobile and auth.user and auth.user.mobile:
  40. employee_mobile = auth.user.mobile
  41. if employee_mobile:
  42. crud_cache = EmployeeCRUD(auth)
  43. employee = await crud_cache.get(employee_mobile=employee_mobile)
  44. if employee:
  45. enterprise_id = employee.enterprise_id
  46. if not enterprise_id:
  47. raise CustomException(msg="缺少企业ID参数")
  48. result = await EmployeeService.info_service(
  49. auth=auth,
  50. employee_id=employee_id,
  51. employee_email=employee_email,
  52. employee_mobile=employee_mobile,
  53. enterprise_id=enterprise_id
  54. )
  55. return SuccessResponse(data=result, msg="查询员工详情成功")
  56. @EmployeeRouter.post(
  57. "",
  58. summary="添加员工",
  59. description="添加员工 (alipay.commerce.ec.employee.add)",
  60. response_model=ResponseSchema[EmployeeOperationOutSchema],
  61. )
  62. async def add_employee_controller(
  63. data: EmployeeCreateOrUpdateSchema,
  64. auth: Annotated[AuthSchema, Depends(AuthPermission(["module_payment:employee:create"]))],
  65. ) -> JSONResponse:
  66. """添加员工"""
  67. result = await EmployeeService.add_employee_service(auth=auth, data=data)
  68. log.info(f"添加员工成功: {data.employee_name}, employee_id={result.employee_id}")
  69. return SuccessResponse(data=result, msg="添加员工成功")
  70. @EmployeeRouter.get(
  71. "",
  72. summary="查询员工列表",
  73. description="分页查询员工列表",
  74. response_model=ResponseSchema[EmployeeListOutSchema],
  75. )
  76. async def list_employee_controller(
  77. auth: Annotated[AuthSchema, Depends(AuthPermission(["module_payment:employee:list"]))],
  78. page_no: Annotated[int, Query(description="页码")] = 1,
  79. page_size: Annotated[int, Query(description="每页数量")] = 20,
  80. enterprise_id: Annotated[str | None, Query(description="企业ID")] = None,
  81. employee_name: Annotated[str | None, Query(description="员工姓名")] = None,
  82. employee_no: Annotated[str | None, Query(description="员工工号")] = None,
  83. status: Annotated[str | None, Query(description="状态")] = None,
  84. ) -> JSONResponse:
  85. """查询员工列表"""
  86. search = {}
  87. if enterprise_id:
  88. search["enterprise_id"] = enterprise_id
  89. if employee_name:
  90. search["employee_name"] = employee_name
  91. if employee_no:
  92. search["employee_no"] = employee_no
  93. if status:
  94. search["status"] = status
  95. result = await EmployeeService.list_service(
  96. auth=auth, page_no=page_no, page_size=page_size, search=search
  97. )
  98. return SuccessResponse(data=result, msg="查询员工列表成功")
  99. @EmployeeRouter.get(
  100. "/detail",
  101. summary="查询员工详情",
  102. description="根据 employee_id 查询员工详情",
  103. )
  104. async def get_detail_controller(
  105. enterprise_id: Annotated[str, Query(description="企业ID")],
  106. auth: Annotated[AuthSchema, Depends(AuthPermission(["module_payment:employee:detail"]))],
  107. employee_id: Annotated[Optional[str], Query(description="员工ID")] = None,
  108. employee_email: Annotated[Optional[str], Query(description="员工邮箱")] = None,
  109. employee_mobile: Annotated[Optional[str], Query(description="员工手机号")] = None,
  110. ) -> JSONResponse:
  111. """查询员工详情"""
  112. result = await EmployeeService.detail_service(auth=auth, employee_id=employee_id, employee_email=employee_email, employee_mobile=employee_mobile, enterprise_id=enterprise_id)
  113. log.info(f"查询员工详情成功: {employee_id}, enterprise_id={enterprise_id}")
  114. return SuccessResponse(data=result, msg="查询员工详情成功")
  115. @EmployeeRouter.delete(
  116. "/{employee_id}",
  117. summary="删除员工",
  118. description="删除员工 (alipay.commerce.ec.employee.delete)",
  119. response_model=ResponseSchema[EmployeeOperationOutSchema],
  120. )
  121. async def delete_employee_controller(
  122. employee_id: Annotated[str, Path(description="员工ID")],
  123. enterprise_id: Annotated[str, Query(description="企业ID")],
  124. auth: Annotated[AuthSchema, Depends(AuthPermission(["module_payment:employee:delete"]))],
  125. ) -> JSONResponse:
  126. """删除员工"""
  127. result = await EmployeeService.delete_employee_service(
  128. auth=auth, employee_id=employee_id, enterprise_id=enterprise_id
  129. )
  130. log.info(f"删除员工成功: {employee_id}, enterprise_id={enterprise_id}")
  131. return SuccessResponse(data=result, msg="删除员工成功")
  132. @EmployeeRouter.post(
  133. "/invite/query",
  134. summary="获取员工签约激活链接",
  135. description="获取员工签约激活链接 (alipay.commerce.ec.employee.invite.query)",
  136. response_model=ResponseSchema[EmployeeInviteQueryOutSchema],
  137. )
  138. async def invite_query_controller(
  139. data: EmployeeInviteQuerySchema,
  140. auth: Annotated[AuthSchema, Depends(AuthPermission(["module_payment:employee:invite"]))],
  141. ) -> JSONResponse:
  142. """获取员工签约激活链接"""
  143. result = await EmployeeService.invite_query_service(auth=auth, data=data)
  144. log.info(f"获取员工签约激活链接成功: enterprise_id={data.enterprise_id}")
  145. return SuccessResponse(data=result, msg="获取员工签约激活链接成功")