Implemented `EventWindow` for viewing events with pagination and table-based UI. Added database migration to remove `UNIQUE` constraint on `external_id` in `events`. Enhanced DAO methods for event retrieval, pagination, and transaction updates. Integrated OCR recognition in transaction scripts and streamlined event handling in services.
265 lines
8.5 KiB
C++
265 lines
8.5 KiB
C++
#include <QSqlQuery>
|
|
#include <QSqlError>
|
|
#include "AccountDAO.h"
|
|
|
|
#include "db/AccountInfo.h"
|
|
#include "DatabaseManager.h"
|
|
#include "time/DateUtils.h"
|
|
|
|
bool AccountDAO::upsertAccount(const AccountInfo &info) {
|
|
QSqlQuery query(DatabaseManager::instance().database());
|
|
|
|
query.prepare(R"(
|
|
INSERT INTO accounts (device_id, app_code, last_numbers, amount, update_time, status, description, currency)
|
|
VALUES (:device_id, :app_code, :last_numbers, :amount, :update_time, :status, :description, :currency)
|
|
ON CONFLICT(device_id, app_code, last_numbers)
|
|
DO UPDATE SET
|
|
amount = excluded.amount,
|
|
update_time = excluded.update_time,
|
|
last_numbers = excluded.last_numbers,
|
|
status = excluded.status,
|
|
description = excluded.description,
|
|
currency = excluded.currency
|
|
)");
|
|
|
|
query.bindValue(":device_id", info.deviceId);
|
|
query.bindValue(":app_code", info.appCode);
|
|
query.bindValue(":last_numbers", info.lastNumbers);
|
|
query.bindValue(":currency", info.currency);
|
|
query.bindValue(":description", info.description);
|
|
query.bindValue(":amount", info.amount);
|
|
query.bindValue(":update_time", DateUtils::toUtcString(info.updateTime));
|
|
query.bindValue(":status", accountStatusToString(info.status));
|
|
|
|
if (!query.exec()) {
|
|
qWarning() << "Ошибка при вставке/обновлении account:" << query.lastError().text();
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
bool AccountDAO::updateAccountById(
|
|
const int id,
|
|
const QString &status,
|
|
const QString &description,
|
|
const QString ¤cy
|
|
) {
|
|
QSqlQuery query(DatabaseManager::instance().database());
|
|
|
|
query.prepare(R"(
|
|
UPDATE accounts
|
|
SET status = :status,
|
|
description = :description,
|
|
last_numbers = :last_numbers,
|
|
currency = :currency
|
|
WHERE id = :id
|
|
)");
|
|
|
|
query.bindValue(":status", status);
|
|
query.bindValue(":description", description);
|
|
query.bindValue(":last_numbers", description.right(4));
|
|
query.bindValue(":currency", currency);
|
|
query.bindValue(":id", id);
|
|
|
|
if (!query.exec()) {
|
|
qWarning() << "Ошибка при обновлении account по id:" << query.lastError().text();
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
bool AccountDAO::deleteAccount(const int id) {
|
|
QSqlQuery query(DatabaseManager::instance().database());
|
|
query.prepare("DELETE FROM accounts WHERE id = :id");
|
|
query.bindValue(":id", id);
|
|
return query.exec();
|
|
}
|
|
|
|
bool AccountDAO::updateCardNumber(const int accountId, const QString &cardNumber) {
|
|
QSqlQuery query(DatabaseManager::instance().database());
|
|
query.prepare(R"(
|
|
UPDATE accounts SET card_number = :card_number WHERE id = :id
|
|
)");
|
|
query.bindValue(":card_number", cardNumber);
|
|
query.bindValue(":id", accountId);
|
|
if (!query.exec()) {
|
|
qWarning() << "Ошибка при обновлении card_number:" << query.lastError().text();
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
bool AccountDAO::updateMaterialId(const int accountId, const QString &materialId) {
|
|
QSqlQuery query(DatabaseManager::instance().database());
|
|
query.prepare("UPDATE accounts SET material_id = :material_id WHERE id = :id");
|
|
query.bindValue(":material_id", materialId);
|
|
query.bindValue(":id", accountId);
|
|
if (!query.exec()) {
|
|
qWarning() << "Ошибка при обновлении material_id:" << query.lastError().text();
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
AccountInfo parseAccount(const QSqlQuery &query) {
|
|
AccountInfo acc;
|
|
acc.id = query.value("id").toInt();
|
|
acc.deviceId = query.value("device_id").toString();
|
|
acc.appCode = query.value("app_code").toString();
|
|
acc.lastNumbers = query.value("last_numbers").toString();
|
|
acc.cardNumber = query.value("card_number").toString();
|
|
acc.materialId = query.value("material_id").toString();
|
|
acc.amount = query.value("amount").toDouble();
|
|
acc.updateTime = DateUtils::fromUtcString(query.value("update_time").toString());
|
|
acc.status = accountStatusFromString(query.value("status").toString());
|
|
acc.description = query.value("description").toString();
|
|
acc.currency = query.value("currency").toString();
|
|
return acc;
|
|
}
|
|
|
|
QList<AccountInfo> AccountDAO::getAccounts(const QString &deviceId, const QString &appName) {
|
|
QList<AccountInfo> accounts;
|
|
QSqlQuery query(DatabaseManager::instance().database());
|
|
|
|
query.prepare(R"(
|
|
SELECT id, device_id, app_code, last_numbers, card_number, material_id, amount, update_time, status, description, currency
|
|
FROM accounts
|
|
WHERE device_id = :device_id AND app_code = :app_code
|
|
)");
|
|
|
|
query.bindValue(":device_id", deviceId);
|
|
query.bindValue(":app_code", appName);
|
|
|
|
if (!query.exec()) {
|
|
qWarning() << "Ошибка при получении accounts:" << query.lastError().text();
|
|
return accounts;
|
|
}
|
|
|
|
while (query.next()) {
|
|
accounts.append(parseAccount(query));
|
|
}
|
|
|
|
return accounts;
|
|
}
|
|
|
|
AccountInfo AccountDAO::getAccountById(const int accountId) {
|
|
QSqlQuery query(DatabaseManager::instance().database());
|
|
|
|
query.prepare(R"(
|
|
SELECT id, device_id, app_code, last_numbers, card_number, material_id, amount, update_time, status, description, currency
|
|
FROM accounts
|
|
WHERE id = :id
|
|
LIMIT 1
|
|
)");
|
|
|
|
query.bindValue(":id", accountId);
|
|
|
|
if (!query.exec()) {
|
|
qWarning() << "Ошибка при получении account по ID:" << query.lastError().text();
|
|
return {};
|
|
}
|
|
|
|
if (query.next()) {
|
|
return parseAccount(query);
|
|
}
|
|
return {};
|
|
}
|
|
|
|
QList<AccountInfo> AccountDAO::getAccountsByAppCode(const QString &appCode) {
|
|
QList<AccountInfo> accounts;
|
|
QSqlQuery query(DatabaseManager::instance().database());
|
|
|
|
query.prepare(R"(
|
|
SELECT id, device_id, app_code, last_numbers, card_number, material_id, amount, update_time, status, description, currency
|
|
FROM accounts
|
|
WHERE app_code = :app_code
|
|
)");
|
|
|
|
query.bindValue(":app_code", appCode);
|
|
|
|
if (!query.exec()) {
|
|
qWarning() << "Ошибка при получении accounts по app_code:" << query.lastError().text();
|
|
return accounts;
|
|
}
|
|
|
|
while (query.next()) {
|
|
accounts.append(parseAccount(query));
|
|
}
|
|
|
|
return accounts;
|
|
}
|
|
|
|
AccountInfo AccountDAO::findAppAccount(const QString &deviceId, const QString &appName, const QString &lastNumber) {
|
|
QSqlQuery query(DatabaseManager::instance().database());
|
|
|
|
query.prepare(R"(
|
|
SELECT id, device_id, app_code, last_numbers, card_number, material_id, amount, update_time, status, description, currency
|
|
FROM accounts
|
|
WHERE app_code = :appName AND last_numbers = :lastNumber AND device_id = :deviceId
|
|
LIMIT 1
|
|
)");
|
|
|
|
query.bindValue(":deviceId", deviceId);
|
|
query.bindValue(":appName", appName);
|
|
query.bindValue(":lastNumber", lastNumber);
|
|
|
|
if (!query.exec()) {
|
|
qWarning() << "Ошибка при получении account по ID:" << query.lastError().text();
|
|
return {};
|
|
}
|
|
|
|
if (query.next()) {
|
|
return parseAccount(query);
|
|
}
|
|
|
|
return {};
|
|
}
|
|
|
|
QStringList AccountDAO::getActiveMaterialIds() {
|
|
QStringList ids;
|
|
QSqlQuery query(DatabaseManager::instance().database());
|
|
query.prepare(R"(
|
|
SELECT a.material_id
|
|
FROM accounts a
|
|
JOIN applications app ON a.device_id = app.device_id AND a.app_code = app.code
|
|
WHERE app.status = 'active'
|
|
AND a.material_id IS NOT NULL AND a.material_id != ''
|
|
)");
|
|
|
|
if (!query.exec()) {
|
|
qWarning() << "Ошибка при получении active material_ids:" << query.lastError().text();
|
|
return ids;
|
|
}
|
|
|
|
while (query.next()) {
|
|
ids << query.value(0).toString();
|
|
}
|
|
return ids;
|
|
}
|
|
|
|
AccountInfo AccountDAO::findByMaterialId(const QString &materialId) {
|
|
QSqlQuery query(DatabaseManager::instance().database());
|
|
|
|
query.prepare(R"(
|
|
SELECT id, device_id, app_code, last_numbers, card_number, material_id, amount, update_time, status, description, currency
|
|
FROM accounts
|
|
WHERE material_id = :material_id
|
|
LIMIT 1
|
|
)");
|
|
|
|
query.bindValue(":material_id", materialId);
|
|
|
|
if (!query.exec()) {
|
|
qWarning() << "Ошибка при поиске account по material_id:" << query.lastError().text();
|
|
return {};
|
|
}
|
|
|
|
if (query.next()) {
|
|
return parseAccount(query);
|
|
}
|
|
|
|
return {};
|
|
}
|