practice_code/python/test/Server/Transmission/Database_formula.py

164 lines
6.9 KiB
Python
Raw Normal View History

2024-04-17 16:45:36 +08:00
import os
import pymssql
import threading
import time
2024-04-17 16:45:36 +08:00
from .Process_Client import *
2024-04-17 16:45:36 +08:00
class database(ProcessClient):
def __init__(self):
2024-04-17 16:45:36 +08:00
ProcessClient.__init__(self)
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)
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()
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):
if self.database_state:
try:
2024-04-18 13:45:10 +08:00
state = 2
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
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)
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):
if self.database_state:
2024-04-13 13:52:36 +08:00
# 获取锁
self.lock.acquire()
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)
except pymssql as a:
2024-04-13 13:52:36 +08:00
self.lock.release()
self.database_state = False
2024-04-13 13:52:36 +08:00
return False
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):
2024-04-19 00:48:24 +08:00
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')}
if Account_content['UpDataTime'] == date:
2024-04-20 01:36:15 +08:00
content = {"client_id": Id, "target": "客户端", "genre": "数据更新", "data": {"更新完成": Id}}
self.Process_client_send('Session_client', 'send_client', content)
2024-04-19 00:48:24 +08:00
return
Account_data = {"Account": Account_content}
datas.append(Account_data)
self.database_cursor.execute(
f"select ContactsId,Remark,State,UpDataTime from Contacts where UserId = {Id} and UpDataTime > '{date}'")
Contacts_database = self.database_cursor.fetchall()
for Contact in Contacts_database:
Contacts_content = {'Contact': Contact[0],
'Remark': Contact[1],
'State': Contact[2],
'UpDataTime': Contact[3].strftime('%Y-%m-%d %H:%M:%S')}
Contacts_data = {"Contacts": Contacts_content}
2024-04-18 13:45:10 +08:00
datas.append(Contacts_data)
2024-04-19 00:48:24 +08:00
self.database_cursor.execute(f"select * from History where Send = {Id} or Receive = {Id} and Time > '{date}'")
History_database = self.database_cursor.fetchall()
for History in History_database:
content = {
'Send': History[0],
'Receive': History[1],
'Type': History[2],
'Content': History[3],
'Time': History[4].strftime('%Y-%m-%d %H:%M:%S')}
History_data = {"History": content}
2024-04-18 13:45:10 +08:00
datas.append(History_data)
2024-04-20 01:36:15 +08:00
datas.append({"更新完成": Id})
2024-04-18 13:45:10 +08:00
2024-04-19 00:48:24 +08:00
for data in datas:
time.sleep(0.1)
content = {"client_id": Id, "target": "客户端", "genre": "数据更新", "data": data}
self.Process_client_send('Session_client', 'send_client', content)
2024-04-18 13:45:10 +08:00
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':
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':
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)
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'])