2023-11-15 21:57:29 +08:00
|
|
|
import os.path
|
2023-11-12 21:51:33 +08:00
|
|
|
import sqlite3
|
2023-11-18 11:51:58 +08:00
|
|
|
import threading
|
2023-11-12 21:51:33 +08:00
|
|
|
|
2023-11-18 11:51:58 +08:00
|
|
|
lock = threading.Lock()
|
2023-11-15 21:57:29 +08:00
|
|
|
DB = None
|
|
|
|
cursor = None
|
2023-12-01 22:37:45 +08:00
|
|
|
db_path = "./app/Database/Msg/MicroMsg.db"
|
2023-11-15 21:57:29 +08:00
|
|
|
|
|
|
|
|
2023-12-01 22:37:45 +08:00
|
|
|
def singleton(cls):
|
|
|
|
_instance = {}
|
|
|
|
|
|
|
|
def inner():
|
|
|
|
if cls not in _instance:
|
|
|
|
_instance[cls] = cls()
|
|
|
|
return _instance[cls]
|
|
|
|
|
|
|
|
return inner
|
2023-11-16 00:13:49 +08:00
|
|
|
|
|
|
|
|
2023-11-15 21:57:29 +08:00
|
|
|
def is_database_exist():
|
2023-12-01 22:37:45 +08:00
|
|
|
return os.path.exists(db_path)
|
|
|
|
|
2023-12-12 13:46:57 +08:00
|
|
|
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()
|
|
|
|
|
2023-12-01 22:37:45 +08:00
|
|
|
|
|
|
|
@singleton
|
|
|
|
class MicroMsg:
|
|
|
|
def __init__(self):
|
|
|
|
self.DB = None
|
|
|
|
self.cursor = None
|
|
|
|
self.open_flag = False
|
|
|
|
self.init_database()
|
2023-11-12 21:51:33 +08:00
|
|
|
|
2023-12-01 22:37:45 +08:00
|
|
|
def init_database(self):
|
|
|
|
if not self.open_flag:
|
|
|
|
if os.path.exists(db_path):
|
|
|
|
self.DB = sqlite3.connect(db_path, check_same_thread=False)
|
|
|
|
# '''创建游标'''
|
|
|
|
self.cursor = self.DB.cursor()
|
|
|
|
self.open_flag = True
|
|
|
|
if lock.locked():
|
|
|
|
lock.release()
|
2023-11-12 21:51:33 +08:00
|
|
|
|
2023-12-01 22:37:45 +08:00
|
|
|
def get_contact(self):
|
|
|
|
if not self.open_flag:
|
|
|
|
return None
|
|
|
|
try:
|
|
|
|
lock.acquire(True)
|
2023-12-04 16:34:26 +08:00
|
|
|
sql = '''SELECT UserName, Alias, Type, Remark, NickName, PYInitial, RemarkPYInitial, ContactHeadImgUrl.smallHeadImgUrl, ContactHeadImgUrl.bigHeadImgUrl
|
|
|
|
FROM Contact
|
|
|
|
INNER JOIN ContactHeadImgUrl ON Contact.UserName = ContactHeadImgUrl.usrName
|
|
|
|
WHERE Type % 2 = 1
|
|
|
|
AND NickName != ''
|
|
|
|
ORDER BY
|
|
|
|
CASE
|
|
|
|
WHEN RemarkPYInitial = '' THEN PYInitial
|
|
|
|
ELSE RemarkPYInitial
|
|
|
|
END ASC
|
2023-12-01 22:37:45 +08:00
|
|
|
'''
|
|
|
|
self.cursor.execute(sql)
|
|
|
|
result = self.cursor.fetchall()
|
|
|
|
finally:
|
|
|
|
lock.release()
|
2023-12-12 13:46:57 +08:00
|
|
|
return MicroMSGMsg().get_contact(result)
|
2023-11-12 21:51:33 +08:00
|
|
|
|
2023-12-06 15:34:56 +08:00
|
|
|
def get_contact_by_username(self, username):
|
|
|
|
if not self.open_flag:
|
|
|
|
return None
|
|
|
|
try:
|
|
|
|
lock.acquire(True)
|
|
|
|
sql = '''SELECT UserName, Alias, Type, Remark, NickName, PYInitial, RemarkPYInitial, ContactHeadImgUrl.smallHeadImgUrl, ContactHeadImgUrl.bigHeadImgUrl
|
|
|
|
FROM Contact
|
|
|
|
INNER JOIN ContactHeadImgUrl ON Contact.UserName = ContactHeadImgUrl.usrName
|
|
|
|
WHERE UserName = ?
|
|
|
|
'''
|
|
|
|
self.cursor.execute(sql, [username])
|
|
|
|
result = self.cursor.fetchone()
|
|
|
|
finally:
|
|
|
|
lock.release()
|
|
|
|
return result
|
|
|
|
|
|
|
|
def get_chatroom_info(self, chatroomname):
|
|
|
|
'''
|
|
|
|
获取群聊信息
|
|
|
|
'''
|
|
|
|
if not self.open_flag:
|
|
|
|
return None
|
|
|
|
try:
|
|
|
|
lock.acquire(True)
|
|
|
|
sql = '''SELECT ChatRoomName, RoomData FROM ChatRoom WHERE ChatRoomName = ?'''
|
|
|
|
self.cursor.execute(sql, [chatroomname])
|
|
|
|
result = self.cursor.fetchone()
|
|
|
|
finally:
|
|
|
|
lock.release()
|
|
|
|
return result
|
|
|
|
|
2023-12-01 22:37:45 +08:00
|
|
|
def close(self):
|
|
|
|
if self.open_flag:
|
|
|
|
try:
|
|
|
|
lock.acquire(True)
|
|
|
|
self.open_flag = False
|
|
|
|
self.DB.close()
|
|
|
|
finally:
|
|
|
|
lock.release()
|
2023-11-12 21:51:33 +08:00
|
|
|
|
2023-12-01 22:37:45 +08:00
|
|
|
def __del__(self):
|
|
|
|
self.close()
|
2023-11-16 23:16:38 +08:00
|
|
|
|
|
|
|
|
2023-11-12 21:51:33 +08:00
|
|
|
if __name__ == '__main__':
|
2023-12-01 22:37:45 +08:00
|
|
|
pass
|
|
|
|
# get_contact()
|