Explorar o código

fix: 成员范围修改时计算新旧差异,同步额度联动(新增/移除员工)

alphah hai 2 semanas
pai
achega
dc67a268ca

+ 45 - 13
backend/app/plugin/module_payment/expense/institution/controller.py

@@ -400,35 +400,66 @@ async def modify_scope_controller(
     auth: Annotated[AuthSchema, Depends(AuthPermission(["module_payment:expense:institution:scope:modify"]))],
 ) -> JSONResponse:
     """设置制度成员"""
+    enterprise_id = data.get("enterprise_id", "")
+
+    # 计算新旧员工差异(用于额度联动)
+    add_ids: list[str] = []
+    delete_ids: list[str] = []
+    adapter_type = data.get("adapter_type", "EMPLOYEE_ALL")
+
+    if adapter_type in ("EMPLOYEE_SELECT", "EMPLOYEE_DEPARTMENT"):
+        new_ids_raw = data.get("add_owner_id_list") or []
+        new_ids = [str(i) for i in new_ids_raw if i is not None and str(i).strip()]
+        try:
+            scope_old = await InstitutionScopeService.scopepageinfo_query_service(
+                auth=auth, institution_id=institution_id, enterprise_id=enterprise_id,
+                page_num=1, page_size=500,
+            )
+            old_ids = [str(i) for i in (scope_old.get("owner_id_list") or []) if i]
+        except Exception:
+            old_ids = []
+            log.warning(f"查询旧scope失败,将全量覆盖: institution_id={institution_id}")
+
+        old_set, new_set = set(old_ids), set(new_ids)
+        add_ids = list(new_set - old_set)
+        delete_ids = list(old_set - new_set)
+
+    # 构建请求数据(传差异)
+    scope_data = {
+        "enterprise_id": enterprise_id,
+        "adapter_type": adapter_type,
+        "owner_type": data.get("owner_type", "EMPLOYEE"),
+    }
+    if add_ids:
+        scope_data["add_owner_id_list"] = add_ids
+    if delete_ids:
+        scope_data["delete_owner_id_list"] = delete_ids
+
     result = await InstitutionScopeService.scope_modify_service(
         auth=auth,
         institution_id=institution_id,
-        data=data,
+        data=scope_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])
+        if adapter_type == "EMPLOYEE_DEPARTMENT" and new_ids:
+            update_data["department_id"] = new_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:
+            from .service import InstitutionService
             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"),
+                "add_owner_id_list": add_ids,
+                "delete_owner_id_list": delete_ids,
             }
             await InstitutionService._sync_modify_quotas_by_scope(
                 auth=auth,
@@ -440,7 +471,8 @@ async def modify_scope_controller(
     except Exception as e:
         log.warning(f"本地scope同步失败(不影响支付宝侧): {e}")
 
-    log.info(f"设置制度成员成功: institution_id={institution_id}, adapter_type={data.get('adapter_type')}")
+    log.info(f"设置制度成员成功: institution_id={institution_id}, adapter_type={adapter_type}, "
+             f"加{len(add_ids)}人, 减{len(delete_ids)}人")
     return SuccessResponse(data=result, msg="设置成功")