institution_handler.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. from redis.asyncio import Redis
  2. from app.api.v1.module_system.auth.schema import AuthSchema
  3. from app.core.exceptions import CustomException
  4. from app.core.logger import log
  5. from .base_handler import BaseHandler
  6. class InstitutionHandler(BaseHandler[dict]):
  7. """费控制度操作通知处理器"""
  8. async def handle(
  9. self, method: str, content: dict, auth: AuthSchema, redis: Redis
  10. ) -> bool:
  11. """处理费控制度操作通知"""
  12. operation_type = content.get("operation_type")
  13. institution_id = content.get("institution_id")
  14. enterprise_id = content.get("enterprise_id")
  15. log.info(
  16. f"处理费控制度操作通知: operation_type={operation_type}, "
  17. f"institution_id={institution_id}"
  18. )
  19. if operation_type == "DELETE":
  20. return await self._handle_delete(institution_id, enterprise_id, auth)
  21. log.info(f"费控制度操作通知无需处理: {operation_type}")
  22. return True
  23. async def _handle_delete(
  24. self, institution_id: str, enterprise_id: str, auth: AuthSchema
  25. ) -> bool:
  26. """处理制度删除:删除本地关联的规则和额度"""
  27. try:
  28. from app.plugin.module_payment.expense.rule.model import ExpenseRuleModel
  29. from app.plugin.module_payment.expense.quota.model import QuotaModel
  30. from app.plugin.module_payment.expense.institution.model import ExpenseInstitutionModel
  31. from sqlalchemy import delete, select
  32. # 删除使用规则
  33. rule_stmt = delete(ExpenseRuleModel).where(
  34. ExpenseRuleModel.institution_id == institution_id
  35. )
  36. await auth.db.execute(rule_stmt)
  37. # 删除额度
  38. quota_stmt = delete(QuotaModel).where(
  39. QuotaModel.institution_id == institution_id
  40. )
  41. await auth.db.execute(quota_stmt)
  42. # 删除制度
  43. inst_stmt = delete(ExpenseInstitutionModel).where(
  44. ExpenseInstitutionModel.institution_id == institution_id
  45. )
  46. await auth.db.execute(inst_stmt)
  47. await auth.db.flush()
  48. log.info(
  49. f"费控制度删除完成: institution_id={institution_id}, "
  50. f"已清理关联规则和额度"
  51. )
  52. return True
  53. except Exception as e:
  54. log.error(f"费控制度删除失败: {e}")
  55. # webhook 处理方法不抛异常,返回 False 让调用方决定
  56. return False