1
0
forked from BRT/arc
arc/database/dao/TransactionDAO.cpp
trnsmkot 404ea7705b 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.
2025-05-06 10:28:34 +07:00

212 lines
8.7 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <QSqlQuery>
#include <QSqlError>
#include <QVariant>
#include "DatabaseManager.h"
#include "TransactionDAO.h"
bool TransactionDAO::insertTransactionIfNotExists(const TransactionInfo &info) {
QSqlQuery query(DatabaseManager::instance().database());
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()) {
qWarning() << "Ошибка при проверке существования transaction:" << query.lastError().text();
return false;
}
if (query.value(0).toInt() > 0) {
return false; // Уже существует
}
query.prepare(R"(
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()) {
qWarning() << "Ошибка при вставке transaction:" << query.lastError().text();
return false;
}
return true;
}
bool TransactionDAO::deleteTransaction(const int id) {
QSqlQuery query(DatabaseManager::instance().database());
query.prepare("DELETE FROM transactions WHERE id = :id");
query.bindValue(":id", id);
return query.exec();
}
QList<TransactionInfo> TransactionDAO::getTransactions(const int accountId) {
QList<TransactionInfo> list;
QSqlQuery query(DatabaseManager::instance().database());
query.prepare(R"(
SELECT * FROM transactions
WHERE account_id = :account_id
ORDER BY timestamp DESC
)");
query.bindValue(":account_id", accountId);
if (!query.exec()) {
qWarning() << "Ошибка при получении transactions:" << query.lastError().text();
return list;
}
while (query.next()) {
TransactionInfo 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.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;
}