예제 #1
0
        /// <summary>
        ///     导入试题
        /// </summary>
        /// <param name="sortID">分类ID</param>
        /// <param name="ds">试题DataSet</param>
        /// <param name="message">错误信息</param>
        /// <param name="count">错误数目</param>
        /// <param name="totalCount">导入的总题数</param>
        /// <returns></returns>
        public List <MQuestionShow> ImportQuestion(int sortID, int key, DataSet ds, ref string message, ref int count,
                                                   ref int totalCount)
        {
            var newlist      = new List <MQuestionShow>();
            var questionList = new List <tbQuestion>();
            var question     = new tbQuestion();
            var rowList      = new List <int>();
            int rowCount     = 0; //记录行号
            Dictionary <int, tbQuestionSort> dicsort = ESortBL.GetAllQuestionSortDictionary(CurrentTenant.TenantId);

            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                rowCount++;
                //如果是空行跳过
                //if (dr[1].ToString() == "")
                //{
                //    message += message == "" ? (rowCount + 1).ToString() : ("、" + (rowCount + 1).ToString());
                //    continue;
                //}
                //为空的时候是答案
                if (dr[3].ToString() == string.Empty && dr[1].ToString() != "")
                {
                    if (question.QuestionType > 0)
                    {
                        questionList[questionList.Count - 1].QuestionAnswer.Add(new QuestionAnswer
                        {
                            Answer     = (question.QuestionType == 4 ? (dr[1].ToString() == "正确" ? "0" : "1") : dr[1].ToString().Trim()),
                            AnswerFlag = dr[2].ToString() == "是" ? 1 : 0,
                            AnswerType = question.QuestionType == 1 ? 1 : 0
                        });
                    }
                }
                //保存试题,初始化model
                else
                {
                    if ((dr[3].ToString() == "问答题" || dr[3].ToString() == "填空题") && dr[6].ToString() == "")
                    {
                        message += message == "" ? (rowCount + 1).ToString() : ("、" + (rowCount + 1).ToString());
                    }
                    else
                    {
                        if (dr[1].ToString() != "" && dr[3].ToString() != "" && dr[4].ToString() != "")
                        {
                            try
                            {
                                question = new tbQuestion
                                {
                                    QuestionAnswer     = new List <QuestionAnswer>(),
                                    QuestionKey        = key,
                                    QuestionAnswerKeys = dr[6].ToString(),
                                    QuestionAnalysis   = dr[5].ToString(),
                                    QuestionContent    =
                                        dr[1].ToString()
                                        .Replace("(", "(")
                                        .Replace("(", "(")
                                        .Replace("(", "(")
                                        .Replace(")", ")")
                                        .Replace(")", ")")
                                        .Replace(")", ")"),
                                    QuestionLevel =
                                        (int)
                                        ((QuestionLevel)
                                         Enum.Parse(typeof(QuestionLevel), dr[4].ToString())),
                                    QuestionType =
                                        (int)
                                        ((QuestionType)
                                         Enum.Parse(typeof(QuestionType), dr[3].ToString())),
                                    Status         = 0,
                                    LastUpdateTime = DateTime.Now,
                                    CreateTime     = DateTime.Now,
                                    QuestionSortID = sortID,
                                    UserID         = CurrentUser.UserId,
                                    FileUpload     = new List <FileUpload>(),
                                    TenantId       = CurrentTenant.TenantId
                                };
                                questionList.Add(question);
                                rowList.Add(rowCount + 1);
                            }
                            catch
                            {
                                message += message == "" ? (rowCount + 1).ToString() : ("、" + (rowCount + 1).ToString());
                            }
                        }
                        else
                        {
                            message += message == "" ? (rowCount + 1).ToString() : ("、" + (rowCount + 1).ToString());
                        }
                    }
                }
            }

            int quCount = 0;

            foreach (tbQuestion qu in questionList)
            {
                qu.QuestionOpen = 1;
                quCount++;
                int qID = 0;
                if ((qu.QuestionType == 1 && qu.QuestionAnswer.Count == 1) //问答
                    ||
                    (qu.QuestionType == 2 && qu.QuestionAnswer.Count > 1 && qu.QuestionAnswer.Count < 27 &&
                     qu.QuestionAnswer.Count(p => p.AnswerFlag == 1) == 1) //单选
                    ||
                    (qu.QuestionType == 3 && qu.QuestionAnswer.Count > 1 && qu.QuestionAnswer.Count < 27 &&
                     qu.QuestionAnswer.Any(p => p.AnswerFlag == 1)) //多选
                    ||
                    (qu.QuestionType == 4 && qu.QuestionAnswer.Count == 2 &&
                     qu.QuestionAnswer.Count(p => p.AnswerFlag == 1) == 1) //判断
                    )
                {
                    int c = 0;
                    qu.QuestionAnswer.ForEach(pz =>
                    {
                        pz.Order = ++c;
                    });
                    if (qu.QuestionType == 4)
                    {
                        qu.QuestionAnswer = qu.QuestionAnswer.FindAll(p => p.AnswerFlag == 1);
                    }
                    qID = EQuestionBL.Insert(qu);
                    var newQu = new MQuestionShow();
                    newQu.FileUpload      = new List <FileUpload>();
                    newQu.QuestionAnswer  = qu.QuestionAnswer;
                    newQu.QuestionContent = qu.QuestionContent;
                    newQu.id = qu._id;
                    newQu.QuestionLevelStr = EnumsLanguage.GetString(((QuestionLevel)qu.QuestionLevel).ToString());
                    newQu.QuestionTypeStr  = EnumsLanguage.GetString(((QuestionType)qu.QuestionType).ToString());
                    newQu.QuestionType     = qu.QuestionType;
                    newQu.QuestionLevel    = qu.QuestionLevel;
                    newQu.SortName         = dicsort[sortID].Title;
                    newlist.Add(newQu);
                }
                else if (qu.QuestionType == 5)
                {
                    //判断()的个数和答案数是否一致,
                    var answerNumber = qu.QuestionContent.Split(new[] { "()" }, StringSplitOptions.None).Length - 1;
                    if (qu.QuestionAnswer.Count == answerNumber)
                    {
                        //关键词的个数是否一样
                        if (qu.QuestionAnswerKeys.Split(' ').Length == answerNumber)
                        {
                            string anStr = "";
                            qu.QuestionAnswer.ForEach(p =>
                            {
                                anStr += anStr == "" ? p.Answer : ("!!%%!!" + p.Answer);
                            });
                            var answer = new QuestionAnswer
                            {
                                Answer     = anStr,
                                AnswerFlag = qu.QuestionAnswer[0].AnswerFlag
                            };
                            var list = new List <QuestionAnswer>();
                            list.Add(answer);
                            qu.QuestionAnswer = list;
                            qID = EQuestionBL.Insert(qu);
                            var newQu = new MQuestionShow();
                            newQu.FileUpload      = new List <FileUpload>();
                            newQu.QuestionAnswer  = qu.QuestionAnswer;
                            newQu.QuestionContent = qu.QuestionContent;
                            newQu.id = qu._id;
                            newQu.QuestionLevelStr = EnumsLanguage.GetString(((QuestionLevel)qu.QuestionLevel).ToString());
                            newQu.QuestionTypeStr  = EnumsLanguage.GetString(((QuestionType)qu.QuestionType).ToString());
                            newQu.QuestionType     = qu.QuestionType;
                            newQu.QuestionLevel    = qu.QuestionLevel;
                            newQu.SortName         = dicsort[sortID].Title;
                            newQu.Creater          = CurrentUser.Realname;
                            newlist.Add(newQu);
                        }
                        else
                        {
                            count++;
                            message += message == ""
                                           ? ((rowList[quCount - 1]).ToString())
                                           : ("、" + (rowList[quCount - 1]).ToString());
                        }
                    }
                    else
                    {
                        count++;
                        message += message == ""
                                       ? ((rowList[quCount - 1] + 1).ToString())
                                       : ("、" + (rowList[quCount - 1] + 1).ToString());
                    }
                }
                else
                {
                    count++;
                    message += message == ""
                                   ? ((rowList[quCount - 1] + 1).ToString())
                                   : ("、" + (rowList[quCount - 1] + 1).ToString());
                }

                totalCount++;
            }
            return(newlist);
        }