Files
QTradeProgram/Sqbase/qsubscriptionmanager.h
2026-02-25 23:01:42 +08:00

76 lines
2.3 KiB
C++

#ifndef QSUBSCRIPTIONMANAGER_H
#define QSUBSCRIPTIONMANAGER_H
#include <QObject>
#include <QList>
#include <QMap>
#include <QSharedPointer>
#include <QDateTime>
#include <QMutex>
#include "BZStruct.h"
struct SubscriptionConfig {
QString stockCode;
double threshold; // 检测阈值
bool enabled;
QDateTime lastUpdated;
SubscriptionConfig() : threshold(0.0), enabled(true) {}
SubscriptionConfig(const QString& code, double thresh, bool en = true)
: stockCode(code), threshold(thresh), enabled(en), lastUpdated(QDateTime::currentDateTime()) {}
bool operator==(const SubscriptionConfig& other) const {
return stockCode == other.stockCode;
}
};
class QSubscriptionManager : public QObject
{
Q_OBJECT
public:
static QSubscriptionManager* instance();
// 订阅管理
bool addSubscription(const QString& stockCode, double threshold);
bool removeSubscription(const QString& stockCode);
bool updateThreshold(const QString& stockCode, double threshold);
bool enableSubscription(const QString& stockCode, bool enabled = true);
// 查询接口
QList<SubscriptionConfig> getAllSubscriptions() const;
double getThreshold(const QString& stockCode) const;
bool isSubscribed(const QString& stockCode) const;
bool isEnabled(const QString& stockCode) const;
// 批量操作
void addSubscriptions(const QList<SubscriptionConfig>& subscriptions);
void removeSubscriptions(const QList<QString>& stockCodes);
// 配置持久化
void loadFromFile(const QString& filePath = "");
void saveToFile(const QString& filePath = "");
// 统计信息
int getSubscriptionCount() const;
int getEnabledSubscriptionCount() const;
signals:
void subscriptionAdded(const QString& stockCode, double threshold);
void subscriptionRemoved(const QString& stockCode);
void thresholdUpdated(const QString& stockCode, double threshold);
void subscriptionEnabledChanged(const QString& stockCode, bool enabled);
void subscriptionsChanged();
private:
explicit QSubscriptionManager(QObject *parent = nullptr);
~QSubscriptionManager();
QMap<QString, SubscriptionConfig> m_subscriptions;
QString m_configPath;
mutable QMutex m_mutex;
QString getDefaultConfigPath() const;
};
#endif // QSUBSCRIPTIONMANAGER_H