Ver código fonte

fix: 全体员工scope + 删除忽略支付宝已删+清关联表

alphah 2 semanas atrás
pai
commit
80d712f821

+ 8 - 2
backend/app/plugin/module_payment/expense/institution/controller.py

@@ -101,14 +101,20 @@ async def create_institution_controller(
     # 解析适用成员数据
     scope_data = None
     adapter_type = data.get("applicable_scope")
-    if adapter_type and adapter_type not in ("NONE", "none", "all"):
-        ADAPTER_TYPE_MAP = {"employee": "EMPLOYEE_SELECT", "department": "DEPARTMENT_SELECT"}
+    if adapter_type and adapter_type not in ("NONE", "none"):
+        ADAPTER_TYPE_MAP = {"all": "EMPLOYEE_ALL", "employee": "EMPLOYEE_SELECT", "department": "DEPARTMENT_SELECT"}
         mapped_adapter = ADAPTER_TYPE_MAP.get(adapter_type, adapter_type)
         scope_data = {
             "adapter_type": mapped_adapter,
             "owner_type": data.get("scope_owner_type", "ENTERPRISE_PAY_UID"),
             "add_owner_id_list": data.get("scope_owner_id_list"),
         }
+        # 全体员工时把 scope 写入创建请求(避免默认无scope导致支付宝后台不可操作)
+        if adapter_type == "all":
+            data["institution_scope_info"] = {
+                "adapter_type": "ALL",
+                "owner_type": "ENTERPRISE_PAY_UID",
+            }
 
     # 解析发放规则数据
     issuerule_data = None

+ 37 - 25
backend/app/plugin/module_payment/expense/institution/service.py

@@ -451,39 +451,51 @@ class InstitutionService:
     @classmethod
     async def delete_institution_service(
         cls, auth: AuthSchema, data: AlipayEbppInvoiceInstitutionDeleteModel
-    ) -> AlipayEbppInvoiceInstitutionDeleteResponse:
+    ) -> dict:
         """
         删除费控制度
         调用: alipay.ebpp.invoice.institution.delete
+        支付宝侧已删时忽略错误,始终清理本地关联表
         """
-        request = AlipayEbppInvoiceInstitutionDeleteRequest()
-        request.biz_model = data
-
-        response = await asyncio.to_thread(cls._execute_alipay, request)
-
-        if not response:
-            raise CustomException(msg="删除费控制度失败: 无响应")
-
-        result = AlipayEbppInvoiceInstitutionDeleteResponse()
-        result.parse_response_content(response)
-
-        if not result.is_success():
-            log.error(f"支付宝接口调用失败: {result.code} - {result.msg}")
-            raise CustomException(msg=f"删除费控制度失败: {result.msg}")
+        institution_id = getattr(data, 'institution_id', None)
 
-        # 同步删除本地记录
+        # 调用支付宝删除(失败时仅告警,不影响本地清理)
         try:
-            crud = InstitutionCRUD(auth)
-            institution_id = getattr(data, 'institution_id', None)
-            if institution_id:
-                obj = await crud.get(institution_id=institution_id)
-                if obj:
-                    await crud.delete(ids=[obj.id])
-                    log.info(f"已删除本地记录: institution_id={institution_id}")
+            request = AlipayEbppInvoiceInstitutionDeleteRequest()
+            request.biz_model = data
+            response = await asyncio.to_thread(cls._execute_alipay, request)
+            if response:
+                result = AlipayEbppInvoiceInstitutionDeleteResponse()
+                result.parse_response_content(response)
+                if result.is_success():
+                    log.info(f"支付宝删除成功: institution_id={institution_id}")
+                else:
+                    log.warning(f"支付宝删除失败(可能已删): {result.code} - {result.msg}")
+            else:
+                log.warning("支付宝删除无响应,继续清理本地")
         except Exception as e:
-            log.warning(f"删除本地记录失败(不影响支付宝侧): {e}")
+            log.warning(f"支付宝删除异常(忽略): {e}")
 
-        return result
+        # 清理本地关联表
+        if institution_id:
+            try:
+                from app.plugin.module_payment.expense.rule.model import ExpenseRuleModel
+                from app.plugin.module_payment.expense.quota.model import QuotaModel
+                from app.plugin.module_payment.expense.institution.model import ExpenseInstitutionModel
+                from sqlalchemy import delete as sa_delete
+
+                # 删规则
+                await auth.db.execute(sa_delete(ExpenseRuleModel).where(ExpenseRuleModel.institution_id == institution_id))
+                # 删额度
+                await auth.db.execute(sa_delete(QuotaModel).where(QuotaModel.institution_id == institution_id))
+                # 删制度
+                await auth.db.execute(sa_delete(ExpenseInstitutionModel).where(ExpenseInstitutionModel.institution_id == institution_id))
+                await auth.db.flush()
+                log.info(f"本地关联数据已清理: institution_id={institution_id}")
+            except Exception as e:
+                log.warning(f"本地清理失败: {e}")
+
+        return {"institution_id": institution_id, "deleted": True}
 
     @classmethod
     async def modify_institution_service(