diff --git a/app.py b/app.py index 90d44843..bbdfd501 100755 --- a/app.py +++ b/app.py @@ -11149,26 +11149,34 @@ def get_events_effectiveness_stats(): stock_stats = [] total_stocks = 0 if event_ids: - # 查询所有关联股票 - related_stocks = db.session.query(EventStock).filter( - EventStock.event_id.in_(event_ids) + # 查询所有关联股票(使用 RelatedStock 模型) + related_stocks = db.session.query(RelatedStock).filter( + RelatedStock.event_id.in_(event_ids) ).all() - # 统计股票数量(去重) + # 构建事件 ID 到事件的映射,用于获取涨跌幅 + event_map = {e.id: e for e in events_query} + + # 统计股票数量(去重),并关联事件涨跌幅 unique_stocks = {} for rs in related_stocks: stock_key = rs.stock_code + # 获取关联事件的最大涨幅作为该股票的涨幅参考 + event = event_map.get(rs.event_id) + event_max_chg = event.related_max_chg if event else 0 + if stock_key not in unique_stocks: unique_stocks[stock_key] = { 'stockCode': rs.stock_code, 'stockName': rs.stock_name, - 'maxChg': rs.chg_pct or 0, + 'maxChg': event_max_chg or 0, 'eventId': rs.event_id, + 'correlation': rs.correlation or 0, } else: # 保留最大涨幅的记录 - if (rs.chg_pct or 0) > unique_stocks[stock_key]['maxChg']: - unique_stocks[stock_key]['maxChg'] = rs.chg_pct or 0 + if (event_max_chg or 0) > unique_stocks[stock_key]['maxChg']: + unique_stocks[stock_key]['maxChg'] = event_max_chg or 0 unique_stocks[stock_key]['eventId'] = rs.event_id total_stocks = len(unique_stocks)