111 lines
4.9 KiB
Python
111 lines
4.9 KiB
Python
"""成交修正的只读取证。单次查询只访问一张表,满上限不得冒充完整历史。"""
|
||
import json
|
||
|
||
from app.db import session as db
|
||
from app.core import ws_codec
|
||
|
||
|
||
def _rows(rows, limit=None, *, strict=True):
|
||
if limit is not None and len(rows) >= limit:
|
||
raise ValueError('成交修正证据超过核验上限,需人工核对')
|
||
for row in rows:
|
||
try:
|
||
row['payload'] = json.loads(row.get('payload_json') or '{}')
|
||
if not isinstance(row['payload'], dict):
|
||
raise ValueError('成交回执不是对象')
|
||
except (TypeError, ValueError):
|
||
if strict:
|
||
raise
|
||
# 保留原文和seq,让worker只挂起坏行,不让它阻塞本批其它更正。
|
||
row['payload'] = None
|
||
return rows
|
||
|
||
|
||
def pending(limit=100):
|
||
# 兼容升级前旧WS将未知类型存成2的原消息,不扫描已消化的重复占位行。
|
||
return _rows(db.fetch_all(
|
||
"SELECT * FROM pms_qmt_inbox WHERE msg_type='trade_correction' "
|
||
"AND (processed=0 OR (processed=2 AND process_note="
|
||
"'trade_correction 由通道进程消化, 不入账')) ORDER BY seq ASC LIMIT :n",
|
||
{'n': max(1, min(int(limit), 500))}), strict=False)
|
||
|
||
|
||
def get_inbox(seq, lock=False):
|
||
row = db.fetch_one('SELECT * FROM pms_qmt_inbox WHERE seq=:seq'+
|
||
(' FOR UPDATE' if lock else ''), {'seq': int(seq)})
|
||
return _rows([row], strict=False)[0] if row else None
|
||
|
||
|
||
def get_trade(trade_no):
|
||
key = ws_codec.dedup_key('trade', {'trade_no': trade_no})
|
||
row = db.fetch_one('SELECT * FROM pms_qmt_inbox WHERE dedup_key=:key',
|
||
{'key': key[:80] if key else None})
|
||
if row:
|
||
row = _rows([row])[0]
|
||
if row['msg_type'] != 'trade' or row['payload'].get('trade_no') != trade_no:
|
||
raise ValueError('成交编号与原回执去重键冲突')
|
||
return row
|
||
|
||
|
||
def _matches_correction(row, correction_id, kind):
|
||
payload = row.get('payload')
|
||
key = ws_codec.dedup_key(kind, {'correction_id': correction_id, 'amended': True})
|
||
# 原文坏损但业务键仍指向本编号时也是冲突证据,不可默默筛掉。
|
||
return (isinstance(payload, dict) and payload.get('correction_id') == correction_id
|
||
or bool(key) and row.get('dedup_key') == key)
|
||
|
||
|
||
def _correction_rows(kind, correction_id, iid):
|
||
# 同编号不同正文不能因主去重行存在而被忽略;也读取空dedup的冲突占位。
|
||
limit = 10001
|
||
rows = _rows(db.fetch_all(
|
||
'SELECT * FROM pms_qmt_inbox WHERE msg_type=:kind ORDER BY seq ASC LIMIT :n',
|
||
{'kind': kind, 'n': limit}), limit, strict=False)
|
||
return [r for r in rows if _matches_correction(r, correction_id, kind)]
|
||
|
||
|
||
def amendments(correction_id, iid):
|
||
# 正常order_update量大,先走child corr_id索引;新类型收包强制corr_id=iid。
|
||
limit = 2001
|
||
rows = _rows(db.fetch_all(
|
||
"SELECT * FROM pms_qmt_inbox WHERE msg_type='order_update' AND corr_id=:iid "
|
||
'ORDER BY seq ASC LIMIT :n', {'iid': iid, 'n': limit}), limit, strict=False)
|
||
# 冲突占位可能篡改instruction_id,不能只在正常child范围内找冲突。
|
||
conflicts = _rows(db.fetch_all(
|
||
"SELECT * FROM pms_qmt_inbox WHERE msg_type='order_update' AND processed=3 "
|
||
'ORDER BY seq ASC LIMIT :n', {'n': limit}), limit, strict=False)
|
||
by_seq = {r['seq']: r for r in [*rows, *conflicts]
|
||
if _matches_correction(r, correction_id, 'order_update')}
|
||
return sorted(by_seq.values(), key=lambda r: int(r['seq']))
|
||
|
||
|
||
def corrections(correction_id, iid):
|
||
return _correction_rows('trade_correction', correction_id, iid)
|
||
|
||
|
||
def pending_for_trade(trade_no, before_seq):
|
||
limit = 10001
|
||
rows = _rows(db.fetch_all(
|
||
"SELECT * FROM pms_qmt_inbox WHERE msg_type='trade_correction' AND seq<:seq "
|
||
"AND (processed IN (0,3) OR (processed=2 AND process_note="
|
||
"'trade_correction 由通道进程消化, 不入账')) ORDER BY seq ASC LIMIT :n",
|
||
{'seq': int(before_seq), 'n': limit}), limit, strict=False)
|
||
return [r for r in rows if (r.get('payload') or {}).get('trade_no') == trade_no]
|
||
|
||
|
||
def orders_for_code(code, limit=10001):
|
||
rows = db.fetch_all('SELECT * FROM pms_qmt_order WHERE ts_code=:code '
|
||
'ORDER BY id ASC LIMIT :n', {'code': code, 'n': int(limit)})
|
||
if len(rows) >= limit:
|
||
raise ValueError('该股委托历史超过核验上限')
|
||
return rows
|
||
|
||
|
||
def trades_for_orders(child_ids, limit=20001):
|
||
# 历史信封的corr_id可能不是子委托;以持久原文instruction_id核对,不猜所属策略。
|
||
ids = set(child_ids)
|
||
rows = _rows(db.fetch_all("SELECT * FROM pms_qmt_inbox WHERE msg_type='trade' "
|
||
'ORDER BY seq ASC LIMIT :n', {'n': int(limit)}), int(limit))
|
||
return [r for r in rows if r['payload'].get('instruction_id') in ids
|
||
and r.get('dedup_key') is not None]
|