ResultUploader.cpp 68 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819
  1. #include "StdAfx.h"
  2. #include "scan_common.h"
  3. #include "ResultUploader.h"
  4. #include <afxinet.h>
  5. #include <fstream>
  6. #include "..\EvaluationUtil\MemFileInputFiled.h"
  7. #include "..\EvaluationUtil\HttpClient.h"
  8. #include "basic_struct.h"
  9. #include <codecvt>
  10. #include "..\ZLibWrapMemLib\MemZipFile.h"
  11. #include "ServerConfig.h"
  12. #include "..\ZLibWrapLib\ZLibWrapLib.h"
  13. #include "..\EvaluationUtil\IInputFiled.h"
  14. #include "..\EvaluationUtil\FileInputFiled.h"
  15. #include <direct.h>
  16. #include <time.h>
  17. #include "../lib/ucloud/include/api.h"
  18. #include "NetOperator.h"
  19. #include "rapidjson/document.h"
  20. #include "rapidjson/writer.h"
  21. #include "rapidjson/stringbuffer.h"
  22. extern bool g_isAllSubject ;//是否是全学科
  23. int HttpDownloadTemplate(std::string from, std::string fileName);
  24. void AddStringMember(rapidjson::Value&root, rapidjson::Document::AllocatorType& allocator, const char*key, const char*value)
  25. {
  26. rapidjson::Value _key(rapidjson::kStringType);
  27. rapidjson::Value string_value(rapidjson::kStringType);
  28. _key.SetString(key, allocator);
  29. string_value.SetString(value, allocator);
  30. root.AddMember(_key, string_value, allocator);
  31. };
  32. //type 0 INT 1 DOUBLE
  33. void AddNumberMember(rapidjson::Value&root, rapidjson::Document::AllocatorType& allocator, const char*key, int value, int type){
  34. rapidjson::Value _key(rapidjson::kStringType);
  35. rapidjson::Value num_value(rapidjson::kNumberType);
  36. _key.SetString(key, allocator);
  37. switch (type){
  38. case 0: num_value.SetInt(value); break; //int
  39. case 1: num_value.SetDouble(value); break;//double
  40. }
  41. root.AddMember(_key, num_value, allocator);
  42. };
  43. long helper_get_file_size(FILE *fp){
  44. long fsize;
  45. fseek(fp, 0, SEEK_END);
  46. fsize = ftell(fp);
  47. fseek(fp, 0, SEEK_SET);
  48. return fsize;
  49. }
  50. struct UploadMsg
  51. {
  52. string strMsg;
  53. int nTotal;
  54. };
  55. static std::string UnicodeToAnsi(const CString& str)
  56. {
  57. std::string ret;
  58. int nLen = WideCharToMultiByte(CP_ACP, 0, str, -1, NULL, 0, NULL, NULL);
  59. if (nLen == 0){
  60. return ret;
  61. }
  62. char* pResult = new char[nLen];
  63. WideCharToMultiByte(CP_ACP, 0, str, -1, pResult, nLen, NULL, NULL);
  64. ret = pResult;
  65. delete[] pResult;
  66. return ret;
  67. }
  68. extern int g_my_scan_type;
  69. extern std::string g_strUpLoadDbPathName;
  70. void ThreadLog3(char* strLogContent, char* fileName)
  71. {
  72. char buf[200];
  73. _getcwd(buf, sizeof(buf));
  74. strcat_s(buf, "\\ThirdParty\\");
  75. strcat_s(buf, fileName);
  76. FILE *file = NULL;
  77. fopen_s(&file, buf, "a+");
  78. if (file == NULL){
  79. return;
  80. }
  81. //fseek(file, 0, SEEK_END);
  82. //char strLogTemp[5000];
  83. //strcpy_s(strLogTemp, sizeof(strLogTemp), strLogContent);
  84. fwrite(strLogContent, strlen(strLogContent), 1, file);
  85. fclose(file);
  86. }
  87. static void DeleteDirectory(CString source)
  88. {
  89. //AfxMessageBox("创建文件夹"+target);
  90. CFileFind finder;
  91. CString path;
  92. path.Format(_T("%s/*.*"), source);
  93. BOOL bWorking = finder.FindFile(path);
  94. while (bWorking){
  95. bWorking = finder.FindNextFile();
  96. if (finder.IsDirectory() && !finder.IsDots()){ //是文件夹 而且 名称不含 . 或 ..
  97. DeleteDirectory(finder.GetFilePath()); //递归创建文件夹+"/"+finder.GetFileName()
  98. }
  99. else{ //是文件 则直接删除
  100. DeleteFile(finder.GetFilePath());
  101. }
  102. }
  103. RemoveDirectory(source);
  104. }
  105. void CopyAllFile(CString dest, CString source)
  106. {
  107. //AfxMessageBox("创建文件夹"+target);
  108. CFileFind finder;
  109. CString path;
  110. path.Format(_T("%s/*.*"), source);
  111. BOOL bWorking = finder.FindFile(path);
  112. while (bWorking){
  113. bWorking = finder.FindNextFile();
  114. if (finder.IsDirectory() && !finder.IsDots()){ //是文件夹 而且 名称不含 . 或 ..
  115. CString fTemp;
  116. fTemp = dest + finder.GetFileName() + _T("\\");
  117. CreateDirectory(fTemp,NULL);
  118. CopyAllFile(fTemp, finder.GetFilePath()); //递归创建文件夹+"/"+finder.GetFileName()
  119. }
  120. else{ //是文件 拷贝文件
  121. CopyFile(finder.GetFilePath(), dest + finder.GetFileName(),FALSE);
  122. }
  123. }
  124. RemoveDirectory(source);
  125. }
  126. #include <boost/uuid/uuid.hpp>
  127. #include <boost/uuid/uuid_io.hpp>
  128. #include <boost/uuid/uuid_generators.hpp>
  129. static CString GetExePath()
  130. {
  131. HMODULE module = GetModuleHandle(0);
  132. TCHAR pFileName[MAX_PATH + 2] = { 0 };
  133. GetModuleFileName(module, pFileName, MAX_PATH);
  134. CString csFullPath(pFileName);
  135. int nPos = csFullPath.ReverseFind(_T('\\'));
  136. if (nPos < 0)
  137. return CString("");
  138. else
  139. return csFullPath.Left(nPos);
  140. }
  141. std::string CResultUploader::get_ucloud_key(const std::string&eid)
  142. {
  143. char school_id[128] = { 0 };
  144. std::string path = UnicodeToAnsi(GetExePath() + _T("\\config.ini"));
  145. GetPrivateProfileStringA("USER", "schoolId", "", school_id, 128, path.c_str());
  146. boost::uuids::uuid a_uuid = boost::uuids::random_generator()();
  147. std::string uuid_string = boost::uuids::to_string(a_uuid);
  148. SYSTEMTIME tm;
  149. GetLocalTime(&tm);
  150. std::string date = std::to_string(tm.wYear) + "/" + std::to_string(tm.wMonth) + "/" + std::to_string(tm.wDay) + "/";
  151. std::string ret = std::string("kehuduan/backupservices/") + school_id + "/" + eid + "/" + date;
  152. ret += uuid_string + ".zip";
  153. return ret;
  154. }
  155. CResultUploader::CResultUploader(void)
  156. {
  157. m_bUploadOnScanModel = false;
  158. m_result_hadler = NULL;
  159. m_hwnd = NULL;
  160. m_nCourseId = readCourseId();
  161. m_bOnlineCard = false;
  162. m_bErrorHandle = false;
  163. m_nSubjectID = 0;
  164. TCHAR exeFullPath[MAX_PATH];
  165. CString strPath;
  166. GetModuleFileName(NULL, exeFullPath, MAX_PATH);
  167. strPath = (CString)exeFullPath;
  168. int position = strPath.ReverseFind('\\');
  169. strPath = strPath.Left(position + 1);
  170. std::string str_path_tmp = UnicodeToAnsi(GetExePath() + _T("\\config.ini"));
  171. const char* FilePath = str_path_tmp.c_str();
  172. char szbucket_name[512];
  173. GetPrivateProfileStringA("GLOBAL", "bucket_name", "zxhx-test", szbucket_name, 512, FilePath);
  174. bucket_name = szbucket_name;
  175. ::InitializeCriticalSection(&m_csUploadOnScanPapers);
  176. }
  177. CResultUploader::~CResultUploader(void)
  178. {
  179. ::DeleteCriticalSection(&m_csUploadOnScanPapers);
  180. }
  181. ServiceState CResultUploader::OnStarting(void)
  182. {
  183. m_nCourseId = readCourseId();
  184. m_failtrue = FALSE;
  185. char sql[512] = { 0 };
  186. if (!m_bUploadOnScanModel){
  187. sprintf_s(sql, "update database set database_state =%d where database_id =%d", DATABASE_STATE_RESULT_UPLOADING, database_id);
  188. EnterCriticalSection(database_db_lock);
  189. database_db->execDML("begin transaction");
  190. database_db->execDML(sql);
  191. database_db->execDML("commit transaction");
  192. LeaveCriticalSection(database_db_lock);
  193. }
  194. if (m_strNewUploadUrl == "")
  195. {
  196. if (m_nCourseId == 3 || m_nCourseId == 6 || m_nCourseId == 51)
  197. url_str.Format(_T("%s/coachio/iointerface_v2/thirdUploadPaper"), 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);
  198. else
  199. url_str.Format(_T("%s/coachio/iointerface_v2/quan_third_upload_paper"), 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);
  200. }
  201. else
  202. {
  203. if (m_nCourseId == 3 || m_nCourseId == 6 || m_nCourseId == 51)
  204. {
  205. url_str.Format(_T("%s/coachio/iointerface_v2/thirdUploadPaper"), m_strNewUploadUrl);
  206. }
  207. //url_str.Format(_T("%s/coachio/iointerface_v2/thirdUploadPaper"), m_strNewUploadUrl);
  208. else
  209. {
  210. url_str.Format(_T("%s/coachio/iointerface_v2/quan_third_upload_paper"), m_strNewUploadUrl);
  211. }
  212. }
  213. sprintf_s(sql, "SELECT count(0) FROM student_paper pp LEFT JOIN student stu ON stu.student_id = pp.student_id LEFT JOIN exam em ON 1 = 1 WHERE (pp.ret_upload = 0 or pp.ret_upload = 3)");
  214. m_total = bantch_db->execScalar(sql);
  215. m_current_count = 0;
  216. m_sccuss_count = 0;
  217. last_student_paper_id = -1;
  218. if (!m_bUploadOnScanModel){
  219. upload_database_db.open(g_strUpLoadDbPathName.c_str());
  220. upload_database_db.execDML("pragma journal_mode = MEMORY");
  221. if (m_hwnd != NULL)::PostMessage(m_hwnd, WM_IDENTIFOR_RESULT_UPLOAD_STARTED, 0, 0);
  222. }
  223. return running;
  224. }
  225. void CResultUploader::getupLoadUrl(char *strUrl, int &nlen)
  226. {
  227. //http://zsyapi.testing.xueping.com/coachio/iointerface_v2/uploadstudentspapersrs
  228. if (m_bOnlineCard){
  229. //魔法词汇宝
  230. if (m_nOnlineScanType == 0)
  231. {
  232. url_str.Format(_T("%s/coachio/iointerface_v2/magic_word_card_upload"), m_strNewUploadUrl);
  233. }
  234. else if (m_nSubjectID == 0)
  235. {
  236. url_str.Format(_T("%s/coachio/iointerface_v2/uploadstudentspapersrs"), m_strNewUploadUrl);
  237. }
  238. else if (m_nSubjectID == 3 || m_nSubjectID == 6 || m_nSubjectID == 51)
  239. {
  240. url_str.Format(_T("%s/coachio/iointerface_v2/thirdUploadPaper"), m_strNewUploadUrl);
  241. }
  242. else
  243. {
  244. url_str.Format(_T("%s/coachio/iointerface_v2/quan_third_upload_paper"), m_strNewUploadUrl);
  245. }
  246. }
  247. else{
  248. if (g_my_scan_type == 1)
  249. {
  250. if (m_strNewUploadUrl == "")
  251. {
  252. url_str.Format(_T("%s/coachio/iointerface_v2/upload_card_duxue"), 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);
  253. }
  254. else
  255. {
  256. url_str.Format(_T("%s/coachio/iointerface_v2/upload_card_duxue"), m_strNewUploadUrl);
  257. }
  258. }
  259. else if (g_my_scan_type == 2)
  260. {
  261. if (m_strNewUploadUrl == "")
  262. {
  263. url_str.Format(_T("%s/coachio/iointerface_v2/UploadStudentProductConfig"), 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);
  264. }
  265. else
  266. {
  267. url_str.Format(_T("%s/coachio/iointerface_v2/UploadStudentProductConfig"), m_strNewUploadUrl);
  268. }
  269. }
  270. else
  271. {
  272. if (m_strNewUploadUrl == "")
  273. {
  274. if (m_nCourseId == 3 || m_nCourseId == 6 || m_nCourseId == 51)
  275. url_str.Format(_T("%s/coachio/iointerface_v2/thirdUploadPaper"), 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);
  276. else
  277. url_str.Format(_T("%s/coachio/iointerface_v2/quan_third_upload_paper"), 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);
  278. }
  279. else
  280. {
  281. if (m_nCourseId == 3 || m_nCourseId == 6 || m_nCourseId == 51)
  282. {
  283. url_str.Format(_T("%s/coachio/iointerface_v2/thirdUploadPaper"), m_strNewUploadUrl);
  284. }
  285. //url_str.Format(_T("%s/coachio/iointerface_v2/thirdUploadPaper"), m_strNewUploadUrl);
  286. else
  287. {
  288. url_str.Format(_T("%s/coachio/iointerface_v2/quan_third_upload_paper"), m_strNewUploadUrl);
  289. }
  290. }
  291. }
  292. }
  293. int mylen = WideCharToMultiByte(CP_UTF8, 0, url_str, -1, strUrl, nlen, NULL, NULL);
  294. nlen = mylen;
  295. }
  296. ServiceState CResultUploader::OnRunning(void)
  297. {
  298. ServiceState nextState = IService::OnRunning();
  299. if (nextState != running) return nextState;
  300. //char sql1[1024] = { 0 };
  301. string strSql;
  302. if (m_bErrorHandle)
  303. {
  304. char sql1[1024] = { 0 };
  305. sprintf_s(sql1, "SELECT pp.student_paper_id,pp.task_id, pp.paper_code,pp.paper_state, pp.student_id, pp.student_code,pp.absent,pp.picFlag, stu.student_name, stu.class_id, em.exam_course_id, pp.score_total,pp.ori_img, em.exam_id, em.full_score,stu.school_id FROM student_paper pp LEFT JOIN student stu ON stu.student_id = pp.student_id LEFT JOIN exam em ON 1 = 1 WHERE pp.student_paper_id>%d AND (pp.ret_upload = 0 or pp.ret_upload = 3) AND pp.paper_state=0 AND pp.Is_Covered !=1 LIMIT 500", last_student_paper_id);
  306. strSql = sql1;
  307. }
  308. else{
  309. if (m_bUploadOnScanModel)
  310. {
  311. ::EnterCriticalSection(&m_csUploadOnScanPapers);
  312. if (!m_vctUploadOnScanPapers.empty()){
  313. std::string strPaperIds;
  314. for (auto it : m_vctUploadOnScanPapers){
  315. strPaperIds += std::to_string(it.first);
  316. strPaperIds += ",";
  317. }
  318. strPaperIds = std::string(strPaperIds, 0, strPaperIds.size() - 1);
  319. // 边扫描边上传 - 上传指定学生的试卷
  320. strSql = "SELECT pp.student_paper_id, pp.paper_code,pp.paper_state, pp.student_id, pp.student_code,pp.absent,pp.picFlag,\
  321. stu.student_name, stu.class_id, em.exam_course_id, pp.score_total,pp.ori_img, em.exam_id, \
  322. em.full_score,stu.school_id FROM student_paper pp LEFT JOIN student stu ON stu.student_id = pp.student_id LEFT JOIN \
  323. exam em ON 1 = 1 WHERE pp.student_paper_id in(";
  324. strSql.append(strPaperIds);
  325. strSql.append(") LIMIT 500");
  326. }
  327. else{
  328. ::LeaveCriticalSection(&m_csUploadOnScanPapers);
  329. return stoping;
  330. }
  331. ::LeaveCriticalSection(&m_csUploadOnScanPapers);
  332. }
  333. else
  334. {
  335. char sql1[1024] = { 0 };
  336. sprintf_s(sql1, "SELECT pp.student_paper_id, pp.paper_code,pp.paper_state, pp.student_id, pp.student_code,pp.absent,pp.picFlag, stu.student_name, stu.class_id, em.exam_course_id, pp.score_total,pp.ori_img, em.exam_id, em.full_score,stu.school_id FROM student_paper pp LEFT JOIN student stu ON stu.student_id = pp.student_id LEFT JOIN exam em ON 1 = 1 WHERE pp.student_paper_id>%d AND (pp.ret_upload = 0 or pp.ret_upload = 3) AND pp.Is_Covered !=1 LIMIT 500", last_student_paper_id);
  337. strSql = sql1;
  338. }
  339. }
  340. CppSQLite3Query q = bantch_db->execQuery(strSql.c_str());
  341. char sql1[1024] = { 0 };
  342. if (m_bOnlineCard){
  343. sprintf_s(sql1, "SELECT r.question_code, r.type, r.answer, r.score,r.question_state,r.isright, qs.score_full, s.school_id,qs.answer_std,qs.question_type,qs.basic_type_id FROM student_paper sp LEFT JOIN page p ON p.student_paper_id = sp.student_paper_id LEFT JOIN result r ON r.page_id = p.page_id LEFT JOIN question_std qs ON qs.question_code = r.question_code LEFT JOIN student s ON s.student_id = sp.student_id WHERE p.student_paper_id = :student_paper_id and qs.question_code is not null");
  344. }
  345. else{
  346. sprintf_s(sql1, "SELECT r.question_code, r.type, r.answer, r.score,r.question_state, qs.score_full, s.school_id,qs.answer_std,qs.question_type FROM student_paper sp LEFT JOIN page p ON p.student_paper_id = sp.student_paper_id LEFT JOIN result r ON r.page_id = p.page_id LEFT JOIN question_std qs ON qs.question_code = r.question_code LEFT JOIN student s ON s.student_id = sp.student_id WHERE p.student_paper_id = :student_paper_id and r.type in(%d,%d,%d) and qs.question_code is not null", RT_KEGUANTI, RT_KEGUANTI_EX, RT_ZHUGUANTI);
  347. }
  348. CppSQLite3Statement stmt2 = bantch_db->compileStatement(sql1);
  349. sprintf_s(sql1, "SELECT r.question_code, r.type, r.answer, r.score, s.school_id FROM student_paper sp LEFT JOIN page p ON p.student_paper_id = sp.student_paper_id LEFT JOIN result r ON r.page_id = p.page_id LEFT JOIN student s ON s.student_id = sp.student_id WHERE p.student_paper_id = :student_paper_id and r.type=%d", RT_XUANZUOTI);
  350. CppSQLite3Statement stmt3 = bantch_db->compileStatement(sql1);
  351. DWORD lastime =GetCurrentTime();
  352. CString timeString =GetTimeString();
  353. string strErrorInfo;
  354. strErrorInfo = "出错:\r\n";
  355. int nErrorCount = 0;
  356. int paperID = -1;
  357. CString moule_dir;
  358. CString tempUpLoadPath;
  359. CString tempUpLoadPathChild;
  360. CString imgPath;
  361. if (!m_bUploadOnScanModel){
  362. GetCurrentModuleDir(moule_dir);
  363. tempUpLoadPath = moule_dir + _T("\\testUpLoad\\");
  364. CreateDirectory(tempUpLoadPath, NULL);
  365. tempUpLoadPath += GetTimeString();
  366. CreateDirectory(tempUpLoadPath, NULL);
  367. tempUpLoadPathChild = tempUpLoadPath + _T("\\result");
  368. tempUpLoadPathChild += GetTimeString();
  369. imgPath = tempUpLoadPathChild + _T("\\img");
  370. }
  371. int rows = 0;
  372. char sql2[512];
  373. char sql3[512];
  374. char sz_file_name[520];
  375. char sz_ori_file_name[520] = {0};
  376. int temp[4] = { 0 };
  377. std::string json_str;
  378. std::string json_ori_str;
  379. std::string exception_json;
  380. std::string file_name;
  381. CppSQLite3Statement stmt4;
  382. int nRecordIndex = 0;//记录索引防止json重名
  383. if(!m_bUploadOnScanModel)
  384. stmt4 = upload_database_db.compileStatement("INSERT INTO student_paper ( student_paper_id, ret_upload, paper_code, student_code, student_id, file_name, json_name,student_name, ret_ori_upload , ori_img ,ori_json ,absent,service_type,exception_json,exc_upload,update_post_data) VALUES ( :student_paper_id, :ret_upload, :paper_code, :student_code, :student_id, :file_name, :json_name,:student_name,:ret_ori_upload ,:ori_img ,:ori_json ,:absent ,:service_type ,:exception_json ,:exc_upload, :update_post_data)");
  385. if (!q.eof()){
  386. int service_type = 0;
  387. TCHAR exeFullPath[MAX_PATH];
  388. CString strPath;
  389. GetModuleFileName(NULL, exeFullPath, MAX_PATH);
  390. strPath = (CString)exeFullPath;
  391. int position = strPath.ReverseFind('\\');
  392. strPath = strPath.Left(position + 1);
  393. TCHAR FilePath[MAX_PATH];
  394. GetModuleFileName(NULL, FilePath, MAX_PATH);
  395. (_tcsrchr(FilePath, '\\'))[1] = 0;
  396. lstrcat(FilePath, _T("paperinfo.ini"));
  397. service_type = GetPrivateProfileInt(_T("paperinfo"), _T("servicetype"), 0, FilePath);
  398. LOGI("开始上传成绩");
  399. std::vector<int> paperids;
  400. while (!q.eof())
  401. {
  402. nRecordIndex++;
  403. if (m_bUploadOnScanModel){
  404. GetCurrentModuleDir(moule_dir);
  405. tempUpLoadPath = moule_dir + _T("\\testUpLoad\\");
  406. CreateDirectory(tempUpLoadPath, NULL);
  407. tempUpLoadPath += GetTimeString();
  408. CreateDirectory(tempUpLoadPath, NULL);
  409. tempUpLoadPathChild = tempUpLoadPath + _T("\\result");
  410. tempUpLoadPathChild += GetTimeString();
  411. imgPath = tempUpLoadPathChild + _T("\\img");
  412. }
  413. else {
  414. stmt4.reset();
  415. }
  416. int paper_state = q.getIntField("paper_state");
  417. CString strStudentCode;
  418. if (m_bOnlineCard){
  419. Json::Value json;
  420. //魔法词汇宝增加json字段mw_id批次id
  421. if (m_nOnlineScanType != -1)
  422. {
  423. if (!m_pWeakResult.expired())
  424. {
  425. string strPaperCode = q.getStringField("paper_code");
  426. string strStudent= q.getStringField("student_id");
  427. boost::shared_ptr<CResultHandler> ptrResult = m_pWeakResult.lock(); //可以使用lock返回shared_ptr
  428. string strCardId = ptrResult->GetOnlineCardPaperId(strPaperCode, strStudent);
  429. //strCardId = "704606658002513920";
  430. json["mw_id"] = strCardId;
  431. }
  432. }
  433. if (m_nSubjectID == 0)
  434. {
  435. CString jsonPath;
  436. jsonPath.Format(L"%s\\%s%d_json.txt", tempUpLoadPathChild, GetTimeString(), nRecordIndex);
  437. //jsonPath = tempUpLoadPathChild + _T("\\") + GetTimeString() + _T("_json.txt");
  438. WideCharToMultiByte(CP_ACP, 0, jsonPath, -1, sz_file_name, 520, NULL, NULL);
  439. CMemFile file;
  440. CreateDirectory(tempUpLoadPathChild, NULL);
  441. CFile zip;
  442. zip.Open(jsonPath, CFile::modeCreate | CFile::modeWrite);
  443. Json::FastWriter writer;
  444. last_student_paper_id = q.getIntField("student_paper_id");
  445. paperids.push_back(q.getIntField("student_paper_id"));
  446. rows++;
  447. CString entryName;
  448. entryName.Format(_T("result%d.json"), rows);
  449. json["clazzId"] = q.getStringField("class_id");
  450. json["onlineCard"] = 1;
  451. json["paperId"] = 0;
  452. json["paperStr"] = q.getStringField("paper_code");
  453. json["studentId"] = q.getStringField("student_id");
  454. json["studentCode"] = q.getStringField("student_id");
  455. json["courseCode"] = "";
  456. json["totalScore"] = q.getStringField("score_total");;
  457. json["fullScore"] = q.getFloatField("full_score");
  458. json["studentPaperId"] = last_student_paper_id;
  459. json["quekaoFlag"] = q.getIntField("absent");
  460. paperID = q.getIntField("student_paper_id");
  461. Json::Value json_array(Json::arrayValue);
  462. Json::Value json_array2(Json::arrayValue);
  463. stmt2.reset();
  464. stmt2.bind(":student_paper_id", q.getIntField("student_paper_id"));
  465. auto fGetQuestionType = [](RESULT_TYPE nType)->int{
  466. switch (nType){
  467. case RT_KEGUANTI: return 1; break; // 单选题
  468. case RT_KEGUANTI_EX: return 2; break; // 多选题
  469. case RT_TIANKONGTI: return 5; break; // 填空题
  470. case RT_ZHUGUANTI: return 7; break; // 主观题
  471. case RT_XUANZUOTI: return 17; break; // 选做题
  472. case RT_BIZUOTI:return 27; break; // 必做题
  473. }
  474. return -1;
  475. };
  476. std::vector<std::string> _vctout;
  477. std::vector<std::string> _vctallitems;
  478. std::vector<std::pair<std::string, Json::Value>> sel_items;
  479. std::string stu_code = "";
  480. int questionNum = 0;
  481. if (!(q.getIntField("absent") == 1)){
  482. CppSQLite3Query q2 = stmt2.execQuery();
  483. while (!q2.eof()){
  484. RESULT_TYPE _type = (RESULT_TYPE)q2.getIntField("type");
  485. if (_type == 0 || _type == 1){
  486. q2.nextRow();
  487. continue;
  488. }
  489. int type_id = fGetQuestionType(_type);
  490. Json::Value item;
  491. item["fullScore"] = q2.getFloatField("score_full");
  492. item["handlerIndex"] = 1;
  493. item["paperId"] = "";
  494. item["questionNum"] = q2.getStringField("question_code");
  495. item["questionSerial"] = ++questionNum;
  496. item["questionTypeId"] = type_id;
  497. if (g_isAllSubject)
  498. {
  499. string basic_type_id = q2.getStringField("basic_type_id");
  500. item["basic_type_id"] = basic_type_id;
  501. if (basic_type_id == "12")//判断题
  502. {
  503. item["questionTypeId"] = "3";
  504. }
  505. }
  506. item["reportFlag"] = 1;
  507. item["rightFlag"] = q2.getIntField("isright");
  508. item["score"] = q2.getFloatField("score");
  509. stu_code = q.getStringField("student_id");
  510. item["studentCode"] = stu_code;
  511. std::string answer = q2.getStringField("answer");
  512. if (type_id == 17){
  513. _vctout.clear();
  514. split(answer, (std::string)"@", &_vctout);
  515. if (_vctout.size() == 5){
  516. item["answer"] = _vctout[1];
  517. if (_vctallitems.empty())
  518. split(_vctout[0], (std::string)",", &_vctallitems);
  519. }
  520. item["questionNum"] = _vctout[4];
  521. sel_items.push_back(std::make_pair(_vctout[4], item));
  522. }
  523. else{
  524. item["answer"] = answer;
  525. json_array.append(item);
  526. }
  527. q2.nextRow();
  528. }
  529. }
  530. for (std::size_t i = 0; i < _vctallitems.size(); ++i){
  531. auto itFind = std::find_if(sel_items.begin(), sel_items.end(), [i, &_vctallitems](std::pair<std::string, Json::Value>&it)->bool{
  532. return it.first == _vctallitems[i];
  533. });
  534. if (itFind != sel_items.end()){
  535. itFind->second["questionSerial"] = std::stoi(itFind->first);
  536. json_array.append(itFind->second);
  537. }
  538. else{
  539. double full = 0.0;
  540. std::string str_q_sql = "select * from question_std where question_code='";
  541. str_q_sql += _vctallitems[i] + "'";
  542. CppSQLite3Query q_full = bantch_db->execQuery(str_q_sql.c_str());
  543. while (!q_full.eof()){
  544. full = q_full.getFloatField("score_full");
  545. q_full.nextRow();
  546. }
  547. Json::Value item;
  548. item["fullScore"] = full;
  549. item["handlerIndex"] = 0;
  550. item["paperId"] = "";
  551. item["questionNum"] = _vctallitems[i];
  552. item["questionSerial"] = std::stoi(_vctallitems[i]);
  553. if (g_isAllSubject)
  554. {
  555. item["basic_type_id"] = q_full.getStringField("basic_type_id");
  556. }
  557. item["questionTypeId"] = 17;
  558. item["reportFlag"] = 1;
  559. item["rightFlag"] = 0;
  560. item["score"] = 0;
  561. item["answer"] = "";
  562. item["studentCode"] = stu_code;
  563. json_array.append(item);
  564. }
  565. }
  566. json["questionList"] = json_array;
  567. if (g_isAllSubject)
  568. {
  569. json["isAllSubject"] = 1;
  570. }
  571. else
  572. {
  573. json["isAllSubject"] = 0;
  574. }
  575. json_str = writer.write(json);
  576. DWORD wide_string_len = MultiByteToWideChar(CP_ACP, 0, json_str.c_str(), -1, NULL, 0);
  577. WCHAR* wide_string = new WCHAR[wide_string_len];
  578. wide_string_len = MultiByteToWideChar(CP_ACP, 0, json_str.c_str(), -1, wide_string, wide_string_len);
  579. DWORD utf8_string_len = WideCharToMultiByte(CP_UTF8, 0, wide_string, -1, NULL, 0, NULL, NULL);
  580. CHAR* utf8_string = new CHAR[utf8_string_len];
  581. utf8_string_len = WideCharToMultiByte(CP_UTF8, 0, wide_string, -1, utf8_string, utf8_string_len, NULL, NULL);
  582. zip.Write((void*)utf8_string, utf8_string_len - 1);
  583. delete wide_string;
  584. delete utf8_string;
  585. zip.Close();
  586. Json::Value json1;
  587. json1["paperStr"] = q.getStringField("paper_code");
  588. json1["studentCode"] = q.getStringField("student_id");
  589. json_ori_str = writer.write(json1);
  590. }
  591. else
  592. {
  593. CString jsonPath;
  594. jsonPath.Format(L"%s\\%s%d_json.txt", tempUpLoadPathChild, GetTimeString(), nRecordIndex);
  595. //jsonPath = tempUpLoadPathChild + _T("\\") + GetTimeString() + _T("_json.txt");
  596. WideCharToMultiByte(CP_ACP, 0, jsonPath, -1, sz_file_name, 520, NULL, NULL);
  597. CMemFile file;
  598. //CMemZipFile zip;
  599. CreateDirectory(tempUpLoadPathChild, NULL);
  600. CFile zip;
  601. zip.Open(jsonPath, CFile::modeCreate | CFile::modeWrite);
  602. Json::FastWriter writer;
  603. last_student_paper_id = q.getIntField("student_paper_id");
  604. paperids.push_back(q.getIntField("student_paper_id"));
  605. rows++;
  606. CString entryName;
  607. entryName.Format(_T("result%d.json"), rows);
  608. //zip.PutNextEntry(entryName);
  609. //json["schoolId"] = (double)q.getInt64Field("school_id");
  610. //json["examCourseId"] = (double)q.getInt64Field("exam_course_id");
  611. json["onlineCard"] = 1;
  612. json["paperStr"] = q.getStringField("paper_code");
  613. //json["examId"] = (double)q.getInt64Field("exam_id");
  614. //json["examineeId"] = q.getStringField("student_id");
  615. //json["recexamineeCode"] = q.getStringField("student_code");
  616. json["studentCode"] = q.getStringField("student_id");
  617. Json::Value json1;
  618. json1["paperStr"] = q.getStringField("paper_code");
  619. json1["studentCode"] = q.getStringField("student_id");
  620. json_ori_str = writer.write(json1);
  621. json["picFlag"] = q.getStringField("picFlag");
  622. CString strStudentCode = CString(q.getStringField("student_id"));
  623. //json["missingFlag"] = q.getIntField("absent");//缺考标记 1缺考
  624. //json["abType"] = 1;//试卷类型 (1,A卷;2,B卷)
  625. //json["scanType"] = 1;//考生识别方式(填涂0,条码1,手写2)
  626. Json::Value json_array(Json::arrayValue);
  627. Json::Value json_array2(Json::arrayValue);
  628. Json::Value itemQuestionSelects;
  629. Json::Value json_array3QuestionSelects(Json::arrayValue);
  630. stmt2.reset();
  631. stmt2.bind(":student_paper_id", q.getIntField("student_paper_id"));
  632. paperID = q.getIntField("student_paper_id");
  633. auto fGetQuestionType = [](RESULT_TYPE nType)->int{
  634. switch (nType){
  635. case RT_KEGUANTI: return 1; break; // 单选题
  636. case RT_KEGUANTI_EX: return 2; break; // 多选题
  637. case RT_TIANKONGTI: return 5; break; // 填空题
  638. case RT_ZHUGUANTI: return 7; break; // 主观题
  639. case RT_XUANZUOTI: return 17; break; // 选做题
  640. case RT_BIZUOTI:return 27; break; // 必做题
  641. }
  642. return -1;
  643. };
  644. Json::Value json_array3(Json::arrayValue);
  645. std::vector<std::string> _vctout;
  646. std::vector<std::string> _vctallitems;
  647. std::vector<std::pair<std::string, Json::Value>> sel_items;
  648. std::string stu_code = "";
  649. if (!(q.getIntField("absent") == 1)){
  650. CppSQLite3Query q2 = stmt2.execQuery();
  651. int questionNum = 0;
  652. while (!q2.eof()){
  653. RESULT_TYPE _type = (RESULT_TYPE)q2.getIntField("type");
  654. if (_type == 0 || _type == 1){
  655. q2.nextRow();
  656. continue;
  657. }
  658. int type_id = fGetQuestionType(_type);
  659. Json::Value item;
  660. item["fullScore"] = q2.getFloatField("score_full");
  661. item["handlerIndex"] = 1;
  662. item["paperId"] = "";
  663. item["questionNum"] = q2.getStringField("question_code");
  664. item["questionSerial"] = ++questionNum;
  665. item["questionTypeId"] = type_id;
  666. if (g_isAllSubject)
  667. {
  668. item["basic_type_id"] = q2.getStringField("basic_type_id");
  669. if (q2.getStringField("basic_type_id") == "12")//判断题
  670. {
  671. item["questionTypeId"] = "3";
  672. }
  673. }
  674. item["reportFlag"] = 1;
  675. item["rightFlag"] = q2.getIntField("isright");
  676. item["score"] = q2.getFloatField("score");
  677. stu_code = q.getStringField("student_id");
  678. item["studentCode"] = stu_code;
  679. std::string answer = q2.getStringField("answer");
  680. if (type_id == 17){
  681. _vctout.clear();
  682. split(answer, (std::string)"@", &_vctout);
  683. if (_vctout.size() == 5){
  684. item["answer"] = _vctout[1];
  685. bool flagfind = false;
  686. for (std::size_t i = 0; i < _vctallitems.size(); ++i){
  687. if (_vctallitems[i] == _vctout[4])
  688. {
  689. flagfind = true;
  690. break;
  691. }
  692. }
  693. if (!flagfind)
  694. {
  695. if (_vctallitems.empty())
  696. {
  697. split(_vctout[0], (std::string)",", &_vctallitems);
  698. if (json_array3QuestionSelects.size() > 0)
  699. {
  700. itemQuestionSelects["qids"] = json_array3QuestionSelects;
  701. json_array2.append(itemQuestionSelects);
  702. }
  703. }
  704. else
  705. {
  706. std::vector<std::string> _vctallitemstemp;
  707. split(_vctout[0], (std::string)",", &_vctallitemstemp);
  708. if (_vctallitemstemp[0] != _vctallitems[0])
  709. {
  710. for (std::size_t i = 0; i < _vctallitems.size(); ++i){
  711. auto itFind = std::find_if(sel_items.begin(), sel_items.end(), [i, &_vctallitems](std::pair<std::string, Json::Value>&it)->bool{
  712. return it.first == _vctallitems[i];
  713. });
  714. if (itFind != sel_items.end()){
  715. itFind->second["questionSerial"] = std::stoi(itFind->first);
  716. json_array3.append(itFind->first);
  717. json_array.append(itFind->second);
  718. }
  719. else{
  720. double full = 0.0;
  721. std::string str_q_sql = "select * from question_std where question_code='";
  722. str_q_sql += _vctallitems[i] + "'";
  723. CppSQLite3Query q_full = bantch_db->execQuery(str_q_sql.c_str());
  724. while (!q_full.eof()){
  725. full = q_full.getFloatField("score_full");
  726. q_full.nextRow();
  727. }
  728. Json::Value item;
  729. item["fullScore"] = full;
  730. item["handlerIndex"] = 0;
  731. item["paperId"] = "";
  732. item["questionNum"] = _vctallitems[i];
  733. item["questionSerial"] = std::stoi(_vctallitems[i]);
  734. if (g_isAllSubject)
  735. {
  736. item["basic_type_id"] = q2.getStringField("basic_type_id");
  737. }
  738. item["questionTypeId"] = 17;
  739. item["reportFlag"] = 1;
  740. item["rightFlag"] = 0;
  741. item["score"] = 0;
  742. item["answer"] = "";
  743. item["stdAnswer"] = "";
  744. item["studentCode"] = stu_code;
  745. json_array.append(item);
  746. }
  747. }
  748. _vctallitems.clear();
  749. split(_vctout[0], (std::string)",", &_vctallitems);
  750. itemQuestionSelects["qids"] = json_array3QuestionSelects;
  751. json_array2.append(itemQuestionSelects);
  752. }
  753. }
  754. itemQuestionSelects.clear();
  755. json_array3QuestionSelects.clear();
  756. json_array3QuestionSelects.append(_vctout[4]);
  757. itemQuestionSelects["markUnit"] = questionNum;
  758. }
  759. else
  760. {
  761. json_array3QuestionSelects.append(_vctout[4]);
  762. }
  763. }
  764. item["questionNum"] = _vctout[4];
  765. item["stdAnswer"] = "";
  766. sel_items.push_back(std::make_pair(_vctout[4], item));
  767. }
  768. else if (type_id == 1 || type_id == 2)
  769. {
  770. item["stdAnswer"] = q2.getStringField("answer_std");
  771. item["answer"] = answer;
  772. json_array.append(item);
  773. }
  774. else if (type_id == 5 || type_id == 7)
  775. {
  776. item["stdAnswer"] = "";
  777. item["answer"] = answer;
  778. json_array.append(item);
  779. }
  780. q2.nextRow();
  781. //item["subExamCourseId"] = (double)q.getInt64Field("exam_course_id");
  782. }
  783. }
  784. //Json::Value json_array3(Json::arrayValue);
  785. for (std::size_t i = 0; i < _vctallitems.size(); ++i){
  786. auto itFind = std::find_if(sel_items.begin(), sel_items.end(), [i, &_vctallitems](std::pair<std::string, Json::Value>&it)->bool{
  787. return it.first == _vctallitems[i];
  788. });
  789. if (itFind != sel_items.end()){
  790. itFind->second["questionSerial"] = std::stoi(itFind->first);
  791. json_array3.append(itFind->first);
  792. json_array.append(itFind->second);
  793. }
  794. else{
  795. double full = 0.0;
  796. std::string str_q_sql = "select * from question_std where question_code='";
  797. str_q_sql += _vctallitems[i] + "'";
  798. CppSQLite3Query q_full = bantch_db->execQuery(str_q_sql.c_str());
  799. while (!q_full.eof()){
  800. full = q_full.getFloatField("score_full");
  801. q_full.nextRow();
  802. }
  803. Json::Value item;
  804. item["fullScore"] = full;
  805. item["handlerIndex"] = 0;
  806. item["paperId"] = "";
  807. item["questionNum"] = _vctallitems[i];
  808. item["questionSerial"] = std::stoi(_vctallitems[i]);
  809. if (g_isAllSubject)
  810. {
  811. item["basic_type_id"] = q_full.getStringField("basic_type_id");
  812. }
  813. item["questionTypeId"] = 17;
  814. item["reportFlag"] = 1;
  815. item["rightFlag"] = 0;
  816. item["score"] = 0;
  817. item["answer"] = "";
  818. item["stdAnswer"] = "";
  819. item["studentCode"] = stu_code;
  820. json_array.append(item);
  821. }
  822. }
  823. if (_vctallitems.size() > 0)
  824. {
  825. itemQuestionSelects["qids"] = json_array3QuestionSelects;
  826. json_array2.append(itemQuestionSelects);
  827. }
  828. json["questionList"] = json_array;
  829. if (g_isAllSubject)
  830. {
  831. json["isAllSubject"] = 1;
  832. }
  833. else
  834. {
  835. json["isAllSubject"] = 0;
  836. }
  837. json["questionSelects"] = json_array2;
  838. json_str = writer.write(json);
  839. DWORD wide_string_len = MultiByteToWideChar(CP_ACP, 0, json_str.c_str(), -1, NULL, 0);
  840. WCHAR* wide_string = new WCHAR[wide_string_len];
  841. wide_string_len = MultiByteToWideChar(CP_ACP, 0, json_str.c_str(), -1, wide_string, wide_string_len);
  842. DWORD utf8_string_len = WideCharToMultiByte(CP_UTF8, 0, wide_string, -1, NULL, 0, NULL, NULL);
  843. CHAR* utf8_string = new CHAR[utf8_string_len];
  844. utf8_string_len = WideCharToMultiByte(CP_UTF8, 0, wide_string, -1, utf8_string, utf8_string_len, NULL, NULL);
  845. zip.Write((void*)utf8_string, utf8_string_len - 1);
  846. // LOG4CPLUS_INFO_FMT(pTestLogger, LOG4CPLUS_TEXT("%s"), CString(json_str.c_str()));
  847. delete wide_string;
  848. delete utf8_string;
  849. zip.Close();
  850. }
  851. }
  852. else{
  853. if (g_my_scan_type == 1)
  854. {
  855. Json::FastWriter writer;
  856. last_student_paper_id = q.getIntField("student_paper_id");
  857. paperids.push_back(q.getIntField("student_paper_id"));
  858. rows++;
  859. Json::Value json;
  860. Json::Value json1;
  861. json["paperStr"] = q.getStringField("paper_code");
  862. json["studentCode"] = q.getStringField("student_id");
  863. json1["paperStr"] = q.getStringField("paper_code");
  864. json1["studentCode"] = q.getStringField("student_id");
  865. json_ori_str = writer.write(json1);
  866. CString strStudentCode = CString(q.getStringField("student_id"));
  867. Json::Value json_array(Json::arrayValue);
  868. Json::Value json_array2(Json::arrayValue);
  869. stmt2.reset();
  870. stmt2.bind(":student_paper_id", q.getIntField("student_paper_id"));
  871. paperID = q.getIntField("student_paper_id");
  872. if (!(q.getIntField("absent") == 1)){
  873. CppSQLite3Query q2 = stmt2.execQuery();
  874. while (!q2.eof()){
  875. if (q2.getIntField("question_state")&QSF_UNSELECT){//没有选择的题目
  876. q2.nextRow();
  877. continue;
  878. }
  879. Json::Value item;
  880. std::string strtempcha = q2.getStringField("question_code");
  881. std::string codechar = q2.getStringField("answer");
  882. int number = 0;
  883. number = (codechar.length() == 0) ? 0 : (codechar.at(0) - 'A');
  884. {
  885. switch (strtempcha.at(0))
  886. {
  887. case '1':
  888. temp[0] = number;
  889. break;
  890. case '2':
  891. temp[1] = number;
  892. break;
  893. case '3':
  894. temp[2] = number;
  895. break;
  896. case '4':
  897. temp[3] = number;
  898. break;
  899. }
  900. }
  901. json_array.append(item);
  902. q2.nextRow();
  903. }
  904. }
  905. json["total"] = temp[0] * 10 + temp[1];
  906. json["correct"] = temp[2] * 10 + temp[3];
  907. //json["examineeScores"] = json_array;
  908. json_str = writer.write(json);
  909. //
  910. }
  911. else if (g_my_scan_type == 2)
  912. {
  913. Json::FastWriter writer;
  914. last_student_paper_id = q.getIntField("student_paper_id");
  915. paperids.push_back(q.getIntField("student_paper_id"));
  916. rows++;
  917. Json::Value json;
  918. CString strStudentCode = CString(q.getStringField("student_id"));
  919. Json::Value json_array(Json::arrayValue);
  920. Json::Value json_array2(Json::arrayValue);
  921. stmt2.reset();
  922. stmt2.bind(":student_paper_id", q.getIntField("student_paper_id"));
  923. paperID = q.getIntField("student_paper_id");
  924. if (!(q.getIntField("absent") == 1)){
  925. CppSQLite3Query q2 = stmt2.execQuery();
  926. while (!q2.eof()){
  927. if (q2.getIntField("question_state")&QSF_UNSELECT){//没有选择的题目
  928. q2.nextRow();
  929. continue;
  930. }
  931. else
  932. {
  933. Json::Value item;
  934. item["questionNum"] = q2.getStringField("question_code");
  935. item["answer"] = q2.getStringField("answer");
  936. json_array.append(item);
  937. q2.nextRow();
  938. }
  939. }
  940. }
  941. json_str = writer.write(json_array);
  942. }
  943. else
  944. {
  945. CString jsonPath;
  946. jsonPath.Format(L"%s\\%s%d_json.txt", tempUpLoadPathChild, GetTimeString(), nRecordIndex);
  947. //jsonPath = tempUpLoadPathChild + _T("\\") + GetTimeString() + _T("_json.txt");
  948. WideCharToMultiByte(CP_ACP, 0, jsonPath, -1, sz_file_name, 520, NULL, NULL);
  949. CMemFile file;
  950. //CMemZipFile zip;
  951. CreateDirectory(tempUpLoadPathChild, NULL);
  952. CFile zip;
  953. zip.Open(jsonPath, CFile::modeCreate | CFile::modeWrite);
  954. Json::FastWriter writer;
  955. last_student_paper_id = q.getIntField("student_paper_id");
  956. paperids.push_back(q.getIntField("student_paper_id"));
  957. rows++;
  958. CString entryName;
  959. entryName.Format(_T("result%d.json"), rows);
  960. //zip.PutNextEntry(entryName);
  961. Json::Value json;
  962. //json["schoolId"] = (double)q.getInt64Field("school_id");
  963. //json["examCourseId"] = (double)q.getInt64Field("exam_course_id");
  964. json["onlineCard"] = 0;
  965. json["paperStr"] = q.getStringField("paper_code");
  966. //json["examId"] = (double)q.getInt64Field("exam_id");
  967. //json["examineeId"] = q.getStringField("student_id");
  968. //json["recexamineeCode"] = q.getStringField("student_code");
  969. json["studentCode"] = q.getStringField("student_id");
  970. Json::Value json1;
  971. json1["paperStr"] = q.getStringField("paper_code");
  972. json1["studentCode"] = q.getStringField("student_id");
  973. json_ori_str = writer.write(json1);
  974. json["picFlag"] = q.getStringField("picFlag");
  975. CString strStudentCode = CString(q.getStringField("student_id"));
  976. //json["missingFlag"] = q.getIntField("absent");//缺考标记 1缺考
  977. //json["abType"] = 1;//试卷类型 (1,A卷;2,B卷)
  978. //json["scanType"] = 1;//考生识别方式(填涂0,条码1,手写2)
  979. Json::Value json_array(Json::arrayValue);
  980. Json::Value json_array2(Json::arrayValue);
  981. stmt2.reset();
  982. stmt2.bind(":student_paper_id", q.getIntField("student_paper_id"));
  983. paperID = q.getIntField("student_paper_id");
  984. if (!(q.getIntField("absent") == 1)){
  985. CppSQLite3Query q2 = stmt2.execQuery();
  986. while (!q2.eof()){
  987. if (q2.getIntField("question_state")&QSF_UNSELECT){//没有选择的题目
  988. q2.nextRow();
  989. continue;
  990. }
  991. Json::Value item;
  992. //item["subExamCourseId"] = (double)q.getInt64Field("exam_course_id");
  993. item["questionNum"] = q2.getStringField("question_code");
  994. item["stdAnswer"] = q2.getStringField("answer_std");
  995. //item["type"] = (q2.getIntField("question_type") & 0xff);
  996. item["score"] = q2.getFloatField("score");
  997. item["answer"] = q2.getStringField("answer");
  998. json_array.append(item);
  999. q2.nextRow();
  1000. }
  1001. }
  1002. json["questionList"] = json_array;
  1003. //json["examineeScores"] = json_array;
  1004. stmt3.reset();
  1005. stmt3.bind(":student_paper_id", q.getIntField("student_paper_id"));
  1006. CppSQLite3Query q3 = stmt3.execQuery();
  1007. while (!q3.eof()){
  1008. Json::Value item;
  1009. item["markUnit"] = atoi(q3.getStringField("question_code"));
  1010. string answer = q3.getStringField("answer");
  1011. Json::Value json_array3(Json::arrayValue);
  1012. if (answer.length() > 0){
  1013. vector<string> questions;
  1014. split(answer, ",", &questions);
  1015. for (int i = 0; i < questions.size(); i++){
  1016. json_array3.append(questions[i]);
  1017. }
  1018. }
  1019. item["qids"] = json_array3;
  1020. json_array2.append(item);
  1021. q3.nextRow();
  1022. }
  1023. json["questionSelects"] = json_array2;
  1024. json_str = writer.write(json);
  1025. DWORD wide_string_len = MultiByteToWideChar(CP_ACP, 0, json_str.c_str(), -1, NULL, 0);
  1026. WCHAR* wide_string = new WCHAR[wide_string_len];
  1027. wide_string_len = MultiByteToWideChar(CP_ACP, 0, json_str.c_str(), -1, wide_string, wide_string_len);
  1028. DWORD utf8_string_len = WideCharToMultiByte(CP_UTF8, 0, wide_string, -1, NULL, 0, NULL, NULL);
  1029. CHAR* utf8_string = new CHAR[utf8_string_len];
  1030. utf8_string_len = WideCharToMultiByte(CP_UTF8, 0, wide_string, -1, utf8_string, utf8_string_len, NULL, NULL);
  1031. zip.Write((void*)utf8_string, utf8_string_len - 1);
  1032. // LOG4CPLUS_INFO_FMT(pTestLogger, LOG4CPLUS_TEXT("%s"), CString(json_str.c_str()));
  1033. delete wide_string;
  1034. delete utf8_string;
  1035. zip.Close();
  1036. }
  1037. }
  1038. // 图片压缩包解压
  1039. char mySql[512] = { 0 };
  1040. CString zipPath;
  1041. sprintf_s(mySql, "select file_name from student_paper where student_id = '%s' and student_paper_id = %d", q.getStringField("student_id"), paperID);
  1042. CppSQLite3Query query = bantch_db->execQuery(mySql);
  1043. if (query.eof())
  1044. {
  1045. if (nErrorCount > 0)
  1046. {
  1047. strErrorInfo += "\r\n";
  1048. }
  1049. strErrorInfo += q.getStringField("student_name");
  1050. strErrorInfo += ":查找图片失败";
  1051. nErrorCount++;
  1052. if (m_hwnd != NULL&&GetCurrentTime() - lastime > 500) {
  1053. lastime = GetCurrentTime();
  1054. int total_bantch_count = (m_total + 499) / 500;
  1055. int current_bantch = (m_current_count) / 500 + 1;
  1056. int current_count = (m_current_count - 1) % 500 + 1;
  1057. float progress = (int)(10000.0 / total_bantch_count)*(current_bantch - 1 + 0.5*current_count / min(500, m_total - (current_bantch - 1) * 500));
  1058. ::PostMessage(m_hwnd, WM_IDENTIFOR_RESULT_UPLOAD_PROGRESS, progress, 0);
  1059. }
  1060. EnterCriticalSection(db_lock);
  1061. bantch_db->execDML("begin transaction");
  1062. char sql6[512];
  1063. sprintf_s(sql6, "update student_paper set ret_upload = 3 where student_id = '%s'", q.getStringField("student_id"));
  1064. bantch_db->execDML(sql6);
  1065. bantch_db->execDML("commit transaction");
  1066. LeaveCriticalSection(db_lock);
  1067. m_current_count++;
  1068. m_failtrue = TRUE;
  1069. zipPath = "";
  1070. }
  1071. else
  1072. {
  1073. file_name = query.getStringField("file_name");
  1074. zipPath = CString(query.getStringField("file_name"));
  1075. if (PathFileExists(zipPath))
  1076. {
  1077. BOOL suc = ZipExtract(zipPath, imgPath);
  1078. if (!suc){
  1079. CreateDirectory(imgPath, NULL);
  1080. }
  1081. }
  1082. }
  1083. if (!m_bUploadOnScanModel){
  1084. //导入上传库
  1085. stmt4.reset();
  1086. stmt4.bind(":student_paper_id", last_student_paper_id);
  1087. stmt4.bind(":ret_upload", 0);
  1088. stmt4.bind(":paper_code", q.getStringField("paper_code"));
  1089. stmt4.bind(":student_code", q.getStringField("student_code"));
  1090. stmt4.bind(":student_id", q.getStringField("student_id"));
  1091. stmt4.bind(":file_name", query.getStringField("file_name"));
  1092. stmt4.bind(":ret_ori_upload", 0);
  1093. stmt4.bind(":ori_json", json_ori_str.c_str());
  1094. if (g_my_scan_type == 1 || g_my_scan_type == 2)
  1095. {
  1096. stmt4.bind(":json_name", json_str.c_str());
  1097. }
  1098. else
  1099. {
  1100. stmt4.bind(":json_name", sz_file_name);
  1101. }
  1102. stmt4.bind(":student_name", q.getStringField("student_name"));
  1103. stmt4.bind(":ori_img", q.getStringField("ori_img"));
  1104. stmt4.bind(":absent", q.getIntField("absent"));
  1105. stmt4.bind(":exc_upload", 0);
  1106. stmt4.bind(":exception_json","normal");
  1107. stmt4.bind(":update_post_data", "nodata");
  1108. if (m_bErrorHandle){
  1109. auto examid = (unsigned long long)q.getInt64Field("exam_id");
  1110. std::string post_data = std::string("{ \"post_data\": \"examGroupId=") + std::to_string(examid) + "&taskIds=";
  1111. post_data += std::string() + q.getStringField("task_id") + "\",\"url\":\"";
  1112. TCHAR exeFullPath[MAX_PATH];
  1113. CString strPath;
  1114. GetModuleFileName(NULL, exeFullPath, MAX_PATH);
  1115. strPath = (CString)exeFullPath;
  1116. int position = strPath.ReverseFind('\\');
  1117. strPath = strPath.Left(position + 1);
  1118. TCHAR FilePath[MAX_PATH];
  1119. GetModuleFileName(NULL, FilePath, MAX_PATH);
  1120. (_tcsrchr(FilePath, '\\'))[1] = 0;
  1121. lstrcat(FilePath, _T("paperinfo.ini"));
  1122. service_type = GetPrivateProfileInt(_T("paperinfo"), _T("servicetype"), 0, FilePath);
  1123. WCHAR url[512] = { 0 };
  1124. GetPrivateProfileString(_T("paperinfo"), _T("errorhandleupdateurl"), _T(""), url, 512, FilePath);
  1125. post_data += UnicodeToAnsi(url) + "\"}";
  1126. stmt4.bind(":update_post_data", post_data.c_str());
  1127. }
  1128. }
  1129. if ((paper_state&MEX_BUKESHANGCHUANG) != 0)
  1130. {
  1131. CString exceptionTmp = CServerConfig::app_data_dir + _T("\\exception_tmp\\");
  1132. DeleteDirectory(exceptionTmp);
  1133. CreateDirectory(exceptionTmp, NULL);
  1134. Json::Value json;
  1135. // 获取student_paper内容
  1136. std::string student_paper_sql = "select * from student_paper where student_paper_id=";
  1137. student_paper_sql += std::to_string(last_student_paper_id);
  1138. CppSQLite3Query stu_paper_qry = bantch_db->execQuery(student_paper_sql.c_str());
  1139. Json::Value json_stu_paper;
  1140. if (!stu_paper_qry.eof()){
  1141. json_stu_paper["picFlag"] = stu_paper_qry.getStringField("picFlag");
  1142. json_stu_paper["location_id"] = stu_paper_qry.getIntField("location_id");
  1143. json_stu_paper["paper_type"] = stu_paper_qry.getStringField("paper_type");
  1144. json_stu_paper["absent"] = stu_paper_qry.getIntField("absent");
  1145. json_stu_paper["score_total"] = stu_paper_qry.getFloatField("score_total");
  1146. json_stu_paper["student_id"] = stu_paper_qry.getStringField("student_id");
  1147. json_stu_paper["paper_state"] = stu_paper_qry.getIntField("paper_state");
  1148. json_stu_paper["student_code"] = stu_paper_qry.getStringField("student_code");
  1149. json_stu_paper["paper_code"] = stu_paper_qry.getStringField("paper_code");
  1150. }
  1151. // 获取student内容
  1152. std::string stu_id = q.getStringField("student_id");
  1153. Json::Value json_stu;
  1154. if (!stu_id.empty() && stu_id != "-1"){
  1155. std::string student_sql = "select * from student where student_id='";
  1156. student_sql += stu_id + "'";
  1157. CppSQLite3Query stu_qry = bantch_db->execQuery(student_sql.c_str());
  1158. if (!stu_qry.eof()){
  1159. json_stu["studentCard"] = stu_qry.getStringField("studentCard");
  1160. json_stu["recoverFlag"] = stu_qry.getIntField("recoverFlag");
  1161. json_stu["student_id"] = stu_qry.getStringField("student_id");
  1162. json_stu["student_code"] = stu_qry.getStringField("student_code");
  1163. json_stu["class_name"] = stu_qry.getStringField("class_name");
  1164. json_stu["school_id"] = std::to_string(stu_qry.getIntField("school_id"));
  1165. json_stu["grade_name"] = stu_qry.getStringField("grade_name");
  1166. json_stu["student_name"] = stu_qry.getStringField("student_name");
  1167. json_stu["class_id"] = std::to_string(stu_qry.getInt64Field("class_id"));
  1168. json_stu["schoolStudentCard"] = stu_qry.getStringField("schoolStudentCard");
  1169. json_stu["score_total"] = stu_qry.getFloatField("score_total");
  1170. }
  1171. }
  1172. // 获取原卷 获取答卷
  1173. Json::Value json_array_page(Json::arrayValue);
  1174. std::string page_sql = "select img_oldpath,img_path ,identified,page_id from page where student_paper_id=";
  1175. page_sql += std::to_string(last_student_paper_id);
  1176. CppSQLite3Query page_qry = bantch_db->execQuery(page_sql.c_str());
  1177. int i = 1;
  1178. while (!page_qry.eof()){
  1179. std::string name = std::to_string(i++) + ".jpg";
  1180. std::string new_file_name = CServerConfig::app_data_dir_string + std::string("\\exception_tmp\\image_") + name;
  1181. std::string path = page_qry.getStringField("img_path");
  1182. int page_id = page_qry.getIntField("page_id");
  1183. int identified = page_qry.getIntField("identified");
  1184. CopyFileA(path.c_str(), new_file_name.c_str(), FALSE);
  1185. Json::Value item;
  1186. item["name"] = name.c_str();
  1187. item["identified"] = identified;
  1188. item["page_id"] = page_id;
  1189. json_array_page.append(item);
  1190. page_qry.nextRow();
  1191. }
  1192. std::string rst_path = query.getStringField("file_name");
  1193. if (!rst_path.empty()){
  1194. /*
  1195. std::string new_rst_path = std::string("D:\\exception_tmp\\rst") + ".zip";
  1196. CopyFileA(rst_path.c_str(), new_rst_path.c_str(), FALSE);
  1197. json["restult_file"] = "rst.zip";
  1198. */
  1199. CString UplaodPath;
  1200. UplaodPath = CString(rst_path.c_str());
  1201. UplaodPath = UplaodPath.Mid(0, UplaodPath.GetLength() - 4);
  1202. rst_path = CT2A(UplaodPath);
  1203. std::string new_rst_path = CServerConfig::app_data_dir_string + std::string("\\exception_tmp\\rst\\");
  1204. CString rstPath = CServerConfig::app_data_dir + _T("\\exception_tmp\\rst\\");
  1205. CreateDirectory(rstPath, NULL);
  1206. //CopyFileA(rst_path.c_str(), new_rst_path.c_str(), FALSE);
  1207. CopyAllFile(CString(new_rst_path.c_str()), CString(rst_path.c_str()));
  1208. json["restult_file"] = "rst";
  1209. }
  1210. else{
  1211. json["rst_file"] = "";
  1212. }
  1213. // 获取异常
  1214. std::string ex_sql = "select * from exception where student_paper_id = ";
  1215. ex_sql += std::to_string(last_student_paper_id);
  1216. CppSQLite3Query ex_qry = bantch_db->execQuery(ex_sql.c_str());
  1217. Json::Value json_array_ex(Json::arrayValue);
  1218. while (!ex_qry.eof()){
  1219. int exception_id = ex_qry.getIntField("exception_id");
  1220. int exception_type = ex_qry.getIntField("exception_type");
  1221. std::string exception_name = ex_qry.getStringField("exception_name");
  1222. Json::Value item;
  1223. item["exception_id"] = exception_id;
  1224. item["exception_type"] = exception_type;
  1225. item["exception_name"] = exception_name;
  1226. if (!ex_qry.fieldIsNull("page_id")){
  1227. int page_id = ex_qry.getIntField("page_id");
  1228. item["page_id"] = page_id;
  1229. }
  1230. json_array_ex.append(item);
  1231. ex_qry.nextRow();
  1232. }
  1233. // 获取识别结果
  1234. std::string rst_sql = "select * from result where student_paper_id = ";
  1235. rst_sql += std::to_string(last_student_paper_id);
  1236. CppSQLite3Query rst_qry = bantch_db->execQuery(rst_sql.c_str());
  1237. Json::Value json_array_rst(Json::arrayValue);
  1238. while (!rst_qry.eof()){
  1239. int result_id = rst_qry.getIntField("result_id");
  1240. int page_id = rst_qry.getIntField("page_id");
  1241. int type = rst_qry.getIntField("type");
  1242. std::string answer = rst_qry.getStringField("answer");
  1243. float score = rst_qry.getFloatField("score");
  1244. std::string question_code = rst_qry.getStringField("question_code");
  1245. float score_paper = rst_qry.getFloatField("score_paper");
  1246. int question_state = rst_qry.getIntField("question_state");
  1247. bool isright = rst_qry.getIntField("isright");
  1248. Json::Value item;
  1249. item["result_id"] = result_id;
  1250. item["page_id"] = page_id;
  1251. item["type"] = type;
  1252. item["answer"] = answer;
  1253. item["score"] = score;
  1254. item["question_code"] = question_code;
  1255. item["score_paper"] = score_paper;
  1256. item["question_state"] = question_state;
  1257. item["isright"] = isright;
  1258. json_array_rst.append(item);
  1259. rst_qry.nextRow();
  1260. }
  1261. json["pages"] = json_array_page;
  1262. json["student_paper"] = json_stu_paper;
  1263. json["exceptions"] = json_array_ex;
  1264. json["result"] = json_array_rst;
  1265. json["student_paper_id"] = last_student_paper_id;
  1266. json["student"] = json_stu;
  1267. Json::FastWriter writer;
  1268. exception_json = writer.write(json);
  1269. std::ofstream ofs;
  1270. std::string json_path = CServerConfig::app_data_dir_string + "\\exception_tmp\\json.txt";
  1271. ofs.open(json_path, ios::out);
  1272. ofs << exception_json;
  1273. ofs.close();
  1274. CString str_ucoud_path = CServerConfig::app_data_dir + _T("\\temp_ucloud_path\\exception_ucloud.zip");
  1275. ZipCompress(CServerConfig::app_data_dir + _T("\\exception_tmp\\*.*"), str_ucoud_path);
  1276. // 调用云上传
  1277. std::string ucloud_key = get_ucloud_key(stu_paper_qry.getStringField("paper_code"));
  1278. std::string exceptionZipPath = CServerConfig::app_data_dir_string + "\\temp_ucloud_path\\exception_ucloud.zip";
  1279. auto error = ufile_put_file_by_filepath(bucket_name.c_str(), ucloud_key.c_str(), "", exceptionZipPath.c_str());
  1280. if UFILE_HAS_ERROR(error.code) {
  1281. }
  1282. else{
  1283. char szucloud_domain[512] = { 0 };
  1284. char YunService[128] = { 0 };
  1285. std::string str_path_tmp = UnicodeToAnsi(GetExePath() + _T("\\config.ini"));
  1286. const char* FilePath = str_path_tmp.c_str();
  1287. GetPrivateProfileStringA("USER", "YunService", "UCLOUD", YunService, sizeof(YunService) / sizeof(char), FilePath);
  1288. if (strcmp(YunService, "UCLOUD") == 0){
  1289. GetPrivateProfileStringA("GLOBAL", "ucloud_domain", "http://zxhx-test.cn-bj.ufileos.com/", szucloud_domain, 512, FilePath);
  1290. }
  1291. else{
  1292. char url[512];
  1293. GetPrivateProfileStringA("GLOBAL", "server_url", "http://127.0.0.1:8080/reportTask", url, 512, FilePath);
  1294. bool isTestEnv = false;
  1295. if (strstr(url, "test") != NULL || strstr(url, "dev") != NULL){
  1296. isTestEnv = true;
  1297. }
  1298. if (isTestEnv){
  1299. GetPrivateProfileStringA("GLOBAL", "ucloud_domain", "http://zxhx-1302712961.cos.ap-shanghai.myqcloud.com/", szucloud_domain, 512, FilePath);
  1300. }
  1301. else{
  1302. GetPrivateProfileStringA("GLOBAL", "ucloud_domain", "http://zxhx-pro-1302712961.cos.ap-beijing.myqcloud.com/", szucloud_domain, 512, FilePath);
  1303. }
  1304. }
  1305. std::string ucloud_url = std::string(szucloud_domain) + ucloud_key;
  1306. LOGFMTI("ucloud_url=%s", ucloud_url.c_str());
  1307. /* HttpDownloadTemplate(ucloud_url, "d:\\11111.zip");*/
  1308. //调用 put 成功;
  1309. Json::Value exception_json_value;
  1310. exception_json_value["cloud_key"] = ucloud_url;
  1311. //exception_json_value["cloud_url"] = ucloud_url;
  1312. exception_json = writer.write(exception_json_value);
  1313. }
  1314. if (!m_bUploadOnScanModel){
  1315. stmt4.bind(":exc_upload", 0);
  1316. stmt4.bind(":service_type", service_type);
  1317. stmt4.bind(":exception_json", exception_json.c_str());
  1318. }
  1319. }
  1320. if (m_bUploadOnScanModel){
  1321. rapidjson::Document doc;
  1322. doc.SetObject();
  1323. rapidjson::Document::AllocatorType& allocator = doc.GetAllocator();
  1324. // 数据头
  1325. AddStringMember(doc, allocator, "net_task_type", "upload_on_scan_upload");
  1326. AddStringMember(doc, allocator, "net_task_cmd", "upload_on_scan_add_upload_task");
  1327. rapidjson::Value _data; _data.SetObject();
  1328. // 数据体
  1329. AddStringMember(_data, allocator, "bantch_code", UnicodeToAnsi(m_batch_code).c_str());
  1330. AddStringMember(_data, allocator, "exam_id", std::to_string(q.getInt64Field("exam_id")).c_str());
  1331. AddNumberMember(_data, allocator, "student_paper_id", last_student_paper_id, 0);
  1332. AddNumberMember(_data, allocator, "ret_upload", 0, 0);
  1333. //导入上传库
  1334. AddStringMember(_data, allocator, "paper_code", q.getStringField("paper_code"));
  1335. AddStringMember(_data, allocator, "student_code", q.getStringField("student_code"));
  1336. AddStringMember(_data, allocator, "student_id", q.getStringField("student_id"));
  1337. AddStringMember(_data, allocator, "file_name", file_name.c_str());
  1338. AddNumberMember(_data, allocator, "ret_ori_upload", 0, 0);
  1339. AddStringMember(_data, allocator, "ori_json", json_ori_str.c_str());
  1340. if (g_my_scan_type == 1 || g_my_scan_type == 2){
  1341. AddStringMember(_data, allocator, "json_name", json_str.c_str());
  1342. }
  1343. else{
  1344. AddStringMember(_data, allocator, "json_name", sz_file_name);
  1345. }
  1346. AddStringMember(_data, allocator, "student_name", q.getStringField("student_name"));
  1347. AddStringMember(_data, allocator, "ori_img", q.getStringField("ori_img"));
  1348. AddNumberMember(_data, allocator, "absent", q.getIntField("absent"), 0);
  1349. AddNumberMember(_data, allocator, "exc_upload", 0, 0);
  1350. AddNumberMember(_data, allocator, "service_type", service_type, 0);
  1351. if ((paper_state&MEX_BUKESHANGCHUANG) != 0){
  1352. AddNumberMember(_data, allocator, "exc_upload", 0, 0);
  1353. AddStringMember(_data, allocator, "exception_json", exception_json.c_str());
  1354. }
  1355. else{
  1356. AddStringMember(_data, allocator, "exception_json", "normal");
  1357. AddStringMember(_data, allocator, "update_post_data", "nodata");
  1358. }
  1359. doc.AddMember("data", _data, allocator);
  1360. rapidjson::StringBuffer buffer;
  1361. rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
  1362. doc.Accept(writer);
  1363. std::string reststring = buffer.GetString();
  1364. // rapidjson::Document __doc;
  1365. // __doc.Parse(reststring.c_str());
  1366. // if (!__doc.HasParseError()){
  1367. // std::string str = __doc["data"]["json_name"].GetString();
  1368. // int id = __doc["data"]["student_paper_id"].GetInt();
  1369. // rapidjson::Document _doc;
  1370. // _doc.Parse(str.c_str());
  1371. // if (!_doc.HasParseError())
  1372. // {
  1373. // int n = 0;
  1374. // ++n;
  1375. // }
  1376. // }
  1377. int nRet = -20;
  1378. std::string sql_qry_paper_state = "select paper_state from student_paper where student_paper_id=";
  1379. sql_qry_paper_state += std::to_string(last_student_paper_id);
  1380. if (db_lock &&bantch_db && bantch_db->is_open()){
  1381. ::EnterCriticalSection(db_lock);
  1382. auto paper_state_qry = bantch_db->execQuery(sql_qry_paper_state.c_str());
  1383. if (!paper_state_qry.eof()){
  1384. paper_state = paper_state_qry.getIntField("paper_state");
  1385. }
  1386. ::LeaveCriticalSection(db_lock);
  1387. LOGFMTI("试卷ID=%d 试卷状态:%d", last_student_paper_id, paper_state);
  1388. ::EnterCriticalSection(&m_csUploadOnScanPapers);
  1389. auto it_find = m_vctUploadOnScanPapers.find(last_student_paper_id);
  1390. if (it_find != m_vctUploadOnScanPapers.end() && it_find->second == (paper_state != 0)){
  1391. LOGD(std::string("向知心慧学端发送上传任务 data=") + reststring);
  1392. nRet = NetOperator::ClientSendData(reststring.c_str());
  1393. if (nRet < 0){
  1394. LOGFMTD("向知心慧学端发送上传任务-失败 删除目录:%s", UnicodeToAnsi(imgPath).c_str());
  1395. DeleteDirectory(imgPath);
  1396. }
  1397. else{
  1398. m_vctUploadOnScanPapers.erase(it_find);
  1399. LOGD("向知心慧学端发送上传任务-成功");
  1400. }
  1401. }
  1402. ::LeaveCriticalSection(&m_csUploadOnScanPapers);
  1403. }
  1404. Sleep(5);
  1405. }
  1406. else{
  1407. stmt4.execDML();
  1408. //修改成功标识
  1409. EnterCriticalSection(db_lock);
  1410. bantch_db->execDML("begin transaction");
  1411. char sql6[512];
  1412. sprintf_s(sql6, "update student_paper set ret_upload = 2 where student_paper_id = %d", last_student_paper_id);
  1413. bantch_db->execDML(sql6);
  1414. bantch_db->execDML("commit transaction");
  1415. LeaveCriticalSection(db_lock);
  1416. }
  1417. q.nextRow();
  1418. }
  1419. if (m_bUploadOnScanModel){
  1420. ServiceState _state = stoping;
  1421. ::EnterCriticalSection(&m_csUploadOnScanPapers);
  1422. if (!m_vctUploadOnScanPapers.empty())
  1423. _state = running;
  1424. ::LeaveCriticalSection(&m_csUploadOnScanPapers);
  1425. return _state;
  1426. }
  1427. else{
  1428. if (m_hwnd != NULL) {
  1429. int total_bantch_count = (m_total + 499) / 500;
  1430. int current_bantch = (m_current_count) / 500 + 1;
  1431. int current_count = (m_current_count - 1) % 500 + 1;
  1432. float progress = (int)(10000.0 / total_bantch_count)*(current_bantch - 1 + 0.8);
  1433. ::PostMessage(m_hwnd, WM_IDENTIFOR_RESULT_UPLOAD_PROGRESS, progress, 0);
  1434. }
  1435. }
  1436. }
  1437. if (rows == 0){
  1438. if (m_result_hadler == NULL || m_result_hadler->GetServiceSate() == stoped){
  1439. Stop();
  1440. }
  1441. else{
  1442. Sleep(1000);
  1443. }
  1444. }
  1445. return running;
  1446. }
  1447. void CResultUploader::SetUploadOnScan(bool bUploadOnScan, int student_paper_id, bool bErrorPaper)
  1448. {
  1449. m_bUploadOnScanModel = bUploadOnScan;
  1450. if (student_paper_id >0 && bUploadOnScan){
  1451. ::EnterCriticalSection(&m_csUploadOnScanPapers);
  1452. auto it_find = m_vctUploadOnScanPapers.find(student_paper_id);
  1453. if (it_find == m_vctUploadOnScanPapers.end()){
  1454. m_vctUploadOnScanPapers.insert(std::make_pair(student_paper_id, bErrorPaper));
  1455. }
  1456. ::LeaveCriticalSection(&m_csUploadOnScanPapers);
  1457. }
  1458. }
  1459. CString UnicodeToChinese(const std::string str)
  1460. {
  1461. CString cstr;
  1462. int i = 0;
  1463. int j = 0;
  1464. int len = 0;
  1465. len = str.length();
  1466. if (len <= 0){
  1467. return false;
  1468. }
  1469. int nValue = 0;
  1470. WCHAR * pWchar;
  1471. wchar_t* szHex;
  1472. char strchar[6] = { '0', 'x', '\0' };
  1473. for (i = 0; i < len; i++)
  1474. {
  1475. if (str[i] == 'x'&& i>0 && str[i - 1] == '\\')
  1476. {
  1477. for (j = 2;str[i] != '\\'&& i < len; j++)
  1478. {
  1479. i++;
  1480. strchar[j] = str[i];
  1481. }
  1482. USES_CONVERSION;
  1483. szHex = A2W(strchar);
  1484. StrToIntExW(szHex, STIF_SUPPORT_HEX, &nValue);
  1485. pWchar = (WCHAR*)& nValue;
  1486. cstr = cstr + pWchar;
  1487. }
  1488. else if (str[i] != '\\'){
  1489. cstr += str[i];
  1490. }
  1491. }
  1492. return cstr;
  1493. }
  1494. //从文件读入到string里
  1495. std::string readFileIntoString(const std::string& filename)
  1496. {
  1497. ifstream ifile(filename);
  1498. //将文件读入到ostringstream对象buf中
  1499. ostringstream buf;
  1500. char ch;
  1501. while (buf&&ifile.get(ch))
  1502. buf.put(ch);
  1503. //返回与流对象buf关联的字符串
  1504. return buf.str();
  1505. }
  1506. wstring strUTF8toW(const string& src)
  1507. {
  1508. wstring result = L"";
  1509. int n = MultiByteToWideChar(CP_UTF8, 0, src.c_str(), -1, NULL, 0);
  1510. result.resize(n);
  1511. ::MultiByteToWideChar(CP_UTF8, 0, src.c_str(), -1, (LPWSTR)result.c_str(), result.length());
  1512. return result;
  1513. }
  1514. CString AnsiToUnicode(const std::string& str);
  1515. std::string UnicodeToAnsi(const CString& str);
  1516. #include "rapidjson/document.h"
  1517. ServiceState CResultUploader::OnStoping(void)
  1518. {
  1519. // 边扫描边上传
  1520. if (m_bUploadOnScanModel){
  1521. LOGI(" m_bUploadOnScanModel 开始上传成绩结束");
  1522. return IService::OnStoping();
  1523. }
  1524. LOGI("开始上传成绩结束");
  1525. upload_database_db.close();
  1526. if (!m_failtrue){
  1527. char sql[512] = { 0 };
  1528. 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);
  1529. int total_count = bantch_db->execScalar(sql);
  1530. sprintf_s(sql, "update database set database_state =%d,total_upload_count=%d where database_id =%d", DATABASE_STATE_RESULT_UPLOADED, total_count, database_id);
  1531. EnterCriticalSection(database_db_lock);
  1532. database_db->execDML("begin transaction");
  1533. database_db->execDML(sql);
  1534. database_db->execDML("commit transaction");
  1535. LeaveCriticalSection(database_db_lock);
  1536. }
  1537. {
  1538. //通信
  1539. CWnd *pWnd = CWnd::FindWindow(NULL, _T("知心慧学学管系统"));
  1540. if (pWnd == NULL)
  1541. {
  1542. UploadMsg* msg = new UploadMsg;
  1543. msg->strMsg = m_strMsg;
  1544. msg->nTotal = (m_total << 16) + m_sccuss_count;
  1545. ::PostMessage(m_hwnd, WM_IDENTIFOR_RESULT_UPLOAD_COMPLETED, FALSE, (LPARAM)msg);
  1546. return IService::OnStoping();
  1547. }
  1548. //发送消息
  1549. MY_DB_INFO fff;
  1550. int len = 1024;
  1551. fff.nmode = 0;
  1552. memset(&fff, 0, sizeof(MY_DB_INFO));
  1553. strncpy(fff.sDbBasePath, g_strUpLoadDbPathName.c_str(), g_strUpLoadDbPathName.length() + 1);
  1554. getupLoadUrl(fff.sUrl, len);
  1555. int service_type = 0;
  1556. TCHAR exeFullPath[MAX_PATH];
  1557. CString strPath;
  1558. GetModuleFileName(NULL, exeFullPath, MAX_PATH);
  1559. strPath = (CString)exeFullPath;
  1560. int position = strPath.ReverseFind('\\');
  1561. strPath = strPath.Left(position + 1);
  1562. TCHAR FilePath[MAX_PATH];
  1563. GetModuleFileName(NULL, FilePath, MAX_PATH);
  1564. {
  1565. TCHAR exam_name[256] = { 0 };
  1566. (_tcsrchr(FilePath, '\\'))[1] = 0;
  1567. lstrcat(FilePath, _T("config.ini"));
  1568. GetPrivateProfileString(_T("THIRD"), _T("exam_name"), _T(""), exam_name, sizeof(exam_name) / sizeof(TCHAR), FilePath);
  1569. sprintf_s(fff.sExamName, "%s", UnicodeToAnsi(exam_name).c_str());
  1570. }
  1571. (_tcsrchr(FilePath, '\\'))[1] = 0;
  1572. lstrcat(FilePath, _T("paperinfo.ini"));
  1573. service_type = GetPrivateProfileInt(_T("paperinfo"), _T("servicetype"), 0, FilePath);
  1574. WCHAR szSubjectID[512] = { 0 };
  1575. GetPrivateProfileString(_T("paperinfo"), _T("subject_id"), _T(""), szSubjectID, 512, FilePath);
  1576. sprintf_s(fff.sSubjectName, "%s", UnicodeToAnsi(szSubjectID).c_str());
  1577. if (m_bErrorHandle)
  1578. {
  1579. WCHAR URL[512] = { 0 };
  1580. GetPrivateProfileString(_T("paperinfo"), _T("uploadurl"), _T(""), URL, 512, FilePath);
  1581. CString tmp = URL;
  1582. if (tmp != _T("nourl")){
  1583. sprintf_s(fff.sabsentUrl, "%s/coachio/iointerface_v2/delPaperStudent", UnicodeToAnsi(tmp).c_str());
  1584. WCHAR session[512] = { 0 };
  1585. GetPrivateProfileString(_T("paperinfo"), _T("session"), _T(""), session, 512, FilePath);
  1586. sprintf_s(fff.sSeesion, "%s", UnicodeToAnsi(session).c_str());
  1587. }
  1588. }
  1589. if (g_my_scan_type == 0)
  1590. fff.ntype = 1;
  1591. else if (g_my_scan_type == 1)
  1592. fff.ntype = 2;
  1593. else if (g_my_scan_type == 2)
  1594. fff.ntype = 3;
  1595. fff.course_id = m_nCourseId;
  1596. fff.bScanType = service_type;
  1597. printf("发送数学开始上传:service_type=%d \n url=%s \n upload_url=%s\n session=%s\n", fff.bScanType, fff.sUrl, fff.sabsentUrl, fff.sSeesion);
  1598. //魔法词汇卡
  1599. if (m_nOnlineScanType == 0)
  1600. {
  1601. fff.nmode = 0;
  1602. fff.ntype = 6;
  1603. }
  1604. COPYDATASTRUCT cpd; // 给COPYDATASTRUCT结构赋值
  1605. cpd.dwData = 0;
  1606. cpd.cbData = sizeof(MY_DB_INFO);
  1607. cpd.lpData = (&fff);
  1608. int ret = pWnd->SendMessage(WM_COPYDATA, NULL, (LPARAM)&cpd); // 发送
  1609. if (m_hwnd != NULL)
  1610. {
  1611. UploadMsg* msg = new UploadMsg;
  1612. msg->strMsg = m_strMsg;
  1613. msg->nTotal = (m_total << 16) + m_sccuss_count;
  1614. ::PostMessage(m_hwnd, WM_IDENTIFOR_RESULT_UPLOAD_COMPLETED, !m_failtrue, (LPARAM)msg);
  1615. }
  1616. }
  1617. return IService::OnStoping();
  1618. }
  1619. void CResultUploader::SetResultHandler(CResultHandler * result_hadler)
  1620. {
  1621. m_result_hadler = result_hadler;
  1622. }
  1623. void CResultUploader::SetResultHandler(boost::shared_ptr<CResultHandler> pPtr)
  1624. {
  1625. m_pWeakResult = pPtr;
  1626. }
  1627. int CResultUploader::SetHandlerWnd(HWND hwnd)
  1628. {
  1629. m_hwnd = hwnd;
  1630. return 0;
  1631. }
  1632. CString CResultUploader::MarkHttpHeaderData()
  1633. {
  1634. TCHAR FilePath[MAX_PATH];
  1635. GetModuleFileName(NULL, FilePath, MAX_PATH);
  1636. CString ret;
  1637. (_tcsrchr(FilePath, '\\'))[1] = 0;
  1638. if (m_bErrorHandle){
  1639. lstrcat(FilePath, _T("paperinfo.ini"));
  1640. WCHAR JSESSIONID[512];
  1641. GetPrivateProfileString(_T("paperinfo"), _T("session"), _T(""), JSESSIONID, 512, FilePath);
  1642. ret = JSESSIONID;
  1643. }
  1644. else{
  1645. lstrcat(FilePath, _T("config.ini"));
  1646. #ifndef BACK_STAGE
  1647. WCHAR SIG[512];
  1648. GetPrivateProfileString(_T("SESSIONID"), _T("SIG"), _T(""), SIG, 512, FilePath);
  1649. WCHAR TIME[512];
  1650. GetPrivateProfileString(_T("SESSIONID"), _T("TIME"), _T(""), TIME, 512, FilePath);
  1651. WCHAR ID[512];
  1652. GetPrivateProfileString(L"SESSIONID", L"ID", L"", ID, 512, FilePath);
  1653. WCHAR JSESSIONID[512];
  1654. GetPrivateProfileString(_T("SESSIONID"), _T("JSESSIONID"), _T(""), JSESSIONID, 512, FilePath);
  1655. ret.Format(_T("userid=%s&time=%s&sig=%s&session=%s"), ID, TIME, SIG, JSESSIONID);
  1656. #else
  1657. WCHAR MYCOOK[512];
  1658. GetPrivateProfileString(_T("SESSIONID"), _T("MYCOOK"), _T(""), MYCOOK, 512, FilePath);
  1659. CString ret;
  1660. ret.Format(_T("%s"), MYCOOK);
  1661. #endif
  1662. }
  1663. return ret;
  1664. }
  1665. void CResultUploader::readNewUploadUrl()
  1666. {
  1667. TCHAR exeFullPath[MAX_PATH];
  1668. CString strPath;
  1669. GetModuleFileName(NULL, exeFullPath, MAX_PATH);
  1670. strPath = (CString)exeFullPath;
  1671. int position = strPath.ReverseFind('\\');
  1672. strPath = strPath.Left(position + 1);
  1673. TCHAR FilePath[MAX_PATH];
  1674. GetModuleFileName(NULL, FilePath, MAX_PATH);
  1675. (_tcsrchr(FilePath, '\\'))[1] = 0;
  1676. lstrcat(FilePath, _T("config.ini"));
  1677. WCHAR GroupId[512];
  1678. GetPrivateProfileString(_T("USER"), _T("groupid"), _T(""), GroupId, 512, FilePath);
  1679. m_strNewUploadUrl = GroupId;
  1680. if (m_bErrorHandle)
  1681. {
  1682. (_tcsrchr(FilePath, '\\'))[1] = 0;
  1683. lstrcat(FilePath, _T("paperinfo.ini"));
  1684. WCHAR URL[512] = { 0 };
  1685. GetPrivateProfileString(_T("paperinfo"), _T("uploadurl"), _T(""), URL, 512, FilePath);
  1686. CString tmp = URL;
  1687. if (tmp != _T("nourl")){
  1688. m_strNewUploadUrl = tmp;
  1689. }
  1690. }
  1691. return;
  1692. }
  1693. int CResultUploader::readCourseId()
  1694. {
  1695. TCHAR exeFullPath[MAX_PATH];
  1696. CString strPath;
  1697. int nSubjectId;
  1698. GetModuleFileName(NULL, exeFullPath, MAX_PATH);
  1699. strPath = (CString)exeFullPath;
  1700. int position = strPath.ReverseFind('\\');
  1701. strPath = strPath.Left(position + 1);
  1702. TCHAR FilePath[MAX_PATH];
  1703. GetModuleFileName(NULL, FilePath, MAX_PATH);
  1704. (_tcsrchr(FilePath, '\\'))[1] = 0;
  1705. lstrcat(FilePath, _T("paperInfo.ini"));
  1706. WCHAR SubjectId[512];
  1707. GetPrivateProfileString(_T("paperinfo"), _T("subject_id"), _T("3"), SubjectId, 512, FilePath);
  1708. nSubjectId = _wtoi(SubjectId);
  1709. return nSubjectId;
  1710. }
  1711. void CResultUploader::logForDbg(string strLog)
  1712. {
  1713. char logName[300] = { 0 };
  1714. char sYear[10], sMonth[5], sDay[25];
  1715. SYSTEMTIME st;
  1716. GetLocalTime(&st);
  1717. int j0 = 0;
  1718. int m_nYear = st.wYear; ///年
  1719. int m_nMonth = st.wMonth; ///月
  1720. int m_nDay = st.wDay; ///日
  1721. int m_nHour = st.wHour; ///小时
  1722. int m_nMinute = st.wMinute; ///分钟
  1723. int m_nSecond = st.wSecond; ///秒
  1724. int m_nMillSecond = st.wMilliseconds; ///毫秒
  1725. char strTime[300];
  1726. sprintf(strTime, "%d_%02d_%02d_%d_%02d_%02d< == >", m_nYear, m_nMonth, m_nDay, m_nHour, m_nMinute, m_nSecond);
  1727. strLog = strTime + strLog;
  1728. sprintf_s(sYear, sizeof(sYear), "%d", m_nYear);
  1729. sprintf_s(sMonth, sizeof(sMonth), "_%02d", m_nMonth);
  1730. sprintf_s(sDay, sizeof(sDay), "_%02d_log.txt", m_nDay);
  1731. strcpy(logName, sYear);
  1732. strcat(logName, sMonth);
  1733. strcat(logName, sDay);
  1734. ThreadLog3((char*)strLog.c_str(),logName);
  1735. }