autosyncpackinfo.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #include "autosyncpackinfo.h"
  2. #include "globalinfo.h"
  3. #include <QTimer>
  4. AutoSyncPackInfo::AutoSyncPackInfo(QObject *parent) : QObject(parent),_server(nullptr)
  5. {
  6. _manger = nullptr;
  7. inQueue = false;
  8. }
  9. void AutoSyncPackInfo::start()
  10. {
  11. if(th == nullptr){
  12. th = new QThread(this);
  13. this->moveToThread(th);
  14. connect(th,&QThread::started,this,&AutoSyncPackInfo::doSend);
  15. th->start();
  16. } else {
  17. QTimer::singleShot(20,this,&AutoSyncPackInfo::doSend);
  18. }
  19. }
  20. void AutoSyncPackInfo::sync(const QDate & dt)
  21. {
  22. if(GlobalInfo::this_()->isNoLine()) return;
  23. mutex.lock();
  24. _dateQueue.append(dt);
  25. mutex.unlock();
  26. QTimer::singleShot(20,this,&AutoSyncPackInfo::doQueue);
  27. }
  28. void AutoSyncPackInfo::doQueue()
  29. {
  30. if(inQueue) return;
  31. if(_manger == nullptr)
  32. _manger = GlobalInfo::this_()->packInfoManger();
  33. if(_server == nullptr) {
  34. _server = new PackInfoSeed2Server(_manger,this,false);
  35. connect(_server,&PackInfoSeed2Server::syncEnd,this,&AutoSyncPackInfo::syncedOne);
  36. }
  37. if(_dateQueue.isEmpty()) return;
  38. QDate n = _dateQueue.first();
  39. auto list = _manger->getPackInfo(n,true);
  40. QList<QSharedPointer<PackInfo>> infos;
  41. for(int i = 0; i < list.size(); ++i){
  42. infos.append(list.at(i).packInfo);
  43. }
  44. _server->sendList(infos);
  45. }
  46. void AutoSyncPackInfo::loadOut()
  47. {
  48. mutex.lock();
  49. _dateQueue.clear();
  50. mutex.unlock();
  51. }
  52. void AutoSyncPackInfo::doSend()
  53. {
  54. if(GlobalInfo::this_()->isNoLine()) return;
  55. if(_manger == nullptr)
  56. _manger = GlobalInfo::this_()->packInfoManger();
  57. QDate dt = QDate::currentDate();
  58. int td = -15;
  59. while(true){
  60. auto tdd = dt.addDays(td);
  61. auto list = _manger->getPackInfo(tdd,true);
  62. if(!list.isEmpty()) {
  63. td -= 7;
  64. } else {
  65. break;
  66. }
  67. }
  68. mutex.lock();
  69. for(int i = td; i < 0; ++i){
  70. _dateQueue.append(dt.addDays(td));
  71. }
  72. // _dateQueue.append(dt);
  73. mutex.unlock();
  74. }
  75. void AutoSyncPackInfo::syncedOne()
  76. {
  77. if(_dateQueue.isEmpty()) return;
  78. mutex.lock();
  79. _dateQueue.dequeue();
  80. mutex.unlock();
  81. QTimer::singleShot(20,this,&AutoSyncPackInfo::doQueue);
  82. }