feat: implement cross-batch Alipay refund reconciliation

When a refund row in an uploaded Alipay bill has no matching expense
row in the same batch (because the original purchase was uploaded in a
prior batch), the refund is now reconciled against the stored record in
bills_cleaned rather than being silently discarded.

Changes:
- analyzer/cleaners/base.py: add unresolved_refunds list to BaseCleaner
- analyzer/cleaners/alipay.py: _aggregate_refunds stores full refund
  metadata (dict); _process_expenses tracks matched keys and populates
  self.unresolved_refunds for unmatched refunds
- analyzer/server.py: thread unresolved_refunds through do_clean,
  CleanResponse, and both /clean endpoints
- server/adapter/adapter.go: add UnresolvedRefund type and field to CleanResult
- server/adapter/http/cleaner.go: deserialize unresolved_refunds from
  Python response and populate CleanResult
- server/repository/repository.go: add ReconcileRefund to BillRepository interface
- server/repository/mongo/repository.go: implement ReconcileRefund —
  full refund soft-deletes the bill, partial refund reduces amount and
  appends remark with original amount and refund order number
- server/handler/upload.go: capture clean result and call ReconcileRefund
  for each unresolved refund after saving cleaned bills
- server/model/response.go: add ReconciledRefundCount to UploadData

Also: add CLAUDE.md (@AGENTS.md), update AGENTS.md, fix DailyTrendChart
missing-date gap by filling zero-expense dates in daily map.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-06-16 19:29:47 +08:00
parent bb717faac3
commit e2e1beb6f7
12 changed files with 342 additions and 133 deletions

View File

@@ -52,6 +52,7 @@ class CleanResponse(BaseModel):
bill_type: str
message: str
output_path: Optional[str] = None
unresolved_refunds: list[dict] = []
class CategoryRequest(BaseModel):
@@ -138,27 +139,27 @@ def do_clean(
start: str = None,
end: str = None,
output_format: str = "csv"
) -> tuple[bool, str, str]:
) -> tuple[bool, str, str, list[dict]]:
"""
执行清洗逻辑
Returns:
(success, bill_type, message)
(success, bill_type, message, unresolved_refunds)
"""
# 检查文件是否存在
if not Path(input_path).exists():
return False, "", f"文件不存在: {input_path}"
return False, "", f"文件不存在: {input_path}", []
# 检测账单类型
if bill_type == "auto":
detected_type = detect_bill_type(input_path)
if detected_type is None:
return False, "", "无法识别账单类型"
return False, "", "无法识别账单类型", []
bill_type = detected_type
# 计算日期范围
start_date, end_date = compute_date_range_from_values(year, month, start, end)
# 创建对应的清理器
try:
if bill_type == "alipay":
@@ -167,15 +168,15 @@ def do_clean(
cleaner = JDCleaner(input_path, output_path, output_format)
else:
cleaner = WechatCleaner(input_path, output_path, output_format)
cleaner.set_date_range(start_date, end_date)
cleaner.clean()
type_names = {"alipay": "支付宝", "wechat": "微信", "jd": "京东白条"}
return True, bill_type, f"{type_names.get(bill_type, bill_type)}账单清洗完成"
return True, bill_type, f"{type_names.get(bill_type, bill_type)}账单清洗完成", cleaner.unresolved_refunds
except Exception as e:
return False, bill_type, f"清洗失败: {str(e)}"
return False, bill_type, f"清洗失败: {str(e)}", []
# =============================================================================
@@ -215,7 +216,7 @@ async def clean_bill(request: CleanRequest):
接收账单文件路径,执行清洗后输出到指定路径
"""
success, bill_type, message = do_clean(
success, bill_type, message, unresolved_refunds = do_clean(
input_path=request.input_path,
output_path=request.output_path,
bill_type=request.bill_type or "auto",
@@ -225,15 +226,16 @@ async def clean_bill(request: CleanRequest):
end=request.end,
output_format=request.format or "csv"
)
if not success:
raise HTTPException(status_code=400, detail=message)
return CleanResponse(
success=True,
bill_type=bill_type,
message=message,
output_path=request.output_path
output_path=request.output_path,
unresolved_refunds=unresolved_refunds
)
@@ -264,7 +266,7 @@ async def clean_bill_upload(
output_path = tmp_output.name
try:
success, detected_type, message = do_clean(
success, detected_type, message, unresolved_refunds = do_clean(
input_path=input_path,
output_path=output_path,
bill_type=bill_type or "auto",
@@ -274,15 +276,16 @@ async def clean_bill_upload(
end=end,
output_format=format or "csv"
)
if not success:
raise HTTPException(status_code=400, detail=message)
return CleanResponse(
success=True,
bill_type=detected_type,
message=message,
output_path=output_path
output_path=output_path,
unresolved_refunds=unresolved_refunds
)
finally: