123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455 |
- #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<fstream>
- #include<iostream>
- 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<std::string, vector<int>> 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<std::string, rapidjson::Value> code_obj_map;
- for (int i = 0; i < rows.Size(); i++)
- {
- code_obj_map[utf82ascii(rows[i]["examineeCode"].GetString())] = rows[i];
- }
- std::map<string, vector<int>>::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<std::string, rapidjson::Value>::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<int> all_conflict_ids;
- vector<int> 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<string, vector<int>>::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<string, vector<int>>::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<int> 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;
- }
|