修复all模式月固定收入规则命中隔离问题
This commit is contained in:
@@ -48,6 +48,21 @@ PHASE2_BASELINE_RULE_CODES = [
|
||||
"SUPPLIER_CONCENTRATION",
|
||||
]
|
||||
|
||||
MONTHLY_FIXED_INCOME_ISOLATED_LARGE_TRANSACTION_RULE_CODES = {
|
||||
"SINGLE_LARGE_INCOME",
|
||||
"CUMULATIVE_INCOME",
|
||||
"ANNUAL_TURNOVER",
|
||||
"LARGE_CASH_DEPOSIT",
|
||||
"FREQUENT_CASH_DEPOSIT",
|
||||
}
|
||||
MONTHLY_FIXED_INCOME_ISOLATED_PHASE1_RULE_CODES = {
|
||||
"SUSPICIOUS_INCOME_KEYWORD",
|
||||
"FOREX_SELL_AMT",
|
||||
}
|
||||
MONTHLY_FIXED_INCOME_ISOLATED_PHASE2_RULE_CODES = {
|
||||
"FIXED_COUNTERPARTY_TRANSFER",
|
||||
}
|
||||
|
||||
RULE_CONFLICT_GROUPS = []
|
||||
ALL_MODE_STATEMENT_BASELINE_RULE_CODES = {
|
||||
"LOW_INCOME_RELATIVE_LARGE_TRANSACTION",
|
||||
@@ -209,6 +224,28 @@ class FileService:
|
||||
"phase2_baseline_hit_rules": list(PHASE2_BASELINE_RULE_CODES),
|
||||
}
|
||||
|
||||
def _build_monthly_fixed_income_isolated_rule_hit_plan(self) -> dict:
|
||||
"""为月固定收入准备不受正向流入污染的 all 模式计划。"""
|
||||
full_plan = self._build_all_compatible_rule_hit_plan()
|
||||
return {
|
||||
"large_transaction_hit_rules": [
|
||||
rule_code
|
||||
for rule_code in full_plan["large_transaction_hit_rules"]
|
||||
if rule_code not in MONTHLY_FIXED_INCOME_ISOLATED_LARGE_TRANSACTION_RULE_CODES
|
||||
],
|
||||
"phase1_hit_rules": [
|
||||
rule_code
|
||||
for rule_code in full_plan["phase1_hit_rules"]
|
||||
if rule_code not in MONTHLY_FIXED_INCOME_ISOLATED_PHASE1_RULE_CODES
|
||||
],
|
||||
"phase2_statement_hit_rules": [
|
||||
rule_code
|
||||
for rule_code in full_plan["phase2_statement_hit_rules"]
|
||||
if rule_code not in MONTHLY_FIXED_INCOME_ISOLATED_PHASE2_RULE_CODES
|
||||
],
|
||||
"phase2_baseline_hit_rules": list(full_plan["phase2_baseline_hit_rules"]),
|
||||
}
|
||||
|
||||
def _apply_conflict_groups(self, rule_plan: dict) -> dict:
|
||||
"""按显式互斥组裁剪规则计划,同组仅保留固定优先级的首个规则。"""
|
||||
resolved_plan = {plan_key: list(rule_codes) for plan_key, rule_codes in rule_plan.items()}
|
||||
@@ -235,6 +272,42 @@ class FileService:
|
||||
return self._apply_conflict_groups(self._build_all_compatible_rule_hit_plan())
|
||||
return self._build_subset_rule_hit_plan(log_id)
|
||||
|
||||
def _apply_rule_hit_plan_to_record(self, file_record: FileRecord, rule_hit_plan: dict) -> None:
|
||||
"""将规则命中计划回填到指定文件记录。"""
|
||||
file_record.large_transaction_hit_rules = list(
|
||||
rule_hit_plan.get("large_transaction_hit_rules", [])
|
||||
)
|
||||
file_record.phase1_hit_rules = list(rule_hit_plan.get("phase1_hit_rules", []))
|
||||
file_record.phase2_statement_hit_rules = list(
|
||||
rule_hit_plan.get("phase2_statement_hit_rules", [])
|
||||
)
|
||||
file_record.phase2_baseline_hit_rules = list(
|
||||
rule_hit_plan.get("phase2_baseline_hit_rules", [])
|
||||
)
|
||||
|
||||
def _rebalance_all_mode_group_rule_plans(self, group_id: int) -> None:
|
||||
"""同项目存在多文件时,隔离月固定收入样本,避免被其他正向流入规则污染。"""
|
||||
if settings.RULE_HIT_MODE != "all":
|
||||
return
|
||||
|
||||
group_records = [
|
||||
record for record in self.file_records.values()
|
||||
if record.group_id == group_id
|
||||
]
|
||||
if not group_records:
|
||||
return
|
||||
|
||||
full_plan = self._apply_conflict_groups(self._build_all_compatible_rule_hit_plan())
|
||||
if len(group_records) == 1:
|
||||
return
|
||||
|
||||
monthly_safe_plan = self._apply_conflict_groups(
|
||||
self._build_monthly_fixed_income_isolated_rule_hit_plan()
|
||||
)
|
||||
self._apply_rule_hit_plan_to_record(group_records[0], monthly_safe_plan)
|
||||
for record in group_records[1:]:
|
||||
self._apply_rule_hit_plan_to_record(record, full_plan)
|
||||
|
||||
def _create_file_record(
|
||||
self,
|
||||
*,
|
||||
@@ -375,6 +448,7 @@ class FileService:
|
||||
|
||||
# 存储记录
|
||||
self.file_records[log_id] = file_record
|
||||
self._rebalance_all_mode_group_rule_plans(group_id)
|
||||
self._apply_phase2_baselines(file_record)
|
||||
|
||||
# 添加后台任务(延迟解析)
|
||||
@@ -705,6 +779,7 @@ class FileService:
|
||||
)
|
||||
|
||||
self.file_records[log_id] = file_record
|
||||
self._rebalance_all_mode_group_rule_plans(group_id)
|
||||
self._apply_phase2_baselines(file_record)
|
||||
|
||||
# 返回成功的响应,包含logId数组
|
||||
|
||||
Reference in New Issue
Block a user