ImgUploader.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. #include "StdAfx.h"
  2. #include "ImgUploader.h"
  3. #include "..\EvaluationUtil\HttpClient.h"
  4. #include "..\EvaluationUtil\IInputFiled.h"
  5. #include "..\EvaluationUtil\FileInputFiled.h"
  6. #include "basic_struct.h"
  7. #include "ServerConfig.h"
  8. CImgUploader::CImgUploader(void)
  9. {
  10. m_hwnd = NULL;
  11. }
  12. CImgUploader::~CImgUploader(void)
  13. {
  14. }
  15. bool DeleteDirectory(CString sDirName)
  16. {
  17. CFileFind tempFind;
  18. CString sTempFileFind=sDirName+_T("\\*.*");
  19. BOOL IsFinded = tempFind.FindFile(sTempFileFind);
  20. while (IsFinded)
  21. {
  22. IsFinded = tempFind.FindNextFile();
  23. CString sFoundFileName=tempFind.GetFilePath();
  24. if(tempFind.IsDirectory()){
  25. if(!tempFind.IsDots()){
  26. DeleteDirectory(sFoundFileName);
  27. }
  28. }else{
  29. if(tempFind.IsReadOnly())
  30. {
  31. SetFileAttributes(sFoundFileName, FILE_ATTRIBUTE_NORMAL);
  32. }
  33. DeleteFile(sFoundFileName);
  34. }
  35. }
  36. tempFind.Close();
  37. SetFileAttributes(sDirName, FILE_ATTRIBUTE_NORMAL);
  38. RemoveDirectory(sDirName);
  39. return true;
  40. }
  41. /*
  42. //�������̼�� ��������������״̬
  43. //���ڴ���һ�����ε����ݿ�
  44. IMG_UPLOAD_STATE_OPENNING,
  45. //����׼��
  46. IMG_UPLOAD_STATE_READYING,
  47. //�����ϴ�
  48. IMG_UPLOAD_STATE_UPLOADING,
  49. //���ڹرյ�ǰ�򿪵����ݿ�
  50. IMG_UPLOAD_STATE_CLOSING,
  51. //��ǰ���ݿ��Ѿ��ر�
  52. IMG_UPLOAD_STATE_CLOSED,
  53. */
  54. ServiceState CImgUploader::OnRunning( void )
  55. {
  56. ServiceState nextState= IService::OnRunning();
  57. if(nextState != running){
  58. if(m_current_database_state==IMG_UPLOAD_STATE_CLOSED)return nextState;
  59. else m_current_database_state = IMG_UPLOAD_STATE_CLOSING;
  60. }
  61. // Logger pTestLogger = Logger::getInstance(LOG4CPLUS_TEXT("LoggerName"));
  62. switch(m_current_database_state){
  63. case IMG_UPLOAD_STATE_OPENNING://����һ��Ҫ�ϴ�������
  64. {
  65. char sql[256];
  66. sprintf_s(sql,"select * from database where database_id>%d and database_state in(%d,%d,%d) limit 1",m_task_info.upload_task_id,DATABASE_STATE_RESULT_UPLOADED,DATABASE_STATE_IMG_UPLOADING,DATABASE_STATE_IMG_PAUSED);
  67. //LOG4CPLUS_INFO_FMT(pTestLogger, LOG4CPLUS_TEXT("select * from database where database_id>%d and database_state in(%d,%d,%d) limit 1"), m_task_info.upload_task_id, DATABASE_STATE_RESULT_UPLOADED, DATABASE_STATE_IMG_UPLOADING, DATABASE_STATE_IMG_PAUSED);
  68. CppSQLite3Query query =database_db->execQuery(sql);
  69. if (query.eof()){
  70. //LOG4CPLUS_INFO_FMT(pTestLogger, LOG4CPLUS_TEXT("��ǰû���µ���������ϴ�"));
  71. if(m_uploading){
  72. m_uploading =FALSE;
  73. if(m_hwnd!=NULL)::PostMessage(m_hwnd,WM_IDENTIFOR_IMG_UPLOAD_STATE,m_uploading,0);
  74. }
  75. Sleep(1000);
  76. }
  77. else{
  78. if(!m_uploading){
  79. m_uploading =TRUE;
  80. if(m_hwnd!=NULL)::PostMessage(m_hwnd,WM_IDENTIFOR_IMG_UPLOAD_STATE,m_uploading,0);
  81. }
  82. // LOG4CPLUS_INFO_FMT(pTestLogger, LOG4CPLUS_TEXT("��ʼ�ϴ��Ծ��µ�����"));
  83. // m_url.Format(_T("%s/upload/uploadImages"),CString(query.getStringField("server_address")));
  84. m_bantch_dir = CString(query.getStringField("bantch_dir"));
  85. const char * db_file_name = query.getStringField("db_file_name");
  86. m_task_info.upload_task_id = query.getIntField("database_id");
  87. strcpy(m_task_info.task_name,query.getStringField("bantch_code"));
  88. m_task_info.upload_task_state = UPLOAD_TASK_STATE_NOTSTART;
  89. m_task_info.exam_id = query.getInt64Field("exam_id");
  90. m_task_info.exam_course_id = query.getInt64Field("exam_course_id");
  91. m_task_info.scan_type = query.getIntField("scan_type");
  92. m_task_info.exam_course_id_from = query.getInt64Field("exam_course_id_from");
  93. m_task_info.batch_code_from = query.getStringField("batch_code_from");
  94. m_task_info.examinne_id_from = query.getStringField("examinne_id_from");
  95. bantch_db.open(db_file_name);
  96. bantch_db.execDML("pragma journal_mode = MEMORY");
  97. char sql[128];
  98. sprintf_s(sql,"update database set database_state =%d where database_id =%d",DATABASE_STATE_IMG_UPLOADING,m_task_info.upload_task_id);
  99. EnterCriticalSection(database_db_lock);
  100. database_db->execDML("begin transaction");
  101. database_db->execDML(sql);
  102. database_db->execDML("commit transaction");
  103. LeaveCriticalSection(database_db_lock);
  104. // LOG4CPLUS_INFO_FMT(pTestLogger, LOG4CPLUS_TEXT("�Ѿ���ʼ�ϴ��Ծ��µ�����(%d)"), m_task_info.upload_task_id);
  105. m_current_has_more=TRUE;
  106. //if(m_hwnd!=NULL)::PostMessage(m_hwnd,WM_IDENTIFOR_IMG_UPLOAD_TASK_STATE,(WPARAM)&m_task_info,0);
  107. m_current_database_state = IMG_UPLOAD_STATE_READYING;
  108. }
  109. }
  110. break;
  111. case IMG_UPLOAD_STATE_READYING://ͳ�����������Ϣ(����׼��)
  112. {
  113. char sql[512];
  114. sprintf_s(sql,"select count(0) from student_paper p left join student s on s.student_id = p.student_id where (p.paper_state&%d)=0",MEX_BUKESHANGCHUANG);
  115. m_task_info.total_count =bantch_db.execScalar(sql);
  116. sprintf_s(sql,"select count(0) from student_paper p left join student s on s.student_id = p.student_id where (p.paper_state&%d)=0 and not p.img_upload = 0 ",MEX_BUKESHANGCHUANG);
  117. m_task_info.upload_count =bantch_db.execScalar(sql);
  118. sprintf_s(sql,"select * from bantch_info");
  119. CppSQLite3Query q=bantch_db.execQuery(sql);
  120. if(!q.eof()){m_school_id.Format(L"%d",(long)q.getInt64Field("school_id"));}else{m_school_id =L"-1";}
  121. m_task_info.upload_task_state = UPLOAD_TASK_STATE_UPLOADING;
  122. IMG_UPLOAD_TASK * msg=new IMG_UPLOAD_TASK;
  123. memcpy(msg, &m_task_info, sizeof(IMG_UPLOAD_TASK));
  124. // LOG4CPLUS_INFO_FMT(pTestLogger, LOG4CPLUS_TEXT("�ϴ��Ծ��µ�����(%d)ͳ������׼�����"), m_task_info.upload_task_id);
  125. if(m_hwnd!=NULL)::PostMessage(m_hwnd,WM_IDENTIFOR_IMG_UPLOAD_TASK_STATE,(WPARAM)msg,0);
  126. m_last_paper_id =-1;
  127. m_current_database_state = IMG_UPLOAD_STATE_UPLOADING;
  128. }
  129. break;
  130. case IMG_UPLOAD_STATE_UPLOADING://ִ���ϴ�
  131. {
  132. char sql[512];
  133. sprintf_s(sql,"select p.file_name,s.school_id,e.exam_id,e.exam_course_id,p.student_id,p.student_paper_id from student_paper p left join student s on s.student_id = p.student_id left join exam e on 1=1 where p.student_paper_id>%d and (p.paper_state&%d)=0 and p.img_upload = 0 limit 20",m_last_paper_id,MEX_BUKESHANGCHUANG);
  134. CppSQLite3Query query =bantch_db.execQuery(sql);
  135. if(query.eof()){
  136. bool has_someone_uploading;
  137. do
  138. {
  139. handleCompleteTask();
  140. has_someone_uploading =false;
  141. for (int index =0;index<UPLOAD_TASK_BUFFER_COUNT;index++)
  142. {
  143. if(taskBuffer[index].task_state ==CUploadTask::TS_UPLOADING ||taskBuffer[index].task_state ==CUploadTask::TS_COMPLETE){
  144. has_someone_uploading =true;
  145. }
  146. }
  147. } while (has_someone_uploading);
  148. switch(m_task_info.scan_type){
  149. case SCAN_TASK_TYPE_ADJUST_BATCH:
  150. {
  151. CHttpClient httpClient;
  152. CString postData;
  153. postData.Format(_T("examId=%I64d&examCourseId=%I64d&batch=%s"),m_task_info.exam_id,m_task_info.exam_course_id_from,CString(m_task_info.batch_code_from.c_str()));
  154. std::string response;
  155. httpClient.HttpPost(m_urlDeletePaperBatch,postData,response);
  156. // LOG4CPLUS_INFO_FMT(pTestLogger, LOG4CPLUS_TEXT("%s"),CString(response.c_str()));
  157. }
  158. break;
  159. case SCAN_TASK_TYPE_ADJUST_EXAMINNE:
  160. {
  161. CHttpClient httpClient;
  162. CString postData;
  163. postData.Format(_T("examId=%I64d&examCourseId=%I64d&batch=%s&examineeId=%s"),m_task_info.exam_id,m_task_info.exam_course_id_from,CString(m_task_info.batch_code_from.c_str()),CString(m_task_info.examinne_id_from.c_str()));
  164. std::string response;
  165. httpClient.HttpPost(m_urlDeletePaperBatch,postData,response);
  166. // LOG4CPLUS_INFO_FMT(pTestLogger, LOG4CPLUS_TEXT("%s"),CString(response.c_str()));
  167. }
  168. break;
  169. }
  170. m_current_has_more=FALSE;
  171. m_current_database_state = IMG_UPLOAD_STATE_CLOSING;
  172. }
  173. else{
  174. while(!query.eof()){
  175. handleCompleteTask();
  176. int index_task_buffer=-1;
  177. for (int index =0;index<UPLOAD_TASK_BUFFER_COUNT;index++)
  178. {
  179. if(taskBuffer[index].task_state ==CUploadTask::TS_NONE ){
  180. index_task_buffer =index;
  181. break;
  182. }
  183. }
  184. int index_worker=-1;
  185. for (int index =0;index<UPLOAD_WORKER_COUNT;index++)
  186. {
  187. if(worker[index].GetUploadState()==CImageUploadWorker::US_WAITING){
  188. index_worker =index;
  189. break;
  190. }
  191. }
  192. if(index_task_buffer>=0&&index_worker>=0){
  193. CUploadTask& task = taskBuffer[index_task_buffer];
  194. task.examCourseId.Format(_T("%d"),query.getInt64Field("exam_course_id"));
  195. task.examId.Format(_T("%d"),query.getInt64Field("exam_id"));
  196. task.file_name=CString(query.getStringField("file_name"));
  197. task.schoolId.Format(_T("%d"),query.getInt64Field("school_id"));
  198. task.student_id=CString(query.getStringField("student_id"));
  199. task.student_paper_id=query.getIntField("student_paper_id");
  200. task.bantch=m_task_info.task_name;
  201. task.task_state =CUploadTask::TS_UPLOADING;
  202. task.upload_result = FALSE;
  203. task.url = m_url;
  204. BOOL success=worker[index_worker].upload(&taskBuffer[index_task_buffer]);
  205. m_last_paper_id=task.student_paper_id;
  206. // LOG4CPLUS_INFO_FMT(pTestLogger, LOG4CPLUS_TEXT("Ϊ�ϴ��߳�(%d)�����Ծ�idΪ(%d)������(%d)[0:ʧ�� 1:�ɹ� ���->%d]"), index_worker, task.student_paper_id, index_task_buffer, success ? 1 : 0);
  207. query.nextRow();
  208. }else{
  209. Sleep(10);
  210. }
  211. }
  212. }
  213. }
  214. break;
  215. case IMG_UPLOAD_STATE_CLOSING://�����ϴ�����
  216. {
  217. bantch_db.close();
  218. char sql[256];
  219. sprintf_s(sql,"update database set database_state =%d where database_id =%d",m_current_has_more?DATABASE_STATE_IMG_PAUSED:DATABASE_STATE_IMG_UPLOADED,m_task_info.upload_task_id);
  220. EnterCriticalSection(database_db_lock);
  221. database_db->execDML("begin transaction");
  222. database_db->execDML(sql);
  223. database_db->execDML("commit transaction");
  224. LeaveCriticalSection(database_db_lock);
  225. m_task_info.upload_task_state = m_current_has_more?UPLOAD_TASK_STATE_PAUSED:UPLOAD_TASK_STATE_COMPLETED;
  226. IMG_UPLOAD_TASK * msg=new IMG_UPLOAD_TASK;
  227. memcpy(msg, &m_task_info, sizeof(IMG_UPLOAD_TASK));
  228. // LOG4CPLUS_INFO_FMT(pTestLogger, LOG4CPLUS_TEXT("�ϴ��Ծ��µ�����(%d)���"), m_task_info.upload_task_id);
  229. if(m_hwnd!=NULL)::PostMessage(m_hwnd,WM_IDENTIFOR_IMG_UPLOAD_TASK_STATE,(WPARAM)msg,0);
  230. m_current_database_state = IMG_UPLOAD_STATE_CLOSED;
  231. }
  232. break;
  233. case IMG_UPLOAD_STATE_CLOSED://�ϴ�����һ���� ������״̬��Ϊ����һ������
  234. //DeleteDirectory(m_bantch_dir);
  235. m_current_database_state = IMG_UPLOAD_STATE_OPENNING;
  236. break;
  237. }
  238. return running;
  239. }
  240. //�ϴ��������׼��
  241. ServiceState CImgUploader::OnStarting( void )
  242. {
  243. // pTestLogger = Logger::getInstance(LOG4CPLUS_TEXT("LoggerName"));
  244. // LOG4CPLUS_INFO_FMT(pTestLogger, LOG4CPLUS_TEXT("�������ͼƬ�ϴ�����"));
  245. m_uploading = FALSE;
  246. for (int index =0;index<UPLOAD_WORKER_COUNT;index++)
  247. {
  248. worker[index].Start();
  249. }
  250. for (int index =0;index<UPLOAD_TASK_BUFFER_COUNT;index++)
  251. {
  252. taskBuffer[index].task_state = CUploadTask::TS_NONE;
  253. }
  254. m_task_info.upload_task_id = -1;
  255. //m_url.Format(_T("%s/upload/uploadImages"),CServerConfig::server_url);
  256. /*m_urlSendDistrMessage.Format(_T("%s/exam/sendDistrMessageByKafka"),CServerConfig::server_url);*/
  257. m_urlDeletePaperBatch.Format(_T("%s/scan/deletePaperBatch"),CServerConfig::server_address_type==SAT_LOCAL_IN?CServerConfig::server_url_local_in:CServerConfig::server_address_type==SAT_LOCAL_OUT?CServerConfig::server_url_local_out:CServerConfig::server_url);
  258. m_current_database_state = IMG_UPLOAD_STATE_OPENNING;
  259. return IService::OnStarting();
  260. }
  261. int CImgUploader::GetCurrentUploadTaskInfo( IMG_UPLOAD_TASK& task )
  262. {
  263. task = m_task_info;
  264. return TRUE;
  265. }
  266. void CImgUploader::handleCompleteTask()
  267. {
  268. for (int index =0;index<UPLOAD_TASK_BUFFER_COUNT;index++)
  269. {
  270. if(taskBuffer[index].task_state ==CUploadTask::TS_COMPLETE ){
  271. // LOG4CPLUS_INFO_FMT(pTestLogger, LOG4CPLUS_TEXT("����%d�ϴ����"), index);
  272. char sql3[512];
  273. sprintf_s(sql3,"update student_paper set img_upload = %d where student_paper_id=%d",taskBuffer[index].upload_result?2:3,taskBuffer[index].student_paper_id);
  274. bantch_db.execDML("begin transaction");
  275. bantch_db.execDML(sql3);
  276. bantch_db.execDML("commit transaction");
  277. taskBuffer[index].task_state = CUploadTask::TS_NONE;
  278. m_task_info.upload_count++;
  279. IMG_UPLOAD_TASK * msg=new IMG_UPLOAD_TASK;
  280. memcpy(msg,&m_task_info,sizeof(IMG_UPLOAD_TASK));
  281. if(m_hwnd!=NULL)::PostMessage(m_hwnd,WM_IDENTIFOR_IMG_UPLOAD_TASK_PROGRESS,(WPARAM)msg,0);
  282. }
  283. }
  284. }
  285. int CImgUploader::QueryUploadePaperImg(vector<IMG_UPLOAD_TASK>& tasks)
  286. {
  287. EnterCriticalSection(database_db_lock);
  288. char sql[256];
  289. sprintf_s(sql, "select * from database where database_state in(%d,%d,%d,%d) order by database_id desc", DATABASE_STATE_RESULT_UPLOADED, DATABASE_STATE_IMG_UPLOADING, DATABASE_STATE_IMG_PAUSED, DATABASE_STATE_IMG_UPLOADED);
  290. CppSQLite3Query query = database_db->execQuery(sql);
  291. while (!query.eof()){
  292. IMG_UPLOAD_TASK task;
  293. task.upload_task_id = query.getIntField("database_id");
  294. task.total_count = query.getIntField("total_upload_count");
  295. //strcpy(task.task_name ,query.getStringField("bantch_code"));
  296. strcpy(task.time, query.getStringField("create_time"));
  297. strcpy(task.task_name, query.getStringField("exam_name"));
  298. sprintf_s(task.task_name, "%s(%s)", query.getStringField("exam_name"), query.getStringField("course_name"));
  299. int database_state = query.getIntField("database_state");
  300. switch (database_state){
  301. case DATABASE_STATE_RESULT_UPLOADED:task.upload_task_state = UPLOAD_TASK_STATE_NOTSTART; break;
  302. case DATABASE_STATE_IMG_UPLOADING:task.upload_task_state = UPLOAD_TASK_STATE_UPLOADING; break;
  303. case DATABASE_STATE_IMG_PAUSED:task.upload_task_state = UPLOAD_TASK_STATE_NOTSTART; break;
  304. case DATABASE_STATE_IMG_UPLOADED:task.upload_task_state = UPLOAD_TASK_STATE_COMPLETED; break;
  305. }
  306. //task.upload_task_state = UPLOAD_TASK_STATE_NOTSTART;
  307. task.upload_count = 0;
  308. tasks.push_back(task);
  309. query.nextRow();
  310. }
  311. LeaveCriticalSection(database_db_lock);
  312. return TRUE;
  313. }
  314. int CImgUploader::ClearUploadedPaperImg()
  315. {
  316. EnterCriticalSection(database_db_lock);
  317. char sql[256];
  318. sprintf_s(sql, "update database set database_state =%d where database_state =%d", DATABASE_STATE_DELETING, DATABASE_STATE_IMG_UPLOADED);
  319. database_db->execDML(sql);
  320. LeaveCriticalSection(database_db_lock);
  321. return TRUE;
  322. }
  323. int CImgUploader::SetHandlerWnd(HWND hwnd)
  324. {
  325. m_hwnd = hwnd;
  326. return 0;
  327. }