#include "StdAfx.h" #include "StudentMatcher.h" #include "..\EvaluationUtil\HttpClient.h" #include "basic_struct.h" #include "ServerConfig.h" #include "rapidjson/document.h" #include "UploadManager.h" extern UploadManager* g_upload_manager; extern int g_my_scan_type; CStudentMatcher::CStudentMatcher(void) { m_hwnd =NULL; m_bErrorHandle = false; m_bUseQr = false; cur_student_paper_id = -1; m_nOnlineScanType = -1; } CStudentMatcher::~CStudentMatcher(void) { } ServiceState CStudentMatcher::OnStarting( void ) { url_str.Format(_T("%s/coachio/iointerface/getThirdStudentCard"),CServerConfig::server_url); url_str_dxk.Format(_T("%s/coachio/iointerface_v2/getDuxueStudentCard"), CServerConfig::server_url); url_str_zxk.Format(_T("%s/coachio/iointerface_v2/getStudentInfoByStudentCards"), CServerConfig::server_url); return running; } std::string ascii2utf8(const string & ascii){ int len_w=MultiByteToWideChar(CP_ACP,0,ascii.c_str(),-1,0,0); WCHAR * wchar=new WCHAR[len_w]; MultiByteToWideChar(CP_ACP,0,ascii.c_str(),-1,wchar,len_w); int len_c=WideCharToMultiByte(CP_UTF8,0,wchar,-1,0,0,0,0); char * chars = new char[len_c]; WideCharToMultiByte(CP_UTF8,0,wchar,-1,chars,len_c,0,0); string result =chars; delete []wchar; delete []chars; return result; } std::string utf82ascii(const string & utf8){ int len_w=MultiByteToWideChar(CP_UTF8,0,utf8.c_str(),-1,0,0); WCHAR * wchar=new WCHAR[len_w]; MultiByteToWideChar(CP_UTF8,0,utf8.c_str(),-1,wchar,len_w); int len_c=WideCharToMultiByte(CP_ACP,0,wchar,-1,0,0,0,0); char * chars = new char[len_c]; WideCharToMultiByte(CP_ACP,0,wchar,-1,chars,len_c,0,0); string result =chars; delete []wchar; delete []chars; return result; } #include #include ServiceState CStudentMatcher::OnRunning(void) { ServiceState nextState = IService::OnRunning(); if (nextState == pausing) return nextState; // if (m_bUseQr){ // return stoping; // } char sql5[512]; sprintf_s(sql5, "SELECT sp.student_paper_id, sp.student_code FROM student_paper sp WHERE (sp.paper_state & %d) = %d", EX_KAOHAODAIPIPEI, EX_KAOHAODAIPIPEI); CppSQLite3Query query = bantch_db->execQuery(sql5); if (!query.eof()){ string response; CString postData; string strStuCode; m_examId = 16; CString strPaperId; readPaperId(strPaperId); //postData.Format(_T("exam_group_id=")); //postData += strPaperId; //postData += "&studentCard="; if (g_my_scan_type == 2) { postData += "studentCard="; } else { postData.Format(_T("exam_group_id=")); postData += strPaperId; postData += "&studentCard="; } // postData.Format(_T("paperIdStr=16670445642153199061&studentCard=")); int studentCount = 0; std::map> code_id_map; while (!query.eof()){ code_id_map[query.getStringField("student_code")].push_back(query.getIntField("student_paper_id")); if (studentCount > 0){ postData.Append(_T(",")); } postData.Append(CString(query.getStringField("student_code"))); strStuCode = query.getStringField("student_code"); query.nextRow(); studentCount++; } //postData.Append(CString("&cardStatus=0")); //CString strBarcodeFlag,strBar; //readBarcodeFlag(strBarcodeFlag); //strBar = "&barcodeFlag="; //strBar = strBar + strBarcodeFlag; //postData.Append(strBar); if (g_my_scan_type == 2) { } else { postData.Append(CString("&cardStatus=0")); CString strBarcodeFlag, strBar; readBarcodeFlag(strBarcodeFlag); strBar = "&barcodeFlag="; strBar = strBar + strBarcodeFlag; postData.Append(strBar); } rapidjson::Document root; CHttpClient httpClient; if (!m_bUseQr || m_strQr.empty()){ httpClient.SetSendHeader(MarkHttpHeaderData()); if (g_my_scan_type == 1) { httpClient.HttpPost(url_str_dxk, postData, response); } else if (g_my_scan_type == 2) { httpClient.HttpPost(url_str_zxk, postData, response); } else { httpClient.HttpPost(url_str, postData, response); } root.Parse(ascii2utf8(response).c_str()); } Sleep(1000);//休眠1秒 EnterCriticalSection(db_lock); if (!m_bUseQr){ if (root.IsObject() && root["success"].IsInt() && root["success"].GetInt()){ rapidjson::Value& rows = root["object"]; std::map code_obj_map; for (int i = 0; i < rows.Size(); i++) { code_obj_map[utf82ascii(rows[i]["examineeCode"].GetString())] = rows[i]; } std::map>::iterator it; char * sql = "INSERT INTO student ( student_id, student_code, class_name, school_id, grade_name, student_name, class_id, score_total ,studentCard,schoolStudentCard ) SELECT t.student_id, t.student_code, t.class_name, t.school_id, t.grade_name, t.student_name, t.class_id, t.score_total,t.studentCard ,t.schoolStudentCard FROM ( SELECT :student_id student_id, :student_code student_code, :class_name class_name, :school_id school_id, :grade_name grade_name, :student_name student_name, :class_id class_id, :score_total score_total, :studentCard studentCard , :schoolStudentCard schoolStudentCard ) t LEFT JOIN student s ON s.student_id = t.student_id WHERE s.student_id IS NULL"; CppSQLite3Statement statement = bantch_db->compileStatement(sql); for (it = code_id_map.begin(); it != code_id_map.end(); ++it) { bool bUpload = true; std::map::iterator it2 = code_obj_map.find(it->first); for (int m = 0; m < it->second.size(); m++) { if (it2 == code_obj_map.end()){//不存在该考生 sprintf_s(sql5, "update student_paper set paper_state =paper_state -%d +%d where student_paper_id =%d", EX_KAOHAODAIPIPEI, EX_KAOHAOBUCUNZAI, it->second[m]); bantch_db->execDML(sql5); bUpload = false; } else{ //检查是否有考号冲突的试卷 sprintf_s(sql5, "select sp.student_paper_id,sp.paper_state,sp.student_id from student_paper sp where sp.student_id = '%s' and sp.student_paper_id!=%d", utf82ascii(it2->second["studentId"].GetString()).c_str(), it->second[m]); int cur_student_paper_id = it->second[m]; CppSQLite3Query q2 = bantch_db->execQuery(sql5); vector all_conflict_ids; vector conflict_ids; while (!q2.eof()){ long paper_state_t = q2.getInt64Field("paper_state"); all_conflict_ids.push_back(q2.getIntField("student_paper_id")); if (!(paper_state_t&EX_KAOHAOCHONGTU)) conflict_ids.push_back(q2.getIntField("student_paper_id")); q2.nextRow(); } bUpload = all_conflict_ids.empty(); if (!all_conflict_ids.empty()){ CppSQLite3Statement stmt3 = bantch_db->compileStatement("insert into exception (exception_type ,exception_name,page_id,student_paper_id) values(:exception_type ,:exception_name,:page_id,:student_paper_id)"); for (int m = 0; m < conflict_ids.size(); m++){//将与之冲突的试卷标记为考号冲突 sprintf_s(sql5, "update student_paper set paper_state =paper_state|%d ,Is_Covered=0 where student_paper_id=%d", EX_KAOHAOCHONGTU, conflict_ids[m]); bantch_db->execDML(sql5); stmt3.bind(":student_paper_id", conflict_ids[m]); stmt3.bind(":exception_type", SHIFT_KAOHAOCHONGTU); stmt3.bind(":exception_name", "考号冲突"); stmt3.execDML(); CppSQLite3Statement stmt4 = bantch_db->compileStatement("insert INTO message (student_paper_id , message_type) VALUES (:student_paper_id,:message_type)"); stmt4.bind(":student_paper_id", conflict_ids[m]); stmt4.bind(":message_type", WM_IDENTIFOR_PAPER_STATE_CHAGED); stmt4.execDML(); if (m_hwnd != NULL) ::PostMessage(m_hwnd, WM_IDENTIFOR_PAPER_STATE_CHAGED, (WPARAM)NULL, (LPARAM)conflict_ids[m]); if (g_upload_manager&&g_upload_manager->IsEanble()){ if (conflict_ids[m] != cur_student_paper_id) g_upload_manager->UploadOnScanCmd(UPLOAD_ON_SCAN_CMD::CMD_DEL_UPLOAD_REPORT, std::to_string(conflict_ids[m]).c_str()); } } stmt3.bind(":student_paper_id", it->second[m]); stmt3.bind(":exception_type", SHIFT_KAOHAOCHONGTU); stmt3.bind(":exception_name", "考号冲突"); stmt3.execDML(); } sprintf_s(sql5, "update student_paper set paper_state =paper_state -%d+%d,student_id ='%s',Is_Covered=0 where student_paper_id =%d", EX_KAOHAODAIPIPEI, all_conflict_ids.empty() ? 0 : EX_KAOHAOCHONGTU, utf82ascii(it2->second["studentId"].GetString()).c_str(), it->second[m]); bantch_db->execDML(sql5); statement.bind(":student_id", utf82ascii(it2->second["studentId"].GetString()).c_str()); statement.bind(":student_code", utf82ascii(it2->second["examineeCode"].GetString()).c_str()); statement.bind(":class_name", utf82ascii(it2->second["clazzName"].GetString()).c_str()); statement.bind(":school_id", it2->second["schoolId"].GetInt()); statement.bind(":grade_name", utf82ascii(it2->second["gradeName"].GetString()).c_str()); statement.bind(":student_name", utf82ascii(it2->second["studentName"].GetString()).c_str()); statement.bind(":class_id", (long long)it2->second["clazzId"].GetInt64()); statement.bind(":score_total", 0); statement.bind(":studentCard", it2->second["studentCard"].GetString()); statement.bind(":schoolStudentCard", it2->second["schoolStudentCard"].GetString()); statement.execDML(); } CppSQLite3Statement stmt4 = bantch_db->compileStatement("insert INTO message (student_paper_id , message_type) VALUES (:student_paper_id,:message_type)"); stmt4.bind(":student_paper_id", it->second[m]); stmt4.bind(":message_type", WM_IDENTIFOR_PAPER_STATE_CHAGED); stmt4.execDML(); if (m_hwnd != NULL)::PostMessage(m_hwnd, WM_IDENTIFOR_PAPER_STATE_CHAGED, (WPARAM)NULL, (LPARAM)it->second[m]); if (bUpload&&g_upload_manager){ std::string paper_state_qry_sql = "select paper_state from student_paper where student_paper_id="; paper_state_qry_sql += std::to_string(it->second[m]); auto paper_state_qry = bantch_db->execQuery(paper_state_qry_sql.c_str()); if (!paper_state_qry.eof() && paper_state_qry.getIntField("paper_state") == 0){ g_upload_manager->UploadOnScanCmd(UPLOAD_ON_SCAN_CMD::CMD_UPLOAD_SUCCESSSJUAN, std::to_string(it->second[m]).c_str()); } } } } } else{ std::map>::iterator it; for (it = code_id_map.begin(); it != code_id_map.end(); ++it) { for (int m = 0; m < it->second.size(); m++) { sprintf_s(sql5, "update student_paper set paper_state =paper_state -%d +%d where student_paper_id =%d", EX_KAOHAODAIPIPEI, EX_KAOHAOBUCUNZAI, it->second[m]); bantch_db->execDML(sql5); CppSQLite3Statement stmt4 = bantch_db->compileStatement("insert INTO message (student_paper_id , message_type) VALUES (:student_paper_id,:message_type)"); stmt4.bind(":student_paper_id", it->second[m]); stmt4.bind(":message_type", WM_IDENTIFOR_PAPER_STATE_CHAGED); stmt4.execDML(); if (m_hwnd != NULL)::PostMessage(m_hwnd, WM_IDENTIFOR_PAPER_STATE_CHAGED, (WPARAM)NULL, (LPARAM)it->second[m]); } } } } else{ std::string _student_code; std::string _student_id; if (!m_strQr.empty()){ std::vector< std::string > _out; split(m_strQr, "@", &_out); if (_out.size() >= 3){ _student_code = _out[1]; } } else{ _student_code = strStuCode; if (root.IsObject() && root["success"].IsInt() && root["success"].GetInt()){ if (root["object"].IsArray() && root["object"].Size()>0){ _student_id = utf82ascii(root["object"][0]["studentId"].GetString()); } } else{ std::map>::iterator it; for (it = code_id_map.begin(); it != code_id_map.end(); ++it) { for (int m = 0; m < it->second.size(); m++) { sprintf_s(sql5, "update student_paper set paper_state =paper_state -%d +%d where student_paper_id =%d", EX_KAOHAODAIPIPEI, EX_KAOHAOBUCUNZAI, it->second[m]); bantch_db->execDML(sql5); CppSQLite3Statement stmt4 = bantch_db->compileStatement("insert INTO message (student_paper_id , message_type) VALUES (:student_paper_id,:message_type)"); stmt4.bind(":student_paper_id", it->second[m]); stmt4.bind(":message_type", WM_IDENTIFOR_PAPER_STATE_CHAGED); stmt4.execDML(); if (m_hwnd != NULL)::PostMessage(m_hwnd, WM_IDENTIFOR_PAPER_STATE_CHAGED, (WPARAM)NULL, (LPARAM)it->second[m]); } } LeaveCriticalSection(db_lock); return running; } } if (!_student_code.empty() && cur_student_paper_id > 0){ //检查是否有考号冲突的试卷 sprintf_s(sql5, "select sp.student_paper_id,sp.paper_state,sp.student_id from student_paper sp where sp.student_code = '%s' and sp.student_paper_id!=%d", _student_code.c_str(), cur_student_paper_id); CppSQLite3Query q2 = bantch_db->execQuery(sql5); std::vector all_conflict_ids; while (!q2.eof()){ long paper_state_t = q2.getInt64Field("paper_state"); all_conflict_ids.push_back(q2.getIntField("student_paper_id")); q2.nextRow(); } if (!all_conflict_ids.empty()){ CppSQLite3Statement stmt3 = bantch_db->compileStatement("insert into exception (exception_type ,exception_name,page_id,student_paper_id) values(:exception_type ,:exception_name,:page_id,:student_paper_id)"); for (int m = 0; m < all_conflict_ids.size(); m++){//将与之冲突的试卷标记为考号冲突 sprintf_s(sql5, "update student_paper set paper_state =paper_state|%d,Is_Covered=0 where student_paper_id=%d", EX_KAOHAOCHONGTU, all_conflict_ids[m]); bantch_db->execDML(sql5); stmt3.bind(":student_paper_id", all_conflict_ids[m]); stmt3.bind(":exception_type", SHIFT_KAOHAOCHONGTU); stmt3.bind(":exception_name", "考号冲突"); stmt3.execDML(); CppSQLite3Statement stmt4 = bantch_db->compileStatement("insert INTO message (student_paper_id , message_type) VALUES (:student_paper_id,:message_type)"); stmt4.bind(":student_paper_id", all_conflict_ids[m]); stmt4.bind(":message_type", WM_IDENTIFOR_PAPER_STATE_CHAGED); stmt4.execDML(); if (m_hwnd != NULL) ::PostMessage(m_hwnd, WM_IDENTIFOR_PAPER_STATE_CHAGED, (WPARAM)NULL, (LPARAM)all_conflict_ids[m]); if (g_upload_manager&&g_upload_manager->IsEanble()){ if (all_conflict_ids[m] != cur_student_paper_id) g_upload_manager->UploadOnScanCmd(UPLOAD_ON_SCAN_CMD::CMD_DEL_UPLOAD_REPORT, std::to_string(all_conflict_ids[m]).c_str()); } } stmt3.bind(":student_paper_id", cur_student_paper_id); stmt3.bind(":exception_type", SHIFT_KAOHAOCHONGTU); stmt3.bind(":exception_name", "考号冲突"); stmt3.execDML(); } if (_student_id.empty()){ sprintf_s(sql5, "update student_paper set paper_state =paper_state -%d+%d,Is_Covered=0 where student_paper_id =%d", EX_KAOHAODAIPIPEI, all_conflict_ids.empty() ? 0 : EX_KAOHAOCHONGTU, cur_student_paper_id); } else{ sprintf_s(sql5, "update student_paper set paper_state =paper_state -%d+%d,student_id ='%s',Is_Covered=0 where student_paper_id =%d", EX_KAOHAODAIPIPEI, all_conflict_ids.empty() ? 0 : EX_KAOHAOCHONGTU, _student_id.c_str(), cur_student_paper_id); } bantch_db->execDML(sql5); CppSQLite3Statement stmt4 = bantch_db->compileStatement("insert INTO message (student_paper_id , message_type) VALUES (:student_paper_id,:message_type)"); stmt4.bind(":student_paper_id", cur_student_paper_id); stmt4.bind(":message_type", WM_IDENTIFOR_PAPER_STATE_CHAGED); stmt4.execDML(); if (m_hwnd != NULL)::PostMessage(m_hwnd, WM_IDENTIFOR_PAPER_STATE_CHAGED, (WPARAM)NULL, (LPARAM)cur_student_paper_id); } } LeaveCriticalSection(db_lock); } else if (m_resultHandler->GetServiceSate() == stoped){ return stoping; } //休息5秒 Sleep(2 * 1000); return running; } ServiceState CStudentMatcher::OnStoping( void ) { return stoped; } void CStudentMatcher::SetResultHandler( CResultHandler* result_handler ) { m_resultHandler = result_handler; } void CStudentMatcher::setExamId( long examId ) { m_examId =examId; } CString CStudentMatcher::MarkHttpHeaderData() { TCHAR FilePath[MAX_PATH]; GetModuleFileName(NULL, FilePath, MAX_PATH); CString ret; (_tcsrchr(FilePath, '\\'))[1] = 0; if (m_bErrorHandle){ lstrcat(FilePath, _T("paperinfo.ini")); WCHAR JSESSIONID[512]; GetPrivateProfileString(_T("paperinfo"), _T("session"), _T(""), JSESSIONID, 512, FilePath); ret = JSESSIONID; } else{ lstrcat(FilePath, _T("config.ini")); #ifndef BACK_STAGE WCHAR SIG[512]; GetPrivateProfileString(_T("SESSIONID"), _T("SIG"), _T(""), SIG, 512, FilePath); WCHAR TIME[512]; GetPrivateProfileString(_T("SESSIONID"), _T("TIME"), _T(""), TIME, 512, FilePath); WCHAR ID[512]; GetPrivateProfileString(L"SESSIONID", L"ID", L"", ID, 512, FilePath); WCHAR JSESSIONID[512]; GetPrivateProfileString(_T("SESSIONID"), _T("JSESSIONID"), _T(""), JSESSIONID, 512, FilePath); ret.Format(_T("userid=%s&time=%s&sig=%s&session=%s"), ID, TIME, SIG, JSESSIONID); #else WCHAR MYCOOK[512]; GetPrivateProfileString(_T("SESSIONID"), _T("MYCOOK"), _T(""), MYCOOK, 512, FilePath); CString ret; ret.Format(_T("%s"), MYCOOK); #endif } return ret; } bool CStudentMatcher::readPaperId(CString& strPaperId) { TCHAR exeFullPath[MAX_PATH]; CString strPath; GetModuleFileName(NULL, exeFullPath, MAX_PATH); strPath = (CString)exeFullPath; int position = strPath.ReverseFind('\\'); strPath = strPath.Left(position + 1); TCHAR FilePath[MAX_PATH]; GetModuleFileName(NULL, FilePath, MAX_PATH); (_tcsrchr(FilePath, '\\'))[1] = 0; lstrcat(FilePath, _T("paperInfo.ini")); WCHAR PaperId[512]; GetPrivateProfileString(_T("paperinfo"), _T("paper_str"), _T(""), PaperId, 512, FilePath); strPaperId = PaperId; return false; } bool CStudentMatcher::readBarcodeFlag(CString& strBarcodeFlag) { TCHAR exeFullPath[MAX_PATH]; CString strPath; GetModuleFileName(NULL, exeFullPath, MAX_PATH); strPath = (CString)exeFullPath; int position = strPath.ReverseFind('\\'); strPath = strPath.Left(position + 1); TCHAR FilePath[MAX_PATH]; GetModuleFileName(NULL, FilePath, MAX_PATH); (_tcsrchr(FilePath, '\\'))[1] = 0; lstrcat(FilePath, _T("paperInfo.ini")); WCHAR BarcodeFlag[512]; GetPrivateProfileString(_T("paperinfo"), _T("barcode_flag"), _T("1"), BarcodeFlag, 512, FilePath); strBarcodeFlag = BarcodeFlag; return false; }