diff --git a/database/dao/TransactionDAO.cpp b/database/dao/TransactionDAO.cpp index 31de003..662da36 100644 --- a/database/dao/TransactionDAO.cpp +++ b/database/dao/TransactionDAO.cpp @@ -1,5 +1,6 @@ #include #include +#include #include "DatabaseManager.h" #include "TransactionDAO.h" @@ -7,13 +8,22 @@ bool TransactionDAO::insertTransactionIfNotExists(const TransactionInfo &info) { QSqlQuery query(DatabaseManager::instance().database()); - query.prepare(R"( - SELECT COUNT(*) FROM transactions - WHERE account_id = :account_id AND amount = :amount AND timestamp = :timestamp - )"); + if (info.amount < 0) { + // Если исходящая, то мы знаем время timestamp + query.prepare(R"( + SELECT COUNT(*) FROM transactions + 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(":amount", info.amount); + query.bindValue(":type", transactionTypeToString(info.type)); query.bindValue(":timestamp", info.timestamp.toString(Qt::ISODate)); if (!query.exec() || !query.next()) { @@ -26,13 +36,35 @@ bool TransactionDAO::insertTransactionIfNotExists(const TransactionInfo &info) { } query.prepare(R"( - INSERT INTO transactions (account_id, amount, description, timestamp) - VALUES (:account_id, :amount, :description, :timestamp) + INSERT INTO transactions ( + 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(":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()) { @@ -55,8 +87,7 @@ QList TransactionDAO::getTransactions(const int accountId) { QSqlQuery query(DatabaseManager::instance().database()); query.prepare(R"( - SELECT id, account_id, amount, description, timestamp - FROM transactions + SELECT * FROM transactions WHERE account_id = :account_id ORDER BY timestamp DESC )"); @@ -73,10 +104,108 @@ QList TransactionDAO::getTransactions(const int accountId) { 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.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); + list.append(tx); } 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; +} diff --git a/database/dao/TransactionDAO.h b/database/dao/TransactionDAO.h index f8b5d36..6c9b3e1 100644 --- a/database/dao/TransactionDAO.h +++ b/database/dao/TransactionDAO.h @@ -8,4 +8,7 @@ public: [[nodiscard]] static bool deleteTransaction(int id); [[nodiscard]] static QList getTransactions(int accountId); + + [[nodiscard]] static bool updateTransaction(const TransactionInfo &info); + [[nodiscard]] static TransactionInfo getTransactionById(int id); }; diff --git a/models/db/TransactionInfo.h b/models/db/TransactionInfo.h index fe791d3..81996bc 100644 --- a/models/db/TransactionInfo.h +++ b/models/db/TransactionInfo.h @@ -1,11 +1,113 @@ #pragma once +#include #include +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 { - int id; - int accountId; - double amount; + int id = -1; + int accountId = -1; + + 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 updatedDescription; + + QString bankName; + QString bankTime; + + QString trExternalId; + + QDateTime updateTime; + QDateTime completeTime; QDateTime timestamp; -}; \ No newline at end of file +}; + +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)); +}