fund_classification_rules.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. """
  2. 资金分类规则模块
  3. 用于管理日记账生成中的资金分类规则
  4. 采用策略模式和规则链模式,便于扩展和维护
  5. """
  6. class FundClassificationRule:
  7. """资金分类规则基类"""
  8. def match(self, context):
  9. """
  10. 判断是否匹配该规则
  11. Args:
  12. context: 包含交易上下文信息的字典
  13. - is_settlement: 是否为结算交易
  14. - income: 收入金额
  15. - opponent_name: 对手户名称
  16. - summary: 摘要
  17. - flow_remark: 流水备注
  18. Returns:
  19. bool: 是否匹配该规则
  20. """
  21. return False
  22. def apply(self, context):
  23. """
  24. 应用规则,返回分类结果
  25. Args:
  26. context: 包含交易上下文信息的字典
  27. Returns:
  28. dict: 包含资金分类-1级、资金分类-2级、资金分类-3级的字典
  29. """
  30. return {"level1": "", "level2": "", "level3": ""}
  31. class SettlementRule(FundClassificationRule):
  32. """结算收入/支出规则"""
  33. def match(self, context):
  34. return context["is_settlement"]
  35. def apply(self, context):
  36. if context["income"] > 0:
  37. return {"level1": "结算收入", "level2": "结算收入", "level3": "结算收入"}
  38. else:
  39. return {"level1": "结算支出", "level2": "结算支出", "level3": "结算支出"}
  40. class LoanPaymentRule(FundClassificationRule):
  41. """贷款支出规则"""
  42. def match(self, context):
  43. return "款" in context["flow_remark"] and context["opponent_name"] in ["赵丽"]
  44. def apply(self, context):
  45. return {"level1": "贷款支出", "level2": "贷款支出", "level3": "贷款支出"}
  46. class TelecomRule(FundClassificationRule):
  47. """电信日常管理费用规则"""
  48. def match(self, context):
  49. return "电信" in context["opponent_name"]
  50. def apply(self, context):
  51. return {"level1": "日常管理", "level2": "日常管理", "level3": "日常管理费用"}
  52. class WithdrawalRule(FundClassificationRule):
  53. """会长提现支付规则"""
  54. def match(self, context):
  55. return "会长提现" in context["flow_remark"] or "提现" in context["flow_remark"]
  56. def apply(self, context):
  57. return {"level1": "游戏业务", "level2": "结算支出", "level3": "会长提现支付"}
  58. class YilianRule(FundClassificationRule):
  59. """易练推广支付规则"""
  60. def match(self, context):
  61. return "易练" in context["flow_remark"] and "云账户" in context["opponent_name"]
  62. def apply(self, context):
  63. return {"level1": "易练结算", "level2": "易练结算", "level3": "易练推广支付"}
  64. class FeeRule(FundClassificationRule):
  65. """手续费规则"""
  66. def match(self, context):
  67. return "手续费" in context["summary"] or "服务费" in context["summary"]
  68. def apply(self, context):
  69. return {"level1": "财务费用", "level2": "财务费用", "level3": "手续费"}
  70. class CloudServiceRule(FundClassificationRule):
  71. """云服务日常管理费用规则"""
  72. def match(self, context):
  73. cloud_providers = ["七牛", "阿里云", "京东云", "金山云"]
  74. return any(provider in context["opponent_name"] for provider in cloud_providers)
  75. def apply(self, context):
  76. return {"level1": "日常管理", "level2": "日常管理", "level3": "日常管理费用"}
  77. class PendingSettlementRule(FundClassificationRule):
  78. """待清算个人会长充值规则"""
  79. def match(self, context):
  80. return "待清算" in context["opponent_name"]
  81. def apply(self, context):
  82. return {"level1": "游戏业务", "level2": "会长收款", "level3": "个人会长充值"}
  83. def get_classification_rules():
  84. """
  85. 获取所有资金分类规则列表
  86. Returns:
  87. list: 资金分类规则对象列表
  88. """
  89. return [
  90. SettlementRule(),
  91. LoanPaymentRule(),
  92. TelecomRule(),
  93. WithdrawalRule(),
  94. YilianRule(),
  95. FeeRule(),
  96. CloudServiceRule(),
  97. PendingSettlementRule()
  98. ]