CaiWuXiTong/scripts/cmb-mail/run-server.sh
shanshi 39769abd26 feat(财务台账): 接入招商银行邮件账单 (#1392)
接入招商银行邮件账单下载、整目录导入和余额同步。
2026-11-25 10:11:08 +08:00

125 lines
3.3 KiB
Bash

#!/bin/sh
set -eu
BASE_DIR="${CMB_MAIL_BASE_DIR:-/opt/caiwuxitong/runtime/private/cmb-mail}"
CONFIG_FILE="$BASE_DIR/config.json"
FETCH_SCRIPT="$BASE_DIR/fetch_daily.py"
PYTHON_PATH="$BASE_DIR/python"
DATE="${1:-$(TZ=Asia/Shanghai date -d yesterday +%F)}"
STAMP="$(TZ=Asia/Shanghai date +%Y%m%d-%H%M%S)"
RUN_DIR="$BASE_DIR/exports/$DATE/server-$STAMP"
LOG_DIR="$BASE_DIR/logs"
LOG_FILE="$LOG_DIR/$(TZ=Asia/Shanghai date +%F).log"
STATE_FILE="$RUN_DIR/last-fetch.json"
IMPORT_MARKER="$BASE_DIR/state/$DATE.imported.json"
umask 077
mkdir -p "$RUN_DIR" "$LOG_DIR" "$BASE_DIR/state"
exec >>"$LOG_FILE" 2>&1
log() {
echo "[$(TZ=Asia/Shanghai date '+%F %T')] $*"
}
if [ ! -f "$CONFIG_FILE" ]; then
log "缺少招商银行邮件配置:$CONFIG_FILE"
exit 1
fi
if [ ! -f "$FETCH_SCRIPT" ]; then
log "缺少招商银行邮件抓取脚本:$FETCH_SCRIPT"
exit 1
fi
log "开始获取招商银行邮件账单 $DATE"
fetch_status=0
PYTHONPATH="$PYTHON_PATH" python3 -X utf8 "$FETCH_SCRIPT" fetch \
--config "$CONFIG_FILE" \
--date "$DATE" \
--out "$BASE_DIR/exports/$DATE" \
--run-dir "$RUN_DIR" || fetch_status=$?
if [ ! -f "$STATE_FILE" ]; then
log "抓取任务没有生成状态文件,停止导入"
exit 1
fi
archive_sha="$(
python3 - "$STATE_FILE" <<'PY'
import json
import sys
with open(sys.argv[1], encoding="utf-8") as stream:
state = json.load(stream)
print(state.get("archive_sha256") or "")
PY
)"
if [ -z "$archive_sha" ]; then
log "招商银行邮件状态文件缺少账单指纹"
exit 1
fi
if [ -f "$IMPORT_MARKER" ]; then
imported_sha="$(
python3 - "$IMPORT_MARKER" <<'PY'
import json
import sys
with open(sys.argv[1], encoding="utf-8") as stream:
state = json.load(stream)
print(state.get("archive_sha256") or "")
PY
)"
if [ "$imported_sha" = "$archive_sha" ]; then
log "当天相同招商银行账单已导入,跳过重复处理"
exit "$fetch_status"
fi
fi
umask 022
statement_directory="$(
python3 - "$STATE_FILE" <<'PY'
import json
import sys
from pathlib import Path
with open(sys.argv[1], encoding="utf-8") as stream:
state = json.load(stream)
files = [item.get("path") for item in state.get("files") or [] if isinstance(item, dict)]
print(str(Path(files[0]).parent) if files else "")
PY
)"
if [ -z "$statement_directory" ] || [ ! -d "$statement_directory" ]; then
log "招商银行 ZIP 中没有导入文件"
exit 1
fi
log "开始一次导入招商银行账单目录:$statement_directory"
/bin/sh /opt/caiwuxitong/run-finance-batch.sh \
finance-ledger:import-bank-bills-directory \
"$statement_directory" \
--bank=招商银行
python3 - "$STATE_FILE" "$IMPORT_MARKER" <<'PY'
import json
import sys
from datetime import datetime
from zoneinfo import ZoneInfo
with open(sys.argv[1], encoding="utf-8") as stream:
state = json.load(stream)
state["imported_files"] = len(state.get("files") or [])
state["imported_at"] = datetime.now(ZoneInfo("Asia/Shanghai")).isoformat(timespec="seconds")
with open(sys.argv[2], "w", encoding="utf-8") as stream:
json.dump(state, stream, ensure_ascii=False, indent=2)
stream.write("\n")
PY
if [ "$fetch_status" -ne 0 ]; then
log "招商银行邮件抓取返回失败,但已下载内容完成导入"
exit "$fetch_status"
fi
log "招商银行邮件账单已自动接入银行账单"