private async Task <bool> Import(QuestionType questionType, QuestionChapter questionChapter, List <int> questionList, int intOrderIndex) { if (!questionList.Any()) { return(true); } try { ImportDto <List <ImportQuestionDto> > importDto = HttpHelper.Get <ImportDto <List <ImportQuestionDto> > >("http://api2.jiakaobaodian.com/api/open/question/question-list.htm?_r=114972216825686778106&questionIds=" + string.Join("%2C", questionList) + "&_=0.8455804460813621"); if (!importDto.Success) { return(false); } foreach (ImportQuestionDto importQuestionDto in importDto.Data) { if (!(await Import(questionType, questionChapter, importQuestionDto, intOrderIndex++))) { return(false); } } return(true); } catch (Exception ex) { LogHelper.Error(ex); return(false); } }
private async Task <bool> Import(QuestionType questionType, ImportChapterDto importChapterDto) { try { QuestionChapter questionChapter = new QuestionChapter { QuestionTypeId = questionType.Id, Name = importChapterDto.Title, OrderIndex = importChapterDto.Chapter }; _Db.QuestionChapters.Add(questionChapter); await _Db.SaveChangesAsync(); ImportDto <ImportChapterQuestionDto> importDto = HttpHelper.Get <ImportDto <ImportChapterQuestionDto> >("https://api2.jiakaobaodian.com/api/open/exercise/chapter.htm?_appName=jiakaobaodian&_platform=wap&_r=110028075929574772092&carType=car&chapterId=" + importChapterDto.Id + "&cityCode=430100&course=kemu1"); if (!importDto.Success) { return(false); } int intOrderIndex = 0; List <int> questionList = new List <int>(); foreach (int question in importDto.Data.questionList) { if (questionList.Count >= 50) { await Import(questionType, questionChapter, questionList, intOrderIndex); intOrderIndex += questionList.Count; questionList.Clear(); } questionList.Add(question); } await Import(questionType, questionChapter, questionList, intOrderIndex); return(true); } catch (Exception ex) { LogHelper.Error(ex); return(false); } }
private async Task <bool> Import(QuestionType questionType, QuestionChapter questionChapter, ImportQuestionDto importQuestionDto, int intOrderIndex) { string strAnswer = GetCharAnswer(importQuestionDto.Answer); QuestionDto model = new QuestionDto { Question = new Question { QuestionTypeId = questionType.Id, QuestionChapterId = questionChapter.Id, Description = importQuestionDto.Question, PicPath = importQuestionDto.MediaContent, Explanation = importQuestionDto.Explain, OrderIndex = intOrderIndex, Mark = 1, AnswerType = (AnswerType)(importQuestionDto.OptionType + 1) }, Answers = new List <Answer> { new Answer { Description = importQuestionDto.OptionA, OrderIndex = 1, IsAnswer = strAnswer.Contains("A") }, new Answer { Description = importQuestionDto.OptionB, OrderIndex = 2, IsAnswer = strAnswer.Contains("B") } } }; if (!string.IsNullOrEmpty(importQuestionDto.OptionC)) { model.Answers.Add(new Answer { Description = importQuestionDto.OptionC, OrderIndex = 3, IsAnswer = strAnswer.Contains("C") }); } if (!string.IsNullOrEmpty(importQuestionDto.OptionD)) { model.Answers.Add(new Answer { Description = importQuestionDto.OptionD, OrderIndex = 4, IsAnswer = strAnswer.Contains("D") }); } if (!string.IsNullOrEmpty(importQuestionDto.OptionE)) { model.Answers.Add(new Answer { Description = importQuestionDto.OptionE, OrderIndex = 5, IsAnswer = strAnswer.Contains("E") }); } if (!string.IsNullOrEmpty(importQuestionDto.OptionF)) { model.Answers.Add(new Answer { Description = importQuestionDto.OptionF, OrderIndex = 6, IsAnswer = strAnswer.Contains("F") }); } if (!string.IsNullOrEmpty(importQuestionDto.OptionG)) { model.Answers.Add(new Answer { Description = importQuestionDto.OptionG, OrderIndex = 7, IsAnswer = strAnswer.Contains("G") }); } if (!string.IsNullOrEmpty(importQuestionDto.OptionH)) { model.Answers.Add(new Answer { Description = importQuestionDto.OptionH, OrderIndex = 8, IsAnswer = strAnswer.Contains("H") }); } return(await SaveAsync(model)); }