From 0e92c5859988f68c2c6e5223dbfd542ec2a87f48 Mon Sep 17 00:00:00 2001 From: STDquantum <405720329@qq.com> Date: Tue, 12 Dec 2023 13:46:57 +0800 Subject: [PATCH] =?UTF-8?q?=E8=BF=9B=E5=85=A5=E7=95=8C=E9=9D=A2=E5=90=8E?= =?UTF-8?q?=E8=81=94=E7=B3=BB=E4=BA=BA=E6=8C=89=E7=85=A7=E6=9C=80=E6=96=B0?= =?UTF-8?q?=E8=81=8A=E5=A4=A9=E6=97=B6=E9=97=B4=E6=8E=92=E5=BA=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/DataBase/micro_msg.py | 57 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 56 insertions(+), 1 deletion(-) diff --git a/app/DataBase/micro_msg.py b/app/DataBase/micro_msg.py index 23e7d7e..7cc2ee2 100644 --- a/app/DataBase/micro_msg.py +++ b/app/DataBase/micro_msg.py @@ -22,6 +22,61 @@ def singleton(cls): def is_database_exist(): return os.path.exists(db_path) +lockMSG = threading.Lock() +DBMSG = None +cursorMSG = None +db_msg_path = "./app/Database/Msg/MSG.db" + +@singleton +class MicroMSGMsg: + def __init__(self): + self.DBMSG = None + self.cursorMSG = None + self.open_flag = False + self.init_database() + + def init_database(self): + if not self.open_flag: + if os.path.exists(db_msg_path): + self.DBMSG = sqlite3.connect(db_msg_path, check_same_thread=False) + # '''创建游标''' + self.cursorMSG = self.DBMSG.cursor() + self.open_flag = True + if lockMSG.locked(): + lockMSG.release() + + def get_contact(self, contacts): + if not self.open_flag: + return None + try: + lockMSG.acquire(True) + sql = '''select StrTalker, MAX(CreateTime) from MSG group by StrTalker''' + self.cursorMSG.execute(sql) + res = self.cursorMSG.fetchall() + res = {StrTalker: CreateTime for StrTalker, CreateTime in res} + contacts = [list(cur_contact) for cur_contact in contacts] + for i, cur_contact in enumerate(contacts): + if cur_contact[0] in res: + contacts[i].append(res[cur_contact[0]]) + else: + contacts[i].append(0) + contacts.sort(key=lambda cur_contact: cur_contact[-1], reverse=True) + finally: + lockMSG.release() + return contacts + + def close(self): + if self.open_flag: + try: + lockMSG.acquire(True) + self.open_flag = False + self.DBMSG.close() + finally: + lockMSG.release() + + def __del__(self): + self.close() + @singleton class MicroMsg: @@ -61,7 +116,7 @@ class MicroMsg: result = self.cursor.fetchall() finally: lock.release() - return result + return MicroMSGMsg().get_contact(result) def get_contact_by_username(self, username): if not self.open_flag: