64 lines
2.8 KiB
Python
64 lines
2.8 KiB
Python
"""执行依据只更新独立JSON字段,避免刷新任务覆盖委托进度。"""
|
||
import json
|
||
from app.db import session as db
|
||
|
||
|
||
def save_basis(instruction_id, value):
|
||
affected = db.execute("UPDATE pms_instruction SET progress_json=JSON_SET("
|
||
"COALESCE(progress_json,JSON_OBJECT()),'$.execution_basis_cache',JSON_EXTRACT(:body,'$')) "
|
||
"WHERE instruction_id=:iid", {'iid': instruction_id, 'body': json.dumps(value, ensure_ascii=False)})
|
||
if not affected:
|
||
raise RuntimeError('执行依据缓存没有写入父指令')
|
||
|
||
|
||
def pending_trades(child_ids):
|
||
if not child_ids:
|
||
return 0
|
||
args = {'p': 0, 'orphan': 3}
|
||
keys = []
|
||
for index, iid in enumerate(child_ids):
|
||
key = 'c'+str(index)
|
||
args[key] = iid
|
||
keys.append(':'+key)
|
||
ids = ','.join(keys)
|
||
row = db.fetch_one("SELECT COUNT(*) AS n FROM pms_qmt_inbox WHERE ((msg_type='trade' "
|
||
"AND processed IN (:p,:orphan)) OR ("+_correction_sql()+" AND "+_pending_correction_sql()+")) AND (corr_id IN ("+ids+
|
||
") OR JSON_UNQUOTE(JSON_EXTRACT(payload_json,'$.instruction_id')) IN ("+ids+'))', args)
|
||
return int((row or {}).get('n') or 0)
|
||
|
||
|
||
def _correction_sql():
|
||
return ("(msg_type='trade_correction' OR (msg_type='order_update' AND "
|
||
"((JSON_CONTAINS_PATH(payload_json,'one','$.amended') AND "
|
||
"NOT COALESCE(JSON_CONTAINS(payload_json,'false','$.amended'),0)) OR "
|
||
"JSON_CONTAINS_PATH(payload_json,'one','$.correction_id') OR "
|
||
"JSON_UNQUOTE(JSON_EXTRACT(payload_json,'$.reason'))='trade_correction')))")
|
||
|
||
|
||
def _pending_correction_sql():
|
||
# 升级前未知类型或amended被通道存成2,不等于修正已经入账;重复占位的note不同。
|
||
return ("(processed IN (0,3) OR (processed=2 AND process_note IN "
|
||
"('trade_correction 由通道进程消化, 不入账','order_update 由通道进程消化, 不入账')))")
|
||
|
||
|
||
def pending_corrections(ts_code=None, child_ids=None):
|
||
"""账户买入读全部未核实修正;按证券或子单查询供卖出及已有父指令守卫。"""
|
||
args = {}
|
||
where = [_correction_sql(), _pending_correction_sql()]
|
||
identity = []
|
||
if ts_code:
|
||
args['code'] = ts_code
|
||
identity.append("JSON_UNQUOTE(JSON_EXTRACT(payload_json,'$.ts_code'))=:code")
|
||
if child_ids:
|
||
keys = []
|
||
for index, iid in enumerate(child_ids):
|
||
key = 'c'+str(index)
|
||
args[key] = iid
|
||
keys.append(':'+key)
|
||
ids = ','.join(keys)
|
||
identity.append("(corr_id IN ("+ids+") OR JSON_UNQUOTE(JSON_EXTRACT(payload_json,'$.instruction_id')) IN ("+ids+'))')
|
||
if identity:
|
||
where.append('('+' OR '.join(identity)+')')
|
||
row = db.fetch_one('SELECT COUNT(*) AS n FROM pms_qmt_inbox WHERE '+' AND '.join(where), args)
|
||
return int((row or {}).get('n') or 0)
|