123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532 |
- 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<Func<test_paper, bool>> 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);
- }
- /// <summary>
- /// 根据一序列的条件查找不同题型的题数是多少
- /// </summary>
- /// <param name="lib_ids"></param>
- /// <param name="diff_min"></param>
- /// <param name="diff_max"></param>
- /// <param name="err_rate"></param>
- /// <param name="usecount_min"></param>
- /// <param name="usecount_max"></param>
- /// <param name="question_type"></param>
- /// <returns></returns>
- public object CountQuesSumByCondition(List<decimal> 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<List<QuestionLibaryArrangeView>>(ques_lib);
- return string.Join(",", libarys.Select(q => q.Libaryid).ToArray());
- }
- private decimal CalcPaperScore(string ques)
- {
- var json = new JavaScriptSerializer();
- var libarys = json.Deserialize<List<QuestionTypeView>>(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;
- }
- /// <summary>
- /// 获取选择值
- /// </summary>
- /// <returns></returns>
- 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<List<QuestionLibaryArrangeView>>(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;
- }
- /// <summary>
- /// 按题库数量从各题库筛选试题
- /// </summary>
- /// <param name="dt"></param>
- /// <param name="typeid"></param>
- /// <returns></returns>
- 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;
- }
- /// <summary>
- /// 试题过滤
- /// </summary>
- /// <param name="dt"></param>
- /// <param name="chooseSortBy"></param>
- /// <param name="pageType"></param>
- /// <param name="quesDis"></param>
- /// <param name="pageSetting"></param>
- /// <returns></returns>
- private DataTable QuestionFilter(DataTable dt, string chooseSortBy, string pageType, string quesDis, string pageSetting)
- {
- var json = new JavaScriptSerializer();
- var pageSettingArray = json.Deserialize<List<QuestionTypeView>>(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;
- }
- /// <summary>
- /// 抽取试题第一道工序
- /// 过滤不同类型的试题形成各个子Table
- /// </summary>
- /// <param name="dt"></param>
- /// <param name="typeid"></param>
- /// <returns></returns>
- 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<QuestionXml> xmlValues = new List<QuestionXml>();
- 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<QuestionXml>));
- 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<QuestionXml> xmlValues = new List<QuestionXml>();
- 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<QuestionXml>));
- 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();
- }
- /// <summary>
- /// 根据试卷id获取试卷信息以json格式返回
- /// </summary>
- /// <param name="paperid"></param>
- /// <returns></returns>
- 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);
- }
- }
- /// <summary>
- /// 获取动态试卷
- /// </summary>
- /// <param name="paperid"></param>
- /// <returns></returns>
- 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<List<QuestionLibaryArrangeView>>(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<QuestionSettingJsonObj[]>(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<object>();
- 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 "";
- }
- }
- }
|