sync scripts/mofin_db.py with root + knowledge log
This commit is contained in:
+21
-7
@@ -21,7 +21,7 @@ from datetime import datetime
|
||||
from pathlib import Path
|
||||
from typing import Optional, Callable
|
||||
|
||||
DATA_DIR = Path(__file__).parent.parent / "data"
|
||||
DATA_DIR = Path(__file__).parent / "data"
|
||||
DB_PATH = DATA_DIR / "mofin.db"
|
||||
|
||||
# ═══════════════════════════════════════════════════════════
|
||||
@@ -1088,8 +1088,19 @@ def write_holding_strategy(conn, code: str, name: str, data: dict) -> tuple[bool
|
||||
quality_issues_j = _json.dumps(data.get('quality_issues', {}), ensure_ascii=False) if isinstance(data.get('quality_issues'), dict) else data.get('quality_issues_json', '')
|
||||
signal_factors_j = _json.dumps(data.get('signal_factors', []), ensure_ascii=False) if isinstance(data.get('signal_factors'), list) else data.get('signal_factors_json', '')
|
||||
|
||||
# 在DELETE前保留现有的full_analysis和reassessed_at(防止被regenerate_all等清空)
|
||||
_existing_fa = data.get('full_analysis', '')
|
||||
_existing_ra = data.get('reassessed_at', '')
|
||||
if not _existing_fa:
|
||||
try:
|
||||
_old = conn.execute("SELECT full_analysis, reassessed_at FROM holding_strategies WHERE code=? ORDER BY id DESC LIMIT 1", (code,)).fetchone()
|
||||
if _old:
|
||||
if _old[0]: _existing_fa = _old[0]
|
||||
if _old[1]: _existing_ra = _old[1]
|
||||
except:
|
||||
pass
|
||||
|
||||
# DELETE + INSERT
|
||||
conn.execute("PRAGMA foreign_keys=OFF") # 临时禁用FK(自选股可能不在stocks表)
|
||||
conn.execute("DELETE FROM holding_strategies WHERE code=?", (code,))
|
||||
conn.execute("""
|
||||
INSERT INTO holding_strategies
|
||||
@@ -1100,10 +1111,11 @@ def write_holding_strategy(conn, code: str, name: str, data: dict) -> tuple[bool
|
||||
source, reason, updated_at,
|
||||
avg_price, decision_timestamp, note, quality_check,
|
||||
quality_checked_at, quality_issues_json, position_advice,
|
||||
signal_factors_json, time_horizon, decision_type, full_analysis)
|
||||
signal_factors_json, time_horizon, decision_type,
|
||||
full_analysis, reassessed_at)
|
||||
VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,
|
||||
datetime('now','localtime'),
|
||||
?,?,?,?,?,?,?,?,?,?,?)
|
||||
?,?,?,?,?,?,?,?,?,?,?,?)
|
||||
""", (
|
||||
code, name,
|
||||
data.get('version', 1), data.get('price'), data.get('cost'),
|
||||
@@ -1119,14 +1131,16 @@ def write_holding_strategy(conn, code: str, name: str, data: dict) -> tuple[bool
|
||||
data.get('avg_price', 0),
|
||||
data.get('timestamp') or data.get('created_at', ''),
|
||||
data.get('note', ''),
|
||||
data.get('quality_check', 'pending'),
|
||||
data.get('quality_check', ''),
|
||||
data.get('quality_checked_at', ''),
|
||||
quality_issues_j,
|
||||
data.get('position_advice', ''),
|
||||
signal_factors_j,
|
||||
data.get('time_horizon', ''),
|
||||
data.get('decision_type', data.get('strategy_type', 'holding')),
|
||||
data.get('full_analysis', ''),
|
||||
data.get('type', data.get('strategy_type', 'holding')),
|
||||
# 保留full_analysis和reassessed_at
|
||||
_existing_fa,
|
||||
_existing_ra,
|
||||
))
|
||||
conn.commit()
|
||||
return True, f"策略 {code} 已写入"
|
||||
|
||||
Reference in New Issue
Block a user