autouploadfile.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. #include "autouploadfile.h"
  2. #include "sncdatabase.h"
  3. #include "fileprepare.h"
  4. #include "respsubmitshkjfile.h"
  5. #include "resppackmerge.h"
  6. #include "resppacktran.h"
  7. #include "respuserserviceverify.h"
  8. #include "filesplitter.h"
  9. AutoUploadFile::AutoUploadFile()
  10. {
  11. m_stop = false;
  12. m_cubeWs = new WSCube2();
  13. m_moduleID = "shkj";
  14. m_clientID = "";
  15. m_skip = false;
  16. m_stop = false;
  17. }
  18. void AutoUploadFile::abort()
  19. {
  20. m_stop = true;
  21. m_cubeWs->abort();
  22. }
  23. void AutoUploadFile::setSkip(bool skip)
  24. {
  25. m_skip = skip;
  26. }
  27. AutoUploadFile::~AutoUploadFile()
  28. {
  29. m_cubeWs->abort();
  30. delete m_cubeWs;
  31. qDebug()<<QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss")\
  32. <<"FormRiddle.AutoUpload Destroyed.";
  33. }
  34. void AutoUploadFile::setAccountInfo(QMap<QString, QString> accountInfo)
  35. {
  36. m_account = accountInfo;
  37. qDebug()<<m_account;
  38. }
  39. void AutoUploadFile::run()
  40. {
  41. if(m_skip)
  42. return ;
  43. m_stop = false;
  44. QString userCode = m_account.value("usercode");
  45. QString ssoHash = m_account.value("ssohash");
  46. if(ssoHash.isEmpty()||userCode.isEmpty())
  47. return ;
  48. QMultiMap<QString,QString> list = SNCDataBase::getAllUpload(m_moduleID,m_clientID,userCode);
  49. int cols = list.uniqueKeys().size();
  50. int rows = 0;
  51. if(cols!=0)
  52. rows = list.size()/cols;
  53. for(int i=0;i<rows;i++)
  54. {
  55. if(m_stop)
  56. break;
  57. int fileid = list.values("fileid").at(i).toInt();
  58. QString filepath = list.values("filepath").at(i);
  59. QString filetype = list.values("filetype").at(i);
  60. QString memo = list.values("memo").at(i);
  61. int state = list.values("uploadstate").at(i).toInt();
  62. QString tasktype = list.values("tasktype").at(i);
  63. if(state==0 || state == 2)
  64. {
  65. m_errCode = 0;
  66. m_errMsg = "";
  67. m_errDetail = "";
  68. SNCDataBase::updateFileUploadState(fileid,1,m_errMsg,m_errDetail);
  69. int ret = upload(fileid,filepath,filetype,tasktype,memo);
  70. switch(ret)
  71. {
  72. case 0:
  73. SNCDataBase::updateFileUploadState(fileid,2,m_errMsg,m_errDetail);
  74. break;
  75. case 1:
  76. SNCDataBase::updateFileUploadState(fileid,3,m_errMsg,m_errDetail);
  77. emit uploadSucceed();
  78. break;
  79. case -1:
  80. SNCDataBase::updateFileUploadState(fileid,m_errCode,m_errMsg,m_errDetail);
  81. emit uploadFailed();
  82. break;
  83. }
  84. }
  85. }
  86. }
  87. int AutoUploadFile::upload(int fileid,QString filePath, QString fileType,QString taskType, QString memo)
  88. {
  89. bool multi;
  90. if(taskType=="1")
  91. multi = false;
  92. else
  93. multi = true;
  94. FilePrepare filePrepare(filePath,fileType,multi,this->m_moduleID,this->m_clientID);
  95. if(!filePrepare.prepare())
  96. {
  97. m_errCode = -1;
  98. m_errMsg = filePrepare.getRtnMsg();
  99. return -1;
  100. }
  101. if(m_stop)
  102. {
  103. return 0;
  104. }
  105. qDebug()<<"sumit"<<filePrepare.getCount();
  106. QString json = m_cubeWs->userServiceVerify("SubmitShkjFile",QString("%1").arg(filePrepare.getCount()),m_account.value("ssohash"));
  107. RespUserServiceVerify response(json);
  108. qDebug()<<json;
  109. if(response.getRtnCode()<=0)
  110. {
  111. m_errMsg = response.getRtnMemo();
  112. m_errCode = -4;
  113. if(response.getRtnCode()==RTNCODE_NETWORK_ABORT)
  114. {
  115. return 0;
  116. }
  117. else if(response.getRtnCode()==RTNCODE_NETWORK_NOTAVAILABLE)
  118. {
  119. m_errCode = -2;
  120. return -1;
  121. }
  122. else
  123. {
  124. return -1;
  125. }
  126. }
  127. FileSplitter splitter(filePrepare.getLastUploadFileName(),QString::number(fileid));
  128. QStringList list = splitter.getSplitResult();
  129. bool allupload = true;
  130. QString packName;
  131. for(int i=0;i<list.size();i++)
  132. {
  133. if(m_stop)
  134. {
  135. return 0;
  136. }
  137. packName = list.at(i);
  138. QString json = m_cubeWs->packTran(packName,m_account.value("ssohash"));
  139. RespPackTran response(json);
  140. if(response.getRtnCode()==1)
  141. {
  142. QFile::remove(packName);
  143. }
  144. else
  145. {
  146. m_errMsg = response.getRtnMemo();
  147. m_errCode = -4;
  148. if(response.getRtnCode()==RTNCODE_NETWORK_ABORT)
  149. {
  150. return 0;
  151. }
  152. else if(response.getRtnCode()==RTNCODE_NETWORK_NOTAVAILABLE)
  153. {
  154. m_errCode = -2;
  155. return -1;
  156. }
  157. allupload = false;
  158. break;
  159. }
  160. SNCDataBase::updateFileUploadPercent(fileid,(i+1)*100.0/list.size());
  161. emit uploadProgressUpdated();
  162. }
  163. if(allupload)
  164. {
  165. splitter.cleanup();
  166. QString json = m_cubeWs->packMerge(packName,m_account.value("ssohash"));
  167. qDebug()<<packName;
  168. RespPackMerge response(json);
  169. if(response.getRtnCode()==1)
  170. {
  171. json = m_cubeWs->submitShkjFile(filePrepare.getLastUploadFileName(),response.getFileHash(),"PackMerge",fileType,memo,m_account.value("ssohash"));
  172. qDebug()<<filePrepare.getLastUploadFileName()<<response.getFileHash()<<fileType<<memo<<m_account.value("ssohash");
  173. RespSubmitShkjFile response(json);
  174. if(response.getRtnCode()==2)
  175. {
  176. m_errCode = 1;
  177. m_errMsg = "上传成功,号码全部接收。";
  178. m_errDetail = response.getRtnMemo();
  179. //saveToFile(response.getBatchID(),m_errDetail);
  180. SNCDataBase::updateFileUploadSrvInfo(fileid,response.getLtfid(),response.getInsertTime());
  181. }
  182. else if(response.getRtnCode()==1)
  183. {
  184. m_errCode = 1;
  185. m_errMsg = "上传成功,文件中含有部分非法数据。";
  186. m_errDetail = response.getRtnMemo();
  187. saveToFile(response.getLtfid(),m_errDetail);
  188. SNCDataBase::updateFileUploadSrvInfo(fileid,response.getLtfid(),response.getInsertTime());
  189. }
  190. else
  191. {
  192. qDebug()<<"AutoUploadFile"<<response.getRtnMemo();
  193. m_errMsg = response.getRtnMemo();
  194. m_errCode = -4;
  195. if(response.getRtnCode()==RTNCODE_NETWORK_ABORT)
  196. {
  197. return 0;
  198. }
  199. else if(response.getRtnCode()==RTNCODE_NETWORK_NOTAVAILABLE)
  200. {
  201. m_errCode = -2;
  202. return -1;
  203. }
  204. }
  205. }
  206. else
  207. {
  208. m_errMsg = response.getRtnMemo();
  209. m_errCode = -4;
  210. if(response.getRtnCode()==RTNCODE_NETWORK_ABORT)
  211. {
  212. return 0;
  213. }
  214. else if(response.getRtnCode()==RTNCODE_NETWORK_NOTAVAILABLE)
  215. {
  216. m_errCode = -2;
  217. return -1;
  218. }
  219. }
  220. }
  221. if(m_errCode<0)
  222. return -1;
  223. else
  224. return 1;
  225. }
  226. int AutoUploadFile::saveToFile(QString ltfid, QString content)
  227. {
  228. QString userCode = m_account.value("usercode");
  229. QString cachePath = QCoreApplication::applicationDirPath()+\
  230. QString("/../data/%1/cache/shkj/upload/").arg(userCode);
  231. QDir dir;
  232. dir.mkpath(cachePath);
  233. QString fileName = cachePath + QString("%1.rtn").arg(ltfid);
  234. QFile file(fileName);
  235. file.open(QIODevice::WriteOnly);
  236. QTextStream out(&file);
  237. out<<content;
  238. file.close();
  239. }