修复公示导出:标的名按名字接口真实形状(简称/全称字典)提取为字符串

This commit is contained in:
zlt 2026-08-28 16:06:42 +08:00
parent 88df8762b0
commit fa00abe7e8
1 changed files with 13 additions and 2 deletions

View File

@ -54,6 +54,15 @@ def _as_date(v):
return None return None
def _display_name(v, code: str) -> str:
"""标的列显示名。downstream_repo.fetch_names 返回 {"name": 简称, "full": 全称}
(取不到的代码它退回 name=代码本身) 公示表用简称, 缺简称用全称, 都没有用代码
单元格必须是字符串, 字典直接写会被 openpyxl 拒收 (2026-08-28 实机报错修)"""
if isinstance(v, dict):
v = v.get("name") or v.get("full")
return str(v) if v else code.split(".")[0]
def compute_snapshot() -> dict: def compute_snapshot() -> dict:
"""组装公示快照: 持仓明细 / 平仓明细 / 汇总 / 当日净值。全部只读。""" """组装公示快照: 持仓明细 / 平仓明细 / 汇总 / 当日净值。全部只读。"""
today = date.today() today = date.today()
@ -87,7 +96,8 @@ def compute_snapshot() -> dict:
od = _as_date(r.get("open_date")) or today od = _as_date(r.get("open_date")) or today
chg = (px / cost - 1.0) if cost > 0 else 0.0 chg = (px / cost - 1.0) if cost > 0 else 0.0
holdings.append({ holdings.append({
"account": account, "code": code.split(".")[0], "name": names.get(code) or code, "account": account, "code": code.split(".")[0],
"name": _display_name(names.get(code), code),
"amount": round(cost * qty, 2), "open_date": od, "upd_date": today, "amount": round(cost * qty, 2), "open_date": od, "upd_date": today,
"structure": structure, "cost": cost, "qty": qty, "price": round(px, 3), "structure": structure, "cost": cost, "qty": qty, "price": round(px, 3),
"days": (today - od).days, "per_share": round(px - cost, 3), "days": (today - od).days, "per_share": round(px - cost, 3),
@ -106,7 +116,8 @@ def compute_snapshot() -> dict:
amt = cost * cq amt = cost * cq
ret = (pnl / amt) if amt > 0 else 0.0 ret = (pnl / amt) if amt > 0 else 0.0
closed.append({ closed.append({
"account": account, "code": code.split(".")[0], "name": names.get(code) or code, "account": account, "code": code.split(".")[0],
"name": _display_name(names.get(code), code),
"amount": round(amt, 2), "open_date": od, "close_date": cd, "amount": round(amt, 2), "open_date": od, "close_date": cd,
"structure": structure, "cost": cost, "qty": cq, "settle": round(settle, 3), "structure": structure, "cost": cost, "qty": cq, "settle": round(settle, 3),
"days": (cd - od).days, "per_share": round(settle - cost, 3), "days": (cd - od).days, "per_share": round(settle - cost, 3),