packinfoseed2server.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #include "packinfoseed2server.h"
  2. #include <QTimer>
  3. #include <QJsonDocument>
  4. #include "globalinfo.h"
  5. PackInfoSeed2Server::PackInfoSeed2Server(PackInfoManger * manger, QObject *parent, bool autoSync) : QObject(parent),
  6. _manger(manger),_autoSync(autoSync)
  7. {
  8. connect(&_requst,&HttpRequest::result,this,&PackInfoSeed2Server::result);
  9. connect(&_rukuRequest,&HttpRequest::result,this,&PackInfoSeed2Server::rukuResult);
  10. _netErrorCount = 0;
  11. _oneSyncError = 0;
  12. log = PQ::PQLogManger::this_()->getLog("PackInfoSeed2Server");
  13. }
  14. void PackInfoSeed2Server::send(QSharedPointer<PackInfo> & info,bool isruku)
  15. {
  16. isRuKu = isruku;
  17. if(_manger == nullptr) return;
  18. _queue.append(info);
  19. QTimer::singleShot(20,this,&PackInfoSeed2Server::doSend);
  20. }
  21. void PackInfoSeed2Server::doSend()
  22. {
  23. if(_current.isNull()){
  24. if(_queue.isEmpty()) {
  25. emit syncEnd();
  26. return;
  27. }
  28. _current = _queue.first();
  29. //docs: http://doc.vanlai.net:3001/web/#/1?page_id=64
  30. QString url = GlobalInfo::this_()->config()->baseUrl+"/v1/product/pending";
  31. _requst.setUserToken(QString::number(GlobalInfo::this_()->user().accountId),GlobalInfo::this_()->user().acessToken);
  32. _requst.post(url,_current->toObject());
  33. }
  34. }
  35. void PackInfoSeed2Server::sendList(QList<QSharedPointer<PackInfo>> & infos)
  36. {
  37. if(_manger == nullptr) return;
  38. _queue.append(infos);
  39. QTimer::singleShot(20,this,&PackInfoSeed2Server::doSend);
  40. }
  41. void PackInfoSeed2Server::ruKu(QSharedPointer<PackInfo> current,int product)
  42. {
  43. QJsonObject obj;
  44. QJsonArray array;
  45. for(int i = 0;i<current->boxInfoSize();i++){
  46. array.append(current->boxes().at(i)->caseNumber());
  47. }
  48. obj.insert("product_id",product);
  49. obj.insert("details",array);
  50. _rukuCurrent.warehouse_id = current->warehouse_id;
  51. _rukuCurrent.way = 2;
  52. _rukuCurrent.product_details.append(obj);
  53. _rukuCurrent.remark = current->remark;
  54. _rukuQueue.append(_rukuCurrent);
  55. QTimer::singleShot(20,this,&PackInfoSeed2Server::doRuKu);
  56. }
  57. void PackInfoSeed2Server::doRuKu()
  58. {
  59. if(_rukuQueue.isEmpty()){
  60. return;
  61. }
  62. QString url = GlobalInfo::this_()->config()->baseUrl+"/v1/product/inbound";
  63. _rukuRequest.setUserToken(QString::number(GlobalInfo::this_()->user().accountId),GlobalInfo::this_()->user().acessToken);
  64. _rukuRequest.post(url,_rukuCurrent.toObject());
  65. _rukuQueue.dequeue();
  66. }
  67. void PackInfoSeed2Server::rukuResult(int code, const QJsonObject & body){
  68. qDebug()<<body;
  69. }
  70. void PackInfoSeed2Server::result(int code, const QJsonObject & body)
  71. {
  72. qDebug()<<body;
  73. if(code != 200 || body.isEmpty()){
  74. _current.clear();
  75. QTimer::singleShot(500,this,&PackInfoSeed2Server::doSend);
  76. _netErrorCount ++;
  77. return;
  78. }
  79. auto error = body.value("code").toInt();
  80. auto message = body.value("message").toString();
  81. if(error == 200 || message.indexOf("已存在") >= 0){ // TODO: fix error
  82. _queue.dequeue();
  83. _manger->setPackInfoSync(_current->codeSingle(),_current->time(),_autoSync);
  84. int product_id = body.value("data").toObject().value("product_id").toInt();
  85. if(isRuKu){
  86. ruKu(_current,product_id);
  87. }
  88. _current.clear();
  89. QTimer::singleShot(20,this,&PackInfoSeed2Server::doSend);
  90. _netErrorCount = 0;
  91. _oneSyncError = 0;
  92. } else {
  93. _oneSyncError ++;
  94. if(_oneSyncError > 3){
  95. pqWarning(log) << "sync error,and to skip: data : \n" << QJsonDocument(_current->toObject()).toJson() <<
  96. "\n error: \n " << QJsonDocument(body).toJson();
  97. _oneSyncError = 0;
  98. _queue.dequeue();
  99. }
  100. _current.clear();
  101. QTimer::singleShot(20,this,&PackInfoSeed2Server::doSend);
  102. }
  103. }