from redis.asyncio import Redis from app.api.v1.module_system.auth.schema import AuthSchema from app.core.exceptions import CustomException from app.core.logger import log from .base_handler import BaseHandler class InstitutionHandler(BaseHandler[dict]): """费控制度操作通知处理器""" async def handle( self, method: str, content: dict, auth: AuthSchema, redis: Redis ) -> bool: """处理费控制度操作通知""" operation_type = content.get("operation_type") institution_id = content.get("institution_id") enterprise_id = content.get("enterprise_id") log.info( f"处理费控制度操作通知: operation_type={operation_type}, " f"institution_id={institution_id}" ) if operation_type == "DELETE": return await self._handle_delete(institution_id, enterprise_id, auth) log.info(f"费控制度操作通知无需处理: {operation_type}") return True async def _handle_delete( self, institution_id: str, enterprise_id: str, auth: AuthSchema ) -> bool: """处理制度删除:删除本地关联的规则和额度""" 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, select # 删除使用规则 rule_stmt = delete(ExpenseRuleModel).where( ExpenseRuleModel.institution_id == institution_id ) await auth.db.execute(rule_stmt) # 删除额度 quota_stmt = delete(QuotaModel).where( QuotaModel.institution_id == institution_id ) await auth.db.execute(quota_stmt) # 删除制度 inst_stmt = delete(ExpenseInstitutionModel).where( ExpenseInstitutionModel.institution_id == institution_id ) await auth.db.execute(inst_stmt) await auth.db.flush() log.info( f"费控制度删除完成: institution_id={institution_id}, " f"已清理关联规则和额度" ) return True except Exception as e: log.error(f"费控制度删除失败: {e}") # webhook 处理方法不抛异常,返回 False 让调用方决定 return False