| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- """
- 费控制度成员联动同步工具
- 用于部门停用/员工解约时自动移除相关制度中的成员引用。
- """
- from app.api.v1.module_system.auth.schema import AuthSchema
- from app.core.alipay import AlipayClient
- from app.core.logger import log
- from app.plugin.module_payment.expense.institution.crud import InstitutionCRUD
- async def remove_department_from_institution_scopes(
- auth: AuthSchema,
- enterprise_id: str,
- department_id: str,
- ) -> None:
- """
- 当部门被停用时,扫描所有引用该部门的制度,移除该部门
- 此方法被 department/service.py 的停用方法调用
- """
- try:
- crud = InstitutionCRUD(auth)
- # 查所有该企业下的有效制度
- institutions = await crud.list(
- search={"enterprise_id": enterprise_id, "status__ne": "INSTITUTION_DELETE"},
- order_by=[{"id": "desc"}],
- )
- if not institutions:
- return
- for inst in institutions:
- inst_id = inst.institution_id
- if not inst_id:
- continue
- from .service import InstitutionScopeService
- await InstitutionScopeService.scope_modify_service(
- auth=auth,
- institution_id=inst_id,
- data={
- "enterprise_id": enterprise_id,
- "adapter_type": "EMPLOYEE_DEPARTMENT",
- "delete_owner_id_list": [department_id],
- },
- )
- log.info(f"已从制度 {inst_id} 中移除停用部门 {department_id}")
- except Exception as e:
- log.error(f"移除部门失败(不影响主体操作): {e}")
- async def remove_employee_from_institution_scopes(
- auth: AuthSchema,
- enterprise_id: str,
- employee_id: str,
- ) -> None:
- """
- 当员工被解约时,扫描所有按员工模式引用该员工的制度,移除该员工
- 此方法被 employee/service.py 的删除方法调用
- """
- try:
- crud = InstitutionCRUD(auth)
- institutions = await crud.list(
- search={"enterprise_id": enterprise_id, "status__ne": "INSTITUTION_DELETE"},
- order_by=[{"id": "desc"}],
- )
- if not institutions:
- return
- for inst in institutions:
- inst_id = inst.institution_id
- if not inst_id:
- continue
- from .service import InstitutionScopeService
- await InstitutionScopeService.scope_modify_service(
- auth=auth,
- institution_id=inst_id,
- data={
- "enterprise_id": enterprise_id,
- "adapter_type": "EMPLOYEE_SELECT",
- "delete_owner_id_list": [employee_id],
- },
- )
- log.info(f"已从制度 {inst_id} 中移除解约员工 {employee_id}")
- except Exception as e:
- log.error(f"移除员工失败(不影响主体操作): {e}")
|