1.今日任务:
试卷管理 组卷规则管理
2.核心代码
控制器代码:
1 using Exam.BLL; 2 using Exam.Model; 3 using Exam.UI.Filter; 4 using PagedList; 5 using System; 6 using System.Collections.Generic; 7 using System.Data; 8 using System.Linq; 9 using System.Web; 10 using System.Web.Mvc; 11 12 13 namespace Exam.UI.Controllers 14 { 15 [StudentFilter] 16 public class PaperRuleController : Controller 17 { 18 // GET: PaperRule 19 public ActionResult Index(int page = 1) 20 { 21 IPagedList list = PaperRuleService.GetList(page); 22 return View(list); 23 } 24 public ActionResult DeleteRuleDetail(int id) 25 { 26 try 27 { 28 RuleDetailService.Delete(id); 29 } 30 catch (Exception ex) 31 { 32 33 return Json(new { msg = "删除失败" + ex, success = false }); 34 } 35 return Json(new { msg = "删除成功", success = false }); 36 } 37 public ActionResult AddPaper() 38 { 39 return View(); 40 } 41 public ActionResult RuleDetail(int id) 42 { 43 ViewBag.Paper =PaperRuleService.FindPaperRuleByID(id); 44 ViewData["Num"] = RuleDetailService.GetDetailQuestionCount(id).ToString(); 45 46 var list = RuleDetailService.GetList(id); 47 return View(list); 48 } 49 ///50 /// 编辑试卷规则详情信息 51 /// 52 /// 53 /// 54 public ActionResult EditRuleDetail(int id) 55 { 56 var list = LibraryService.GetAll(); 57 ViewBag.data = RuleDetailService.GetDetailByID(id); 58 return View(list); 59 } 60 [HttpPost] 61 public ActionResult EditRuleDetail(int questionnum, int libraryid, int ruleid,int paperruleid,int oldnum) 62 { 63 try 64 { 65 ///查询规则详情中 试卷题目数量 66 int num = RuleDetailService.GetDetailQuestionCount(paperruleid); 67 //查询试卷规则 题目总数 68 69 var data = PaperRuleService.FindPaperRuleByID(paperruleid); 70 if (questionnum > data.QuestionNum - num+ oldnum) 71 { 72 return Json(new { msg = "修改失败,要添加的题目数量大于试卷题目总数", success = false }); 73 } 74 else 75 { 76 Exam_RuleDetail detail = new Exam_RuleDetail { QuestionNum = questionnum, LibraryID = libraryid, PaperRuleID = paperruleid, RuleID=ruleid }; 77 RuleDetailService.Update(detail); 78 } 79 } 80 catch (Exception ex) 81 { 82 return Json(new { msg = "修改失败" + ex, success = false }); 83 84 } 85 return Json(new { msg = "修改成功", success = false }); 86 } 87 88 [HttpPost] 89 public ActionResult AddPaper(string rulename, string rulestarttime, int time, int Score, int questionnum) 90 { 91 try 92 { 93 DateTime dt = Convert.ToDateTime(rulestarttime); 94 Exam_PaperRule paperRule = new Exam_PaperRule 95 { 96 QuestionNum = questionnum, 97 RuleStartDate = dt, 98 RuleEndDate = dt.AddMinutes(time), 99 RuleName = rulename, 100 Score = Score, 101 States = true 102 }; 103 PaperRuleService.InsertPaperRule(paperRule); 104 } 105 catch (Exception ex) 106 { 107 return Json(new { msg = "添加失败" + ex, success = false }); 108 109 } 110 return Json(new { msg = "添加成功", success = true }); 111 } 112 /// 113 /// 编辑试卷规则 114 /// 115 /// 116 /// 117 public ActionResult EditPaper(int id) 118 { 119 var list = PaperRuleService.FindPaperRuleByID(id); 120 return View(list); 121 } 122 [HttpPost] 123 public ActionResult EditPaper(int id,string rulename, string rulestarttime, int time, int Score, int questionnum) 124 { 125 try 126 { 127 DateTime dt = Convert.ToDateTime(rulestarttime); 128 Exam_PaperRule paperRule = new Exam_PaperRule 129 { 130 QuestionNum = questionnum, 131 RuleStartDate = dt, 132 RuleEndDate = dt.AddMinutes(time), 133 RuleName = rulename, 134 Score = Score, 135 States = true, 136 PaperRuleID=id 137 }; 138 PaperRuleService.Update(paperRule); 139 } 140 catch (Exception ex) 141 { 142 return Json(new { msg = "添加失败" + ex, success = false }); 143 144 } 145 return Json(new { msg = "添加成功", success = true }); 146 } 147 public ActionResult AddPaperRule() 148 { 149 150 ViewBag.Library = LibraryService.GetAllEnable(); 151 var list = PaperRuleService.GetAll(); 152 153 return View(list); 154 } 155 [HttpPost] 156 public ActionResult AddPaperRule(int questionnum, int libraryid, int paperruleid) 157 { 158 try 159 { 160 int libraryquestionnum = QuestionService.GetCountByLibraryID(libraryid); 161 162 ///查询规则详情中 试卷题目数量 163 int num = RuleDetailService.GetDetailQuestionCount(paperruleid); 164 //查询试卷规则 题目总数 165 166 var data = PaperRuleService.FindPaperRuleByID(paperruleid); 167 if (questionnum > data.QuestionNum - num) 168 { 169 return Json(new { msg = "添加失败,要添加的题目数量大于试卷题目总数", success = false }); 170 } 171 else if(libraryquestionnum < questionnum) 172 { 173 return Json(new { msg = "添加失败,题库中的题目数量小于您输入的数量", success = false }); 174 } 175 else 176 { 177 Exam_RuleDetail detail = new Exam_RuleDetail { QuestionNum = questionnum, LibraryID = libraryid, PaperRuleID = paperruleid }; 178 RuleDetailService.AddRuleDetail(detail); 179 } 180 } 181 catch (Exception ex) 182 { 183 return Json(new { msg = "添加失败" + ex, success = false }); 184 185 } 186 return Json(new { msg = "添加成功", success = true }); 187 } 188 189 /// 190 /// 禁用试卷 191 /// 192 /// 193 /// 194 public ActionResult EnablePaper(int id) 195 { 196 try 197 { 198 int res = PaperRuleService.EnablePaperRule(id); 199 } 200 catch (Exception ex) 201 { 202 return Json(new { msg = "启用失败" + ex, success = false }); 203 204 } 205 return Json(new { msg = "启用成功", success = true }); 206 } 207 /// 208 /// 禁用试卷 209 /// 210 /// 211 /// 212 public ActionResult DisablePaper(int id) 213 { 214 try 215 { 216 int res = PaperRuleService.DisablePaperRule(id); 217 } 218 catch (Exception ex) 219 { 220 return Json(new { msg = "禁用失败" + ex, success = false }); 221 222 } 223 return Json(new { msg = "禁用成功", success = true }); 224 } 225 } 226 }
Service层方法:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 using Exam.Model; 7 using Exam.DAL; 8 using PagedList; 9 10 namespace Exam.BLL 11 { 12 public class PaperRuleService 13 { 14 ///15 /// 获取所有试卷 16 /// 17 /// 18 /// 19 /// 20 public static IPagedList GetList(int page = 1) 21 { 22 using (ExamSysDBContext db = new ExamSysDBContext()) 23 { 24 int pagesize = 10; 25 IPagedList list = db.Exam_PaperRule.OrderBy(x => x.PaperRuleID).ToPagedList(page, pagesize); 26 return list; 27 } 28 29 } 30 31 /// 32 /// 获取所有试卷 状态正常的 33 /// 34 /// 35 /// 36 /// 37 public static IPagedList GetListEnable(int page = 1) 38 { 39 using (ExamSysDBContext db = new ExamSysDBContext()) 40 { 41 int pagesize = 10; 42 IPagedList list = db.Exam_PaperRule.Where(x=>x.States==true).OrderBy(x => x.PaperRuleID).ToPagedList(page, pagesize); 43 return list; 44 } 45 46 } 47 public static List GetAll() 48 { 49 using (ExamSysDBContext db = new ExamSysDBContext()) 50 { 51 var list = db.Exam_PaperRule.ToList(); 52 return list; 53 } 54 55 } 56 /// 57 /// 增加试卷 58 /// 59 /// 60 /// 61 public static int InsertPaperRule(Exam_PaperRule paperRule) 62 { 63 using (ExamSysDBContext dBContext = new ExamSysDBContext()) 64 { 65 dBContext.Exam_PaperRule.Add(paperRule); 66 return dBContext.SaveChanges(); 67 } 68 69 } 70 71 /// 72 /// 通过ID找到该试卷 73 /// 74 /// 75 /// 76 public static Exam_PaperRule FindPaperRuleByID(int id) 77 { 78 using (ExamSysDBContext dBContext = new ExamSysDBContext()) 79 { 80 var data = dBContext.Exam_PaperRule.Where(x => x.PaperRuleID == id).FirstOrDefault(); 81 return data; 82 } 83 84 } 85 /// 86 /// 禁用试卷 87 /// 88 /// 89 /// 90 public static int DisablePaperRule(int id) 91 { 92 using (ExamSysDBContext dBContext = new ExamSysDBContext()) 93 { 94 var data = dBContext.Exam_PaperRule.Where(x => x.PaperRuleID == id).FirstOrDefault(); 95 96 data.States = false; 97 return dBContext.SaveChanges(); 98 } 99 } 100 /// 101 /// 启用试卷 102 /// 103 /// 104 /// 105 public static int EnablePaperRule(int id) 106 { 107 using (ExamSysDBContext dBContext = new ExamSysDBContext()) 108 { 109 110 var data = dBContext.Exam_PaperRule.Where(x => x.PaperRuleID == id).FirstOrDefault(); 111 112 data.States = true; 113 return dBContext.SaveChanges(); 114 } 115 116 } 117 /// 118 /// 修改试卷信息 119 /// 120 /// 121 /// 122 public static int Update(Exam_PaperRule paperRule) 123 { 124 using (ExamSysDBContext dBContext = new ExamSysDBContext()) 125 { 126 127 var data = dBContext.Exam_PaperRule.Where(x => x.PaperRuleID == paperRule.PaperRuleID).FirstOrDefault(); 128 data.RuleName = paperRule.RuleName; 129 data.RuleStartDate = paperRule.RuleStartDate; 130 data.RuleEndDate = paperRule.RuleEndDate; 131 data.Score = paperRule.Score; 132 data.QuestionNum = paperRule.QuestionNum; 133 134 return dBContext.SaveChanges(); 135 } 136 } 137 } 138 }
1 using Exam.DAL; 2 using Exam.Model; 3 using PagedList; 4 using System; 5 using System.Collections.Generic; 6 using System.Data; 7 using System.Linq; 8 using System.Text; 9 using System.Threading.Tasks; 10 11 namespace Exam.BLL 12 { 13 public class RuleDetailService 14 { 15 ///16 /// 获取当前规则试题数量总和 17 /// 18 /// 19 /// 20 public static int GetDetailQuestionCount(int ruleid) 21 { 22 using (ExamSysDBContext db = new ExamSysDBContext()) 23 { 24 int num = db.Exam_RuleDetail.Where(x => x.PaperRuleID == ruleid).Select(x => x.QuestionNum).DefaultIfEmpty().Sum(); 25 26 return num; 27 } 28 } 29 /// 30 /// 查询试题规则详情 31 /// 32 /// 33 /// 34 public static List GetDetailQuestion(int ruleid) 35 { 36 using (ExamSysDBContext db = new ExamSysDBContext()) 37 { 38 return db.Exam_RuleDetail.Where(x => x.PaperRuleID == ruleid).ToList(); 39 } 40 } 41 /// 42 /// 添加规则详情 43 /// 44 /// 45 /// 46 public static int AddRuleDetail(Exam_RuleDetail detail) 47 { 48 using (ExamSysDBContext db = new ExamSysDBContext()) 49 { 50 db.Exam_RuleDetail.Add(detail); 51 return db.SaveChanges(); 52 } 53 54 } 55 /// 56 /// 删除规则详情 57 /// 58 /// 59 /// 60 public static int Delete(int detailid) 61 { 62 using (ExamSysDBContext db = new ExamSysDBContext()) 63 { 64 var detai = db.Exam_RuleDetail.Where(x => x.RuleID == detailid).FirstOrDefault(); 65 db.Exam_RuleDetail.Remove(detai); 66 return db.SaveChanges(); 67 } 68 69 } 70 /// 71 /// 更新规则详情信息 72 /// 73 /// 74 /// 75 public static int Update(Exam_RuleDetail detail) 76 { 77 using (ExamSysDBContext db = new ExamSysDBContext()) 78 { 79 80 var detai = db.Exam_RuleDetail.Where(x => x.RuleID == detail.RuleID).FirstOrDefault(); 81 detai.LibraryID = detai.LibraryID; 82 detai.QuestionNum = detai.QuestionNum; 83 return db.SaveChanges(); 84 } 85 } 86 public static Exam_RuleDetail GetDetailByID(int id) 87 { 88 using (ExamSysDBContext db = new ExamSysDBContext()) 89 { 90 var data = db.Exam_RuleDetail.Where(x => x.RuleID == id).FirstOrDefault(); 91 return data; 92 } 93 94 } 95 public static IPagedList GetList(int id,int page = 1) 96 { 97 using (ExamSysDBContext db = new ExamSysDBContext()) 98 { 99 int pagesize = 10; 100 101 IPagedList list = db.Exam_RuleDetail.Where(x => x.PaperRuleID == id).OrderBy(x => x.RuleID).ToPagedList(page, pagesize); 102 103 104 return list; 105 } 106 107 } 108 } 109 }
3.遇到问题
无
4.解决方案
无