"""
|
下单队列管理
|
"""
|
|
# 尚未处理
|
import json
|
import random
|
import time
|
|
from utils import tool
|
from db import mysql_data_delegate as mysql_data
|
|
STATE_NOT_PROCESS = 0
|
STATE_NOT_BUY = 1
|
STATE_BUY = 2
|
|
|
@tool.singleton
|
class PlaceOrderRecordManager:
|
"""
|
下单记录管理
|
"""
|
|
def __init__(self, day):
|
self.day = day
|
# (ID,代码,板块信息, 大单信息, 时间, 价格, 涨幅)
|
self.not_process_records = []
|
self.mysql_db = mysql_data.Mysqldb()
|
self.__load_data()
|
|
def __load_data(self):
|
results = self.mysql_db.select_all(
|
f"select * from low_suction_place_order_records where day ='{self.day}' and state = {STATE_NOT_PROCESS} order by time_str")
|
self.not_process_records.clear()
|
if results:
|
self.not_process_records = [(r[0], r[2], json.loads(r[3]), r[6], r[7], float(r[8]), float(r[9])) for r in
|
results]
|
|
def set_buy(self, id):
|
record = None
|
for r in self.not_process_records:
|
if r[0] == id:
|
record = r
|
if record:
|
self.not_process_records.remove(record)
|
self.mysql_db.execute(
|
f"update low_suction_place_order_records set state={STATE_BUY},update_time=now() where id ='{id}'")
|
# 将后续还未处理的同代码的拒绝
|
ids = [r[0] for r in self.not_process_records if r[1] == record[1]]
|
for id_ in ids:
|
self.set_not_buy(id_, "已买入")
|
|
def set_not_buy(self, id, desc):
|
for r in self.not_process_records:
|
if r[0] == id:
|
self.not_process_records.remove(r)
|
self.mysql_db.execute(
|
f"update low_suction_place_order_records set state={STATE_NOT_BUY},update_time=now(), state_desc='{desc}' where id ='{id}'")
|
|
def add_record(self, code, time_str, plate_infos, big_order_info_str, price, rate):
|
_id = f"{self.day}_{code}_{time_str.replace(':', '')}_{random.randint(0, 10000)}"
|
data = (_id, code, plate_infos, big_order_info_str, time_str, price, rate)
|
self.mysql_db.execute(
|
f"insert into low_suction_place_order_records(id,day,code,plates_info,state, big_orders_info, time_str,price, rate, create_time) values('{_id}','{self.day}','{code}','{json.dumps(plate_infos, ensure_ascii=False)}',{STATE_NOT_PROCESS},'{big_order_info_str}','{time_str}', {price},{rate},now())")
|
self.not_process_records.append(data)
|
|
def get_not_process_record(self):
|
if not self.not_process_records:
|
return None
|
return self.not_process_records[0]
|
|
def get_not_process_record_by_id(self, _id):
|
for r in self.not_process_records:
|
if r[0] == _id:
|
return r
|
return None
|
|
def get_not_process_records_by_code(self, code):
|
return [r for r in self.not_process_records if r[1] == code]
|
|
|
if __name__ == "__main__":
|
manager = PlaceOrderRecordManager("2025-07-07")
|
print(manager.not_process_records)
|
manager.add_record("000333", '09:30:32', {"家电": ["000111", "000222"]}, "500w/600w", 10.23, 5.2)
|
# manager.add_record("000333", '09:30:30', {"家电": ["000111", "000222"]}, "500w/600w")
|
|
# manager.set_not_buy("2025-07-07_000333_1751880205848_3395", "人工驳回")
|
print(manager.not_process_records)
|