TradeDatabaseMgr* TradeDatabaseMgr::Instance()
{
if(!m_instance)
{
m_instance = new TradeDatabaseMgr(NULL);
}
return m_instance;
}
//创建TradeDb类的实例,
//TradeDb封装了trade.db的所有表的操作
QSharedPointer<TradeInterface>TradeDatabaseMgr::GetTradeDb()
{
if(m_spTradedb.isNull())
{
//QString dbFile = SuperDir().GetTradeDBFileEx(m_UserName);
QSqlDatabase tradeDb;
//("Ware", "Ware", wareDb)
DatabaseHelper::Instance()->GetDatabaseManage()->GetDB(PluginName,"Trade",tradeDb);// = m_spSqlite->GetDatabase(dbFile);
m_spTradedb = QSharedPointer<TradeDb>(new TradeDb(tradeDb));
}
return m_spTradedb;
}
//TradeDb类从TradeInterface派生,这个很关键,因为QSharedPointer<TradeInterface>TradeDatabaseMgr::GetTradeDb()返回的是TradeInterface的指针,
//再一个很关键的就是:TradeDb中,封装了trade.db数据库中,每个表的操作类dml类,
#ifndef TRADEDB_H
#define TRADEDB_H
#include "TradeInterface.h"
#include <QSqlDatabase>
#include "dml/OrderDataDml.h"
#include "dml/OrderItemDml.h"
#include "dml/CouponDetailDml.h"
#include "dml/CompanyCustomDml.h"
#include "dml/DeliveryCompanyDml.h"
#include "dml/OrderDownloadDml.h"
#include <TradeStructDef.h>
using WorkStation::utils::ORDER_LIST_STRUCT;
namespace WorkStation{
namespace database{
class TradeDb : public TradeInterface
{
Q_OBJECT
public:
TradeDb(QSqlDatabase db, QObject *parent=NULL);
~TradeDb();
virtual bool CheckDb();
virtual bool CheckUserMatch(QStringconst& strVenderId);
virtual bool DeleteTable();
virtual bool AddOrder(OrderDataEntity*orderData, bool&bExist, bool updateState=true);//order_data表中增加一条记录
virtual bool GetOrderDataFromId(qint64orderId, OrderDataEntity*entity);//从order_data表 order_item表获取order信息
virtual QList<OrderDataEntity*>GetOrderDataFromId(QList<qint64>const& orderIds, bool& isok);
private:
QSqlDatabase m_TradeDb;
QSharedPointer<dml::OrderDataDml> m_spOrderDataDml;
QSharedPointer<dml::OrderItemDml> m_spOrderItemDml;
QSharedPointer<dml::CouponDetailDml> m_spCouponDetailDml;
QSharedPointer<dml::CompanyCustomDml> m_spCompanyCustomDml;
QSharedPointer<dml::DeliveryCompanyDml> m_spDeliveryCompanyDml;
QSharedPointer<dml::OrderDownloadDml> m_spOrderDownloadDml;
};
}
}
#endif // TRADEDB_H
bool TradeDb::AddOrder(OrderDataEntity*orderData, bool&bExist, bool updateState)
{
if(!orderData)
{
Q_ASSERT(false);
return false;
}
//先看数据库中是否存在
bExist = m_spOrderDataDml->IsOrderExist(orderData->GetOrder_id());
bool isok = false;
if(m_TradeDb.transaction())
{
if(bExist) //存在就更新
{
isok = m_spOrderDataDml->UpdateOrderData(orderData,updateState);
}
else
{
isok = m_spOrderDataDml->AddOrderData(orderData);
}
if(!bExist &&isok)
{
isok = m_spOrderItemDml->AddOrderItems(orderData->GetItemsList());
if(isok)
{
isok = m_spCouponDetailDml->AddCouponDetails(orderData->GetCouponDetailsList());
}
}
if(isok)
m_TradeDb.commit();
else
m_TradeDb.rollback();
}
// {
// delete orderData;
// orderData = NULL;
// }
return isok;
}