|
|
|
@ -6,7 +6,8 @@ from enum import Enum
|
|
|
|
|
from multiprocessing import Queue
|
|
|
|
|
from settings import sqlite_file
|
|
|
|
|
from queue import Empty
|
|
|
|
|
|
|
|
|
|
import random
|
|
|
|
|
import json
|
|
|
|
|
|
|
|
|
|
class MessageType(Enum):
|
|
|
|
|
ENTER_LIVE_ROOM = 1
|
|
|
|
@ -215,17 +216,72 @@ class LiveChatConfig:
|
|
|
|
|
self.conn.commit()
|
|
|
|
|
cursor.close()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def flush_message(self):
|
|
|
|
|
cursor = self.conn.cursor()
|
|
|
|
|
# 1.删除所有batch_number=0且status=1的数据
|
|
|
|
|
cursor.execute('delete from message where batch_number = 0 and status = 1')
|
|
|
|
|
# 2.将batch_number=1的数据的更新为0
|
|
|
|
|
cursor.execute('update message set batch_number = 0 where batch_number = 1')
|
|
|
|
|
# 3.生成新的备用系统文案,batch_number=1
|
|
|
|
|
|
|
|
|
|
self.conn.commit()
|
|
|
|
|
cursor.close()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def system_messages_random_mix_dict(self, product_id=None, ensure_mixed=True):
|
|
|
|
|
"""
|
|
|
|
|
随机跨组拼接,返回 {type: message} 的字典 JSON
|
|
|
|
|
"""
|
|
|
|
|
cursor = self.conn.cursor()
|
|
|
|
|
if product_id is None:
|
|
|
|
|
cursor.execute(
|
|
|
|
|
"""
|
|
|
|
|
select script_type_order_num, script_group_id, type, message
|
|
|
|
|
from system_message
|
|
|
|
|
order by script_type_order_num asc, script_group_id asc, id asc
|
|
|
|
|
"""
|
|
|
|
|
)
|
|
|
|
|
rows = cursor.fetchall()
|
|
|
|
|
else:
|
|
|
|
|
cursor.execute(
|
|
|
|
|
"""
|
|
|
|
|
select script_type_order_num, script_group_id, type, message
|
|
|
|
|
from system_message
|
|
|
|
|
where product_id = ?
|
|
|
|
|
order by script_type_order_num asc, script_group_id asc, id asc
|
|
|
|
|
""",
|
|
|
|
|
(product_id,)
|
|
|
|
|
)
|
|
|
|
|
rows = cursor.fetchall()
|
|
|
|
|
cursor.close()
|
|
|
|
|
|
|
|
|
|
# 分层整理
|
|
|
|
|
by_order = {}
|
|
|
|
|
for order_num, group_id, _type, msg in rows:
|
|
|
|
|
by_order.setdefault(order_num, [])
|
|
|
|
|
by_order[order_num].append({
|
|
|
|
|
"order": int(order_num),
|
|
|
|
|
"group": int(group_id),
|
|
|
|
|
"type": _type,
|
|
|
|
|
"message": msg
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
# 逐层随机挑选
|
|
|
|
|
result_dict = {}
|
|
|
|
|
used_groups = set()
|
|
|
|
|
orders = sorted(by_order.keys())
|
|
|
|
|
for order_num in orders:
|
|
|
|
|
candidates = by_order[order_num]
|
|
|
|
|
if not candidates:
|
|
|
|
|
continue
|
|
|
|
|
if ensure_mixed:
|
|
|
|
|
prefer = [c for c in candidates if c["group"] not in used_groups]
|
|
|
|
|
pick_source = prefer if prefer else candidates
|
|
|
|
|
else:
|
|
|
|
|
pick_source = candidates
|
|
|
|
|
choice = random.choice(pick_source)
|
|
|
|
|
result_dict[choice["type"]] = choice["message"]
|
|
|
|
|
used_groups.add(choice["group"])
|
|
|
|
|
|
|
|
|
|
return json.dumps(result_dict, ensure_ascii=False, indent=4)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class PromptQueue:
|
|
|
|
|
def __init__(self, maxsize=0):
|
|
|
|
|
self.queue = Queue(maxsize)
|
|
|
|
|