123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339 |
- #include "StdAfx.h"
- #include "ImgUploader.h"
- #include "..\EvaluationUtil\HttpClient.h"
- #include "..\EvaluationUtil\IInputFiled.h"
- #include "..\EvaluationUtil\FileInputFiled.h"
- #include "basic_struct.h"
- #include "ServerConfig.h"
- CImgUploader::CImgUploader(void)
- {
- m_hwnd = NULL;
- }
- CImgUploader::~CImgUploader(void)
- {
- }
- bool DeleteDirectory(CString sDirName)
- {
- CFileFind tempFind;
- CString sTempFileFind=sDirName+_T("\\*.*");
- BOOL IsFinded = tempFind.FindFile(sTempFileFind);
- while (IsFinded)
- {
- IsFinded = tempFind.FindNextFile();
- CString sFoundFileName=tempFind.GetFilePath();
- if(tempFind.IsDirectory()){
- if(!tempFind.IsDots()){
- DeleteDirectory(sFoundFileName);
- }
- }else{
- if(tempFind.IsReadOnly())
- {
- SetFileAttributes(sFoundFileName, FILE_ATTRIBUTE_NORMAL);
- }
- DeleteFile(sFoundFileName);
- }
- }
- tempFind.Close();
- SetFileAttributes(sDirName, FILE_ATTRIBUTE_NORMAL);
- RemoveDirectory(sDirName);
- return true;
- }
- /*
- //�������̼�� ��������������״̬
- //���ڴ���һ�����ε����ݿ�
- IMG_UPLOAD_STATE_OPENNING,
- //������
- IMG_UPLOAD_STATE_READYING,
- //�����ϴ�
- IMG_UPLOAD_STATE_UPLOADING,
- //���ڹرյ�ǰ�����ݿ�
- IMG_UPLOAD_STATE_CLOSING,
- //��ǰ���ݿ��Ѿ��ر�
- IMG_UPLOAD_STATE_CLOSED,
- */
- ServiceState CImgUploader::OnRunning( void )
- {
- ServiceState nextState= IService::OnRunning();
- if(nextState != running){
- if(m_current_database_state==IMG_UPLOAD_STATE_CLOSED)return nextState;
- else m_current_database_state = IMG_UPLOAD_STATE_CLOSING;
- }
- // Logger pTestLogger = Logger::getInstance(LOG4CPLUS_TEXT("LoggerName"));
- switch(m_current_database_state){
- case IMG_UPLOAD_STATE_OPENNING://����һ��Ҫ�ϴ�������
- {
- char sql[256];
- 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);
-
- //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);
- CppSQLite3Query query =database_db->execQuery(sql);
- if (query.eof()){
- //LOG4CPLUS_INFO_FMT(pTestLogger, LOG4CPLUS_TEXT("��ǰû���µ���������ϴ�"));
- if(m_uploading){
- m_uploading =FALSE;
- if(m_hwnd!=NULL)::PostMessage(m_hwnd,WM_IDENTIFOR_IMG_UPLOAD_STATE,m_uploading,0);
- }
- Sleep(1000);
- }
- else{
- if(!m_uploading){
- m_uploading =TRUE;
- if(m_hwnd!=NULL)::PostMessage(m_hwnd,WM_IDENTIFOR_IMG_UPLOAD_STATE,m_uploading,0);
- }
- // LOG4CPLUS_INFO_FMT(pTestLogger, LOG4CPLUS_TEXT("��ʼ�ϴ��Ծ��µ�����"));
- // m_url.Format(_T("%s/upload/uploadImages"),CString(query.getStringField("server_address")));
- m_bantch_dir = CString(query.getStringField("bantch_dir"));
- const char * db_file_name = query.getStringField("db_file_name");
- m_task_info.upload_task_id = query.getIntField("database_id");
- strcpy(m_task_info.task_name,query.getStringField("bantch_code"));
- m_task_info.upload_task_state = UPLOAD_TASK_STATE_NOTSTART;
- m_task_info.exam_id = query.getInt64Field("exam_id");
- m_task_info.exam_course_id = query.getInt64Field("exam_course_id");
- m_task_info.scan_type = query.getIntField("scan_type");
- m_task_info.exam_course_id_from = query.getInt64Field("exam_course_id_from");
- m_task_info.batch_code_from = query.getStringField("batch_code_from");
- m_task_info.examinne_id_from = query.getStringField("examinne_id_from");
- bantch_db.open(db_file_name);
- bantch_db.execDML("pragma journal_mode = MEMORY");
- char sql[128];
- sprintf_s(sql,"update database set database_state =%d where database_id =%d",DATABASE_STATE_IMG_UPLOADING,m_task_info.upload_task_id);
- EnterCriticalSection(database_db_lock);
- database_db->execDML("begin transaction");
- database_db->execDML(sql);
- database_db->execDML("commit transaction");
- LeaveCriticalSection(database_db_lock);
- // LOG4CPLUS_INFO_FMT(pTestLogger, LOG4CPLUS_TEXT("�Ѿ���ʼ�ϴ��Ծ��µ�����(%d)"), m_task_info.upload_task_id);
- m_current_has_more=TRUE;
- //if(m_hwnd!=NULL)::PostMessage(m_hwnd,WM_IDENTIFOR_IMG_UPLOAD_TASK_STATE,(WPARAM)&m_task_info,0);
- m_current_database_state = IMG_UPLOAD_STATE_READYING;
- }
- }
- break;
- case IMG_UPLOAD_STATE_READYING://ͳ�����������Ϣ(������)
- {
- char sql[512];
- 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);
- m_task_info.total_count =bantch_db.execScalar(sql);
- 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);
- m_task_info.upload_count =bantch_db.execScalar(sql);
- sprintf_s(sql,"select * from bantch_info");
- CppSQLite3Query q=bantch_db.execQuery(sql);
- if(!q.eof()){m_school_id.Format(L"%d",(long)q.getInt64Field("school_id"));}else{m_school_id =L"-1";}
- m_task_info.upload_task_state = UPLOAD_TASK_STATE_UPLOADING;
- IMG_UPLOAD_TASK * msg=new IMG_UPLOAD_TASK;
- memcpy(msg, &m_task_info, sizeof(IMG_UPLOAD_TASK));
- // LOG4CPLUS_INFO_FMT(pTestLogger, LOG4CPLUS_TEXT("�ϴ��Ծ��µ�����(%d)ͳ�����������"), m_task_info.upload_task_id);
- if(m_hwnd!=NULL)::PostMessage(m_hwnd,WM_IDENTIFOR_IMG_UPLOAD_TASK_STATE,(WPARAM)msg,0);
- m_last_paper_id =-1;
- m_current_database_state = IMG_UPLOAD_STATE_UPLOADING;
- }
- break;
- case IMG_UPLOAD_STATE_UPLOADING://ִ���ϴ�
- {
- char sql[512];
- 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);
- CppSQLite3Query query =bantch_db.execQuery(sql);
- if(query.eof()){
- bool has_someone_uploading;
- do
- {
- handleCompleteTask();
- has_someone_uploading =false;
- for (int index =0;index<UPLOAD_TASK_BUFFER_COUNT;index++)
- {
- if(taskBuffer[index].task_state ==CUploadTask::TS_UPLOADING ||taskBuffer[index].task_state ==CUploadTask::TS_COMPLETE){
- has_someone_uploading =true;
- }
- }
- } while (has_someone_uploading);
- switch(m_task_info.scan_type){
- case SCAN_TASK_TYPE_ADJUST_BATCH:
- {
- CHttpClient httpClient;
- CString postData;
- 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()));
- std::string response;
- httpClient.HttpPost(m_urlDeletePaperBatch,postData,response);
- // LOG4CPLUS_INFO_FMT(pTestLogger, LOG4CPLUS_TEXT("%s"),CString(response.c_str()));
- }
- break;
- case SCAN_TASK_TYPE_ADJUST_EXAMINNE:
- {
- CHttpClient httpClient;
- CString postData;
- 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()));
- std::string response;
- httpClient.HttpPost(m_urlDeletePaperBatch,postData,response);
- // LOG4CPLUS_INFO_FMT(pTestLogger, LOG4CPLUS_TEXT("%s"),CString(response.c_str()));
- }
- break;
- }
- m_current_has_more=FALSE;
- m_current_database_state = IMG_UPLOAD_STATE_CLOSING;
- }
- else{
- while(!query.eof()){
- handleCompleteTask();
- int index_task_buffer=-1;
- for (int index =0;index<UPLOAD_TASK_BUFFER_COUNT;index++)
- {
- if(taskBuffer[index].task_state ==CUploadTask::TS_NONE ){
- index_task_buffer =index;
- break;
- }
- }
- int index_worker=-1;
- for (int index =0;index<UPLOAD_WORKER_COUNT;index++)
- {
- if(worker[index].GetUploadState()==CImageUploadWorker::US_WAITING){
- index_worker =index;
- break;
- }
- }
- if(index_task_buffer>=0&&index_worker>=0){
- CUploadTask& task = taskBuffer[index_task_buffer];
- task.examCourseId.Format(_T("%d"),query.getInt64Field("exam_course_id"));
- task.examId.Format(_T("%d"),query.getInt64Field("exam_id"));
- task.file_name=CString(query.getStringField("file_name"));
- task.schoolId.Format(_T("%d"),query.getInt64Field("school_id"));
- task.student_id=CString(query.getStringField("student_id"));
- task.student_paper_id=query.getIntField("student_paper_id");
- task.bantch=m_task_info.task_name;
- task.task_state =CUploadTask::TS_UPLOADING;
- task.upload_result = FALSE;
- task.url = m_url;
- BOOL success=worker[index_worker].upload(&taskBuffer[index_task_buffer]);
- m_last_paper_id=task.student_paper_id;
- // 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);
- query.nextRow();
- }else{
- Sleep(10);
- }
- }
- }
- }
- break;
- case IMG_UPLOAD_STATE_CLOSING://�����ϴ�����
- {
- bantch_db.close();
- char sql[256];
- 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);
- EnterCriticalSection(database_db_lock);
- database_db->execDML("begin transaction");
- database_db->execDML(sql);
- database_db->execDML("commit transaction");
- LeaveCriticalSection(database_db_lock);
- m_task_info.upload_task_state = m_current_has_more?UPLOAD_TASK_STATE_PAUSED:UPLOAD_TASK_STATE_COMPLETED;
- IMG_UPLOAD_TASK * msg=new IMG_UPLOAD_TASK;
- memcpy(msg, &m_task_info, sizeof(IMG_UPLOAD_TASK));
- // LOG4CPLUS_INFO_FMT(pTestLogger, LOG4CPLUS_TEXT("�ϴ��Ծ��µ�����(%d)���"), m_task_info.upload_task_id);
- if(m_hwnd!=NULL)::PostMessage(m_hwnd,WM_IDENTIFOR_IMG_UPLOAD_TASK_STATE,(WPARAM)msg,0);
- m_current_database_state = IMG_UPLOAD_STATE_CLOSED;
- }
- break;
- case IMG_UPLOAD_STATE_CLOSED://�ϴ�����һ���� ������״̬��Ϊ����һ������
- //DeleteDirectory(m_bantch_dir);
- m_current_database_state = IMG_UPLOAD_STATE_OPENNING;
- break;
- }
- return running;
- }
- //�ϴ����������
- ServiceState CImgUploader::OnStarting( void )
- {
- // pTestLogger = Logger::getInstance(LOG4CPLUS_TEXT("LoggerName"));
- // LOG4CPLUS_INFO_FMT(pTestLogger, LOG4CPLUS_TEXT("�������ͼƬ�ϴ�����"));
- m_uploading = FALSE;
- for (int index =0;index<UPLOAD_WORKER_COUNT;index++)
- {
- worker[index].Start();
- }
- for (int index =0;index<UPLOAD_TASK_BUFFER_COUNT;index++)
- {
- taskBuffer[index].task_state = CUploadTask::TS_NONE;
- }
- m_task_info.upload_task_id = -1;
- //m_url.Format(_T("%s/upload/uploadImages"),CServerConfig::server_url);
- /*m_urlSendDistrMessage.Format(_T("%s/exam/sendDistrMessageByKafka"),CServerConfig::server_url);*/
- 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);
- m_current_database_state = IMG_UPLOAD_STATE_OPENNING;
- return IService::OnStarting();
- }
- int CImgUploader::GetCurrentUploadTaskInfo( IMG_UPLOAD_TASK& task )
- {
- task = m_task_info;
- return TRUE;
- }
- void CImgUploader::handleCompleteTask()
- {
- for (int index =0;index<UPLOAD_TASK_BUFFER_COUNT;index++)
- {
- if(taskBuffer[index].task_state ==CUploadTask::TS_COMPLETE ){
- // LOG4CPLUS_INFO_FMT(pTestLogger, LOG4CPLUS_TEXT("����%d�ϴ����"), index);
- char sql3[512];
- 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);
- bantch_db.execDML("begin transaction");
- bantch_db.execDML(sql3);
- bantch_db.execDML("commit transaction");
- taskBuffer[index].task_state = CUploadTask::TS_NONE;
- m_task_info.upload_count++;
- IMG_UPLOAD_TASK * msg=new IMG_UPLOAD_TASK;
- memcpy(msg,&m_task_info,sizeof(IMG_UPLOAD_TASK));
- if(m_hwnd!=NULL)::PostMessage(m_hwnd,WM_IDENTIFOR_IMG_UPLOAD_TASK_PROGRESS,(WPARAM)msg,0);
- }
- }
- }
- int CImgUploader::QueryUploadePaperImg(vector<IMG_UPLOAD_TASK>& tasks)
- {
- EnterCriticalSection(database_db_lock);
- char sql[256];
- 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);
- CppSQLite3Query query = database_db->execQuery(sql);
- while (!query.eof()){
- IMG_UPLOAD_TASK task;
- task.upload_task_id = query.getIntField("database_id");
- task.total_count = query.getIntField("total_upload_count");
- //strcpy(task.task_name ,query.getStringField("bantch_code"));
- strcpy(task.time, query.getStringField("create_time"));
- strcpy(task.task_name, query.getStringField("exam_name"));
- sprintf_s(task.task_name, "%s(%s)", query.getStringField("exam_name"), query.getStringField("course_name"));
- int database_state = query.getIntField("database_state");
- switch (database_state){
- case DATABASE_STATE_RESULT_UPLOADED:task.upload_task_state = UPLOAD_TASK_STATE_NOTSTART; break;
- case DATABASE_STATE_IMG_UPLOADING:task.upload_task_state = UPLOAD_TASK_STATE_UPLOADING; break;
- case DATABASE_STATE_IMG_PAUSED:task.upload_task_state = UPLOAD_TASK_STATE_NOTSTART; break;
- case DATABASE_STATE_IMG_UPLOADED:task.upload_task_state = UPLOAD_TASK_STATE_COMPLETED; break;
- }
- //task.upload_task_state = UPLOAD_TASK_STATE_NOTSTART;
- task.upload_count = 0;
- tasks.push_back(task);
- query.nextRow();
- }
- LeaveCriticalSection(database_db_lock);
- return TRUE;
- }
- int CImgUploader::ClearUploadedPaperImg()
- {
- EnterCriticalSection(database_db_lock);
- char sql[256];
- sprintf_s(sql, "update database set database_state =%d where database_state =%d", DATABASE_STATE_DELETING, DATABASE_STATE_IMG_UPLOADED);
- database_db->execDML(sql);
- LeaveCriticalSection(database_db_lock);
- return TRUE;
- }
- int CImgUploader::SetHandlerWnd(HWND hwnd)
- {
- m_hwnd = hwnd;
- return 0;
- }
|