controller.py 7.1 KB

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