using System; using System.Collections.Generic; using System.Linq; using System.Text; using EMISOnline.DataLogic.ExamSetting; using Bowin.Common.Linq.Entity; using EMISOnline.ViewModel.ExamView; using System.Linq.Expressions; using EMISOnline.Entities; using System.Linq.Expressions; using Bowin.Common.Linq; using EMISOnline.ViewModel; using Bowin.Common; using System.Web.Script.Serialization; using System.Xml.Serialization; using System.IO; using System.Xml.Linq; using EMISOnline.DataLogic.SystemDAL; using System.Runtime.Remoting.Contexts; using System.Data; using System.Text.RegularExpressions; namespace EMISOnline.CommonLogic.ExamServices { public class QuestionSettingServices : IQuestionSettingServices { public QuestionSettingDAL SettingDAL { get; set; } public object GetQuestionList(string Content, decimal? QuestionType, decimal? libaryid, int maximumRows, int startRowIndex) { Expression> exp = (x => true); if (!string.IsNullOrEmpty(Content)) exp = exp.And(q => q.content.Contains(Content)); if (QuestionType.HasValue) exp = exp.And(q => q.base_question_type_id == QuestionType); if (libaryid.HasValue) { var libarys = getSubListid(libaryid.Value); exp = exp.And(q => libarys.Contains(q.test_question_libary_id.Value )); } var question = SettingDAL.GetQuestionList(exp); return question.OrderBy(q => q.test_question_Id).ToGridResultSet(maximumRows, startRowIndex + 1); } public List getSubListid(decimal libaryid) { IList list = new List(); SettingDAL.GetSubLibary(null, libaryid, list); List libaryids = list.Select(q => q.test_question_libary_id).ToList(); libaryids.Add(libaryid); return libaryids; } public QuestionSettingView GetQuestion(decimal test_question_Id) { var entry = SettingDAL.questionRepository.GetSingle(q => q.test_question_Id == test_question_Id); test_question_libary libary = GetQuestionLibary(entry.test_question_libary_id.Value); var question_type = SettingDAL.typeRepository.GetSingle(q => q.base_question_type_id == entry.base_question_type_id); var question_provid_answer = SettingDAL.answerRepository.Entities.Where(q => q.test_question_Id == test_question_Id).ToList(); var question = new QuestionSettingView() { test_question_Id = entry.test_question_Id, base_question_type_id = entry.base_question_type_id, content = entry.content, question_file_id = entry.question_file_id, note = entry.note, test_question_libary_id = entry.test_question_libary_id, score = entry.score, difficulty_degree = entry.difficulty_degree, is_vaild = entry.is_vaild, TypeName = question_type.Name, question_provid_answer = question_provid_answer.Select(a => new test_question_provid_answer() { answer_name = a.answer_name, provid_answer_id = a.provid_answer_id, file_id = a.file_id }).ToList(), answers = entry.answers, libaryName = libary.name }; return question; } public test_question_libary GetQuestionLibary(decimal libary_Id) { var question = SettingDAL.libaryRepository.GetSingle(q => q.test_question_libary_id == libary_Id); return question; } public object GetQuestionType(bool? isEmpty) { var question = SettingDAL.typeRepository.GetList(q => true); var result = question.Select(q => new { q.Name, base_question_type_id = q.base_question_type_id.ToString() }).ToList(); if (isEmpty == true) result.Insert(0, new { Name = "全选", base_question_type_id = "" }); return result; } public object GetDifficulty() { var question = SettingDAL.paramRepository.GetList(q => q.base_param_parent_id == 3); return question.Select(q => new { q.name }).ToList(); } private static Regex regAnser = new Regex(@"^[A-Z][、]"); /// /// 保存试题、保存试题答案 /// public void InsertByTrans(test_question tq, List tqpaList, List rightAnswersList, string userid) { //ToDo:保存到数据库 tq.question_file_id = tq.question_file_id == 0 ? null : tq.question_file_id; tq.created_date = DateTime.Now; tq.created_by = "0"; tq.used_count = 1; tq.wrong_count = 0; string rightAnswers = ""; foreach (test_question_provid_answer tqpa in tqpaList) { tqpa.created_by = userid; tqpa.created_date = DateTime.Now; tqpa.test_question_Id = tq.test_question_Id; tq.test_question_provid_answer.Add(tqpa); } SettingDAL.questionRepository.UnitOfWork.Add(tq); SettingDAL.questionRepository.UnitOfWork.Commit(); foreach (test_question_provid_answer tqpa in tq.test_question_provid_answer) { foreach (string s in rightAnswersList) { if (regAnser.Replace(tqpa.answer_name, "") == regAnser.Replace(s, "")) { rightAnswers += tqpa.provid_answer_id + ","; break; } } } if (rightAnswers.EndsWith(",")) rightAnswers = rightAnswers.TrimEnd(','); tq.answers = rightAnswers; SettingDAL.questionRepository.UnitOfWork.Update(tq); SettingDAL.questionRepository.UnitOfWork.Commit(); } /// /// 更新试题、更新试题答案 /// public void UpdateByTrans(test_question setting, List tqpaList, List rightAnswersList, string userid) { var question = SettingDAL.questionRepository.GetSingle(q => q.test_question_Id == setting.test_question_Id); Entry(setting, question); string rightAnswers = ""; var provid_answer_ids = tqpaList.Select(q => q.provid_answer_id).ToArray(); SettingDAL.answerRepository.UnitOfWork.Delete(q => !provid_answer_ids.Contains(q.provid_answer_id) && q.test_question_Id == question.test_question_Id); foreach (test_question_provid_answer tqpa in tqpaList) { foreach (string s in rightAnswersList) { if (tqpa.answer_name == s) { rightAnswers += tqpa.provid_answer_id + ","; break; } } tqpa.created_by = userid; tqpa.created_date = DateTime.Now; tqpa.test_question_Id = question.test_question_Id; question.test_question_provid_answer.Add(tqpa); if (tqpa.provid_answer_id > 0) SettingDAL.answerRepository.UnitOfWork.Update(tqpa); else SettingDAL.answerRepository.UnitOfWork.Add(tqpa); } rightAnswers = rightAnswers.TrimEnd(','); question.answers = rightAnswers; SettingDAL.questionRepository.UnitOfWork.Commit(); } public void Entry(test_question entry, test_question question) { question.base_question_type_id = entry.base_question_type_id; question.content = entry.content; question.question_file_id = entry.question_file_id == 0 ? null : entry.question_file_id; question.note = entry.note; question.test_question_libary_id = entry.test_question_libary_id; question.score = entry.score; question.difficulty_degree = entry.difficulty_degree; question.is_vaild = entry.is_vaild; question.answers = entry.answers; ; } } }