autosyncdata.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. #include "autosyncdata.h"
  2. #include "respgetrecommendseg.h"
  3. #include "sncdatabase.h"
  4. #include "unzip.h"
  5. #include "respgetsegdyinfo.h"
  6. #include "respdldyseginfo.h"
  7. AutoSyncData::AutoSyncData()
  8. {
  9. m_stop = false;
  10. m_skip = false;
  11. m_cubeWs = new WSCube();
  12. m_numchk = new WSNumChk();
  13. m_first = true;
  14. m_clientID = "";
  15. m_moduleID = "";
  16. }
  17. AutoSyncData::~AutoSyncData()
  18. {
  19. m_cubeWs->abort();
  20. m_numchk->abort();
  21. delete m_cubeWs;
  22. delete m_numchk;
  23. qDebug()<<QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss")\
  24. <<"FormRec.AutoSyncData Destroyed.";
  25. }
  26. void AutoSyncData::setSkip(bool skip)
  27. {
  28. m_skip = skip;
  29. }
  30. void AutoSyncData::abort()
  31. {
  32. m_stop = true;
  33. m_cubeWs->abort();
  34. m_numchk->abort();
  35. }
  36. void AutoSyncData::setAccountInfo(QMap<QString, QString> accountInfo)
  37. {
  38. m_account = accountInfo;
  39. m_first = true;
  40. }
  41. void AutoSyncData::sync()
  42. {
  43. QString ssoHash = m_account.value("ssohash");
  44. QString userCode = m_account.value("usercode");
  45. if(ssoHash.isEmpty() || userCode.isEmpty())
  46. return ;
  47. if(!checkSynCondition())
  48. return;
  49. QTime t;
  50. t.start();
  51. updateSegRecInfo();
  52. emit dataSync();
  53. }
  54. bool AutoSyncData::checkSynCondition()
  55. {
  56. //从服务器同步的条件
  57. //1、用户还未同步过一次
  58. if(m_first)
  59. {
  60. m_first = false;
  61. return true;
  62. }
  63. return false;
  64. }
  65. void AutoSyncData::run()
  66. {
  67. if(m_skip)
  68. return ;
  69. m_stop = false;
  70. sync();
  71. }
  72. QString AutoSyncData::dlSegRecInfo()
  73. {
  74. QString mcode = SNCDataBase::getAppHash();
  75. QString clientVer = SNCDataBase::getAppVersion();
  76. QString userhash = m_account.value("userhash");
  77. QString json = m_cubeWs->getRecommendSeg(clientVer,mcode,userhash,"","");
  78. QString zipFile;
  79. RespGetRecommendSeg response(json);
  80. if(response.getRtnCode()>=0)
  81. {
  82. zipFile = QDir::tempPath()+"/snc_recseg.zip";
  83. QString segRecInfo = response.getSegRecInfo();
  84. QByteArray arrayBase = QByteArray::fromBase64(segRecInfo.toAscii());
  85. QFile file(zipFile);
  86. file.open(QIODevice::WriteOnly);
  87. file.write(arrayBase);
  88. file.close();
  89. }
  90. return zipFile;
  91. }
  92. bool AutoSyncData::updateSegRecInfo()
  93. {
  94. QString zipFile = dlSegRecInfo();
  95. if(zipFile.isEmpty())
  96. return false;
  97. QString csvFile = extractZipFile(zipFile);
  98. if(csvFile.isEmpty())
  99. return false;
  100. SNCDataBase::updateSegRecInfo(csvFile);
  101. QFile::remove(zipFile);
  102. QFile::remove(csvFile);
  103. return true;
  104. }
  105. QString AutoSyncData::dlSegDynamicInfoByAreaCode(QString areacode)
  106. {
  107. QString json = m_numchk->dlDySegInfo(areacode);
  108. RespDlDySegInfo response(json);
  109. QString zipFile;
  110. if(response.getRtnCode()>0)
  111. {
  112. zipFile = QDir::tempPath()+"/snc_dynamicseg.zip";
  113. QString segChkInfo = response.getSegChkInfo();
  114. QByteArray arrayBase = QByteArray::fromBase64(segChkInfo.toAscii());
  115. QFile file(zipFile);
  116. file.open(QIODevice::WriteOnly);
  117. file.write(arrayBase);
  118. file.close();
  119. }
  120. return zipFile;
  121. }
  122. bool AutoSyncData::updateSegDynamicInfoByAreaCode(QString areacode)
  123. {
  124. QString zipFile = dlSegDynamicInfoByAreaCode(areacode);
  125. if(zipFile.isEmpty())
  126. return false;
  127. QString csvFile = extractZipFile(zipFile);
  128. if(csvFile.isEmpty())
  129. return false;
  130. //qDebug()<<zipFile;
  131. QString today = QDate::currentDate().toString("yyyy-MM-dd");
  132. SNCDataBase::updateDynamicSegInfo(today,areacode,csvFile);
  133. //qDebug()<<csvFile;
  134. //QFile::remove(QDir::tempPath()+"/snc_recseg.zip");
  135. //QFile::remove(QDir::tempPath()+"/speednumcube/recseg.csv");
  136. return true;
  137. }
  138. QString AutoSyncData::dlSegDynamicInfoBySeg(QString seg)
  139. {
  140. QString json = m_numchk->getDySegInfo(seg);
  141. RespGetSegDyInfo response(json);
  142. QString segChkInfo;
  143. if(response.getRtnCode()==1)
  144. {
  145. segChkInfo = response.getSegChkInfo();
  146. }
  147. return segChkInfo;
  148. }
  149. bool AutoSyncData::updateSegDynamicInfoBySeg(QString seg)
  150. {
  151. QString chkinfo = dlSegDynamicInfoBySeg(seg);
  152. if(chkinfo.isEmpty())
  153. return false;
  154. QStringList list = chkinfo.split(",");
  155. if(list.size()!=8)
  156. return false;
  157. QString today = QDate::currentDate().toString("yyyy-MM-dd");
  158. SNCDataBase::updateDynamicSegInfo(today,list);
  159. return true;
  160. }
  161. QString AutoSyncData::extractZipFile(QString zipFile)
  162. {
  163. //准备临时目录并删除该目录下的所有文件
  164. QDir dir;
  165. if(!dir.exists(QDir::tempPath()+"/snctemp/rec/"))
  166. {
  167. dir.mkpath(QDir::tempPath()+"/snctemp/rec/");
  168. }
  169. dir.cd(QDir::tempPath()+"/snctemp/rec/");
  170. QFileInfoList list = dir.entryInfoList();
  171. for(int i=0;i<list.size();i++)
  172. {
  173. if(list.at(i).isFile())
  174. {
  175. //qDebug()<<"AutoSyncData::extractZipFile delete file :"<<list.at(i).absoluteFilePath();
  176. QFile::remove(list.at(i).absoluteFilePath());
  177. }
  178. }
  179. //解压缩zip文件
  180. UnZip::ErrorCode ec;
  181. UnZip uz;
  182. ec = uz.openArchive(zipFile);
  183. if (ec != UnZip::Ok) {
  184. qDebug() << "AutoSyncData::extractZipFile Failed to open archive: " << uz.formatError(ec).toAscii().data() << endl << endl;
  185. return "" ;
  186. }
  187. ec = uz.extractAll(QDir::tempPath()+"/snctemp/rec/");
  188. if (ec != UnZip::Ok) {
  189. qDebug() << "AutoSyncData::extractZipFile Extraction failed: " << uz.formatError(ec).toAscii().data() << endl << endl;
  190. uz.closeArchive();
  191. return "" ;
  192. }
  193. //返回解压缩后的文件,找到第一个csv格式即返回
  194. dir.refresh();
  195. list = dir.entryInfoList();
  196. QString csvFile;
  197. for(int i=0;i<list.size();i++)
  198. {
  199. if(list.at(i).isFile())
  200. {
  201. if(list.at(i).suffix().toLower()=="csv")
  202. {
  203. csvFile = list.at(i).absoluteFilePath();
  204. break;
  205. }
  206. }
  207. }
  208. return csvFile;
  209. }