2024-04-17 16:45:36 +08:00
|
|
|
import os
|
|
|
|
|
2024-04-13 01:40:25 +08:00
|
|
|
import pymssql
|
|
|
|
import threading
|
|
|
|
import time
|
2024-04-17 16:45:36 +08:00
|
|
|
from .Process_Client import *
|
2024-04-13 01:40:25 +08:00
|
|
|
|
|
|
|
|
2024-04-17 16:45:36 +08:00
|
|
|
class database(ProcessClient):
|
2024-04-13 01:40:25 +08:00
|
|
|
def __init__(self):
|
2024-04-17 16:45:36 +08:00
|
|
|
ProcessClient.__init__(self)
|
2024-04-13 01:40:25 +08:00
|
|
|
self.database_server = 'localhost'
|
|
|
|
self.database_user = 'Chat'
|
|
|
|
self.database_password = '123456'
|
|
|
|
self.database_port = 8972
|
|
|
|
self.database_name = 'Chat'
|
|
|
|
self.database_state = False
|
|
|
|
self.database_conn = None
|
|
|
|
self.database_cursor = None
|
|
|
|
|
2024-04-17 16:45:36 +08:00
|
|
|
current_file_path = __file__
|
|
|
|
current_file_name = os.path.basename(current_file_path).split('.')[0]
|
|
|
|
self.Process_client_send("Server", "Name", current_file_name)
|
|
|
|
|
2024-04-13 01:40:25 +08:00
|
|
|
self.link_database_thread = threading.Thread(target=self.link_database)
|
|
|
|
self.link_database_thread.start()
|
|
|
|
|
2024-04-13 13:52:36 +08:00
|
|
|
# 创建一个互斥锁对象
|
|
|
|
self.lock = threading.Lock()
|
|
|
|
|
2024-04-13 01:40:25 +08:00
|
|
|
def link_database(self):
|
|
|
|
while not self.database_state:
|
|
|
|
time.sleep(1)
|
|
|
|
try:
|
|
|
|
self.database_conn = pymssql.connect(server=self.database_server,
|
|
|
|
user=self.database_user,
|
|
|
|
password=self.database_password,
|
|
|
|
port=self.database_port,
|
|
|
|
database=self.database_name)
|
|
|
|
self.database_cursor = self.database_conn.cursor()
|
|
|
|
self.database_state = True
|
|
|
|
except pymssql.Error as a:
|
|
|
|
print(a)
|
|
|
|
|
2024-04-18 13:45:10 +08:00
|
|
|
def check_account_state(self, client_id, Id: int, password):
|
2024-04-13 01:40:25 +08:00
|
|
|
if self.database_state:
|
|
|
|
try:
|
2024-04-18 13:45:10 +08:00
|
|
|
state = 2
|
2024-04-13 01:40:25 +08:00
|
|
|
self.database_cursor.execute(f"select Id,Password from Account where Id = {Id}")
|
|
|
|
request_data = self.database_cursor.fetchall()
|
|
|
|
if len(request_data) != 0:
|
|
|
|
if request_data[0][1] == password:
|
2024-04-18 13:45:10 +08:00
|
|
|
state = 0
|
2024-04-13 01:40:25 +08:00
|
|
|
else:
|
2024-04-18 13:45:10 +08:00
|
|
|
state = 1
|
|
|
|
|
|
|
|
content = {"account": Id, "status": state}
|
|
|
|
data = {"client_id": client_id, "target": "客户端", "genre": "登录", "data": content}
|
|
|
|
self.Process_client_send('Session_client', 'send_client', data)
|
|
|
|
|
2024-04-13 01:40:25 +08:00
|
|
|
except pymssql as a:
|
|
|
|
self.database_state = False
|
|
|
|
print(a)
|
|
|
|
|
2024-04-18 13:45:10 +08:00
|
|
|
def sign_account(self, client_id, NetName, Password):
|
2024-04-13 01:40:25 +08:00
|
|
|
if self.database_state:
|
2024-04-13 13:52:36 +08:00
|
|
|
# 获取锁
|
|
|
|
self.lock.acquire()
|
2024-04-13 01:40:25 +08:00
|
|
|
try:
|
2024-04-13 13:52:36 +08:00
|
|
|
self.database_cursor.execute("insert into Account(NetName, Password)"
|
|
|
|
f"values ('{NetName}','{Password}')")
|
|
|
|
self.database_conn.commit()
|
|
|
|
self.database_cursor.execute(f"SELECT SCOPE_IDENTITY() AS LastInsertedId;")
|
|
|
|
Id = self.database_cursor.fetchone()[0]
|
|
|
|
# 释放锁
|
|
|
|
self.lock.release()
|
2024-04-18 13:45:10 +08:00
|
|
|
info = {"Id": int(Id), "NetName": NetName, "Password": Password}
|
|
|
|
|
|
|
|
content = {"client_id": client_id, "target": "客户端", "genre": "注册", "data": info}
|
|
|
|
self.Process_client_send('Session_client', 'send_client', content)
|
|
|
|
|
2024-04-13 01:40:25 +08:00
|
|
|
except pymssql as a:
|
2024-04-13 13:52:36 +08:00
|
|
|
self.lock.release()
|
2024-04-13 01:40:25 +08:00
|
|
|
self.database_state = False
|
2024-04-13 13:52:36 +08:00
|
|
|
return False
|
2024-04-13 01:40:25 +08:00
|
|
|
|
2024-04-13 13:52:36 +08:00
|
|
|
def alter_state_database(self, Id: int, sate):
|
|
|
|
if self.database_state:
|
|
|
|
try:
|
|
|
|
self.database_cursor.execute(f"update Account set State = N'{sate}' where Id = {Id}")
|
|
|
|
self.database_conn.commit()
|
|
|
|
except pymssql as a:
|
|
|
|
self.database_state = False
|
|
|
|
print(a)
|
2024-04-17 16:45:36 +08:00
|
|
|
|
2024-04-18 13:45:10 +08:00
|
|
|
def detection_data(self, Id, date):
|
|
|
|
if date is None:
|
|
|
|
datas = []
|
|
|
|
|
|
|
|
self.database_cursor.execute(f"select Id,Password,NetName,UpDataTime from Account where Id = {Id}")
|
|
|
|
Account_database = self.database_cursor.fetchall()
|
|
|
|
Account_content = {'Id': Account_database[0][0],
|
|
|
|
'Password': Account_database[0][1],
|
|
|
|
'NetName': Account_database[0][2],
|
|
|
|
'UpDataTime': Account_database[0][3].strftime('%Y-%m-%d %H:%M:%S')}
|
|
|
|
Account_data = {"Account": Account_content}
|
|
|
|
datas.append(Account_data)
|
|
|
|
|
|
|
|
self.database_cursor.execute(f"select ContactsId,Remark,State from Contacts where UserId = {Id}")
|
|
|
|
Contacts_database = self.database_cursor.fetchall()
|
|
|
|
Contacts_data = {"Contacts": Contacts_database}
|
|
|
|
datas.append(Contacts_data)
|
|
|
|
|
|
|
|
self.database_cursor.execute(f"select ContactsId,Remark,State from Contacts where UserId = {Id}")
|
|
|
|
Contacts_database = self.database_cursor.fetchall()
|
|
|
|
Contacts_data = {"Contacts": Contacts_database}
|
|
|
|
datas.append(Contacts_data)
|
|
|
|
|
|
|
|
self.database_cursor.execute(f"select * from History where Send = {Id} or Receive ={Id}")
|
|
|
|
History_database = self.database_cursor.fetchall()
|
|
|
|
History_data = {"History": History_database}
|
|
|
|
datas.append(History_data)
|
|
|
|
for data in datas:
|
|
|
|
content = {"client_id": Id, "target": "客户端", "genre": "数据更新", "data": data}
|
|
|
|
self.Process_client_send('Session_client', 'send_client', content)
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-04-17 16:45:36 +08:00
|
|
|
def Process_client_pick(self, data):
|
|
|
|
if data['target'] in ['ALL', 'Database_formula']:
|
|
|
|
match data['function']:
|
|
|
|
case 'check_account_state':
|
2024-04-18 01:14:04 +08:00
|
|
|
client_id = data['content']['client_id']
|
2024-04-17 16:45:36 +08:00
|
|
|
account = data['content']['account']
|
|
|
|
password = data['content']['password']
|
2024-04-18 13:45:10 +08:00
|
|
|
self.check_account_state(client_id, account, password)
|
2024-04-17 16:45:36 +08:00
|
|
|
|
|
|
|
case 'sign_account':
|
2024-04-18 01:14:04 +08:00
|
|
|
client_id = data['content']['client_id']
|
2024-04-17 16:45:36 +08:00
|
|
|
account = data['content']['account']
|
|
|
|
password = data['content']['password']
|
2024-04-18 13:45:10 +08:00
|
|
|
self.sign_account(client_id, account, password)
|
2024-04-18 01:14:04 +08:00
|
|
|
|
|
|
|
case 'alter_state_database':
|
|
|
|
self.alter_state_database(data['content']['Id'], data['content']['sate'])
|
|
|
|
|
2024-04-18 13:45:10 +08:00
|
|
|
case 'detection_data':
|
|
|
|
self.detection_data(data['content']['client_id'], data['content']['date'])
|