fund_classification_rules.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. class NongxinMerchant238Rule(FundClassificationRule):
  84. """农信银行商户238165993385171入账规则"""
  85. def match(self, context):
  86. return "商户238165993385171入账" in context["summary"] or "商户238165993385171入账" in context["opponent_name"]
  87. def apply(self, context):
  88. return {"level1": "游戏业务", "level2": "会长收款", "level3": "个人会长充值"}
  89. class NongxinFeeRule(FundClassificationRule):
  90. """农信银行手续费规则"""
  91. def match(self, context):
  92. return "手续费" in context["summary"]
  93. def apply(self, context):
  94. return {"level1": "财务费用", "level2": "财务费用", "level3": "手续费"}
  95. class NongxinYilianRule(FundClassificationRule):
  96. """农信银行易练规则"""
  97. def match(self, context):
  98. return "易练" in context["summary"]
  99. def apply(self, context):
  100. return {"level1": "易练结算", "level2": "易练结算", "level3": "易练推广支付"}
  101. class NongxinLoanRule(FundClassificationRule):
  102. """农信银行贷款规则"""
  103. def match(self, context):
  104. return "贷款" in context["summary"]
  105. def apply(self, context):
  106. return {"level1": "贷款支出", "level2": "贷款支出", "level3": "贷款支出"}
  107. class NongxinMerchant238ExpenseRule(FundClassificationRule):
  108. """农信银行商户238165993385171入账支出规则"""
  109. def match(self, context):
  110. return "商户238165993385171入账" in context["opponent_name"]
  111. def apply(self, context):
  112. return {"level1": "日常管理", "level2": "日常管理", "level3": "法务费"}
  113. def get_classification_rules():
  114. """
  115. 获取所有资金分类规则列表
  116. Returns:
  117. list: 资金分类规则对象列表
  118. """
  119. return [
  120. SettlementRule(),
  121. LoanPaymentRule(),
  122. TelecomRule(),
  123. WithdrawalRule(),
  124. YilianRule(),
  125. FeeRule(),
  126. CloudServiceRule(),
  127. PendingSettlementRule(),
  128. NongxinMerchant238Rule(),
  129. NongxinFeeRule(),
  130. NongxinYilianRule(),
  131. NongxinLoanRule(),
  132. NongxinMerchant238ExpenseRule()
  133. ]