161 lines
6.3 KiB
C++
161 lines
6.3 KiB
C++
#include "GetLastDaysHistoryTossScript.h"
|
||
|
||
#include <QJsonObject>
|
||
#include <QRandomGenerator>
|
||
#include <QThread>
|
||
#include <utility>
|
||
|
||
#include "DatabaseManager.h"
|
||
#include "adb/AdbUtils.h"
|
||
#include "dao/AccountDAO.h"
|
||
#include "dao/ApplicationDAO.h"
|
||
#include "dao/DeviceDAO.h"
|
||
#include "dao/TransactionDAO.h"
|
||
#include "db/DeviceInfo.h"
|
||
#include "net/NetworkService.h"
|
||
#include "time/DateUtils.h"
|
||
|
||
GetLastDaysHistoryTossScript::GetLastDaysHistoryTossScript(
|
||
AccountInfo account,
|
||
QObject *parent
|
||
) : CommonTossScript(parent),
|
||
m_account(std::move(account)) {
|
||
}
|
||
|
||
GetLastDaysHistoryTossScript::~GetLastDaysHistoryTossScript() = default;
|
||
|
||
bool GetLastDaysHistoryTossScript::saveAndPostNewTransactionData(
|
||
const AccountInfo &account,
|
||
const TransactionInfo &transaction,
|
||
const TransactionStatus &status
|
||
) {
|
||
TransactionInfo tr;
|
||
tr.accountId = account.id;
|
||
tr.description = transaction.description;
|
||
tr.bankTime = transaction.bankTime;
|
||
tr.bankName = transaction.bankName;
|
||
tr.amount = transaction.amount;
|
||
tr.phone = transaction.phone;
|
||
tr.updateTime = DateUtils::getUtcNow();
|
||
tr.fee = transaction.fee;
|
||
tr.name = transaction.name;
|
||
tr.bankTrExternalId = transaction.bankTrExternalId;
|
||
tr.status = status;
|
||
tr.type = TransactionType::Card;
|
||
tr.completeTime = transaction.completeTime;
|
||
tr.id = TransactionDAO::insertTransaction(tr);
|
||
|
||
if (tr.id == -1) {
|
||
return false;
|
||
}
|
||
postNewTransactionData(account, tr);
|
||
return true;
|
||
}
|
||
|
||
bool GetLastDaysHistoryTossScript::updateAndPostTransactionData(
|
||
const AccountInfo &account,
|
||
const int transactionId,
|
||
const TransactionStatus status,
|
||
const QString &oldDesc,
|
||
const QString &newDesc
|
||
) {
|
||
if (TransactionDAO::updateTransaction(transactionId, status, oldDesc, newDesc)) {
|
||
updateTransactionData(account, transactionId, status, oldDesc, newDesc);
|
||
return true;
|
||
}
|
||
return false;
|
||
}
|
||
|
||
void GetLastDaysHistoryTossScript::doStart() {
|
||
// берем все транзакции из БД
|
||
const DeviceInfo device = DeviceDAO::getDeviceById(m_account.deviceId);
|
||
const ApplicationInfo app = ApplicationDAO::getApplication(m_account.deviceId, m_account.appCode);
|
||
|
||
xmlScreenParser.parseAndSaveXml(AdbUtils::getScreenDumpAsXml(m_account.deviceId, ++m_counter));
|
||
if (!xmlScreenParser.isHomeScreen()) {
|
||
if (!runAppAndGoToHomeScreen(device.id, app.package, app.pinCode, device.screenWidth, device.screenHeight)) {
|
||
m_error = "Не смогли дойти до домашней страницы";
|
||
qCritical() << m_error;
|
||
emit finishedWithResult(m_error);
|
||
return;
|
||
}
|
||
}
|
||
|
||
xmlScreenParser.parseAndSaveXml(AdbUtils::getScreenDumpAsXml(m_account.deviceId, ++m_counter));
|
||
|
||
if (xmlScreenParser.isHomeScreen()) {
|
||
QList<AccountInfo> oldAccounts = AccountDAO::getAccounts(m_account.deviceId, m_account.appCode);
|
||
QMap<QString, AccountInfo> accountMap;
|
||
for (AccountInfo &oldAccount: oldAccounts) {
|
||
if (oldAccount.status == AccountStatus::Active) {
|
||
accountMap.insert(oldAccount.lastNumbers, oldAccount);
|
||
}
|
||
}
|
||
|
||
QList<AccountInfo> accounts = xmlScreenParser.parseAccountsInfo();
|
||
if (accounts.length() == 1 && accountMap.size() == 1) {
|
||
AccountInfo account = accounts[0];
|
||
account.deviceId = m_account.deviceId;
|
||
account.appCode = m_account.appCode;
|
||
account.lastNumbers = accountMap.first().lastNumbers;
|
||
account.description = accountMap.first().description;
|
||
account.currency = accountMap.first().currency;
|
||
account.description = accountMap.first().description;
|
||
account.status = AccountStatus::Active;
|
||
if (!AccountDAO::upsertAccount(account)) {
|
||
m_error = "Not updated: " + account.lastNumbers;
|
||
qCritical() << m_error;
|
||
}
|
||
}
|
||
|
||
QList<AccountInfo> savedAccounts = AccountDAO::getAccounts(m_account.deviceId, m_account.appCode);
|
||
postAccountsData(savedAccounts);
|
||
|
||
|
||
xmlScreenParser.parseAndSaveXml(AdbUtils::getScreenDumpAsXml(m_account.deviceId, ++m_counter));
|
||
Node transferText = xmlScreenParser.findFirstAccountLine();
|
||
if (!transferText.isEmpty()) {
|
||
AdbUtils::makeTap(device.id, transferText.x(), transferText.y());
|
||
QThread::msleep(3000);
|
||
|
||
xmlScreenParser.parseAndSaveXml(AdbUtils::getScreenDumpAsXml(m_account.deviceId, ++m_counter));
|
||
|
||
for (int i = 0; i < 5; ++i) {
|
||
Node myBankText = xmlScreenParser.findTextNode("My Toss Bank");
|
||
Node accountText = xmlScreenParser.findTextNode("Account");
|
||
|
||
if (!myBankText.isEmpty() && !accountText.isEmpty()) {
|
||
QList<TransactionInfo> transactions = xmlScreenParser.parsePositiveTransactions();
|
||
QList<TransactionInfo> savedTransactions = TransactionDAO::getTransactionsWithinHours(m_account.id, 48);
|
||
for (TransactionInfo &transaction: transactions) {
|
||
bool isSaved = false;
|
||
for (TransactionInfo &savedTransaction: savedTransactions) {
|
||
if (savedTransaction.amount == transaction.amount
|
||
&& savedTransaction.bankTime == transaction.bankTime) {
|
||
isSaved = true;
|
||
break;
|
||
}
|
||
}
|
||
if (!isSaved) {
|
||
if (!saveAndPostNewTransactionData(m_account, transaction, TransactionStatus::Complete)) {
|
||
qDebug() << "--- saveAndPostNewTransactionData failed";
|
||
}
|
||
}
|
||
}
|
||
break;
|
||
}
|
||
|
||
QThread::msleep(1000);
|
||
xmlScreenParser.parseAndSaveXml(AdbUtils::getScreenDumpAsXml(m_account.deviceId , ++m_counter));
|
||
}
|
||
} else {
|
||
m_error = "Card not found: " + m_account.deviceId + " " + m_account.lastNumbers;
|
||
qWarning() << m_error;
|
||
emit finishedWithResult(m_error);
|
||
return;
|
||
}
|
||
}
|
||
AdbUtils::goBack(m_account.deviceId);
|
||
emit finishedWithResult(m_error);
|
||
}
|