scope_sync.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. """
  2. 费控制度成员联动同步工具
  3. 用于部门停用/员工解约时自动移除相关制度中的成员引用。
  4. """
  5. from app.api.v1.module_system.auth.schema import AuthSchema
  6. from app.core.alipay import AlipayClient
  7. from app.core.logger import log
  8. from app.plugin.module_payment.expense.institution.crud import InstitutionCRUD
  9. async def remove_department_from_institution_scopes(
  10. auth: AuthSchema,
  11. enterprise_id: str,
  12. department_id: str,
  13. ) -> None:
  14. """
  15. 当部门被停用时,扫描所有引用该部门的制度,移除该部门
  16. 此方法被 department/service.py 的停用方法调用
  17. """
  18. try:
  19. crud = InstitutionCRUD(auth)
  20. # 查所有该企业下的有效制度
  21. institutions = await crud.list(
  22. search={"enterprise_id": enterprise_id, "status__ne": "INSTITUTION_DELETE"},
  23. order_by=[{"id": "desc"}],
  24. )
  25. if not institutions:
  26. return
  27. for inst in institutions:
  28. inst_id = inst.institution_id
  29. if not inst_id:
  30. continue
  31. from .service import InstitutionScopeService
  32. await InstitutionScopeService.scope_modify_service(
  33. auth=auth,
  34. institution_id=inst_id,
  35. data={
  36. "enterprise_id": enterprise_id,
  37. "adapter_type": "EMPLOYEE_DEPARTMENT",
  38. "delete_owner_id_list": [department_id],
  39. },
  40. )
  41. log.info(f"已从制度 {inst_id} 中移除停用部门 {department_id}")
  42. except Exception as e:
  43. log.error(f"移除部门失败(不影响主体操作): {e}")
  44. async def remove_employee_from_institution_scopes(
  45. auth: AuthSchema,
  46. enterprise_id: str,
  47. employee_id: str,
  48. ) -> None:
  49. """
  50. 当员工被解约时,扫描所有按员工模式引用该员工的制度,移除该员工
  51. 此方法被 employee/service.py 的删除方法调用
  52. """
  53. try:
  54. crud = InstitutionCRUD(auth)
  55. institutions = await crud.list(
  56. search={"enterprise_id": enterprise_id, "status__ne": "INSTITUTION_DELETE"},
  57. order_by=[{"id": "desc"}],
  58. )
  59. if not institutions:
  60. return
  61. for inst in institutions:
  62. inst_id = inst.institution_id
  63. if not inst_id:
  64. continue
  65. from .service import InstitutionScopeService
  66. await InstitutionScopeService.scope_modify_service(
  67. auth=auth,
  68. institution_id=inst_id,
  69. data={
  70. "enterprise_id": enterprise_id,
  71. "adapter_type": "EMPLOYEE_SELECT",
  72. "delete_owner_id_list": [employee_id],
  73. },
  74. )
  75. log.info(f"已从制度 {inst_id} 中移除解约员工 {employee_id}")
  76. except Exception as e:
  77. log.error(f"移除员工失败(不影响主体操作): {e}")