CBasicDataDownloader.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. #include "StdAfx.h"
  2. #include "CBasicDataDownloader.h"
  3. #include "..\EvaluationUtil\HttpClient.h"
  4. #include "basic_struct.h"
  5. #include "ServerConfig.h"
  6. #include "rapidjson/document.h"
  7. CBasicDataDownloader::CBasicDataDownloader(void)
  8. {
  9. }
  10. CBasicDataDownloader::~CBasicDataDownloader(void)
  11. {
  12. }
  13. int CBasicDataDownloader::download_question_std( CString examId,CString examCourseId )
  14. {
  15. m_task_type = DOWNLOAD_QUESTION_STD;
  16. m_examId =examId;
  17. m_examCourseId = examCourseId;
  18. m_data_state = DS_LOADING;
  19. return IService::Start();
  20. }
  21. ServiceState CBasicDataDownloader::OnRunning( void )
  22. {
  23. switch(m_task_type){
  24. case DOWNLOAD_QUESTION_STD:
  25. download_question_std0(m_examId,m_examCourseId);
  26. break;
  27. case DOWNLOAD_SAME_CLASS_STUDENT:
  28. break;
  29. }
  30. return stoping;
  31. }
  32. int CBasicDataDownloader::GetErrorCode(){
  33. return m_error_code;
  34. };
  35. //下载题目标准信息
  36. int CBasicDataDownloader::download_question_std0( CString examId,CString examCourseId )
  37. {
  38. m_error_code = BDD_ERROR_CODE_NORMAL;
  39. CHttpClient httpClient;
  40. string response;
  41. CString url_str;
  42. url_str.Format(_T("%s/%s"),CServerConfig::server_url,_T("/question/findHandCardQidDetails"));
  43. CString postData;
  44. postData.Format(_T("examId=%s&examCourseId=%s"),examId,examCourseId);
  45. httpClient.HttpPost(url_str,postData,response);
  46. rapidjson::Document doc;
  47. doc.Parse(response.c_str());
  48. rapidjson::Value& root = doc;
  49. if(root["success"].IsInt()){
  50. if(root["success"].GetInt()){
  51. rapidjson::Value& rows = root["object"];
  52. const char* sql ="INSERT INTO question_std (question_code, question_index, question_type, multi_answer, answer_all, answer_std, score_full, score_half, option_count,mark_unit) VALUES (:question_code, :question_index, :question_type, :multi_answer, :answer_all, :answer_std, :score_full, :score_half, :option_count,:mark_unit)";
  53. CppSQLite3Statement statement= bantch_db->compileStatement(sql);
  54. if(rows.IsArray()){
  55. bantch_db->execDML("begin transaction");
  56. int array_size =rows.Size();
  57. for (int i=0;i<array_size;i++)
  58. {
  59. rapidjson::Value& row = rows[i];
  60. statement.bind(":question_code", row["qid"].GetString());
  61. statement.bind(":question_index", i);
  62. statement.bind(":question_type", row["type"].GetInt());
  63. statement.bind(":multi_answer", row["type"].GetInt() == 1 ? 1 : 0);
  64. statement.bind(":answer_all", row.FindMember("selectValues") == row.MemberEnd() ? 0 : row["selectValues"].GetString());
  65. statement.bind(":answer_std", row.FindMember("answer") == row.MemberEnd() ? 0 : row["answer"].GetString());
  66. statement.bind(":score_full", row.FindMember("fullScore") == row.MemberEnd() ? 0 : row["fullScore"].GetDouble());
  67. statement.bind(":score_half", row.FindMember("halfScore") == row.MemberEnd() ? 0 : row["halfScore"].GetDouble());
  68. int option_count = 0;
  69. if (row.FindMember("selectValues") != row.MemberEnd() && row["selectValues"].GetStringLength()>0){
  70. vector<string> options;
  71. split(row["selectValues"].GetString(), ",", &options);
  72. option_count = options.size();
  73. }
  74. else{
  75. option_count = 0;
  76. }
  77. statement.bind(":option_count", option_count);
  78. statement.bind(":mark_unit", row.FindMember("markUnit") == row.MemberEnd() ? 0 : (int)row["markUnit"].GetInt());
  79. statement.execDML();
  80. }
  81. bantch_db->execDML("commit transaction");
  82. }
  83. m_data_state = DS_COMPLETE;
  84. }else{
  85. m_error_code = BDD_ERROR_CODE_SERVER_ERROR;
  86. m_message =root["message"].GetString();
  87. m_data_state = DS_ERROR;
  88. }
  89. }else if(root["session"].IsBool()&&root["session"].GetBool()){
  90. m_error_code = BDD_ERROR_CODE_SERVER_ERROR;
  91. m_message ="长时间未操作,需要重新认证身份!";
  92. m_data_state = DS_ERROR;
  93. }else{
  94. m_error_code = BDD_ERROR_CODE_REQUEST_FAILTURE;
  95. m_data_state = DS_ERROR;
  96. }
  97. return 0;
  98. }
  99. std::string CBasicDataDownloader::GetExamName()
  100. {
  101. return m_exam_name;
  102. }
  103. std::string CBasicDataDownloader::GetCourseCode()
  104. {
  105. return m_course_code;
  106. }
  107. string CBasicDataDownloader::GetErrorMsg()
  108. {
  109. return m_message;
  110. }