-
Notifications
You must be signed in to change notification settings - Fork 36
/
UserManage.py
172 lines (161 loc) · 6.92 KB
/
UserManage.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import qdarkstyle
from PyQt5.QtSql import *
import time
import sip
class UserManage(QDialog):
def __init__(self,parent=None):
super(UserManage, self).__init__(parent)
self.resize(280, 400)
self.layout = QVBoxLayout()
self.setLayout(self.layout)
self.setWindowTitle("管理用户")
# 用户数
self.userCount = 0
self.oldDeleteId = ""
self.oldDeleteName = ""
self.deleteId = ""
self.deleteName = ""
self.setUpUI()
def setUpUI(self):
self.db = QSqlDatabase.addDatabase("QSQLITE")
self.db.setDatabaseName('./db/LibraryManagement.db')
self.db.open()
self.query = QSqlQuery()
self.getResult()
# 表格设置
self.tableWidget = QTableWidget()
self.tableWidget.setRowCount(self.userCount)
self.tableWidget.setColumnCount(2)
self.tableWidget.setHorizontalHeaderLabels(['学号', '姓名'])
# 不可编辑
self.tableWidget.setEditTriggers(QAbstractItemView.NoEditTriggers)
# 标题可拉伸
self.tableWidget.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch)
# 整行选中
self.tableWidget.setSelectionBehavior(QAbstractItemView.SelectRows)
self.layout.addWidget(self.tableWidget)
self.setRows()
self.deleteUserButton = QPushButton("删 除 用 户")
hlayout = QHBoxLayout()
hlayout.addWidget(self.deleteUserButton, Qt.AlignHCenter)
self.widget = QWidget()
self.widget.setLayout(hlayout)
self.widget.setFixedHeight(48)
font = QFont()
font.setPixelSize(15)
self.deleteUserButton.setFixedHeight(36)
self.deleteUserButton.setFixedWidth(180)
self.deleteUserButton.setFont(font)
self.layout.addWidget(self.widget, Qt.AlignCenter)
# 设置信号
self.deleteUserButton.clicked.connect(self.deleteUser)
self.tableWidget.itemClicked.connect(self.getStudentInfo)
def getResult(self):
sql = "SELECT StudentId,Name FROM User WHERE IsAdmin=0"
self.query.exec_(sql)
self.userCount = 0;
while (self.query.next()):
self.userCount += 1;
sql = "SELECT StudentId,Name FROM User WHERE IsAdmin=0"
self.query.exec_(sql)
def setRows(self):
font = QFont()
font.setPixelSize(14)
for i in range(self.userCount):
if (self.query.next()):
StudentIdItem = QTableWidgetItem(self.query.value(0))
StudentNameItem = QTableWidgetItem(self.query.value(1))
StudentIdItem.setFont(font)
StudentNameItem.setFont(font)
StudentIdItem.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
StudentNameItem.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
self.tableWidget.setItem(i, 0, StudentIdItem)
self.tableWidget.setItem(i, 1, StudentNameItem)
return
def getStudentInfo(self, item):
row = self.tableWidget.currentIndex().row()
self.tableWidget.verticalScrollBar().setSliderPosition(row)
self.getResult()
i = 0
while (self.query.next() and i != row):
i = i + 1
self.oldDeleteId = self.deleteId
self.oldDeleteName = self.deleteName
self.deleteId = self.query.value(0)
self.deleteName = self.query.value(1)
def deleteUser(self):
if (self.deleteId == "" and self.deleteName == ""):
print(QMessageBox.warning(self, "警告", "请选中要删除的用户", QMessageBox.Yes, QMessageBox.Yes))
return
elif (self.deleteId == self.oldDeleteId and self.deleteName == self.oldDeleteName):
print(QMessageBox.warning(self, "警告", "请选中要删除的用户", QMessageBox.Yes, QMessageBox.Yes))
return
if (QMessageBox.information(self, "提醒", "删除用户:%s,%s\n用户一经删除将无法恢复,是否继续?" % (self.deleteId, self.deleteName),
QMessageBox.Yes | QMessageBox.No,
QMessageBox.No) == QMessageBox.No):
return
# 从User表删除用户
sql = "DELETE FROM User WHERE StudentId='%s'" % (self.deleteId)
self.query.exec_(sql)
self.db.commit()
# 归还所有书籍
sql = "SELECT * FROM User_Book WHERE StudentId='%s' AND BorrowState=1" % self.deleteId
self.query.exec_(sql)
timenow = time.strftime('%Y-%m-%d', time.localtime(time.time()))
updateQuery=QSqlQuery()
while (self.query.next()):
bookId=self.query.value(1)
sql="UPDATE Book SET NumCanBorrow=NumCanBorrow+1 WHERE BookId='%s'"% bookId
updateQuery.exec_(sql)
self.db.commit()
sql="UPDATE User_Book SET ReturnTime='%s',BorrowState=0 WHERE StudentId='%s' AND BorrowState=1"%(timenow,self.deleteId)
self.query.exec_(sql)
self.db.commit()
print(QMessageBox.information(self,"提醒","删除用户成功!",QMessageBox.Yes,QMessageBox.Yes))
self.updateUI()
return
def updateUI(self):
self.getResult()
self.layout.removeWidget(self.widget)
self.layout.removeWidget(self.tableWidget)
sip.delete(self.widget)
sip.delete(self.tableWidget)
# 表格设置
self.tableWidget = QTableWidget()
self.tableWidget.setRowCount(self.userCount)
self.tableWidget.setColumnCount(2)
self.tableWidget.setHorizontalHeaderLabels(['学号', '姓名'])
# 不可编辑
self.tableWidget.setEditTriggers(QAbstractItemView.NoEditTriggers)
# 标题可拉伸
self.tableWidget.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch)
# 整行选中
self.tableWidget.setSelectionBehavior(QAbstractItemView.SelectRows)
self.layout.addWidget(self.tableWidget)
self.setRows()
self.deleteUserButton = QPushButton("删 除 用 户")
hlayout = QHBoxLayout()
hlayout.addWidget(self.deleteUserButton, Qt.AlignHCenter)
self.widget = QWidget()
self.widget.setLayout(hlayout)
self.widget.setFixedHeight(48)
font = QFont()
font.setPixelSize(15)
self.deleteUserButton.setFixedHeight(36)
self.deleteUserButton.setFixedWidth(180)
self.deleteUserButton.setFont(font)
self.layout.addWidget(self.widget, Qt.AlignCenter)
# 设置信号
self.deleteUserButton.clicked.connect(self.deleteUser)
self.tableWidget.itemClicked.connect(self.getStudentInfo)
if __name__ == "__main__":
app = QApplication(sys.argv)
app.setWindowIcon(QIcon("./images/MainWindow_1.png"))
app.setStyleSheet(qdarkstyle.load_stylesheet_pyqt5())
mainMindow = UserManage()
mainMindow.show()
sys.exit(app.exec_())