#include "stdafx.h" #include "UploadManager.h" #include "basic_struct.h" #include "BatchService.h" #include "ResultUploader.h" #include "rapidjson/document.h" #include "rapidjson/writer.h" #include "rapidjson/stringbuffer.h" //type 0 INT 1 DOUBLE void AddNumberMember(rapidjson::Value&root, rapidjson::Document::AllocatorType& allocator, const char*key, int value, int type); void AddStringMember(rapidjson::Value&root, rapidjson::Document::AllocatorType& allocator, const char*key, const char*value); UploadManager* g_upload_manager; extern int g_my_scan_type; UploadManager::UploadManager() :m_pNotify(nullptr) { m_zxhxCreateUploadDBSuccess = false; m_bScanStartFlag = false; m_pBatchService = nullptr; m_bEnable = false; ::InitializeCriticalSection(&upload_db_lock); } bool UploadManager::Init(bool bEanble, bool bCountiue, bool bErrorHanle, bool bOnlineCard,int nSubjectID, IUploudNotify*pNotify, CppSQLite3DB*rst_db, RTL_CRITICAL_SECTION*rst_db_lock) { if (!pNotify) { m_pNotify = NULL; return true; } m_nSubjectID = nSubjectID; m_bSendUploadRstToZHXH = false; m_bOnlineCard = bOnlineCard; g_upload_manager = this; m_bEnable = bEanble; m_bCoutinue = bCountiue; result_db = rst_db; result_db_lock = rst_db_lock; if (!bEanble)return false; m_pNotify = pNotify; return OpenDB(bCountiue); } namespace OnLineCard{ std::string UnicodeToAnsi(const CString& str); } bool UploadManager::OpenDB(bool bCountiue) { LOGFMTI("OpenDB: 是否继续:%d", bCountiue ? 1 : 0); TCHAR FilePath[MAX_PATH]; GetModuleFileName(NULL, FilePath, MAX_PATH); CString ret; (_tcsrchr(FilePath, '\\'))[1] = 0; lstrcat(FilePath, _T("upload_manager.db")); char db_file_name[520] = { 0 }; WideCharToMultiByte(CP_UTF8, 0, FilePath, -1, db_file_name, 520, NULL, NULL); ::EnterCriticalSection(&upload_db_lock); if (!upload_db.is_open()) upload_db.open(db_file_name); if (!upload_db.tableExists("upload_db")){ std::string strSql = "CREATE TABLE upload_db ( student_paper_id INT, ori_upload_state INT,paper_state INT, rst_upload_state INT, ori_info VARCHAR, rst_info VARCHAR)"; upload_db.execDML(strSql.c_str()); } if (!bCountiue){ LOGI("清空上传缓存"); std::string sql = "delete from upload_db"; upload_db.execDML(sql.c_str()); } ::LeaveCriticalSection(&upload_db_lock); return true; } #include #include UploadManager::~UploadManager(){ m_pNotify = nullptr; if (m_thread.joinable()){ m_bExit.store(true); m_thread.join(); } ::DeleteCriticalSection(&upload_db_lock); } void UploadManager::ScanListFilter(bool bSuccess,int nUploadState) { if ( m_bEnable){ if (m_thread.joinable()){ m_bExit.store(true); m_thread.join(); } m_bExit.store(false); m_thread = std::thread(&UploadManager::ScanListFilterThread, this, bSuccess, nUploadState); } } int UploadManager::GetPaperUploadState(int paper_id, char*szErrorInfo, int nLen) { std::string sql = "select * from upload_db where student_paper_id="; sql += std::to_string(paper_id); EnterCriticalSection(&upload_db_lock); if (upload_db.is_open()){ CppSQLite3Query q = upload_db.execQuery(sql.c_str()); if (!q.eof()){ int state = q.getIntField("rst_upload_state"); if (state == 3){ std::string str_info = q.getStringField("rst_info"); strncpy(szErrorInfo, str_info.c_str(), nLen - 1); LeaveCriticalSection(&upload_db_lock); return 2; } else if (state == 2){ LeaveCriticalSection(&upload_db_lock); return 1; } else if (state == 1){ LeaveCriticalSection(&upload_db_lock); return 3; } else if (state == 0){ LeaveCriticalSection(&upload_db_lock); return 4; } } } LeaveCriticalSection(&upload_db_lock); return 0; } void UploadManager::ScanListFilterThread(bool bSuccess, int nUploadState) { if (!m_bExit.load()){ std::string tmp; { std::lock_guard _lk(m_tx_pre_paperid); if (!m_vctPrePaperid.empty()){ tmp = " and student_paper_id not in("; for (std::size_t i = 0; i < m_vctPrePaperid.size(); ++i){ tmp += std::to_string(m_vctPrePaperid[i]); if (i != m_vctPrePaperid.size() - 1) tmp += ","; else tmp += ")"; } } } std::string sql = "select * from upload_db where "; sql += (bSuccess ?"paper_state=0 ":"paper_state!=0 "); if (nUploadState == 1){// 上传完成 sql += "and rst_upload_state=2"; } else if (nUploadState == 2){ //上传失败 sql += "and rst_upload_state=3"; } else if (nUploadState == 3){// 上传中 sql += "and rst_upload_state=1 "; } else if (nUploadState == 4 || nUploadState == 0){//待上传//全部 if (!bSuccess){ std::string rst_sql = "select student_paper_id from student_paper where paper_state!=0"; EnterCriticalSection(result_db_lock); if (result_db&&result_db->is_open()){ rst_sql += tmp; CppSQLite3Query q_rst = result_db->execQuery(rst_sql.c_str()); if (!q_rst.eof()){ while (!q_rst.eof()){ int student_paper_id = q_rst.getIntField("student_paper_id"); q_rst.nextRow(); if (nUploadState == 4){ std::string sql_tmp = "select count(*) from upload_db where student_paper_id="; sql_tmp += std::to_string(student_paper_id); if (upload_db.execScalar(sql_tmp.c_str()) <= 0){ if (m_pNotify){ m_pNotify->OnScanListFilter(student_paper_id); } } else{ sql += q_rst.eof() ? ")" : ","; } } else{ if (m_pNotify){ m_pNotify->OnScanListFilter(student_paper_id); } } } if (nUploadState != 4){ LeaveCriticalSection(result_db_lock); return; } } else{ LeaveCriticalSection(result_db_lock); return; } } LeaveCriticalSection(result_db_lock); } } EnterCriticalSection(&upload_db_lock); try{ if (upload_db.is_open()){ sql += tmp; CppSQLite3Query q = upload_db.execQuery(sql.c_str()); while (!q.eof()){ if (m_bExit.load())break; if (m_pNotify){ m_pNotify->OnScanListFilter(q.getIntField("student_paper_id")); } q.nextRow(); } } } catch (...){ LeaveCriticalSection(&upload_db_lock); return; } LeaveCriticalSection(&upload_db_lock); } } void UploadManager::NotifyScanProcess(UploadProcess*pRt/* = nullptr*/) { std::string tmp = ""; UploadProcess obj; EnterCriticalSection(&upload_db_lock); try{ if (upload_db.is_open()){ { std::lock_guard _lk(m_tx_pre_paperid); if (!m_vctPrePaperid.empty()){ tmp = " and student_paper_id not in("; for (std::size_t i = 0; i < m_vctPrePaperid.size(); ++i){ tmp += std::to_string(m_vctPrePaperid[i]); if (i != m_vctPrePaperid.size() - 1) tmp += ","; else tmp += ")"; } } } // 答卷上传成功数量 std::string sql = "select count(*) from upload_db where paper_state=0 and rst_upload_state=2"; sql += tmp; obj.m_nRstUploadSuccess = upload_db.execScalar(sql.c_str()); // 答卷上传失败数量 sql = "select count(*) from upload_db where paper_state=0 and rst_upload_state=3"; sql += tmp; obj.m_nRstUploadBad = upload_db.execScalar(sql.c_str()); // 原卷上传成功数量 sql = "select count(*) from upload_db where paper_state=0 and ori_upload_state=2"; sql += tmp; obj.m_nOriUploadSuccess = upload_db.execScalar(sql.c_str()); // 原卷上传失败数量 sql = "select count(*) from upload_db where paper_state=0 and ori_upload_state=3"; sql += tmp; obj.m_nOriUploadBad = upload_db.execScalar(sql.c_str()); // 异常卷上传成功数量 sql = "select count(*) from upload_db where paper_state!=0 and rst_upload_state=2"; sql += tmp; obj.m_nErrorUploadSuccess = upload_db.execScalar(sql.c_str()); // 异常卷上传成功数量 sql = "select count(*) from upload_db where paper_state!=0 and rst_upload_state=3"; sql += tmp; obj.m_nErrorUploadBad = upload_db.execScalar(sql.c_str()); } } catch (...){ LeaveCriticalSection(&upload_db_lock); return; } LeaveCriticalSection(&upload_db_lock); EnterCriticalSection(result_db_lock); try{ if (result_db && result_db->is_open()){ // 异常卷上传总量 std::string sql = "select count(*) from student_paper where paper_state!=0 and Is_Covered!=1"; sql += tmp; obj.m_nErrorUploadTotal = result_db->execScalar(sql.c_str()); // 答/原卷卷上传总量 sql = "select count(*) from student_paper where paper_state=0 and Is_Covered!=1"; sql += tmp; obj.m_nOriUploadTotal = result_db->execScalar(sql.c_str()); obj.m_nRstUploadTotal = obj.m_nOriUploadTotal; } } catch (...){ LeaveCriticalSection(result_db_lock); return; } LeaveCriticalSection(result_db_lock); if (!pRt){ if (m_pNotify){ m_pNotify->OnUploadNotify(UPLOAD_SHIJUAN_TYPE::UPLOAD_PROCESS_RST, &obj); } } else{ *pRt = obj; } } void UploadManager::OnTcpError() { m_zxhxCreateUploadDBSuccess = false; } void UploadManager::OnReConnect(bool bSuccess) { if (bSuccess){ LOGFMTI("通知知心慧学开启边扫边传任务:%s", m_strStartParam.c_str()); // 通知知心慧学开启边扫边传任务 if (m_strStartParam.length()) NetOperator::ClientSendData(m_strStartParam.c_str()); } } void UploadManager::OnNetNotify(const char*param) { if (!param) return; rapidjson::Document root; root.Parse(param); if (!root.HasParseError()){ auto it_task_type = root.FindMember("net_task_type"); auto it_cmd = root.FindMember("net_task_cmd"); if (it_cmd != root.MemberEnd() && it_task_type != root.MemberEnd()){ std::string cmd = it_cmd->value.GetString(); auto it_data = root.FindMember("data"); if (it_data == root.MemberEnd()){ LOGE("没有找到从知心慧学返回的data节点"); return; } // 检查batch_code 不是本批次的放弃处理 auto it_bantch_code = it_data->value.FindMember("bantch_code"); #ifdef _DEBUG auto it_code = it_bantch_code->value.GetString(); #endif if (!(it_bantch_code != it_data->value.MemberEnd() && it_bantch_code->value.IsString() && std::string(it_bantch_code->value.GetString()) == m_bantch_code)) { LOGFMTE("bantch_code 错误 或者 不是本批次的batch_code 放弃之: 当前bantch_code=%s 知心慧学端 bantch_code:%s", std::string(it_bantch_code->value.GetString()).c_str(), m_bantch_code.c_str()); return; } // 服务端是否成功创建数据库 if (cmd == "upload_on_scan_start"){ // auto it_rst = it_data->value.FindMember("success"); if (it_rst != it_data->value.MemberEnd() && it_rst->value.IsInt()){ auto rst = it_rst->value.GetInt(); if (rst == 0){//失败 m_zxhxCreateUploadDBSuccess = false; auto it_msg = it_data->value.FindMember("message"); if (it_msg != it_data->value.MemberEnd() && it_msg->value.IsString()) LOGFMTE("知心慧学端创建数据库失败:%s", it_msg->value.GetString()); } else{ m_zxhxCreateUploadDBSuccess = true; LOGI("知心慧学端创建数据库成功"); SendUploadTaskWhenReConnected(); } if (m_pBatchService){ SetEvent(m_pBatchService->GetEventZXHXCreateDB()); } } } if (!m_zxhxCreateUploadDBSuccess){ return; } if (cmd == "upload_on_scan_add_upload_task"){ auto it_success = it_data->value.FindMember("success"); if (it_success != it_data->value.MemberEnd() && it_success->value.IsInt()){ if (it_success->value.GetInt() != 1){ std::string err_msg, exception_json; int student_paper_id = -1; auto it_msg = it_data->value.FindMember("message"); if (it_msg != it_data->value.MemberEnd() && it_msg->value.IsString()){ err_msg = it_msg->value.GetString(); } else{ LOGI("upload_on_scan_add_upload_task 协议错误:message缺失 或者 message不是string类型 "); return; } auto it_exception_json = it_data->value.FindMember("exception_json"); if (it_exception_json != it_data->value.MemberEnd() && it_exception_json->value.IsString()){ exception_json = it_exception_json->value.GetString(); } else{ LOGI("upload_on_scan_add_upload_task 协议错误:exception_json缺失 或者 exception_json不是string类型 "); return; } auto it_student_paper_id = it_data->value.FindMember("student_paper_id"); if (it_student_paper_id != it_data->value.MemberEnd() && it_student_paper_id->value.IsInt()){ student_paper_id = it_student_paper_id->value.GetInt(); } else{ LOGI("upload_on_scan_add_upload_task 协议错误:student_paper_id缺失 或者 student_paper_id不是int类型 "); return; } char sz_sql[512] = { 0 }; std::string update_sql_fmt = "update upload_db set ori_upload_state=%d, rst_upload_state=3, rst_info='%s' where student_paper_id=%d"; sprintf_s(sz_sql, update_sql_fmt.c_str(), exception_json == "normal" ? 3 : 2, (std::string("write_to_db_error-") + err_msg).c_str(), student_paper_id); ::EnterCriticalSection(&upload_db_lock); if (upload_db.is_open()){ upload_db.execDML((sz_sql)); } ::LeaveCriticalSection(&upload_db_lock); memset(sz_sql, 0, sizeof(sz_sql) / sizeof(char)); // 通知界面更新状态 MY_UPLOAD_REV_INFO _param; _param.paperid = student_paper_id; _param.status = UPLOAD_STATUS::UPLOAD_ERROR; if (exception_json == "normal"){ update_sql_fmt = "update student_paper set ret_upload=3,img_upload=3 where student_paper_id=%d"; sprintf_s(sz_sql, update_sql_fmt.c_str(), student_paper_id); ::EnterCriticalSection(result_db_lock); if (result_db && result_db->is_open()){ result_db->execDML(sz_sql); } ::LeaveCriticalSection(result_db_lock); OnUploadNotify(UPLOAD_SHIJUAN_TYPE::TYPE_RST, &_param); OnUploadNotify(UPLOAD_SHIJUAN_TYPE::TYPE_ORI, &_param); } else{ update_sql_fmt = "update student_paper set ret_upload=3,img_upload=2 where student_paper_id=%d"; sprintf_s(sz_sql, update_sql_fmt.c_str(), student_paper_id); ::EnterCriticalSection(result_db_lock); if (result_db && result_db->is_open()){ result_db->execDML(sz_sql); } ::LeaveCriticalSection(result_db_lock); OnUploadNotify(UPLOAD_SHIJUAN_TYPE::TYPE_ERROR, &_param); } } } else{ LOGI("upload_on_scan_add_upload_task 协议错误:success缺失 或者 success不是int类型 "); } } ////////////////////////////////////////////////////////////////////////// // student_paper_id int 类型 // // bantch_code 批次号 字符串 // // upload_state 1 成功 0 失败 int 类型 // // message 失败原因 字符串 // ////////////////////////////////////////////////////////////////////////// else if (cmd == "upload_on_scan_rst_state_notify" || // 答卷上传状态 cmd == "upload_on_scan_ori_state_notify" || // 原卷上传状态 cmd == "upload_on_scan_error_state_notify") { // 异常卷上传状态 int student_paper_id = 0, upload_state = 0; std::string message; // 解析 student_paper_id auto it_student_paper_id = it_data->value.FindMember("student_paper_id"); if (it_student_paper_id != it_data->value.MemberEnd() && it_student_paper_id->value.IsInt()){ student_paper_id = it_student_paper_id->value.GetInt(); } // 解析 upload_state 1 成功 0失败 auto it_upload_state = it_data->value.FindMember("upload_state"); if (it_upload_state != it_data->value.MemberEnd() && it_upload_state->value.IsInt()){ upload_state = it_upload_state->value.GetInt(); } // 解析失败原因 auto it_message = it_data->value.FindMember("err_msg"); if (it_message != it_data->value.MemberEnd() && it_message->value.IsString()){ message = it_message->value.GetString(); } UPLOAD_SHIJUAN_TYPE _upload_shijuan_type = UPLOAD_SHIJUAN_TYPE::TYPE_ORI; if (cmd == "upload_on_scan_rst_state_notify"){ _upload_shijuan_type = UPLOAD_SHIJUAN_TYPE::TYPE_RST; } else if (cmd == "upload_on_scan_error_state_notify"){ _upload_shijuan_type = UPLOAD_SHIJUAN_TYPE::TYPE_ERROR; } char sz_sql_result_db[512] = { 0 }; char sz_sql_upload_db[512] = { 0 }; switch (_upload_shijuan_type) { // 原卷状态更新到数据库 case UPLOAD_SHIJUAN_TYPE::TYPE_ORI: { std::string sz_sql_result_db_fmt = "update student_paper set img_upload = %d where student_paper_id = %d and paper_state=0"; sprintf_s(sz_sql_result_db, sz_sql_result_db_fmt.c_str(), upload_state, student_paper_id); std::string sql_upload_db_fmt = "update upload_db set ori_upload_state = %d ,ori_info='%s' where student_paper_id=%d and paper_state=0"; sprintf_s(sz_sql_upload_db, sql_upload_db_fmt.c_str(), upload_state, message.c_str(), student_paper_id); } break; // 原卷状态更新到数据库 case UPLOAD_SHIJUAN_TYPE::TYPE_RST: { std::string sz_sql_result_db_fmt = "update student_paper set ret_upload = %d where student_paper_id = %d and paper_state=0"; sprintf_s(sz_sql_result_db, sz_sql_result_db_fmt.c_str(), upload_state, student_paper_id); std::string sql_upload_db_fmt = "update upload_db set rst_upload_state = %d ,rst_info='%s' where student_paper_id=%d and paper_state=0"; sprintf_s(sz_sql_upload_db, sql_upload_db_fmt.c_str(), upload_state, message.c_str(), student_paper_id); } break; // 异常卷状态更新到数据 case UPLOAD_SHIJUAN_TYPE::TYPE_ERROR:{ std::string sz_sql_result_db_fmt = "update student_paper set ret_upload = %d where student_paper_id = %d and paper_state!=0"; sprintf_s(sz_sql_result_db, sz_sql_result_db_fmt.c_str(), upload_state, student_paper_id); std::string sql_upload_db_fmt = "update upload_db set rst_upload_state = %d , ori_upload_state = 2 ,rst_info='%s' where student_paper_id=%d and paper_state!=0"; sprintf_s(sz_sql_upload_db, sql_upload_db_fmt.c_str(), upload_state, message.c_str(), student_paper_id); } break; } EnterCriticalSection(result_db_lock); if (result_db && result_db->is_open()){ result_db->execDML(sz_sql_result_db); LOGFMTI("更新batch_db:%s", sz_sql_result_db); } LeaveCriticalSection(result_db_lock); EnterCriticalSection(&upload_db_lock); if (upload_db.is_open()){ upload_db.execDML(sz_sql_upload_db); LOGFMTI("更新upload_db:%s", sz_sql_upload_db); } LeaveCriticalSection(&upload_db_lock); // 通知界面更新状态 MY_UPLOAD_REV_INFO _param; _param.paperid = student_paper_id; _param.status = ((upload_state == 2)? UPLOAD_STATUS::UPLOAD_SUCCESS : UPLOAD_STATUS::UPLOAD_ERROR); OnUploadNotify(_upload_shijuan_type, &_param); } } } } void UploadManager::OnUploadNotify(int type, void*data) { if (m_pNotify){ m_pNotify->OnUploadNotify(type, data); if (type == UPLOAD_SHIJUAN_TYPE::TYPE_RST || type == UPLOAD_SHIJUAN_TYPE::TYPE_ERROR || type == UPLOAD_SHIJUAN_TYPE::TYPE_ORI) { NotifyScanProcess(); if (m_bScanStopFlag){ NotifyUploadOnScanStop(); } } } } void UploadManager::OnScanListFilter(int paper_id) { // } namespace OnLineCard{ std::string UnicodeToAnsi(const CString& str); } std::string readFileIntoString(const std::string& filename); std::wstring strUTF8toW(const std::string& src); extern std::string g_strUpLoadDbPathName;; #include "rapidjson/document.h" void UploadManager::NotifyUploadOnScanStop() { bool bNotify = true; EnterCriticalSection(&upload_db_lock); if (bNotify){ std::string sql = "select count(*) from upload_db where rst_upload_state=0 OR rst_upload_state=1"; if (upload_db.execScalar(sql.c_str()) > 0) bNotify = false; } LeaveCriticalSection(&upload_db_lock); if (bNotify && m_pNotify){ LOGI("上传结束通告"); NotifyEnd(); } } void UploadManager::NotifyEnd() { if (m_bSendUploadRstToZHXH)return; m_bSendUploadRstToZHXH = true; if (!m_bEnable)return; if (m_pNotify) m_pNotify->OnUploadNotify(UPLOAD_SHIJUAN_TYPE::TYPE_END, NULL); } int UploadManager::UploadOnScanCmd(int nCmd, const char*param, int nOnlineScanType, void*ret) { if (!m_bEnable){ return -1; } switch (nCmd) { case CMD_IS_HAS_UPLOADERROR: // 是否 { int nRet = -1; EnterCriticalSection(&upload_db_lock); if (upload_db.is_open()){ std::string sql = "select count(*) from upload_db where rst_upload_state=3 "; if (strcmp(param, "success") == 0) sql += " and paper_state=0"; else if (strcmp(param, "error") == 0)sql += " and paper_state!=0"; nRet = upload_db.execScalar(sql.c_str()); } LeaveCriticalSection(&upload_db_lock); return nRet; } break; case UPLOAD_ON_SCAN_CMD::CMD_DEL_UPLOAD_REPORT: { if (param != NULL){ std::vector split_param; split(param, (std::string)",", &split_param); if (!m_vctPrePaperid.empty()){ for (auto it_student_papaer_id : split_param) { int student_paper_id = std::stoi(it_student_papaer_id); std::lock_guard _lk(m_tx_pre_paperid); auto it_find = std::find(m_vctPrePaperid.begin(), m_vctPrePaperid.end(), student_paper_id); if (it_find != m_vctPrePaperid.end()){ m_vctPrePaperid.erase(it_find); } } } EnterCriticalSection(&upload_db_lock); if (upload_db.is_open()){ std::string sql = "delete from upload_db where student_paper_id in("; sql += std::string(param) + ")"; upload_db.execDML(sql.c_str()); } LeaveCriticalSection(&upload_db_lock); if (m_zxhxCreateUploadDBSuccess){ for (auto it_student_papaer_id : split_param) { int student_paper_id = std::stoi(it_student_papaer_id); rapidjson::Document doc; doc.SetObject(); rapidjson::Document::AllocatorType& allocator = doc.GetAllocator(); AddStringMember(doc, allocator, "net_task_type", "upload_on_scan_upload"); AddStringMember(doc, allocator, "net_task_cmd", "upload_on_scan_del_paper"); rapidjson::Value root; root.SetObject(); AddStringMember(root, allocator, "bantch_code", m_bantch_code.c_str()); AddNumberMember(root, allocator, "student_paper_id", student_paper_id, 0); doc.AddMember("data", root, allocator); rapidjson::StringBuffer buffer; rapidjson::Writer writer(buffer); doc.Accept(writer); std::string reststring = buffer.GetString(); LOGFMTI("通知知心慧学删除试卷: param=%s", reststring.c_str()); // 通知知心慧学删除试卷 NetOperator::ClientSendData(reststring.c_str()); } } } } break; case UPLOAD_ON_SCAN_CMD::CMD_START_SCAN: { { std::lock_guard _lk(m_tx_pre_paperid); m_vctPrePaperid.clear(); } NotifyScanProcess(); if (!m_bCoutinue){ EnterCriticalSection(result_db_lock); if (result_db && result_db->is_open()){ std::string sql = "select student_paper_id,paper_state,ret_upload,img_upload from student_paper where Is_Covered!=1"; auto qry = result_db->execQuery(sql.c_str()); while (!qry.eof()){ int paper_id = qry.getIntField("student_paper_id"); std::lock_guard _lk(m_tx_pre_paperid); m_vctPrePaperid.push_back(paper_id); qry.nextRow(); } } LeaveCriticalSection(result_db_lock); } rapidjson::Document doc; doc.SetObject(); rapidjson::Document::AllocatorType& allocator = doc.GetAllocator(); AddStringMember(doc, allocator, "net_task_type", "upload_on_scan_upload"); AddStringMember(doc, allocator, "net_task_cmd", "upload_on_scan_start"); rapidjson::Value root; root.SetObject(); ScanParamInfo*pParamInfo = (ScanParamInfo*)(param); if (pParamInfo){ m_bantch_code = pParamInfo->bantch_code; AddStringMember(root, allocator, "bantch_code", m_bantch_code.c_str()); AddStringMember(root, allocator, "exam_id", pParamInfo->exam_id.c_str()); AddStringMember(root, allocator, "subject_name", pParamInfo->course_name.c_str()); AddStringMember(root, allocator, "subject_id", pParamInfo->subject_id.c_str()); } if (m_pBatchService && m_pBatchService->GetRstUploaderPtr()){ char sUrl[1024] = { 0 }; int nLen = sizeof(sUrl) / sizeof(char); m_pBatchService->GetRstUploaderPtr()->readNewUploadUrl(); m_pBatchService->GetRstUploaderPtr()->getupLoadUrl(sUrl, nLen); AddStringMember(root, allocator, "url", sUrl); AddStringMember(root, allocator, "session", ""); //AddStringMember(root, allocator, "session",OnLineCard::UnicodeToAnsi( m_pBatchService->GetRstUploaderPtr()->MarkHttpHeaderData()).c_str()); } // 业务类型 TCHAR FilePath[MAX_PATH]; GetModuleFileName(NULL, FilePath, MAX_PATH); (_tcsrchr(FilePath, '\\'))[1] = 0; lstrcat(FilePath, _T("paperinfo.ini")); int service_type = GetPrivateProfileInt(_T("paperinfo"), _T("servicetype"), 0, FilePath); AddNumberMember(root, allocator, "service_type", service_type, 0); if (nOnlineScanType == 0)//魔法词汇宝 { AddNumberMember(root, allocator, "ntype", 6, 0); } else { AddNumberMember(root, allocator, "ntype", g_my_scan_type + 1, 0); } TCHAR exam_name[256] = { 0 }; (_tcsrchr(FilePath, '\\'))[1] = 0; lstrcat(FilePath, _T("config.ini")); GetPrivateProfileString(_T("THIRD"), _T("exam_name"), _T(""), exam_name, sizeof(exam_name)/sizeof(TCHAR), FilePath); AddStringMember(root, allocator, "exam_name", OnLineCard::UnicodeToAnsi(exam_name).c_str()); doc.AddMember("data",root,allocator); rapidjson::StringBuffer buffer; rapidjson::Writer writer(buffer); doc.Accept(writer); m_strStartParam = buffer.GetString(); LOGFMTI("通知知心慧学开启边扫边传任务:%s", m_strStartParam.c_str()); // 通知知心慧学开启边扫边传任务 NetOperator::ClientSendData(m_strStartParam.c_str()); m_bScanStartFlag = true; } break; case UPLOAD_ON_SCAN_CMD::CMD_SET_SCAN_STOP_FLAG: LOGI("扫描结束"); m_bScanStopFlag = (_stricmp(param, "true") == 0); if (m_bScanStopFlag) NotifyUploadOnScanStop(); break; case CMD_SEND_UPLAOD_RST_TO_ZXHX: if (m_bScanStopFlag) NotifyUploadOnScanStop(); break; case UPLOAD_ON_SCAN_CMD::CMD_GET_UPLOAD_PROCESS: NotifyScanProcess(); break; case UPLOAD_ON_SCAN_CMD::CMD_UPLOAD_YICHANGEJUAN: UploadYichangjuan(); break; case UPLOAD_ON_SCAN_CMD::CMD_UPLOAD_SUCCESSSJUAN:{ if (param){ std::string tmp = param; if(!tmp.empty()) UploadSuccessjuan(std::stoi(tmp)); } } break; case UPLOAD_ON_SCAN_CMD::CMD_UPLOAD_STOP:{ if (m_thread.joinable()){ m_bExit.store(true); m_thread.join(); } NotifyEnd(); } break; case UPLOAD_ON_SCAN_CMD::CMD_RE_UPLOADERROR: { ReUploadUploadError(param); } break; } return 0; } void UploadManager::SendUploadTaskWhenReConnected() { LOGI("重连后发送待上传的试卷到知心慧学"); std::string sql = "select student_paper_id, paper_state from upload_db where (rst_upload_state=0 OR ori_upload_state=0)"; if (!m_bScanStopFlag){ sql += " AND paper_state=0"; } ::EnterCriticalSection(&upload_db_lock); try{ if (upload_db.is_open()){ auto qry = upload_db.execQuery(sql.c_str()); while (!qry.eof()){ int student_paper_id = qry.getIntField("student_paper_id"); int paper_state = qry.getIntField("paper_state"); LOGFMTI("将试卷插入到边上传数据库: id = %d paper_state", student_paper_id, paper_state); if (m_pBatchService && m_zxhxCreateUploadDBSuccess){ m_pBatchService->StartUploadPaper(NULL, student_paper_id, paper_state!=0); } else{ LOGE("将试卷插入到边上传数据库失败"); } qry.nextRow(); } } } catch (...){ LOGE("将试卷插入到边上传数据库失败-SQL异常"); ::LeaveCriticalSection(&upload_db_lock); } ::LeaveCriticalSection(&upload_db_lock); LOGI("重连后发送待上传的试卷到知心慧学-END"); } #define reset_batchdb_sql_rst_fmt "update student_paper set ret_upload=0 where student_paper_id in (%s)" #define reset_batchdb_sql_ori_fmt "update student_paper set ret_upload=0 where student_paper_id in (%s)" #define reset_uploaddb_sql_rst_fmt "update upload_db set rst_upload_state=0 where student_paper_id in (%s)" #define reset_uploaddb_sql_ori_fmt "update upload_db set ori_upload_state=0 where student_paper_id in (%s)" void UploadManager::ReUploadUploadError(const char*param) { if (!param){ return; } bool re_upload_yichang = false; LOGI("开始重新上传[上传失败的试卷]"); std::string sql_qry = "select paper_state,student_paper_id,ret_upload,img_upload from student_paper where "; if (strcmp(param, "success") == 0){ sql_qry += " paper_state=0 and ret_upload=3"; } else if (strcmp(param, "error") == 0){ sql_qry += " paper_state!=0 and ret_upload=3"; re_upload_yichang = true; } else{ sql_qry += std::string("student_paper_id=") + param; } LOGFMTI("重新上传 sql = %s", sql_qry.c_str()); std::vector vct_ori_paperid;//原卷 std::vector vct_rst_paperid;//答卷 std::string str_ori_paper_id; std::string str_rst_paper_id; // 重传扫描成功的试卷 ::EnterCriticalSection(result_db_lock); if (result_db && result_db->is_open()){ LOGFMTD("SQL:%s", sql_qry.c_str()); CppSQLite3Query qry = result_db->execQuery(sql_qry.c_str()); if (qry.eof()) LOGI("没有找到要上传的数据"); while (!qry.eof()){ int paper_state = qry.getIntField("paper_state"); int student_paper_id = qry.getIntField("student_paper_id"); // 答卷上传状态 int ret_upload = qry.getIntField("ret_upload"); if (ret_upload == 3){ str_rst_paper_id += std::to_string(student_paper_id) + ","; vct_rst_paperid.push_back(student_paper_id); } if (paper_state == 0){ // 原卷上传状态 int img_upload = qry.getIntField("img_upload"); if (img_upload == 3){ str_ori_paper_id += std::to_string(student_paper_id) + ","; vct_ori_paperid.push_back(student_paper_id); } } else{ re_upload_yichang = true; } qry.nextRow(); } if (!str_rst_paper_id.empty()){ str_rst_paper_id = std::string(str_rst_paper_id, 0, str_rst_paper_id.size() - 1); char sz_sql[1024] = { 0 }; sprintf_s(sz_sql, reset_batchdb_sql_rst_fmt, str_rst_paper_id.c_str()); result_db->execDML(sz_sql); } if (!str_ori_paper_id.empty()){ str_ori_paper_id = std::string(str_ori_paper_id, 0, str_ori_paper_id.size() - 1); char sz_sql[1024] = { 0 }; sprintf_s(sz_sql, reset_batchdb_sql_ori_fmt, str_ori_paper_id.c_str()); result_db->execDML(sz_sql); } if (upload_db.is_open()){ ::EnterCriticalSection(&upload_db_lock); if (!str_rst_paper_id.empty()){ char sz_sql[1024] = { 0 }; sprintf_s(sz_sql, reset_uploaddb_sql_rst_fmt, str_rst_paper_id.c_str()); upload_db.execDML(sz_sql); } if (!str_ori_paper_id.empty()){ char sz_sql[1024] = { 0 }; sprintf_s(sz_sql, reset_uploaddb_sql_ori_fmt, str_ori_paper_id.c_str()); upload_db.execDML(sz_sql); } ::LeaveCriticalSection(&upload_db_lock); } } ::LeaveCriticalSection(result_db_lock); auto p_send_data_func = [](int student_paper_id, int type, const std::string&batch_code){ rapidjson::Document doc;doc.SetObject(); rapidjson::Document::AllocatorType& allocator = doc.GetAllocator(); rapidjson::Value _data; _data.SetObject(); // 数据头 AddStringMember(doc, allocator, "net_task_type", "upload_on_scan_upload"); AddStringMember(doc, allocator, "net_task_cmd", "upload_on_scan_updata_upload_task"); // 数据体 AddStringMember(_data, allocator, "bantch_code", batch_code.c_str()); AddNumberMember(_data, allocator, "student_paper_id", student_paper_id, 0); //type 0答卷、1原卷、2异常卷 AddNumberMember(_data, allocator, "type", type, 0); doc.AddMember("data", _data, allocator); rapidjson::StringBuffer buffer; rapidjson::Writer writer(buffer); doc.Accept(writer); std::string reststring = buffer.GetString(); LOGFMTD("向知心慧学端发送重传命令: %s", reststring.c_str()); NetOperator::ClientSendData(reststring.c_str()); }; for (auto it : vct_rst_paperid){ p_send_data_func(it, re_upload_yichang ? 2 : 0,m_bantch_code); } for (auto it : vct_ori_paperid){ p_send_data_func(it, 1, m_bantch_code); } LOGI("重传状态设置完成"); } void UploadManager::UploadSuccessjuan(int student_paper_id) { std::string insert_sql_fmt = "INSERT INTO upload_db (student_paper_id, paper_state,ori_upload_state,rst_upload_state,ori_info,rst_info) VALUES (%d,0,0,0,'','')"; if (upload_db.is_open()){ EnterCriticalSection(&upload_db_lock); char szSQL[256] = { 0 }; sprintf(szSQL, insert_sql_fmt.c_str(), student_paper_id); upload_db.execDML(szSQL); LeaveCriticalSection(&upload_db_lock); LOGFMTI("将成功卷插入到边上传数据库: id = %d", student_paper_id); if (m_pBatchService && m_zxhxCreateUploadDBSuccess){ LOGFMTI("开启上传 m_pBatchService id=%d",student_paper_id); m_pBatchService->StartUploadPaper(NULL, student_paper_id,false); } } } void UploadManager::UploadYichangjuan() { LOGI("开始上传异常卷"); std::string sql = "select student_paper_id,paper_state from student_paper where paper_state!=0"; std::string insert_sql_fmt = "INSERT INTO upload_db (student_paper_id, paper_state,ori_upload_state,rst_upload_state,ori_info,rst_info) VALUES (%d,%d,0,0,'','')"; EnterCriticalSection(result_db_lock); if (result_db &&result_db->is_open()){ CppSQLite3Query rs_qry = result_db->execQuery(sql.c_str()); while (!rs_qry.eof()){ if (upload_db.is_open()){ EnterCriticalSection(&upload_db_lock); int student_paper_id = rs_qry.getIntField("student_paper_id"); std::string sql_tmp = std::string("select count(*) from upload_db where student_paper_id=")+ std::to_string(student_paper_id); if (upload_db.execScalar(sql_tmp.c_str()) <= 0){ int paper_state = rs_qry.getIntField("paper_state"); char szSQL[256] = { 0 }; sprintf(szSQL, insert_sql_fmt.c_str(), student_paper_id, paper_state); LOGFMTI("将异常卷插入到边上传数据库: id = %d, state = %d sql=%s", student_paper_id, paper_state, szSQL); upload_db.execDML(szSQL); if (m_pBatchService&&m_zxhxCreateUploadDBSuccess){ LOGFMTI("开启上传 m_pBatchService id=%d", student_paper_id); m_pBatchService->StartUploadPaper(NULL, student_paper_id,true); } } LeaveCriticalSection(&upload_db_lock); } rs_qry.nextRow(); } } LeaveCriticalSection(result_db_lock); LOGI("上传异常卷结束"); }