74 lines
2.2 KiB
Python
Executable File
74 lines
2.2 KiB
Python
Executable File
#!/usr/bin/env python3
|
||
|
||
import argparse
|
||
import json
|
||
from datetime import datetime
|
||
from pathlib import Path
|
||
|
||
|
||
def main() -> int:
|
||
parser = argparse.ArgumentParser()
|
||
parser.add_argument("--month", required=True)
|
||
parser.add_argument("--result-json", required=True)
|
||
parser.add_argument("--input")
|
||
parser.add_argument("--error")
|
||
args = parser.parse_args()
|
||
|
||
if args.input:
|
||
result = json.loads(Path(args.input).read_text(encoding="utf-8"))
|
||
else:
|
||
result = {
|
||
"status": "failed",
|
||
"report_month": args.month,
|
||
"source_filename": None,
|
||
"success": 0,
|
||
"duplicates": 0,
|
||
"failed": 1,
|
||
"mapped": 0,
|
||
"total_in_table": 0,
|
||
"failure_reasons": [
|
||
{
|
||
"row": 0,
|
||
"reason": args.error or "供应商汇总导入未执行",
|
||
}
|
||
],
|
||
}
|
||
|
||
status = result.get("status", "failed")
|
||
success = int(result.get("success") or 0)
|
||
duplicates = int(result.get("duplicates") or 0)
|
||
failed = int(result.get("failed") or 0)
|
||
reasons = result.get("failure_reasons") or []
|
||
report_month = str(result.get("report_month") or f"{args.month}-01")
|
||
try:
|
||
month_label = f"{int(report_month[5:7])} 月"
|
||
except (TypeError, ValueError):
|
||
month_label = args.month
|
||
brief = (
|
||
f"成功导入 {success:,} 条 / 上月 = {month_label}"
|
||
f";重复 {duplicates:,} 条 / 失败 {failed:,} 条"
|
||
)
|
||
if status != "success" and reasons:
|
||
first = str(reasons[0].get("reason", "未知错误"))
|
||
row = reasons[0].get("row")
|
||
location = f"第 {row} 行:" if row else ""
|
||
brief += f";{location}{first}"
|
||
|
||
payload = {
|
||
"finished_at": datetime.now().astimezone().isoformat(timespec="seconds"),
|
||
"brief": brief,
|
||
"result": result,
|
||
}
|
||
output = Path(args.result_json)
|
||
output.parent.mkdir(parents=True, exist_ok=True)
|
||
output.write_text(
|
||
json.dumps(payload, ensure_ascii=False, indent=2) + "\n",
|
||
encoding="utf-8",
|
||
)
|
||
print(brief)
|
||
return 0 if status == "success" else 1
|
||
|
||
|
||
if __name__ == "__main__":
|
||
raise SystemExit(main())
|