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; namespace EMISOnline.CommonLogic.ExamServices { public class ExamPaperServices : IExamPaperServices { public ExamPaperDAL ExamPaperDAL { get; set; } public object ListPaper(int maximumRows, int startRowIndex, string PaperName) { Expression> exp = (x => true); if (!string.IsNullOrEmpty(PaperName)) exp = exp.And(q => q.PaperName.Contains(PaperName)); var query = ExamPaperDAL.ListPaper(maximumRows, startRowIndex, exp); return query.OrderBy(q => q.test_paper_id).ToGridResultSet(maximumRows, startRowIndex); } public test_paper GetPaperByPaperID(decimal PaperID) { return ExamPaperDAL.PaperRepository.GetSingle(q => q.test_paper_id == PaperID); } /// /// 根据一序列的条件查找不同题型的题数是多少 /// /// /// /// /// /// /// /// /// public object CountQuesSumByCondition(List lib_ids, decimal diff_min, decimal diff_max, decimal err_rate, int usecount_min, int usecount_max) { return ExamPaperDAL.CountQuesSumByCondition(lib_ids, diff_min, diff_max, err_rate, usecount_min, usecount_max); } public bool ExamPaperSave(PaperAddView PaperAdd, string LoginID) { var paper = ExamPaperDAL.PaperRepository.GetSingle(q => q.test_paper_id == PaperAdd.PaperID); if (paper == null) { paper = new test_paper(); Assignment(PaperAdd, paper, LoginID); ExamPaperDAL.PaperRepository.UnitOfWork.Add(paper); } else { Assignment(PaperAdd, paper, LoginID); //分数 paper.paper_score = CalcPaperScore(PaperAdd.QuestionTypeString); } ExamPaperDAL.PaperRepository.UnitOfWork.Commit(); return true; } private void Assignment(PaperAddView PaperAdd, test_paper paper, string LoginID) { paper.created_by = LoginID; paper.created_date = DateTime.Now; paper.difficulty_degree_minvalue = Convert.ToDecimal(PaperAdd.QuesLevelBeg); paper.difficulty_degree_maxvalue = Convert.ToDecimal(PaperAdd.QuesLevelEnd); paper.used_count_min = Convert.ToInt32(PaperAdd.UseFBeg); paper.used_count_max = Convert.ToInt32(PaperAdd.UseFEnd); //paper.distributing_option = Convert.ToInt32(this.rblDis.SelectedValue); paper.error_percent = Convert.ToDecimal(PaperAdd.ErrRate); paper.is_vaild = PaperAdd.IsEnable == "0" ? false : true; paper.PaperName = PaperAdd.PageName; paper.Prority = Convert.ToInt32(GetQuesChooseSort(PaperAdd.QuesFCount, PaperAdd.QuesUpdateTime)); paper.question_libary_set_id = TranQuesLib(PaperAdd.LibarysString); paper.is_autogenerate = false; paper.is_dynamic = true; paper.note = PaperAdd.LibarysString; paper.question_setting = PaperAdd.QuestionTypeString; } private string TranQuesLib(string ques_lib) { var json = new JavaScriptSerializer(); var libarys = json.Deserialize>(ques_lib); return string.Join(",", libarys.Select(q => q.Libaryid).ToArray()); } private decimal CalcPaperScore(string ques) { var json = new JavaScriptSerializer(); var libarys = json.Deserialize>(ques); return libarys.Sum(q => q.PSum * q.QScore); } public PageSetupParam SetPageSetupCondition(PaperAddView PaperAdd) { PageSetupParam psp = new PageSetupParam(); psp.IsEnable = PaperAdd.IsEnable; psp.PageName = PaperAdd.PageName; psp.PageType = PaperAdd.PageType; psp.QuesChooseSort = GetQuesChooseSort(PaperAdd.QuesFCount, PaperAdd.QuesUpdateTime); psp.QuesDis = "";//this.rblDis.SelectedValue; psp.QuesLevel = string.Format("{0}|{1}", PaperAdd.QuesLevelBeg, PaperAdd.QuesLevelEnd); psp.QuesLib = PaperAdd.LibarysString; psp.QuesSetting = PaperAdd.QuestionTypeString; psp.ErrRate = PaperAdd.ErrRate; psp.UseCount = string.Format("{0}|{1}", PaperAdd.UseFBeg, PaperAdd.UseFEnd); return psp; } /// /// 获取选择值 /// /// private string GetQuesChooseSort(bool QuesFCount, bool QuesUpdateTime) { if (QuesFCount && !QuesUpdateTime) { return "1"; } else if (!QuesFCount && QuesUpdateTime) { return "2"; } else if (QuesFCount && QuesUpdateTime) { return "3"; } return "0"; } public DataTable GetQuestionByPaperID(decimal paperid) { return ExamPaperDAL.GetQuestionByPaperID(paperid); } public DataTable GetQuestionByParam(PageSetupParam psp) { string test_question_libary_id = TranQuesLib(psp.QuesLib); string difficulty_degree_min = psp.QuesLevel.Split('|')[0]; string difficulty_degree_max = psp.QuesLevel.Split('|')[1]; string wrong_percent = psp.ErrRate; string used_count_min = psp.UseCount.Split('|')[0]; string used_count_max = psp.UseCount.Split('|')[1]; DataTable quesDt = ExamPaperDAL.SelectQuestions(test_question_libary_id, difficulty_degree_min, difficulty_degree_max, wrong_percent, used_count_min, used_count_max); //按照题库数量过滤试题 quesDt = LibaryFilter(quesDt, psp.QuesLib); //按照设置条件过滤试题 quesDt = QuestionFilter(quesDt, psp.QuesChooseSort, psp.PageType, psp.QuesDis, psp.QuesSetting); //quesDt.Select("1=1", "created_date des"); return quesDt; } private DataTable LibaryFilter(DataTable dt, string quesLib) { var json = new JavaScriptSerializer(); var libSettingArray = json.Deserialize>(quesLib); DataTable filteredDT = dt.Clone(); DataTable radioDT = null; foreach (var item in libSettingArray) { string libid = item.Libaryid.ToString(); int libnum = item.Number; radioDT = TableFilterByLib(dt, libid, libnum); filteredDT.Merge(radioDT); } return filteredDT; } /// /// 按题库数量从各题库筛选试题 /// /// /// /// private DataTable TableFilterByLib(DataTable dt, string libid, int number) { DataTable filteredDT = dt.Clone(); int count = 0; var dtView = dt.DefaultView; dtView.RowFilter = "test_question_libary_id=" + libid; foreach (DataRowView dr in dtView) { if (dr["test_question_libary_id"].ToString() == libid) { filteredDT.Rows.Add(dr.Row.ItemArray); if (number == ++count) break; } } return filteredDT; } /// /// 试题过滤 /// /// /// /// /// /// /// private DataTable QuestionFilter(DataTable dt, string chooseSortBy, string pageType, string quesDis, string pageSetting) { var json = new JavaScriptSerializer(); var pageSettingArray = json.Deserialize>(pageSetting); DataTable filteredDT = dt.Clone(); DataTable radioDT = null; foreach (var item in pageSettingArray) { string question_type = item.QType.ToString(); int pickerSum = (int)item.PSum; int totalSum = (int)item.TSum; radioDT = TableFilterByType(dt, question_type); radioDT = TableFilterByCondition(radioDT, chooseSortBy, pageType, quesDis, pickerSum, totalSum); filteredDT.Merge(radioDT); } return filteredDT; } /// /// 抽取试题第一道工序 /// 过滤不同类型的试题形成各个子Table /// /// /// /// private DataTable TableFilterByType(DataTable dt, string typeid) { DataTable filteredDT = dt.Clone(); foreach (DataRow dr in dt.Rows) { if (dr["base_question_type_id"].ToString() == typeid) { filteredDT.Rows.Add(dr.ItemArray); } } return filteredDT; } private DataTable TableFilterByCondition(DataTable dt, string chooseSortBy, string pageType, string quesDis, int pickerSum, int totalSum) { DataTable filteredDT = dt.Clone(); string sortOrder = ""; if (chooseSortBy == "1") { sortOrder = " used_count DESC "; } else if (chooseSortBy == "2") { sortOrder = " created_date DESC "; } else if (chooseSortBy == "3") { sortOrder = " used_count ,created_date DESC "; } int counter = 0; DataRow[] filteredRows = dt.Select("", sortOrder); //静态卷的抽取 if (pageType == "0") { if (filteredRows.Length <= pickerSum) { counter = filteredRows.Length; } else { counter = pickerSum; } } else //动态抽卷 { if (filteredRows.Length <= totalSum) { counter = filteredRows.Length; } else { counter = totalSum; } } //抽取 for (int i = 0; i < counter; i++) { filteredDT.Rows.Add(filteredRows[i].ItemArray); } return filteredDT; } public void PaperEdit(decimal paperid, string question_str) { // test_paper p = paperMgr.GetSingle(paperid); ExamPaperDAL.DeletePaper(paperid); var paper = ExamPaperDAL.PaperRepository.GetSingle(q => q.test_paper_id == paperid); var quesList = question_str.Split(','); int order = 0; decimal score = 0m; List xmlValues = new List(); foreach (var q in quesList) { if (q.Length > 0) { test_paper_question_set paper_ques = new test_paper_question_set(); paper_ques.order = order; decimal? pSore = Convert.ToDecimal(q.Split('|')[1]); xmlValues.Add(new QuestionXml() { Question_id = decimal.Parse(q.Split('|')[0]), Score = pSore.Value }); score += pSore ?? 0; } } XmlSerializer serializer = new XmlSerializer(typeof(List)); MemoryStream mstream = new MemoryStream(); serializer.Serialize(mstream, xmlValues); mstream.Close(); test_paper_question_set qSet = new test_paper_question_set() { order = order, test_paper_id = paperid, questionXml = XElement.Parse(Encoding.Default.GetString(mstream.ToArray())).ToString() }; //分数 paper.paper_score = score; paper.test_paper_question_set.Add(qSet); ExamPaperDAL.PaperRepository.UnitOfWork.Commit(); } public void AddPaperDetail(PageSetupParam psp, string question_str, string UserID) { string difficulty_degree_min = psp.QuesLevel.Split('|')[0]; string difficulty_degree_max = psp.QuesLevel.Split('|')[1]; string used_count_min = psp.UseCount.Split('|')[0]; string used_count_max = psp.UseCount.Split('|')[1]; test_paper paper = new test_paper(); paper.created_by = UserID; paper.created_date = DateTime.Now; paper.difficulty_degree_maxvalue = Convert.ToDecimal(difficulty_degree_max); paper.difficulty_degree_minvalue = Convert.ToDecimal(difficulty_degree_min); //paper.distributing_option = Convert.ToInt32(psp.QuesDis); paper.error_percent = Convert.ToDecimal(psp.ErrRate); paper.is_vaild = psp.IsEnable == "0" ? false : true; paper.PaperName = psp.PageName; //paper.Prority = Convert.ToInt32(psp.QuesDis); paper.question_libary_set_id = TranQuesLib(psp.QuesLib); paper.is_autogenerate = false; paper.is_dynamic = psp.PageType == "0" ? false : true; paper.question_setting = psp.QuesSetting; paper.note = psp.QuesLib; paper.used_count_max = int.Parse(used_count_max); paper.used_count_min = int.Parse(used_count_min); var quesList = question_str.Split(','); int order = 0; decimal score = 0m; List xmlValues = new List(); foreach (var q in quesList) { if (q.Length > 0) { test_paper_question_set paper_ques = new test_paper_question_set(); paper_ques.order = order; decimal? pSore = Convert.ToDecimal(q.Split('|')[1]); xmlValues.Add(new QuestionXml() { Question_id = decimal.Parse(q.Split('|')[0]), Score = pSore.Value }); score += pSore ?? 0; } } XmlSerializer serializer = new XmlSerializer(typeof(List)); MemoryStream mstream = new MemoryStream(); serializer.Serialize(mstream, xmlValues); mstream.Close(); test_paper_question_set qSet = new test_paper_question_set() { order = order, questionXml = XElement.Parse(Encoding.Default.GetString(mstream.ToArray())).ToString() }; //分数 paper.paper_score = score; paper.test_paper_question_set.Add(qSet); ExamPaperDAL.PaperRepository.UnitOfWork.Add(paper); ExamPaperDAL.PaperRepository.UnitOfWork.Commit(); } /// /// 根据试卷id获取试卷信息以json格式返回 /// /// /// public string GetPaperQuestions(decimal paperid) { test_paper paper = ExamPaperDAL.PaperRepository.GetSingle(q => q.test_paper_id == paperid); if (paper.is_dynamic) { var question = GetDynamicPaperById(paperid); return question; } else { var question = ExamPaperDAL.GetPaperQuestions(paperid); return new JavaScriptSerializer().Serialize(question); } } /// /// 获取动态试卷 /// /// /// public string GetDynamicPaperById(decimal paperid) { var t_paper = ExamPaperDAL.PaperRepository.GetSingle(q => q.test_paper_id == paperid); if (t_paper != null) { var json = new JavaScriptSerializer(); var libs = json.Deserialize>(t_paper.note); decimal diff_min = t_paper.difficulty_degree_minvalue.HasValue ? t_paper.difficulty_degree_minvalue.Value : 0m; decimal diff_max = t_paper.difficulty_degree_maxvalue.HasValue ? t_paper.difficulty_degree_maxvalue.Value : 0m; decimal wrong_rate = t_paper.error_percent.HasValue ? t_paper.error_percent.Value : 0m; int usedcount_min = t_paper.used_count_min.HasValue ? t_paper.used_count_min.Value : 0; int usedcount_max = t_paper.used_count_max.HasValue ? t_paper.used_count_max.Value : 0; int prority = t_paper.Prority.HasValue ? t_paper.Prority.Value : 0; var ques_setting = new JavaScriptSerializer().Deserialize(t_paper.question_setting); var questions = from q in ExamPaperDAL.questionRepository.Entities where q.difficulty_degree.Value >= diff_min && q.difficulty_degree.Value <= diff_max && q.used_count >= usedcount_min && q.used_count <= usedcount_max && q.wrong_percent >= wrong_rate && q.filled == false select q; var filterQuestions = questions.Where(q => 1 == 2); foreach (var item in libs) { int libid = Convert.ToInt32(item.Libaryid); int libnum = Convert.ToInt32(item.Number); filterQuestions = filterQuestions.Union(questions.Where(q => q.test_question_libary_id == libid).Take(libnum)); } questions = filterQuestions; if (prority == 1) { questions = questions.OrderByDescending(q => q.used_count); } else if (prority == 2) { questions = questions.OrderByDescending(q => q.created_date); } else if (prority == 3) { questions = questions.OrderByDescending(q => q.created_date).OrderByDescending(q => q.used_count); } var question2 = from q in questions orderby q.test_question_Id select new { question_id = q.test_question_Id, typeName = q.test_base_question_type.Name, q.content, q.base_question_type_id, hasImg = q.question_file_id.HasValue, url = q.question_file_id.HasValue ? q.test_question_file.url : "", q.score, rightAnswer = q.answers, answers = q.test_question_provid_answer.Select(a => new Answers { content = a.answer_name, answer_id = a.provid_answer_id, hasImg = a.file_id.HasValue, url = a.file_id.HasValue ? a.test_question_file.url : "", }) }; var result = new List(); foreach (var t in ques_setting) { var questionInstance = question2.Where(q => q.base_question_type_id == t.QType).Take(t.PSum).ToArray(); var rs = new { id = t.QType, tscore = t.QScore * t.PSum, qscore = t.QScore, name = questionInstance.Select(type => type.typeName).FirstOrDefault(), questions = questionInstance }; if (rs.questions.Count() > 0) result.Add(rs); } return new JavaScriptSerializer().Serialize(result.ToArray()); } return ""; } } }