using System; using System.Collections.Generic; using System.Text; using System.Data; using System.Linq; using WeiSha.Common; using Song.Entities; using WeiSha.Data; using Song.ServiceInterfaces; using System.Data.Common; using System.Text.RegularExpressions; using NPOI.HSSF.UserModel; using NPOI.SS.UserModel; using System.Xml; using System.Reflection; using System.Threading; namespace Song.ServiceImpls { public class QuestionsCom : IQuestions { #region 试题管理 public int QuesAdd(Questions entity) { entity.Qus_CrtTime = DateTime.Now; entity.Qus_LastTime = DateTime.Now; entity.Qus_Title = _ClearString(entity.Qus_Title); entity.Qus_Answer = _ClearString(entity.Qus_Answer); entity.Qus_Explain = _ClearString(entity.Qus_Explain); Song.Entities.Organization org = Business.Do().OrganCurrent(); if (org != null) entity.Org_ID = org.Org_ID; Gateway.Default.Save(entity); this.OnAdd(entity, EventArgs.Empty); return entity.Qus_ID; } public void QuesSave(Questions entity) { entity.Qus_LastTime = DateTime.Now; entity.Qus_IsError = false; entity.Qus_Title = _ClearString(entity.Qus_Title); entity.Qus_Answer = _ClearString(entity.Qus_Answer); //if (entity.Qus_Type == 3) //{ // if (entity.Qus_IsCorrect == false ) entity.Qus_IsError = true; //} if (entity.Qus_Type == 4) { if (entity.Qus_Answer == string.Empty || entity.Qus_Answer.Trim() == "") entity.Qus_IsError = true; } entity.Qus_Explain = _ClearString(entity.Qus_Explain); using (DbTrans tran = Gateway.Default.BeginTrans()) { try { tran.Save(entity); tran.Update(new Field[] { QuesAnswer._.Qus_ID }, new object[] { entity.Qus_ID }, QuesAnswer._.Qus_UID == entity.Qus_UID); tran.Commit(); QuestionsMethod.QuestionsCache.Singleton.UpdateSingle(entity); this.OnSave(entity, EventArgs.Empty); } catch (Exception ex) { tran.Rollback(); throw ex; } finally { tran.Close(); } } } public void QuesInput(Questions entity, List ansItem) { if (entity.Qus_ID < 1) entity.Qus_CrtTime = DateTime.Now; entity.Qus_Title = _ClearString(entity.Qus_Title); entity.Qus_Answer = _ClearString(entity.Qus_Answer); entity.Qus_Explain = _ClearString(entity.Qus_Explain); //答题选项的处理 if (ansItem != null) { for (int i = 0; i < ansItem.Count; i++) { //添加随机的选择项id Random rd = new Random((i + 1) * DateTime.Now.Millisecond); ansItem[i].Ans_ID = rd.Next(1, 1000); //如果有试题id,则加上,好像也无所谓 if (entity.Qus_ID > 0) ansItem[i].Qus_ID = entity.Qus_ID; } entity.Qus_Items = this.AnswerToItems(ansItem.ToArray()); } Gateway.Default.Save(entity); } public void QuesDelete(int identify) { Song.Entities.Questions entity = this.QuesSingle(identify); if (entity == null) return; using (DbTrans tran = Gateway.Default.BeginTrans()) { QuesDelete(entity, tran); } } private void QuesDelete(Questions entity, DbTrans tran) { try { if (entity != null) { tran.Delete(Questions._.Qus_ID == entity.Qus_ID); tran.Delete(QuesAnswer._.Qus_ID == entity.Qus_ID); tran.Delete(QuesAnswer._.Qus_UID == entity.Qus_UID); tran.Delete(Student_Notes._.Qus_ID == entity.Qus_ID); tran.Delete(Student_Collect._.Qus_ID == entity.Qus_ID); tran.Commit(); } } catch (Exception ex) { tran.Rollback(); throw ex; } finally { tran.Close(); } this.OnDelete(entity, null); } public void QuesDelete(string ids) { using (DbTrans tran = Gateway.Default.BeginTrans()) { try { foreach (string idstr in ids.Split(',')) { int intid = 0; int.TryParse(idstr, out intid); if (intid < 1) continue; tran.Delete(Questions._.Qus_ID == intid); tran.Delete(QuesAnswer._.Qus_ID == intid); } tran.Commit(); } catch (Exception ex) { tran.Rollback(); throw ex; } finally { tran.Close(); } } this.OnDelete(null, null); } public Questions QuesSingle(int identify) { Song.Entities.Questions qus = QuestionsMethod.QuestionsCache.Singleton.GetSingle(identify); if (qus == null) qus = Gateway.Default.From().Where(Questions._.Qus_ID == identify).ToFirst(); if (qus == null) return qus; return qus; } /// /// 获取单一实体对象,按主键ID; /// /// /// 是否来自缓存 /// public Questions QuesSingle(int identify, bool cache) { if (cache) return this.QuesSingle(identify); return Gateway.Default.From().Where(Questions._.Qus_ID == identify).ToFirst(); } public Questions QuesSingle(string uid) { if (uid == string.Empty) return null; Song.Entities.Questions qus = QuestionsMethod.QuestionsCache.Singleton.GetSingle(uid); if (qus == null) qus = Gateway.Default.From().Where(Questions._.Qus_UID == uid.Trim() && Questions._.Qus_IsTitle == true).ToFirst(); if (qus == null) return qus; return qus; } public Questions QuesSingle(string titile, int type) { if (titile == string.Empty) return null; WhereClip wc = new WhereClip(); if (type > 0) wc.And(Questions._.Qus_Type == type); if (titile.Trim() != "") wc.And(Questions._.Qus_Title.Like(titile.Trim())); Song.Entities.Questions qus = Gateway.Default.From().Where(wc).ToFirst(); if (qus == null) return qus; return qus; } public QuesAnswer[] QuestionsAnswer(Questions qus, bool? isCorrect) { Song.Entities.QuesAnswer[] ans = this.ItemsToAnswer(qus.Qus_Items, isCorrect); return ans; } public Questions[] QuesCount(int type, bool? isUse, int count) { WhereClip wc = Questions._.Qus_IsError == false && Questions._.Qus_IsWrong == false; if (type > 0) wc.And(Questions._.Qus_Type == type); if (isUse != null) wc.And(Questions._.Qus_IsUse == (bool)isUse); return Gateway.Default.From().Where(wc) .OrderBy(Questions._.Qus_Type.Asc && Questions._.Qus_Tax.Asc && Questions._.Qus_ID.Asc) .ToArray(count); } public Questions[] QuesCount(int orgid, int sbjid, int couid, int olid, int type, int diff, bool? isUse, int count) { WhereClip wc = new WhereClip(); if (orgid > 0) wc.And(Questions._.Org_ID == orgid); if (sbjid > 0) wc.And(Questions._.Sbj_ID == sbjid); if (couid > 0) wc.And(Questions._.Cou_ID == couid); //当前章节,以及当前章节之下的所有试题 if (olid > 0) { WhereClip wcSbjid = new WhereClip(); List list = Business.Do().TreeID(olid); foreach (int l in list) wcSbjid.Or(Questions._.Ol_ID == l); wc.And(wcSbjid); } if (type > 0) wc.And(Questions._.Qus_Type == type); if (diff > 0) wc.And(Questions._.Qus_Diff == diff); if (isUse != null) wc.And(Questions._.Qus_IsUse == (bool)isUse); return Gateway.Default.From().Where(wc) .OrderBy(Questions._.Qus_Type.Asc && Questions._.Qus_Tax.Asc && Questions._.Qus_ID.Asc) .ToArray(count); } /// /// 获取某个课程或章节试题 /// /// 机构id /// 专业id /// 课程id /// 章节id /// 试题类型 /// 难度等级 /// /// 起始索引 /// 取多少条 /// public Questions[] QuesCount(int orgid, int sbjid, int couid, int olid, int type, int diff, bool? isUse, int index, int count) { WhereClip wc = new WhereClip(); if (orgid > 0) wc.And(Questions._.Org_ID == orgid); if (sbjid > 0) wc.And(Questions._.Sbj_ID == sbjid); if (couid > 0) wc.And(Questions._.Cou_ID == couid); //当前章节,以及当前章节之下的所有试题 if (olid > 0) { WhereClip wcSbjid = new WhereClip(); List list = Business.Do().TreeID(olid); foreach (int l in list) wcSbjid.Or(Questions._.Ol_ID == l); wc.And(wcSbjid); } if (type > 0) wc.And(Questions._.Qus_Type == type); if (diff > 0) wc.And(Questions._.Qus_Diff == diff); if (isUse != null) wc.And(Questions._.Qus_IsUse == (bool)isUse); return Gateway.Default.From().Where(wc) .OrderBy(Questions._.Qus_Type.Asc && Questions._.Qus_Tax.Asc && Questions._.Qus_ID.Asc) .ToArray(count, index); } /// /// 计算试题数量 /// /// 机构id /// 专业id /// 课程id /// 章节id /// 试题类型 /// 是否使用 /// public int QuesOfCount(int orgid, int sbjid, int couid, int olid, int type, bool? isUse) { WhereClip wc = new WhereClip(); if (orgid > -1) wc.And(Questions._.Org_ID == orgid); if (sbjid > 0) wc.And(Questions._.Sbj_ID == sbjid); if (couid > 0) wc.And(Questions._.Cou_ID == couid); //当前章节,以及当前章节之下的所有试题 if (olid > 0) { WhereClip wcSbjid = new WhereClip(); List list = Business.Do().TreeID(olid); foreach (int l in list) wcSbjid.Or(Questions._.Ol_ID == l); wc.And(wcSbjid); } if (type > 0) wc.And(Questions._.Qus_Type == type); if (isUse != null) wc.And(Questions._.Qus_IsUse == (bool)isUse); return Gateway.Default.Count(wc); } public int QuesOfCount(int orgid, int sbjid, int couid, int olid, int type, int diff, bool? isUse) { WhereClip wc = new WhereClip(); if (orgid > -1) wc.And(Questions._.Org_ID == orgid); if (sbjid > 0) wc.And(Questions._.Sbj_ID == sbjid); if (couid > 0) wc.And(Questions._.Cou_ID == couid); //当前章节,以及当前章节之下的所有试题 if (olid > 0) { WhereClip wcSbjid = new WhereClip(); List list = Business.Do().TreeID(olid); foreach (int l in list) wcSbjid.Or(Questions._.Ol_ID == l); wc.And(wcSbjid); } if (type > 0) wc.And(Questions._.Qus_Type == type); if (diff > 0) wc.And(Questions._.Qus_Diff == diff); if (isUse != null) wc.And(Questions._.Qus_IsUse == (bool)isUse); return Gateway.Default.Count(wc); } public DataSet QuesAns(int identify) { return Gateway.Default.From().InnerJoin(QuesAnswer._.Qus_UID == Questions._.Qus_UID) .Where(Questions._.Qus_ID == identify).OrderBy(QuesAnswer._.Ans_ID.Asc).ToDataSet(); } /// /// 获取随机试题 /// /// 机构id /// 专业id /// 课程id /// 章节id /// 试题类型 /// 难度范围 /// 难度范围 /// 是否允许 /// 取的数量 /// public Questions[] QuesRandom(int orgid, int sbjid, int couid, int olid, int type, int diff1, int diff2, bool? isUse, int count) { #region //试题类型 string[] types = WeiSha.Common.App.Get["QuesType"].Split(','); if (type < 1 || type > types.Length) type = -1; //难度区间 diff1 = diff1 < 1 ? 1 : diff1; diff2 = diff2 < 1 || diff2 > 5 ? 5 : diff2; //基本属性 string where = " Qus_IsError=false and org_id=" + orgid + " "; where = string.Format(where, diff1, diff2); if (type > 0) where += " and Qus_Type=" + type; //试题类型 if (diff1 > 0) where += " and Qus_Diff>=" + diff1; //最小难度等级 if (diff2 > 0) where += " and Qus_Diff<=" + diff2; //最大难度 if (isUse != null) where += " and Qus_IsUse=" + ((bool)isUse).ToString().ToLower(); //是否包括未使用的试题,true为只限启用的试题 if (olid > 0) { //章节id string olstr = _quesRandom_buildOlid(olid); where += " and (Ol_ID=" + olid + olstr + ")"; } else { if (couid > 0) { where += " and Cou_ID=" + couid; //课程 } else { if (sbjid > 0) where += " and Sbj_ID=" + sbjid; //专业id } } //根据不同的数据库拼接SQL语句 string sql = ""; string dataype = WeiSha.Common.Server.DatabaseType; //数据库类型 if (dataype != "access") { sql = "select top " + count + " *, newid() as n from Questions where " + where + " order by n"; sql = sql.Replace("true", "1"); sql = sql.Replace("false", "0"); } else { int rdid = new System.Random(unchecked((int)DateTime.Now.Ticks)).Next(); sql = "select top " + count + " * from Questions where " + where + " order by rnd(" + (-1 * rdid) + "*Qus_ID)"; sql = "select * from (" + sql + ") as t order by t.Qus_Type asc"; return Gateway.Default.FromSql(sql).ToArray(); } sql = "select * from (" + sql + ") as t order by t.Qus_Type asc"; #endregion // //string sqlProc = "exec PROC_QuesRandom {0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}, {8}"; //sqlProc = string.Format(sqlProc, orgid, sbjid, couid, olid, type, diff1, diff2, 1, count); return Gateway.Default.FromSql(sql).ToArray(); } /// /// 为了获取章节下面的子章节中的试题,生成相关条件判断 /// /// /// private string _quesRandom_buildOlid(int olid) { string sql = ""; Outline[] ols = Gateway.Default.From().Where(Outline._.Ol_PID == olid).ToArray(); foreach (Outline o in ols) { sql += " or Ol_ID=" + o.Ol_ID; sql += _quesRandom_buildOlid(o.Ol_ID); } return sql; } public Questions[] QuesRandom(int type, int sbjId, int couid, int diff1, int diff2, bool? isUse, int count) { return this.QuesRandom(-1, sbjId, couid, -1, type, diff1, diff2, isUse, count); } public Questions[] QuesPager(int orgid, int type, bool? isUse, string searTxt, int size, int index, out int countSum) { WhereClip wc = new WhereClip(); if (orgid > -1) wc.And(Questions._.Org_ID == orgid); if (type > 0) wc.And(Questions._.Qus_Type == type); if (isUse != null) wc.And(Questions._.Qus_IsUse == (bool)isUse); if (!string.IsNullOrWhiteSpace(searTxt) && searTxt.Trim() != "") { wc.And(Questions._.Qus_Title.Like("%" + searTxt.Trim() + "%")); } countSum = Gateway.Default.Count(wc); return Gateway.Default.From().Where(wc) .OrderBy(Questions._.Qus_Tax.Asc && Questions._.Qus_ID.Desc) .ToArray(size, (index - 1) * size); } /// /// 分页获取试题 /// /// 试题类型,1为单选,2为多选 /// 试题是否启用的状态,如为null取所有试题 /// 难度 /// 检索的字符串 /// 每页显示多少条 /// 当前页索引 /// 共计多少条 /// public Questions[] QuesPager(int orgid, int type, bool? isUse, int diff, string searTxt, int size, int index, out int countSum) { WhereClip wc = new WhereClip(); if (orgid > -1) wc.And(Questions._.Org_ID == orgid); if (type > 0) wc.And(Questions._.Qus_Type == type); if (isUse != null) wc.And(Questions._.Qus_IsUse == (bool)isUse); if (diff > 0 && diff <= 5) wc.And(Questions._.Qus_Diff == diff); if (searTxt != string.Empty && searTxt.ToLower() != "") wc.And(Questions._.Qus_Title.Like("%" + searTxt.Trim() + "%")); countSum = Gateway.Default.Count(wc); return Gateway.Default.From() .Where(wc).OrderBy(Questions._.Qus_Tax.Asc && Questions._.Qus_ID.Desc) .ToArray(size, (index - 1) * size); } public Questions[] QuesPager(int orgid, int type, int sbjId, int couid, int olid, bool? isUse, bool? isError, bool? isWrong, int diff, string searTxt, int size, int index, out int countSum) { WhereClip wc = new WhereClip(); if (orgid > -1) wc.And(Questions._.Org_ID == orgid); if (type > 0) wc.And(Questions._.Qus_Type == type); if (sbjId > 0) wc.And(Questions._.Sbj_ID == sbjId); if (couid > 0) wc.And(Questions._.Cou_ID == couid); //当前章节,以及当前章节之下的所有试题 if (olid > 0) { WhereClip wcSbjid = new WhereClip(); List list = Business.Do().TreeID(olid); foreach (int l in list) wcSbjid.Or(Questions._.Ol_ID == l); wc.And(wcSbjid); } if (isUse != null) wc.And(Questions._.Qus_IsUse == (bool)isUse); if (isError != null) wc.And(Questions._.Qus_IsError == (bool)isError); if (isWrong != null) wc.And(Questions._.Qus_IsWrong == (bool)isWrong); if (diff > 0 && diff <= 5) wc.And(Questions._.Qus_Diff == diff); if (!string.IsNullOrWhiteSpace(searTxt) && searTxt.ToLower() != "") { wc.And(Questions._.Qus_Title.Like("%" + searTxt.Trim() + "%")); } countSum = Gateway.Default.Count(wc); return Gateway.Default.From().Where(wc) .OrderBy(Questions._.Qus_Tax.Asc && Questions._.Qus_ID.Desc) .ToArray(size, (index - 1) * size); } #endregion #region 试题导出 /// /// 导出试题 /// /// 所属机构 /// 试题类型,如单选,多选等 /// 专业id /// 课程id /// 章节id /// 难度等级,如1,2这样的字符串 /// 是否包括错误的试题,如果为空,则不作判断 /// 是否包括学员反馈的试题,如果为空,则不作判断 /// public HSSFWorkbook QuestionsExport(int orgid, string type, int sbjId, int couid, int olid, string diff, bool? isError, bool? isWrong) { HSSFWorkbook hssfworkbook = new HSSFWorkbook(); WhereClip wc = new WhereClip(); if (orgid > -1) wc.And(Questions._.Org_ID == orgid); if (sbjId > 0) wc.And(Questions._.Sbj_ID == sbjId); if (couid > 0) wc.And(Questions._.Cou_ID == couid); if (olid > 0) wc.And(Questions._.Ol_ID == olid); if (isError != null) wc.And(Questions._.Qus_IsError == (bool)isError); if (isWrong != null) wc.And(Questions._.Qus_IsWrong == (bool)isWrong); //难度等级 WhereClip diffWc = new WhereClip(); foreach (string s in diff.Split(',')) { if (string.IsNullOrWhiteSpace(s)) continue; int df = 0; int.TryParse(s, out df); if (df == 0) continue; diffWc.Or(Questions._.Qus_Diff == df); } wc.And(diffWc); //试题类型,通过不同的试题类型返回工作表 foreach (string t in type.Split(',')) { if (string.IsNullOrWhiteSpace(t)) continue; int ty = 0; int.TryParse(t, out ty); if (ty == 0) continue; if (ty == 1) _buildExcelSql_1(hssfworkbook, wc); if (ty == 2) _buildExcelSql_2(hssfworkbook, wc); if (ty == 3) _buildExcelSql_3(hssfworkbook, wc); if (ty == 4) _buildExcelSql_4(hssfworkbook, wc); if (ty == 5) _buildExcelSql_5(hssfworkbook, wc); } return hssfworkbook; } private void _buildExcelSql_1(HSSFWorkbook hssfworkbook, WhereClip where) { WhereClip wc = (Questions._.Qus_Type == 1).And(where); Song.Entities.Questions[] ques = Gateway.Default.From().Where(wc).OrderBy(Questions._.Qus_ID.Asc).ToArray(); //创建工作簿对象 ISheet sheet = hssfworkbook.CreateSheet("单选题"); //创建数据行对象 IRow rowHead = sheet.CreateRow(0); //创建表头 string[] cells = new string[] { "ID", "题目", "专业", "课程", "章节", "难度", "答案选项1", "答案选项2", "答案选项3", "答案选项4", "答案选项5", "答案选项6", "正确答案", "试题讲解" }; for (int h = 0; h < cells.Length; h++) rowHead.CreateCell(h).SetCellValue(cells[h]); //生成数据行 ICellStyle style_size = hssfworkbook.CreateCellStyle(); style_size.WrapText = true; int i = 0; foreach (Song.Entities.Questions q in ques) { IRow row = sheet.CreateRow(i + 1); string tmpVal = ""; QuesAnswer[] qas = Business.Do().QuestionsAnswer(q, null); int ansIndex = 0; for (int j = 0; j < qas.Length; j++) { QuesAnswer c = qas[j]; tmpVal = c.Ans_Context; row.CreateCell(6 + j).SetCellValue(tmpVal); if (c.Ans_IsCorrect) ansIndex = j + 1; } row.CreateCell(0).SetCellValue(q.Qus_ID); row.CreateCell(1).SetCellValue(q.Qus_Title); //专业,课程,章节 row.CreateCell(2).SetCellValue(Business.Do().SubjectName(q.Sbj_ID)); row.CreateCell(3).SetCellValue(Business.Do().CourseName(q.Cou_ID)); row.CreateCell(4).SetCellValue(Business.Do().OutlineName(q.Ol_ID)); row.CreateCell(5).SetCellValue((int)q.Qus_Diff); row.CreateCell(12).SetCellValue(ansIndex.ToString()); row.CreateCell(13).SetCellValue(q.Qus_Explain); i++; } } //多选题导出 private void _buildExcelSql_2(HSSFWorkbook hssfworkbook,WhereClip where) { WhereClip wc = (Questions._.Qus_Type == 2).And(where); Song.Entities.Questions[] ques = Gateway.Default.From().Where(wc).OrderBy(Questions._.Qus_ID.Asc).ToArray(); //创建工作簿对象 ISheet sheet = hssfworkbook.CreateSheet("多选题"); //sheet.DefaultColumnWidth = 30; //创建数据行对象 IRow rowHead = sheet.CreateRow(0); //创建表头 string[] cells = new string[] { "ID", "题目", "专业", "课程", "章节", "难度", "答案选项1", "答案选项2", "答案选项3", "答案选项4", "答案选项5", "答案选项6", "正确答案", "试题讲解" }; for (int h = 0; h < cells.Length; h++) rowHead.CreateCell(h).SetCellValue(cells[h]); //生成数据行 ICellStyle style_size = hssfworkbook.CreateCellStyle(); style_size.WrapText = true; int i = 0; foreach (Song.Entities.Questions q in ques) { IRow row = sheet.CreateRow(i + 1); string tmpVal = ""; QuesAnswer[] qas = Business.Do().QuestionsAnswer(q, null); string ansIndex = ""; for (int j = 0; j < qas.Length; j++) { QuesAnswer c = qas[j]; tmpVal = c.Ans_Context; row.CreateCell(6 + j).SetCellValue(tmpVal); if (c.Ans_IsCorrect) ansIndex += Convert.ToString(j + 1) + ","; } if (ansIndex.Length > 0) { if (ansIndex.Substring(ansIndex.Length - 1) == ",") ansIndex = ansIndex.Substring(0, ansIndex.Length - 1); } row.CreateCell(0).SetCellValue(q.Qus_ID); row.CreateCell(1).SetCellValue(q.Qus_Title); //专业,课程,章节 row.CreateCell(2).SetCellValue(Business.Do().SubjectName(q.Sbj_ID)); row.CreateCell(3).SetCellValue(Business.Do().CourseName(q.Cou_ID)); row.CreateCell(4).SetCellValue(Business.Do().OutlineName(q.Ol_ID)); row.CreateCell(5).SetCellValue((int)q.Qus_Diff); row.CreateCell(12).SetCellValue(ansIndex.ToString()); row.CreateCell(13).SetCellValue(q.Qus_Explain); i++; } } //判断题导出 private void _buildExcelSql_3(HSSFWorkbook hssfworkbook, WhereClip where) { WhereClip wc = (Questions._.Qus_Type == 3).And(where); Song.Entities.Questions[] ques = Gateway.Default.From().Where(wc).OrderBy(Questions._.Qus_ID.Asc).ToArray(); //创建工作簿对象 ISheet sheet = hssfworkbook.CreateSheet("判断题"); //sheet.DefaultColumnWidth = 30; //创建数据行对象 IRow rowHead = sheet.CreateRow(0); //创建表头 string[] cells = new string[] { "ID", "题目", "专业", "课程", "章节", "难度", "答案", "试题讲解" }; for (int h = 0; h < cells.Length; h++) rowHead.CreateCell(h).SetCellValue(cells[h]); //生成数据行 ICellStyle style_size = hssfworkbook.CreateCellStyle(); style_size.WrapText = true; int i = 0; foreach (Song.Entities.Questions q in ques) { string ans = ""; if (Convert.ToString(q.Qus_IsCorrect) == "False") { ans = "错误"; } else { ans = "正确"; } IRow row = sheet.CreateRow(i + 1); row.CreateCell(0).SetCellValue(q.Qus_ID); row.CreateCell(1).SetCellValue(q.Qus_Title); //专业,课程,章节 row.CreateCell(2).SetCellValue(Business.Do().SubjectName(q.Sbj_ID)); row.CreateCell(3).SetCellValue(Business.Do().CourseName(q.Cou_ID)); row.CreateCell(4).SetCellValue(Business.Do().OutlineName(q.Ol_ID)); row.CreateCell(5).SetCellValue((int)q.Qus_Diff); row.CreateCell(6).SetCellValue(ans); row.CreateCell(7).SetCellValue(q.Qus_Explain); i++; } } //简答题导出 private void _buildExcelSql_4(HSSFWorkbook hssfworkbook, WhereClip where) { WhereClip wc = (Questions._.Qus_Type == 4).And(where); Song.Entities.Questions[] ques = Gateway.Default.From().Where(wc).OrderBy(Questions._.Qus_ID.Asc).ToArray(); //创建工作簿对象 ISheet sheet = hssfworkbook.CreateSheet("简答题"); //创建数据行对象 IRow rowHead = sheet.CreateRow(0); //创建表头 string[] cells = new string[] { "ID", "题目", "专业", "课程", "章节", "难度", "答案", "试题讲解" }; for (int h = 0; h < cells.Length; h++) rowHead.CreateCell(h).SetCellValue(cells[h]); //生成数据行 ICellStyle style_size = hssfworkbook.CreateCellStyle(); style_size.WrapText = true; int i = 0; foreach (Song.Entities.Questions q in ques) { IRow row = sheet.CreateRow(i + 1); row.CreateCell(0).SetCellValue(q.Qus_ID); row.CreateCell(1).SetCellValue(q.Qus_Title); //专业,课程,章节 row.CreateCell(2).SetCellValue(Business.Do().SubjectName(q.Sbj_ID)); row.CreateCell(3).SetCellValue(Business.Do().CourseName(q.Cou_ID)); row.CreateCell(4).SetCellValue(Business.Do().OutlineName(q.Ol_ID)); row.CreateCell(5).SetCellValue((int)q.Qus_Diff); row.CreateCell(6).SetCellValue(q.Qus_Answer); row.CreateCell(7).SetCellValue(q.Qus_Explain); i++; } } //填空题导出 private void _buildExcelSql_5(HSSFWorkbook hssfworkbook, WhereClip where) { WhereClip wc = (Questions._.Qus_Type == 5).And(where); Song.Entities.Questions[] ques = Gateway.Default.From().Where(wc).OrderBy(Questions._.Qus_ID.Asc).ToArray(); //创建工作簿对象 ISheet sheet = hssfworkbook.CreateSheet("填空题"); //创建数据行对象 IRow rowHead = sheet.CreateRow(0); //创建表头 string[] cells = new string[] { "ID", "题目", "专业", "课程", "章节", "难度", "答案选项1", "答案选项2", "答案选项3", "答案选项4", "答案选项5", "答案选项6", "试题讲解" }; for (int h = 0; h < cells.Length; h++) rowHead.CreateCell(h).SetCellValue(cells[h]); //生成数据行 ICellStyle style_size = hssfworkbook.CreateCellStyle(); style_size.WrapText = true; int i = 0; foreach (Song.Entities.Questions q in ques) { IRow row = sheet.CreateRow(i + 1); string tmpVal = ""; QuesAnswer[] qas = Business.Do().QuestionsAnswer(q, null); for (int j = 0; j < qas.Length; j++) { QuesAnswer c = qas[j]; tmpVal = c.Ans_Context; row.CreateCell(6 + j).SetCellValue(tmpVal); } row.CreateCell(0).SetCellValue(q.Qus_ID); row.CreateCell(1).SetCellValue(q.Qus_Title); //专业,课程,章节 row.CreateCell(2).SetCellValue(Business.Do().SubjectName(q.Sbj_ID)); row.CreateCell(3).SetCellValue(Business.Do().CourseName(q.Cou_ID)); row.CreateCell(4).SetCellValue(Business.Do().OutlineName(q.Ol_ID)); row.CreateCell(5).SetCellValue((int)q.Qus_Diff); row.CreateCell(12).SetCellValue(q.Qus_Explain); i++; } } #endregion #region 试题分类管理 /// /// 添加试题类型 /// /// 业务实体 public int TypeAdd(QuesTypes entity) { //如果没有排序号,则自动计算 if (entity.Qt_Tax < 1) { object obj = Gateway.Default.Max(QuesTypes._.Qt_Tax, QuesTypes._.Cou_ID == entity.Cou_ID); entity.Qt_Tax = obj is int ? (int)obj + 1 : 0; } Gateway.Default.Save(entity); return entity.Qt_ID; } /// /// 修改 /// /// 业务实体 public void TypeSave(QuesTypes entity) { Gateway.Default.Save(entity); } /// /// 删除,按主键ID; /// /// 实体的主键 public void TypeDelete(int identify) { Questions[] ques = Gateway.Default.From().Where(Questions._.Qt_ID == identify).ToArray(); foreach (Questions q in ques) { using (DbTrans tran = Gateway.Default.BeginTrans()) { QuesDelete(q, tran); } } Gateway.Default.Delete(QuesTypes._.Qt_ID == identify); } /// /// 清理课程下的试题分类 /// /// 课程id public void TypeClear(int couid) { QuesTypes[] types = Gateway.Default.From().Where(QuesTypes._.Cou_ID == couid).ToArray(); foreach (QuesTypes t in types) { TypeDelete(t.Qt_ID); } } /// /// 获取单一实体对象,按主键ID; /// /// 实体的主键 /// public QuesTypes TypeSingle(int identify) { Song.Entities.QuesTypes qus = Gateway.Default.From().Where(QuesTypes._.Qt_ID == identify).ToFirst(); return qus; } /// /// 获取某个学科的所有试题分类 /// /// 课程id /// 是否展示 /// 取多少条,小于1取所有 /// public QuesTypes[] TypeCount(int couid, bool? isUse, int count) { WhereClip wc = new WhereClip(); if (couid > 0) wc.And(QuesTypes._.Cou_ID == couid); if (isUse != null) wc.And(QuesTypes._.Qt_IsUse == (bool)isUse); return Gateway.Default.From().Where(wc).OrderBy(QuesTypes._.Qt_Tax.Asc).ToArray(count); } /// /// 将当前项目向上移动;仅在当前对象的同层移动,即同一父节点下的对象这前移动; /// /// /// 如果已经处于顶端,则返回false;移动成功,返回true public bool TypeRemoveUp(int id) { //当前对象 QuesTypes current = Gateway.Default.From().Where(QuesTypes._.Qt_ID == id).ToFirst(); int tax = (int)current.Qt_Tax; //上一个对象,即兄长对象;兄长不存则直接返回false; QuesTypes prev = Gateway.Default.From() .Where(QuesTypes._.Qt_Tax < tax && QuesTypes._.Cou_ID == current.Cou_ID) .OrderBy(QuesTypes._.Qt_Tax.Desc).ToFirst(); if (prev == null) return false; //交换排序号 current.Qt_Tax = prev.Qt_Tax; prev.Qt_Tax = tax; using (DbTrans tran = Gateway.Default.BeginTrans()) { try { tran.Save(current); tran.Save(prev); tran.Commit(); return true; } catch { tran.Rollback(); throw; } finally { tran.Close(); } } } /// /// 将当前项目向下移动;仅在当前对象的同层移动,即同一父节点下的对象这前移动; /// /// /// 如果已经处于顶端,则返回false;移动成功,返回true public bool TypeRemoveDown(int id) { //当前对象 QuesTypes current = Gateway.Default.From().Where(QuesTypes._.Qt_ID == id).ToFirst(); int tax = (int)current.Qt_Tax; //下一个对象,即弟弟对象;弟弟不存则直接返回false; QuesTypes next = Gateway.Default.From() .Where(QuesTypes._.Qt_Tax > tax && QuesTypes._.Cou_ID == current.Cou_ID) .OrderBy(QuesTypes._.Qt_Tax.Asc).ToFirst(); if (next == null) return false; //交换排序号 current.Qt_Tax = next.Qt_Tax; next.Qt_Tax = tax; using (DbTrans tran = Gateway.Default.BeginTrans()) { try { tran.Save(current); tran.Save(next); tran.Commit(); return true; } catch { tran.Rollback(); throw; } finally { tran.Close(); } } } #endregion #region 试题答案或选项 /// /// 将试题的答题选项转换为xml字符串 /// /// /// public string AnswerToItems(Song.Entities.QuesAnswer[] ans) { if (ans == null || ans.Length < 1) return null; XmlDocument xmlDoc = new XmlDocument(); //创建根节点 XmlNode root = xmlDoc.CreateElement("Items"); xmlDoc.AppendChild(root); //创建单个记录的节点 foreach (Song.Entities.QuesAnswer a in ans) { root.AppendChild(_answerToItem(xmlDoc, a)); } return xmlDoc.OuterXml; } /// /// 将单个答题选项转换为xml字符串 /// /// /// /// private XmlNode _answerToItem(XmlDocument xmlDoc, Song.Entities.QuesAnswer ans) { XmlNode item = xmlDoc.CreateElement("item"); Type t = ans.GetType(); foreach (PropertyInfo pi in t.GetProperties()) { XmlNode node = xmlDoc.CreateNode(XmlNodeType.Element, pi.Name, null); object obj = pi.GetValue(ans, null); if (pi.PropertyType.FullName == "System.String") { XmlCDataSection cd = xmlDoc.CreateCDataSection(obj != null ? obj.ToString() : ""); node.AppendChild(cd); } else { node.InnerText = obj != null ? obj.ToString() : ""; } item.AppendChild(node); } return item; } /// /// 将答题选项的xml字符串,转换为QuesAnswer对象数组 /// /// /// 是否返回正确的选项,null返回全部,true只返回正确的答案,false只返回错误 /// public Song.Entities.QuesAnswer[] ItemsToAnswer(string xml, bool? isCorrect) { if (string.IsNullOrWhiteSpace(xml)) return new QuesAnswer[0]; XmlDocument xmlDoc = new XmlDocument(); if (!string.IsNullOrWhiteSpace(xml)) xml = xml.Trim(); xmlDoc.LoadXml(xml, false); XmlNodeList list = xmlDoc.SelectNodes("Items/item"); List anslist =new List(); for (int i = 0; i < list.Count; i++) { QuesAnswer ans = new QuesAnswer(); Type t = ans.GetType(); foreach (XmlNode n in list[i].ChildNodes) { if (string.IsNullOrWhiteSpace(n.InnerText)) continue; PropertyInfo pi = t.GetProperty(n.Name); if (pi == null) continue; object v = Convert.ChangeType(n.InnerText, pi.PropertyType); t.GetProperty(n.Name).SetValue(ans, v, null); } if (isCorrect == null) { anslist.Add(ans); } else { if ((bool)isCorrect && ans.Ans_IsCorrect) anslist.Add(ans); if (!(bool)isCorrect && !ans.Ans_IsCorrect) anslist.Add(ans); } } return anslist.ToArray(); } /// /// 将答题选项的xml字符串,转换为QuesAnswer对象数组 /// /// /// public Song.Entities.QuesAnswer ItemToAnswer(string xml) { if (string.IsNullOrWhiteSpace(xml)) return null; XmlDocument xmlDoc = new XmlDocument(); xmlDoc.LoadXml(xml, false); XmlNode item = xmlDoc.FirstChild; QuesAnswer ans = new QuesAnswer(); Type t = ans.GetType(); foreach (XmlNode n in item.ChildNodes) { if (string.IsNullOrWhiteSpace(n.InnerText)) continue; object v = Convert.ChangeType(n.InnerText, t.GetProperty(n.Name).PropertyType); t.GetProperty(n.Name).SetValue(ans, v, null); } return ans; } #endregion #region 计算试题得分 /// /// 计算当前试题的得分 /// /// 试题的ID /// 答案,选择题为id,判断题为数字,填空或简答为字符 /// 该题的分数 /// 正确返回true public bool ClacQues(int id, string ans) { if (string.IsNullOrWhiteSpace(ans)) return false; if (ans.Trim() == "" || ans.Trim() == "undefined") return false; // Questions qus = null; qus = this.QuesSingle4Cache(id); if (qus == null) qus = Gateway.Default.From().Where(Questions._.Qus_ID == id).ToFirst(); if (qus == null) return false; if (qus.Qus_Type == 1) return _clacQues1(qus, ans); if (qus.Qus_Type == 2) return _clacQues2(qus, ans); if (qus.Qus_Type == 3) return _clacQues3(qus, ans); if (qus.Qus_Type == 4) return _clacQues4(qus, ans); if (qus.Qus_Type == 5) return _clacQues5(qus, ans); return false; } /// /// 计算单选题 /// /// /// /// private bool _clacQues1(Questions ques, string ans) { QuesAnswer[] ans1 = ItemsToAnswer(ques.Qus_Items, true); if (ans1.Length < 1) return false; foreach (string s in ans.Split(',')) { if (string.IsNullOrWhiteSpace(s) || s.Trim() == "") continue; if (Convert.ToInt32(s) == ans1[0].Ans_ID) return true; } return false; } /// /// 计算多选题 /// /// /// /// private bool _clacQues2(Questions ques, string ans) { QuesAnswer[] ans2 = ItemsToAnswer(ques.Qus_Items, true); if (ans2.Length < 1) return false; if (ans.Length > 0 && ans.Substring(ans.Length - 1) == ",") ans = ans.Substring(0, ans.Length - 1); string[] ansArr = ans.Split(','); if (ansArr.Length != ans2.Length) return false; int tm = ansArr.Length; foreach (string s in ansArr) { if (string.IsNullOrWhiteSpace(s) || s.Trim() == "") continue; foreach (QuesAnswer qa in ans2) { if (Convert.ToInt32(s) == qa.Ans_ID) { tm--; break; } } } return tm == 0; } /// /// 计算判断题 /// /// /// /// private bool _clacQues3(Questions ques, string ans) { ans = ans.Replace(",", ""); if (Convert.ToInt32(ans) == 0 && ques.Qus_IsCorrect == true) return true; if (Convert.ToInt32(ans) == 1 && ques.Qus_IsCorrect == false) return true; return false; } /// /// 计算简答题 /// /// /// /// private bool _clacQues4(Questions ques, string ans) { if (ans.Trim() == "") return false; return false; } /// /// 填空题 /// /// /// /// private bool _clacQues5(Questions ques, string ans) { QuesAnswer[] ans5 = ItemsToAnswer(ques.Qus_Items, null); if (ans5.Length < 1) return false; if (ans.Length > 0 && ans.Substring(ans.Length - 1) == ",") ans = ans.Substring(0, ans.Length - 1); string[] ansText = ans.Split(','); int corrNum = 0; for (int j = 0; j < ansText.Length; j++) { if (ansText[j].Trim() == "") continue; if (ans5.Length <= j || ans5[j] == null) continue; string corentTxt = ans5[j].Ans_Context; foreach (string tm in corentTxt.Split(',')) { if (tm == string.Empty || tm.Trim() == "") continue; if (tm.Trim() == ansText[j].Trim()) { corrNum++; break; } } } return corrNum == ans5.Length; } #endregion #region 私有方法 /// /// 清理字符中的非法字符 /// /// /// private string _ClearString(string html) { if (string.IsNullOrWhiteSpace(html)) return html; //删除脚本 html = Regex.Replace(html, @"]+?>[\s\S]*?", "", RegexOptions.IgnoreCase); //删除HTML //html = Regex.Replace(html, @"<(.[^>]*)>", "", RegexOptions.IgnoreCase); html = Regex.Replace(html, @"([\r\n])[\s]+", "", RegexOptions.IgnoreCase); html = Regex.Replace(html, @"-->", "", RegexOptions.IgnoreCase); html = Regex.Replace(html, @"