Prechádzať zdrojové kódy

fix: 成员范围修改后同步更新本地制度记录

alphah 2 týždňov pred
rodič
commit
e5a4f378aa

+ 35 - 0
backend/app/plugin/module_payment/expense/institution/controller.py

@@ -405,6 +405,41 @@ async def modify_scope_controller(
         institution_id=institution_id,
         data=data,
     )
+    # 成功后在本地同步 scope 数据
+    try:
+        from .crud import InstitutionCRUD
+        from .enums import InstitutionStatusEnum
+        crud = InstitutionCRUD(auth)
+        adapter_type = data.get("adapter_type", "EMPLOYEE_ALL")
+        scope_map = {"EMPLOYEE_ALL": "all", "EMPLOYEE_SELECT": "employee", "EMPLOYEE_DEPARTMENT": "department"}
+        applicable_scope = scope_map.get(adapter_type, "all")
+        update_data = {"applicable_scope": applicable_scope}
+        if adapter_type == "EMPLOYEE_DEPARTMENT":
+            add_ids = data.get("add_owner_id_list") or []
+            if add_ids:
+                update_data["department_id"] = str(add_ids[0])
+        await crud.update_by_institution_id(institution_id, update_data)
+
+        # 同步员工额度记录
+        from .service import InstitutionService
+        enterprise_id = data.get("enterprise_id", "")
+        if enterprise_id:
+            scope_info = {
+                "adapter_type": adapter_type,
+                "owner_type": "EMPLOYEE",
+                "add_owner_id_list": data.get("add_owner_id_list"),
+                "delete_owner_id_list": data.get("delete_owner_id_list"),
+            }
+            await InstitutionService._sync_modify_quotas_by_scope(
+                auth=auth,
+                institution_id=institution_id,
+                enterprise_id=enterprise_id,
+                scope_info=scope_info,
+                raw_data={},
+            )
+    except Exception as e:
+        log.warning(f"本地scope同步失败(不影响支付宝侧): {e}")
+
     log.info(f"设置制度成员成功: institution_id={institution_id}, adapter_type={data.get('adapter_type')}")
     return SuccessResponse(data=result, msg="设置成功")