AutoRpa/rpa_rebate/run_batch.py

74 lines
2.6 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""返利班次入口:领表 4 起草行 / 表 3 只录协议行,runner 查号 → 总单 → 细单 → 送审,写表 4。"""
from __future__ import annotations
import argparse
from apa_browser_adapter import ApaBrowserAdapter
from db import load_runtime_env
from job import recorder, run_batch
from persist import KIND_NOT_FOUND, AgreementOutcome, RebateGroup
from rebate_finder import RebateFinder, context_from_env
from rebate_flow_steps import (
import_draft_for_contract,
run_header_flow,
run_import_flow,
run_submit_flow,
)
from repo import OracleRebateRepo
def parse_args(argv: list[str] | None = None) -> argparse.Namespace:
parser = argparse.ArgumentParser(description="返利班次:查号 + 总单 + 细单 + 送审")
parser.add_argument("--limit", type=int, default=None, help="本班最多领取 N 条")
return parser.parse_args(argv)
def production_runner(browser, ctx, finder: RebateFinder, group: RebateGroup) -> AgreementOutcome:
contract_no = group.contract_no
print(f"find contract_no={contract_no}", flush=True)
try:
found = bool(finder(contract_no))
except Exception:
return AgreementOutcome(KIND_NOT_FOUND, None, None, None)
print(f"found={'yes' if found else 'no'} contract_no={contract_no}", flush=True)
if not found:
return AgreementOutcome(KIND_NOT_FOUND, None, None, None)
def header(number: str) -> str:
print(f"header contract_no={number}", flush=True)
return run_header_flow(browser, ctx, number)
def importer(number: str):
print(f"import contract_no={number}", flush=True)
return run_import_flow(browser, ctx, number, import_draft_for_contract(number))
def submitter(number: str):
print(f"submit contract_no={number}", flush=True)
return run_submit_flow(browser, ctx, number)
return recorder(contract_no, header, importer, submitter)
def main(argv: list[str] | None = None) -> int:
args = parse_args(argv)
load_runtime_env()
repo = OracleRebateRepo.from_env()
ctx = context_from_env()
browser = ApaBrowserAdapter(url=ctx.rebate_url, flow="REBATE_FIND")
finder = RebateFinder(browser, ctx)
summary = run_batch(
repo,
runner=lambda group: production_runner(browser, ctx, finder, group),
limit=args.limit,
)
print(
f"claimed={summary.claimed} groups={summary.groups} "
f"ok={summary.ok} not_found={summary.not_found} failed={summary.failed}",
flush=True,
)
return 0 if summary.failed == 0 else 1
if __name__ == "__main__":
raise SystemExit(main())