#include "packinfoseed2server.h" #include #include #include "globalinfo.h" PackInfoSeed2Server::PackInfoSeed2Server(PackInfoManger * manger, QObject *parent, bool autoSync) : QObject(parent), _manger(manger),_autoSync(autoSync) { connect(&_requst,&HttpRequest::result,this,&PackInfoSeed2Server::result); connect(&_rukuRequest,&HttpRequest::result,this,&PackInfoSeed2Server::rukuResult); _netErrorCount = 0; _oneSyncError = 0; log = PQ::PQLogManger::this_()->getLog("PackInfoSeed2Server"); } void PackInfoSeed2Server::send(QSharedPointer & info,bool isruku) { isRuKu = isruku; if(_manger == nullptr) return; _queue.append(info); QTimer::singleShot(20,this,&PackInfoSeed2Server::doSend); } void PackInfoSeed2Server::doSend() { if(_current.isNull()){ if(_queue.isEmpty()) { emit syncEnd(); return; } _current = _queue.first(); //docs: http://doc.vanlai.net:3001/web/#/1?page_id=64 QString url = GlobalInfo::this_()->config()->baseUrl+"/v1/product/pending"; _requst.setUserToken(QString::number(GlobalInfo::this_()->user().accountId),GlobalInfo::this_()->user().acessToken); _requst.post(url,_current->toObject()); } } void PackInfoSeed2Server::sendList(QList> & infos) { if(_manger == nullptr) return; _queue.append(infos); QTimer::singleShot(20,this,&PackInfoSeed2Server::doSend); } void PackInfoSeed2Server::ruKu(QSharedPointer current,int product) { QJsonObject obj; QJsonArray array; for(int i = 0;iboxInfoSize();i++){ array.append(current->boxes().at(i)->caseNumber()); } obj.insert("product_id",product); obj.insert("details",array); _rukuCurrent.warehouse_id = current->warehouse_id; _rukuCurrent.way = 2; _rukuCurrent.product_details.append(obj); _rukuCurrent.remark = current->remark; _rukuQueue.append(_rukuCurrent); QTimer::singleShot(20,this,&PackInfoSeed2Server::doRuKu); } void PackInfoSeed2Server::doRuKu() { if(_rukuQueue.isEmpty()){ return; } QString url = GlobalInfo::this_()->config()->baseUrl+"/v1/product/inbound"; _rukuRequest.setUserToken(QString::number(GlobalInfo::this_()->user().accountId),GlobalInfo::this_()->user().acessToken); _rukuRequest.post(url,_rukuCurrent.toObject()); _rukuQueue.dequeue(); } void PackInfoSeed2Server::rukuResult(int code, const QJsonObject & body){ qDebug()<= 0){ // TODO: fix error _queue.dequeue(); _manger->setPackInfoSync(_current->codeSingle(),_current->time(),_autoSync); int product_id = body.value("data").toObject().value("product_id").toInt(); if(isRuKu){ ruKu(_current,product_id); } _current.clear(); QTimer::singleShot(20,this,&PackInfoSeed2Server::doSend); _netErrorCount = 0; _oneSyncError = 0; } else { _oneSyncError ++; if(_oneSyncError > 3){ pqWarning(log) << "sync error,and to skip: data : \n" << QJsonDocument(_current->toObject()).toJson() << "\n error: \n " << QJsonDocument(body).toJson(); _oneSyncError = 0; _queue.dequeue(); } _current.clear(); QTimer::singleShot(20,this,&PackInfoSeed2Server::doSend); } }