"""受控库存 RPA v2 repair 工具。 默认只做 dry-run;--apply 时仍调用正式 callback ingest API,不执行旁路 SQL。 """ from __future__ import annotations import argparse import json from pathlib import Path def classify(path: Path, *, expected_source: str | None = None) -> str: if not path.is_file(): return 'rejected' if path.suffix.lower() not in {'.xlsx', '.xls', '.csv'}: return 'rejected' if expected_source and expected_source.upper() not in path.name.upper(): return 'needs_review' return 'repaired' def main() -> int: parser = argparse.ArgumentParser() parser.add_argument('--payload', required=True, help='v2 callback JSON 文件') parser.add_argument('--apply', action='store_true', help='调用正式 ingest;默认 dry-run') args = parser.parse_args() payload = json.loads(Path(args.payload).read_text(encoding='utf-8')) items = payload.get('results') if isinstance(payload, dict) else None if not isinstance(items, list): print(json.dumps({'repair_result': 'rejected', 'error_code': 'callback_schema_invalid'}, ensure_ascii=False)) return 2 decisions = [] for item in items: item = dict(item or {}) path = Path(str(item.get('file_path') or '')) decisions.append({ 'source_run_no': item.get('source_run_no'), 'callback_id': item.get('callback_id'), 'repair_result': classify(path, expected_source=item.get('source_code')), }) if not args.apply: print(json.dumps({'dry_run': True, 'items': decisions}, ensure_ascii=False, indent=2)) return 0 # 正式 API 调用由部署环境注入;脚本不持有凭据,也不旁路写库。 print(json.dumps({'apply_requested': True, 'items': decisions, 'next_action': 'POST /api/freight-rpa/onhand-repair'}, ensure_ascii=False, indent=2)) return 0 if __name__ == '__main__': raise SystemExit(main())