105 lines
4.2 KiB
Python
105 lines
4.2 KiB
Python
from PySide6.QtWidgets import QWidget, QApplication, QFileDialog, QTableWidgetItem
|
|
from Ui_新增目标关系 import Ui_Form
|
|
from database_tools_2 import *
|
|
|
|
class MyWindow_xzgx(QWidget,Ui_Form):
|
|
def __init__(self,database_name):
|
|
super().__init__()
|
|
self.database_name = database_name
|
|
self.current_database_name = database_name
|
|
self.setupUi(self)
|
|
self.bind()
|
|
def setdbname(self,string):
|
|
self.database_name = string
|
|
self.pushButton_import.setDisabled(True)
|
|
self.pushButton_commit.setDisabled(True)
|
|
try:
|
|
self.db.close()
|
|
except:
|
|
pass
|
|
self.db = connect(hostname=db_host, port=db_port, username= db_user, pwd= db_pwd, db=self.database_name)
|
|
# print(self.database_name)
|
|
def bind(self):
|
|
self.pushButton_import.setDisabled(True)
|
|
self.pushButton_commit.setDisabled(True)
|
|
self.pushButton_checkvalidity.clicked.connect(self.checkvalidity)
|
|
self.pushButton_import.clicked.connect(self.importdata)
|
|
self.pushButton_commit.clicked.connect(self.commit)
|
|
self.plainTextEdit_data.textChanged.connect(self.disable_import)
|
|
self.db = connect(hostname=db_host, port=db_port, username= db_user, pwd= db_pwd, db=self.database_name)
|
|
self.mycursor = self.db.cursor()
|
|
self.mycursor.execute("SELECT objid from lessonobj;")
|
|
self.objids = [item[0] for item in self.mycursor.fetchall()]
|
|
# print(self.objids)
|
|
|
|
def checkvalidity(self):
|
|
self.data_raw = self.plainTextEdit_data.toPlainText().strip()
|
|
self.data = re.sub(r"[ \t]+","\t",self.data_raw)
|
|
self.datalines = [line.strip() for line in self.data.split("\n") if not line.strip() == ""]
|
|
output = f"一共有 {len(self.datalines)} 行数据.\n"
|
|
self.valid_lines = []
|
|
validstatus = True
|
|
for line in self.datalines:
|
|
linevalidflag = True
|
|
objids = line.split("\t")
|
|
mainid = objids.pop(0)
|
|
if not mainid in self.objids:
|
|
linevalidflag = False
|
|
for objid in objids:
|
|
if not objid in self.objids:
|
|
linevalidflag = False
|
|
break
|
|
if linevalidflag == False:
|
|
output += f"首个id为 {mainid} 的行数据有误\n"
|
|
validstatus = False
|
|
else:
|
|
self.valid_lines.append((mainid,objids))
|
|
if validstatus == True:
|
|
output += "数据全部为有效数据"
|
|
self.pushButton_import.setEnabled(True)
|
|
# print(self.valid_lines)
|
|
self.plainTextEdit_validity.setPlainText(output)
|
|
|
|
|
|
|
|
def disable_import(self):
|
|
self.pushButton_import.setDisabled(True)
|
|
def importdata(self):
|
|
self.mycursor = self.db.cursor()
|
|
output = "导入记录:\n"
|
|
for line in tqdm.tqdm(self.valid_lines):
|
|
mainid = line[0]
|
|
for precid in line[1]:
|
|
self.mycursor.execute("SELECT * FROM objinherit WHERE objid = %s AND inherit_from = %s;",(mainid,precid))
|
|
ret_list = self.mycursor.fetchall()
|
|
if len(ret_list) >= 1:
|
|
output += f"*** {mainid} 以 {precid} 为前序的记录已在数据库 {self.database_name} 中\n"
|
|
else:
|
|
output += f"--- {mainid} 与 {precid} 的前序关系已被记录\n"
|
|
sql1 = "INSERT INTO objinherit (objid,inherit_from) VALUE (%s,%s);"
|
|
val1 = (mainid,precid)
|
|
self.mycursor.execute(sql1,val1)
|
|
sql2 = "INSERT INTO logs (DATE,TIME,username,action,db_content) VALUE (%s,%s,%s,%s,%s);"
|
|
val2 = (GetDate(),GetTime(),get_git_username(),"新增目标前序",f"{mainid} <- {precid}")
|
|
self.mycursor.execute(sql2,val2)
|
|
self.plainTextEdit_validity.setPlainText(output)
|
|
self.pushButton_commit.setEnabled(True)
|
|
def commit(self):
|
|
self.db.commit()
|
|
self.pushButton_import.setDisabled(True)
|
|
self.pushButton_commit.setDisabled(True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
app = QApplication([])
|
|
windows = MyWindow_xzgx("tikutest")
|
|
windows.show()
|
|
app.exec()
|
|
|