From cb194bc5ab1bc43ea0007931cd980765493f98dd Mon Sep 17 00:00:00 2001 From: iridiumR Date: Sun, 4 Jun 2023 14:10:30 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BF=AE=E6=94=B9=E8=AE=B0=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/budget/_trans.py | 94 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 92 insertions(+), 2 deletions(-) diff --git a/src/budget/_trans.py b/src/budget/_trans.py index d5ccf5f..693f4af 100644 --- a/src/budget/_trans.py +++ b/src/budget/_trans.py @@ -10,7 +10,6 @@ class TransTab(TabPage): super().__init__() self.initUI() self.pg = parent.pg - self.rows = [] def initUI(self): # 创建顶部控件 @@ -124,7 +123,98 @@ class TransTab(TabPage): QMessageBox.critical(self, 'Error', str(e)) def onTransModifyClicked(self): - pass + # 获取当前选择的行 + self.rows = self.transTable.selectionModel().selectedRows() + if len(self.rows) == 0: + QMessageBox.critical(self, 'Error', 'No row selected') + return + + # 创建修改数据对话框 + self.tDialog = QDialog(self) + self.tDialog.setWindowTitle('Modify Transaction')# 时间 金额 描述 + timeLabel=QLabel("Time:") + timeLine=QLineEdit() + timeLine.setText(self.transData[self.rows[0].row()][4].strftime("%Y-%m-%d %H:%M:%S")) + + amountLabel = QLabel("Amount:") + amountLine = QLineEdit() + num = self.transData[self.rows[0].row()][5] + # 去掉¥符号 + num = num[1:] + amountLine.setText(num) + # 只允许输入两位小数 + amountLine.setValidator(QDoubleValidator(0.00, 999999999.99, 2)) + + descriptionLabel = QLabel("Description:") + descriptionLine = QLineEdit() + descriptionLine.setText(self.transData[self.rows[0].row()][6]) + + cButton = QPushButton("Confirm") + aButton = QPushButton("Abort") + buttonLayout = QHBoxLayout() + buttonLayout.addWidget(cButton) + buttonLayout.addWidget(aButton) + + cButton.clicked.connect(self.tDialog.accept) + aButton.clicked.connect(self.tDialog.reject) + + self.dialogLayout = QFormLayout() + self.dialogLayout.addRow(timeLabel, timeLine) + self.dialogLayout.addRow(amountLabel, amountLine) + self.dialogLayout.addRow(descriptionLabel, descriptionLine) + self.dialogLayout.addRow(buttonLayout) + self.dialogLayout.setSpacing(12) + self.dialogLayout.setContentsMargins(15, 15, 15, 15) + + self.tDialog.setMinimumWidth(400) + self.tDialog.setLayout(self.dialogLayout) + + if self.tDialog.exec() == QDialog.DialogCode.Accepted: + try: + # 若修改金额,则修改账户余额 + if num != amountLine.text(): + amount = float(amountLine.text()) + amount = amount - float(num) + print(amount) + self.pg.execute("UPDATE account SET balance = balance + (%s::NUMERIC)::MONEY WHERE a_id = %s",\ + (amount, self.transData[self.rows[0].row()][1],)) + + # 检测时间格式 + time = timeLine.text() + time = datetime.datetime.strptime(time, "%Y-%m-%d %H:%M:%S") + + # 把 meta 取回来 + self.pg.execute("SELECT meta FROM transaction WHERE t_id = %s", (self.transData[self.rows[0].row()][0],)) + meta = self.pg.fetchall()[0][0] + meta = json.dumps(meta) + + # 修改描述 + meta = json.loads(meta) + meta["description"] = descriptionLine.text() + meta = json.dumps(meta) + + + # 修改数据, 使用JSONB + self.pg.execute("UPDATE transaction SET time = %s, amount = %s, meta = %s WHERE t_id = %s",\ + (time, amountLine.text(), meta, self.transData[self.rows[0].row()][0],)) + + # 刷新表格 + self.selected() + + except Exception as e: + print(e) + QMessageBox.critical(self, 'Error', str(e)) + else: + return + + + + + + + + +