Add support for transaction types, statuses, and detailed fields
Enhanced `TransactionInfo` with new fields such as type, status, fee, and timestamps. Updated DAO functionality to handle these changes, including insertion, updates, and retrieval methods. Additionally, implemented utility functions for type/status conversion.
This commit is contained in:
parent
1bf701e3f4
commit
a269de840b
@ -1,5 +1,6 @@
|
|||||||
#include <QSqlQuery>
|
#include <QSqlQuery>
|
||||||
#include <QSqlError>
|
#include <QSqlError>
|
||||||
|
#include <QVariant>
|
||||||
|
|
||||||
#include "DatabaseManager.h"
|
#include "DatabaseManager.h"
|
||||||
#include "TransactionDAO.h"
|
#include "TransactionDAO.h"
|
||||||
@ -7,13 +8,22 @@
|
|||||||
bool TransactionDAO::insertTransactionIfNotExists(const TransactionInfo &info) {
|
bool TransactionDAO::insertTransactionIfNotExists(const TransactionInfo &info) {
|
||||||
QSqlQuery query(DatabaseManager::instance().database());
|
QSqlQuery query(DatabaseManager::instance().database());
|
||||||
|
|
||||||
|
if (info.amount < 0) {
|
||||||
|
// Если исходящая, то мы знаем время timestamp
|
||||||
query.prepare(R"(
|
query.prepare(R"(
|
||||||
SELECT COUNT(*) FROM transactions
|
SELECT COUNT(*) FROM transactions
|
||||||
WHERE account_id = :account_id AND amount = :amount AND timestamp = :timestamp
|
WHERE account_id = :account_id AND amount = :amount AND timestamp = :timestamp AND type = :type
|
||||||
)");
|
)");
|
||||||
|
} else {
|
||||||
|
query.prepare(R"(
|
||||||
|
SELECT COUNT(*) FROM transactions
|
||||||
|
WHERE account_id = :account_id AND amount = :amount AND bankTime = :bankTime AND type = :type
|
||||||
|
)");
|
||||||
|
}
|
||||||
|
|
||||||
query.bindValue(":account_id", info.accountId);
|
query.bindValue(":account_id", info.accountId);
|
||||||
query.bindValue(":amount", info.amount);
|
query.bindValue(":amount", info.amount);
|
||||||
|
query.bindValue(":type", transactionTypeToString(info.type));
|
||||||
query.bindValue(":timestamp", info.timestamp.toString(Qt::ISODate));
|
query.bindValue(":timestamp", info.timestamp.toString(Qt::ISODate));
|
||||||
|
|
||||||
if (!query.exec() || !query.next()) {
|
if (!query.exec() || !query.next()) {
|
||||||
@ -26,13 +36,35 @@ bool TransactionDAO::insertTransactionIfNotExists(const TransactionInfo &info) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
query.prepare(R"(
|
query.prepare(R"(
|
||||||
INSERT INTO transactions (account_id, amount, description, timestamp)
|
INSERT INTO transactions (
|
||||||
VALUES (:account_id, :amount, :description, :timestamp)
|
account_id, amount, fee, status, phone, type,
|
||||||
|
description, updated_description, short_description, updated_short_description,
|
||||||
|
bank_name, bank_time, name, tr_external_id,
|
||||||
|
update_time, complete_time, timestamp
|
||||||
|
) VALUES (
|
||||||
|
:account_id, :amount, :fee, :status, :phone, :type,
|
||||||
|
:description, :updated_description, :short_description, :updated_short_description,
|
||||||
|
:bank_name, :bank_time, :name, :tr_external_id,
|
||||||
|
:update_time, :complete_time, :timestamp
|
||||||
|
)
|
||||||
)");
|
)");
|
||||||
|
|
||||||
query.bindValue(":account_id", info.accountId);
|
query.bindValue(":account_id", info.accountId);
|
||||||
query.bindValue(":amount", info.amount);
|
query.bindValue(":amount", info.amount);
|
||||||
|
query.bindValue(":fee", info.fee);
|
||||||
|
query.bindValue(":status", transactionStatusToString(info.status));
|
||||||
|
query.bindValue(":type", transactionTypeToString(info.type));
|
||||||
|
query.bindValue(":phone", info.phone);
|
||||||
|
query.bindValue(":name", info.name);
|
||||||
query.bindValue(":description", info.description);
|
query.bindValue(":description", info.description);
|
||||||
|
query.bindValue(":updated_description", info.updatedDescription);
|
||||||
|
query.bindValue(":short_description", info.shortDescription);
|
||||||
|
query.bindValue(":updated_short_description", info.updatedShortDescription);
|
||||||
|
query.bindValue(":bank_name", info.bankName);
|
||||||
|
query.bindValue(":bank_time", info.bankTime);
|
||||||
|
query.bindValue(":tr_external_id", info.trExternalId);
|
||||||
|
query.bindValue(":update_time", info.updateTime.toString(Qt::ISODate));
|
||||||
|
query.bindValue(":complete_time", info.completeTime.toString(Qt::ISODate));
|
||||||
query.bindValue(":timestamp", info.timestamp.toString(Qt::ISODate));
|
query.bindValue(":timestamp", info.timestamp.toString(Qt::ISODate));
|
||||||
|
|
||||||
if (!query.exec()) {
|
if (!query.exec()) {
|
||||||
@ -55,8 +87,7 @@ QList<TransactionInfo> TransactionDAO::getTransactions(const int accountId) {
|
|||||||
QSqlQuery query(DatabaseManager::instance().database());
|
QSqlQuery query(DatabaseManager::instance().database());
|
||||||
|
|
||||||
query.prepare(R"(
|
query.prepare(R"(
|
||||||
SELECT id, account_id, amount, description, timestamp
|
SELECT * FROM transactions
|
||||||
FROM transactions
|
|
||||||
WHERE account_id = :account_id
|
WHERE account_id = :account_id
|
||||||
ORDER BY timestamp DESC
|
ORDER BY timestamp DESC
|
||||||
)");
|
)");
|
||||||
@ -73,10 +104,108 @@ QList<TransactionInfo> TransactionDAO::getTransactions(const int accountId) {
|
|||||||
tx.id = query.value("id").toInt();
|
tx.id = query.value("id").toInt();
|
||||||
tx.accountId = query.value("account_id").toInt();
|
tx.accountId = query.value("account_id").toInt();
|
||||||
tx.amount = query.value("amount").toDouble();
|
tx.amount = query.value("amount").toDouble();
|
||||||
|
tx.fee = query.value("fee").toDouble();
|
||||||
|
tx.status = transactionStatusFromString(query.value("status").toString());
|
||||||
|
tx.type = transactionTypeFromString(query.value("type").toString());
|
||||||
|
tx.phone = query.value("phone").toString();
|
||||||
|
tx.name = query.value("name").toString();
|
||||||
tx.description = query.value("description").toString();
|
tx.description = query.value("description").toString();
|
||||||
|
tx.updatedDescription = query.value("updated_description").toString();
|
||||||
|
tx.shortDescription = query.value("short_description").toString();
|
||||||
|
tx.updatedShortDescription = query.value("updated_short_description").toString();
|
||||||
|
tx.bankName = query.value("bank_name").toString();
|
||||||
|
tx.bankTime = query.value("bank_time").toString();
|
||||||
|
tx.trExternalId = query.value("tr_external_id").toString();
|
||||||
|
tx.updateTime = QDateTime::fromString(query.value("update_time").toString(), Qt::ISODate);
|
||||||
|
tx.completeTime = QDateTime::fromString(query.value("complete_time").toString(), Qt::ISODate);
|
||||||
tx.timestamp = QDateTime::fromString(query.value("timestamp").toString(), Qt::ISODate);
|
tx.timestamp = QDateTime::fromString(query.value("timestamp").toString(), Qt::ISODate);
|
||||||
|
|
||||||
list.append(tx);
|
list.append(tx);
|
||||||
}
|
}
|
||||||
|
|
||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool TransactionDAO::updateTransaction(const TransactionInfo &info) {
|
||||||
|
QSqlQuery query(DatabaseManager::instance().database());
|
||||||
|
|
||||||
|
query.prepare(R"(
|
||||||
|
UPDATE transactions SET
|
||||||
|
amount = :amount,
|
||||||
|
fee = :fee,
|
||||||
|
status = :status,
|
||||||
|
type = :type,
|
||||||
|
phone = :phone,
|
||||||
|
description = :description,
|
||||||
|
updated_description = :updated_description,
|
||||||
|
short_description = :short_description,
|
||||||
|
updated_short_description = :updated_short_description,
|
||||||
|
bank_name = :bank_name,
|
||||||
|
bank_time = :bank_time,
|
||||||
|
name = :name,
|
||||||
|
tr_external_id = :tr_external_id,
|
||||||
|
update_time = :update_time,
|
||||||
|
complete_time = :complete_time,
|
||||||
|
timestamp = :timestamp
|
||||||
|
WHERE id = :id
|
||||||
|
)");
|
||||||
|
|
||||||
|
query.bindValue(":id", info.id);
|
||||||
|
query.bindValue(":amount", info.amount);
|
||||||
|
query.bindValue(":fee", info.fee);
|
||||||
|
query.bindValue(":status", transactionStatusToString(info.status));
|
||||||
|
query.bindValue(":type", transactionTypeToString(info.type));
|
||||||
|
query.bindValue(":phone", info.phone);
|
||||||
|
query.bindValue(":name", info.name);
|
||||||
|
query.bindValue(":description", info.description);
|
||||||
|
query.bindValue(":updated_description", info.updatedDescription);
|
||||||
|
query.bindValue(":short_description", info.shortDescription);
|
||||||
|
query.bindValue(":updated_short_description", info.updatedShortDescription);
|
||||||
|
query.bindValue(":bank_name", info.bankName);
|
||||||
|
query.bindValue(":bank_time", info.bankTime);
|
||||||
|
query.bindValue(":tr_external_id", info.trExternalId);
|
||||||
|
query.bindValue(":update_time", info.updateTime.toString(Qt::ISODate));
|
||||||
|
query.bindValue(":complete_time", info.completeTime.toString(Qt::ISODate));
|
||||||
|
query.bindValue(":timestamp", info.timestamp.toString(Qt::ISODate));
|
||||||
|
|
||||||
|
if (!query.exec()) {
|
||||||
|
qWarning() << "Ошибка при обновлении transaction:" << query.lastError().text();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
TransactionInfo TransactionDAO::getTransactionById(const int id) {
|
||||||
|
QSqlQuery query(DatabaseManager::instance().database());
|
||||||
|
query.prepare("SELECT * FROM transactions WHERE id = :id");
|
||||||
|
query.bindValue(":id", id);
|
||||||
|
|
||||||
|
|
||||||
|
TransactionInfo tx;
|
||||||
|
if (!query.exec() || !query.next()) {
|
||||||
|
qWarning() << "Ошибка или не найден transaction с id:" << id << query.lastError().text();
|
||||||
|
return tx;
|
||||||
|
}
|
||||||
|
|
||||||
|
tx.id = query.value("id").toInt();
|
||||||
|
tx.accountId = query.value("account_id").toInt();
|
||||||
|
tx.amount = query.value("amount").toDouble();
|
||||||
|
tx.fee = query.value("fee").toDouble();
|
||||||
|
tx.status = transactionStatusFromString(query.value("status").toString());
|
||||||
|
tx.type = transactionTypeFromString(query.value("type").toString());
|
||||||
|
tx.phone = query.value("phone").toString();
|
||||||
|
tx.name = query.value("name").toString();
|
||||||
|
tx.description = query.value("description").toString();
|
||||||
|
tx.shortDescription = query.value("short_description").toString();
|
||||||
|
tx.updatedDescription = query.value("updated_description").toString();
|
||||||
|
tx.updatedShortDescription = query.value("updated_short_description").toString();
|
||||||
|
tx.bankName = query.value("bank_name").toString();
|
||||||
|
tx.bankTime = query.value("bank_time").toString();
|
||||||
|
tx.trExternalId = query.value("tr_external_id").toString();
|
||||||
|
tx.updateTime = QDateTime::fromString(query.value("update_time").toString(), Qt::ISODate);
|
||||||
|
tx.completeTime = QDateTime::fromString(query.value("complete_time").toString(), Qt::ISODate);
|
||||||
|
tx.timestamp = QDateTime::fromString(query.value("timestamp").toString(), Qt::ISODate);
|
||||||
|
|
||||||
|
return tx;
|
||||||
|
}
|
||||||
|
|||||||
@ -8,4 +8,7 @@ public:
|
|||||||
[[nodiscard]] static bool deleteTransaction(int id);
|
[[nodiscard]] static bool deleteTransaction(int id);
|
||||||
|
|
||||||
[[nodiscard]] static QList<TransactionInfo> getTransactions(int accountId);
|
[[nodiscard]] static QList<TransactionInfo> getTransactions(int accountId);
|
||||||
|
|
||||||
|
[[nodiscard]] static bool updateTransaction(const TransactionInfo &info);
|
||||||
|
[[nodiscard]] static TransactionInfo getTransactionById(int id);
|
||||||
};
|
};
|
||||||
|
|||||||
@ -1,11 +1,113 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <QString>
|
||||||
#include <QDateTime>
|
#include <QDateTime>
|
||||||
|
|
||||||
|
enum class TransactionType {
|
||||||
|
Card,
|
||||||
|
Phone,
|
||||||
|
Unknown
|
||||||
|
};
|
||||||
|
|
||||||
|
enum class TransactionStatus {
|
||||||
|
Wait,
|
||||||
|
Complete,
|
||||||
|
Unknown
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
inline QString transactionTypeToString(const TransactionType type) {
|
||||||
|
switch (type) {
|
||||||
|
case TransactionType::Card: return "card";
|
||||||
|
case TransactionType::Phone: return "phone";
|
||||||
|
default: return "unknown";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
inline TransactionType transactionTypeFromString(const QString &str) {
|
||||||
|
if (str == "card") return TransactionType::Card;
|
||||||
|
if (str == "phone") return TransactionType::Phone;
|
||||||
|
return TransactionType::Unknown;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline QString transactionStatusToString(const TransactionStatus type) {
|
||||||
|
switch (type) {
|
||||||
|
case TransactionStatus::Wait: return "wait";
|
||||||
|
case TransactionStatus::Complete: return "complete";
|
||||||
|
default: return "unknown";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
inline TransactionStatus transactionStatusFromString(const QString &str) {
|
||||||
|
if (str == "wait") return TransactionStatus::Wait;
|
||||||
|
if (str == "complete") return TransactionStatus::Complete;
|
||||||
|
return TransactionStatus::Unknown;
|
||||||
|
}
|
||||||
|
|
||||||
struct TransactionInfo {
|
struct TransactionInfo {
|
||||||
int id;
|
int id = -1;
|
||||||
int accountId;
|
int accountId = -1;
|
||||||
double amount;
|
|
||||||
|
double amount = 0;
|
||||||
|
double fee = 0;
|
||||||
|
TransactionStatus status = TransactionStatus::Unknown;
|
||||||
|
TransactionType type = TransactionType::Unknown;
|
||||||
|
QString phone;
|
||||||
|
QString name;
|
||||||
|
|
||||||
|
QString shortDescription;
|
||||||
|
QString updatedShortDescription;
|
||||||
QString description;
|
QString description;
|
||||||
|
QString updatedDescription;
|
||||||
|
|
||||||
|
QString bankName;
|
||||||
|
QString bankTime;
|
||||||
|
|
||||||
|
QString trExternalId;
|
||||||
|
|
||||||
|
QDateTime updateTime;
|
||||||
|
QDateTime completeTime;
|
||||||
QDateTime timestamp;
|
QDateTime timestamp;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
inline QString convertTransactionToString(const TransactionInfo &tx) {
|
||||||
|
return QString(
|
||||||
|
"TransactionInfo {\n"
|
||||||
|
" id: %1\n"
|
||||||
|
" accountId: %2\n"
|
||||||
|
" amount: %3\n"
|
||||||
|
" fee: %4\n"
|
||||||
|
" status: %5\n"
|
||||||
|
" type: %6\n"
|
||||||
|
" phone: %7\n"
|
||||||
|
" shortDescription: %8\n"
|
||||||
|
" updatedShortDescription: %9\n"
|
||||||
|
" description: %10\n"
|
||||||
|
" updatedDescription: %11\n"
|
||||||
|
" bankName: %12\n"
|
||||||
|
" bankTime: %13\n"
|
||||||
|
" name: %14\n"
|
||||||
|
" trExternalId: %15\n"
|
||||||
|
" updateTime: %16\n"
|
||||||
|
" completeTime: %17\n"
|
||||||
|
" timestamp: %18\n"
|
||||||
|
"}"
|
||||||
|
).arg(tx.id)
|
||||||
|
.arg(tx.accountId)
|
||||||
|
.arg(tx.amount)
|
||||||
|
.arg(tx.fee)
|
||||||
|
.arg(transactionStatusToString(tx.status))
|
||||||
|
.arg(transactionTypeToString(tx.type))
|
||||||
|
.arg(tx.phone)
|
||||||
|
.arg(tx.shortDescription)
|
||||||
|
.arg(tx.updatedShortDescription)
|
||||||
|
.arg(tx.description)
|
||||||
|
.arg(tx.updatedDescription)
|
||||||
|
.arg(tx.bankName)
|
||||||
|
.arg(tx.bankTime)
|
||||||
|
.arg(tx.name)
|
||||||
|
.arg(tx.trExternalId)
|
||||||
|
.arg(tx.updateTime.toString(Qt::ISODate))
|
||||||
|
.arg(tx.completeTime.toString(Qt::ISODate))
|
||||||
|
.arg(tx.timestamp.toString(Qt::ISODate));
|
||||||
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user