projectinfomanger.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. #include "projectinfomanger.h"
  2. #include <QTimer>
  3. #include "globalinfo.h"
  4. #include <QSet>
  5. #include <pqQtlib/utils/pqfileutils.h>
  6. #include <QJsonDocument>
  7. ProjectInfoManger::ProjectInfoManger(PackInfoManger *manger, QObject *parent) : QObject(parent),_manger(manger)
  8. {
  9. }
  10. bool ProjectInfoManger::loadServerList()
  11. {
  12. if(_status != IDIL) return false;
  13. _serverInfo.clear();
  14. _cacheInfo.clear();
  15. _oneSyncError = 0;
  16. _netErrorCount = 0;
  17. QTimer::singleShot(0,this,&ProjectInfoManger::doSendLoad);
  18. return true;
  19. }
  20. bool ProjectInfoManger::syncLocalList()
  21. {
  22. if(_status != IDIL) return false;
  23. auto info = GlobalInfo::this_()->cacheInfo();
  24. _serverInfo.clear();
  25. _cacheInfo.clear();
  26. _pushQueue.clear();
  27. for(int i = 0; i < info.size(); ++i){
  28. _pushQueue.append(info.at(i));
  29. }
  30. QTimer::singleShot(0,this,&ProjectInfoManger::syncLocalList);
  31. return true;
  32. }
  33. void ProjectInfoManger::doSendLoad()
  34. {
  35. //TODO:小宋,请求获取批号列表信息
  36. // docs: http://doc.vanlai.net:3001/web/#/1?page_id=60
  37. }
  38. void ProjectInfoManger::doSendCreate()
  39. {
  40. if(!_pushQueue.isEmpty()){
  41. _status = Sync_Local_List;
  42. QSharedPointer<ProjectInfo> info = _pushQueue.first();
  43. // TODO: 小宋, 调用新建批号的接口
  44. // docs: http://doc.vanlai.net:3001/web/#/1?page_id=59
  45. } else {
  46. _status = IDIL;
  47. QTimer::singleShot(0,this,&ProjectInfoManger::doSendLoad);
  48. }
  49. }
  50. void ProjectInfoManger::handleList()
  51. {
  52. QSet<QString> remoted;
  53. for(int i = 0; i < _serverInfo.size(); ++i){
  54. remoted.insert(_serverInfo.at(i)->batch_no);
  55. }
  56. _cacheInfo.clear();
  57. auto list = _manger->getLocalProjectInfo();
  58. for(int i = 0; i < list.size(); ++i){
  59. auto info = list.at(i);
  60. if(!remoted.contains(info->batch_no)){
  61. _cacheInfo.append(info);
  62. }
  63. }
  64. }
  65. void ProjectInfoManger::result(int code, const QJsonObject & body)
  66. {
  67. switch (_status) {
  68. case Load_Server_List:
  69. handleGetList(code,body);
  70. break;
  71. case Sync_Local_List:
  72. handlSync(code,body);
  73. break;
  74. default:
  75. break;
  76. }
  77. }
  78. void ProjectInfoManger::handlSync(int code, const QJsonObject & body)
  79. {
  80. if(code != 200){
  81. _netErrorCount ++;
  82. if(_netErrorCount > 3) {
  83. emit synced(this,false);
  84. } else {
  85. QTimer::singleShot(500,this,&ProjectInfoManger::syncLocalList);
  86. }
  87. return;
  88. }
  89. // auto error = body.value("code").toInt();
  90. // if(error == 200){
  91. _pushQueue.dequeue();
  92. QTimer::singleShot(50,this,&ProjectInfoManger::syncLocalList);
  93. // } else {
  94. // _pushQueue.dequeue();
  95. // QTimer::singleShot(50,this,&ProjectInfoManger::syncLocalList);
  96. // }
  97. }
  98. void ProjectInfoManger::handleGetList(int code, const QJsonObject & body)
  99. {
  100. if(code != 200){
  101. _netErrorCount ++;
  102. if(_netErrorCount > 3) {
  103. auto dt = PQ::CacheFile::readFile("ProjectInfoList.cache");
  104. auto ary = QJsonDocument::fromJson(dt).array();
  105. hanldeServerList(ary,false);
  106. } else {
  107. QTimer::singleShot(500,this,&ProjectInfoManger::syncLocalList);
  108. }
  109. return;
  110. }
  111. auto error = body.value("code").toInt();
  112. if(error == 200){
  113. _oneSyncError = 0;
  114. auto ary = body.value("data").toArray();
  115. hanldeServerList(ary);
  116. } else {
  117. _oneSyncError ++;
  118. if(_oneSyncError > 3) {
  119. auto dt = PQ::CacheFile::readFile("ProjectInfoList.cache");
  120. auto ary = QJsonDocument::fromJson(dt).array();
  121. hanldeServerList(ary,false);
  122. } else {
  123. QTimer::singleShot(50,this,&ProjectInfoManger::syncLocalList);
  124. }
  125. }
  126. }
  127. void ProjectInfoManger::hanldeServerList(QJsonArray & ary,bool inServer)
  128. {
  129. if(inServer){
  130. auto dt = QJsonDocument(ary).toJson();
  131. PQ::CacheFile::writeFile("ProjectInfoList.cache",dt);
  132. }
  133. //TODO: 小宋 解析返回的到 _serverInfo
  134. // docs: http://doc.vanlai.net:3001/web/#/1?page_id=60
  135. }