feat: trans table

This commit is contained in:
iridiumR 2023-06-03 14:16:40 +08:00
parent a090a89589
commit 66679afdea
No known key found for this signature in database
GPG key ID: 49735733EB1A32C8
2 changed files with 42 additions and 10 deletions

View file

@ -29,6 +29,8 @@ def initUI(self):
self.pg.onConnectClicked() self.pg.onConnectClicked()
self.onTabChanged(0)
def onTabChanged(self, index): def onTabChanged(self, index):
self.centralWidget().widget(index).selected() self.centralWidget().widget(index).selected()

View file

@ -3,6 +3,7 @@ from ._tab import TabPage
from ._pg import PostgresTab from ._pg import PostgresTab
from PyQt6.QtGui import QDoubleValidator from PyQt6.QtGui import QDoubleValidator
import json import json
import datetime
class TransTab(TabPage): class TransTab(TabPage):
def __init__(self, parent): def __init__(self, parent):
@ -13,19 +14,19 @@ class TransTab(TabPage):
def initUI(self): def initUI(self):
# 创建顶部控件 # 创建顶部控件
self.transAddButton = QPushButton('Add') transAddButton = QPushButton('Add')
self.transAddButton.clicked.connect(self.onTransAddClicked) transAddButton.clicked.connect(self.onTransAddClicked)
self.transModifyButton = QPushButton('Modify') transModifyButton = QPushButton('Modify')
self.transModifyButton.clicked.connect(self.onTransModifyClicked) transModifyButton.clicked.connect(self.onTransModifyClicked)
# 创建控件布局 # 创建控件布局
topLayout = QHBoxLayout() topLayout = QHBoxLayout()
topLayout.addWidget(self.transAddButton) topLayout.addWidget(transAddButton)
topLayout.addWidget(self.transModifyButton) topLayout.addWidget(transModifyButton)
transTable = QTableWidget() self.transTable = QTableWidget()
tableLayout = QVBoxLayout() tableLayout = QVBoxLayout()
tableLayout.addWidget(transTable) tableLayout.addWidget(self.transTable)
# 创建中间控件 # 创建中间控件
transLayout = QVBoxLayout() transLayout = QVBoxLayout()
@ -105,12 +106,12 @@ class TransTab(TabPage):
description = descriptionLine.text() description = descriptionLine.text()
# JSONB插入数据 # JSONB插入数据
data = {"description": description} data = {"type":type, "description": description}
data = json.dumps(data) data = json.dumps(data)
print("insert data:", data) print("insert data:", data)
self.pg.execute( self.pg.execute(
"INSERT INTO transaction (c_id, a_id, amount, meta) VALUES (%s,%s,%s,%s)", (category, account, amount, data,)) "INSERT INTO transaction (c_id, a_id, time, amount, meta) VALUES (%s,%s,now(), %s,%s)", (category, account, amount, data,))
self.pg.execute("UPDATE account SET balance = balance + (%s::NUMERIC)::MONEY WHERE a_id = %s", (amount, account,)) self.pg.execute("UPDATE account SET balance = balance + (%s::NUMERIC)::MONEY WHERE a_id = %s", (amount, account,))
@ -150,3 +151,32 @@ class TransTab(TabPage):
self.dialogLayout.itemAt(5).widget().clear() self.dialogLayout.itemAt(5).widget().clear()
for i in range(len(self.accountData)): for i in range(len(self.accountData)):
self.dialogLayout.itemAt(5).widget().addItem(self.accountData[i][1]) self.dialogLayout.itemAt(5).widget().addItem(self.accountData[i][1])
def selected(self):
self.pg.execute("SELECT t_id, a_id, c_id, s_id, time, translate(amount::varchar,'$',''), meta->>'description' FROM transaction ORDER BY time")
self.transData = self.pg.fetchall()
self.pg.execute("SELECT a_id, meta ->> 'name' FROM account ORDER BY a_id")
self.accountData = self.pg.fetchall()
self.pg.execute("SELECT c_id, meta ->> 'name' FROM category ORDER BY c_id")
self.categoryData = self.pg.fetchall()
print(self.transData)
# 初始化表格
self.transTable.clear()
self.transTable.setRowCount(len(self.transData))
self.transTable.setColumnCount(5) # 时间 账户 类别 金额 描述
self.transTable.setHorizontalHeaderLabels(['Time', 'Account', 'Category', 'Amount', 'Description'])
# 设置自动调整列宽
self.transTable.horizontalHeader().setSectionResizeMode(QHeaderView.ResizeMode.Stretch)
# 填充表格
for i in range(len(self.transData)):
self.transTable.setItem(i, 0, QTableWidgetItem(self.transData[i][4].strftime("%Y-%m-%d %H:%M:%S")))
self.transTable.setItem(i, 1, QTableWidgetItem(self.accountData[self.transData[i][1]-1][1]))
if self.transData[i][2] is None:
self.transTable.setItem(i, 2, QTableWidgetItem("None"))
else:
self.transTable.setItem(i, 2, QTableWidgetItem(self.categoryData[self.transData[i][2]-1][1]))
self.transTable.setItem(i, 3, QTableWidgetItem(str(self.transData[i][5])))
self.transTable.setItem(i, 4, QTableWidgetItem(self.transData[i][6]))