controller.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. # 不触发额度联动:员工需完成签约激活后才享受费控制度
  70. # 由 webhook employee.activated 通知触发
  71. return SuccessResponse(data=result, msg="添加员工成功")
  72. @EmployeeRouter.get(
  73. "",
  74. summary="查询员工列表",
  75. description="分页查询员工列表",
  76. response_model=ResponseSchema[EmployeeListOutSchema],
  77. )
  78. async def list_employee_controller(
  79. auth: Annotated[AuthSchema, Depends(AuthPermission(["module_payment:employee:list"]))],
  80. page_no: Annotated[int, Query(description="页码")] = 1,
  81. page_size: Annotated[int, Query(description="每页数量")] = 20,
  82. enterprise_id: Annotated[str | None, Query(description="企业ID")] = None,
  83. employee_name: Annotated[str | None, Query(description="员工姓名")] = None,
  84. employee_no: Annotated[str | None, Query(description="员工工号")] = None,
  85. status: Annotated[str | None, Query(description="状态")] = None,
  86. ) -> JSONResponse:
  87. """查询员工列表"""
  88. search = {}
  89. if enterprise_id:
  90. search["enterprise_id"] = enterprise_id
  91. if employee_name:
  92. search["employee_name"] = employee_name
  93. if employee_no:
  94. search["employee_no"] = employee_no
  95. if status:
  96. search["status"] = status
  97. result = await EmployeeService.list_service(
  98. auth=auth, page_no=page_no, page_size=page_size, search=search
  99. )
  100. return SuccessResponse(data=result, msg="查询员工列表成功")
  101. @EmployeeRouter.get(
  102. "/detail",
  103. summary="查询员工详情",
  104. description="根据 employee_id 查询员工详情",
  105. )
  106. async def get_detail_controller(
  107. enterprise_id: Annotated[str, Query(description="企业ID")],
  108. auth: Annotated[AuthSchema, Depends(AuthPermission(["module_payment:employee:detail"]))],
  109. employee_id: Annotated[Optional[str], Query(description="员工ID")] = None,
  110. employee_email: Annotated[Optional[str], Query(description="员工邮箱")] = None,
  111. employee_mobile: Annotated[Optional[str], Query(description="员工手机号")] = None,
  112. ) -> JSONResponse:
  113. """查询员工详情"""
  114. result = await EmployeeService.detail_service(auth=auth, employee_id=employee_id, employee_email=employee_email, employee_mobile=employee_mobile, enterprise_id=enterprise_id)
  115. log.info(f"查询员工详情成功: {employee_id}, enterprise_id={enterprise_id}")
  116. return SuccessResponse(data=result, msg="查询员工详情成功")
  117. @EmployeeRouter.delete(
  118. "/{employee_id}",
  119. summary="删除员工",
  120. description="删除员工 (alipay.commerce.ec.employee.delete)",
  121. response_model=ResponseSchema[EmployeeOperationOutSchema],
  122. )
  123. async def delete_employee_controller(
  124. employee_id: Annotated[str, Path(description="员工ID")],
  125. enterprise_id: Annotated[str, Query(description="企业ID")],
  126. auth: Annotated[AuthSchema, Depends(AuthPermission(["module_payment:employee:delete"]))],
  127. ) -> JSONResponse:
  128. """删除员工"""
  129. result = await EmployeeService.delete_employee_service(
  130. auth=auth, employee_id=employee_id, enterprise_id=enterprise_id
  131. )
  132. log.info(f"删除员工成功: {employee_id}, enterprise_id={enterprise_id}")
  133. return SuccessResponse(data=result, msg="删除员工成功")
  134. @EmployeeRouter.post(
  135. "/invite/query",
  136. summary="获取员工签约激活链接",
  137. description="获取员工签约激活链接 (alipay.commerce.ec.employee.invite.query)",
  138. response_model=ResponseSchema[EmployeeInviteQueryOutSchema],
  139. )
  140. async def invite_query_controller(
  141. data: EmployeeInviteQuerySchema,
  142. auth: Annotated[AuthSchema, Depends(AuthPermission(["module_payment:employee:invite"]))],
  143. ) -> JSONResponse:
  144. """获取员工签约激活链接"""
  145. result = await EmployeeService.invite_query_service(auth=auth, data=data)
  146. log.info(f"获取员工签约激活链接成功: enterprise_id={data.enterprise_id}")
  147. return SuccessResponse(data=result, msg="获取员工签约激活链接成功")