/// <summary> /// 添加实体信息,返回添加成功后的主键ID /// </summary> public int Insert(SingleItem single) { int id = 0; const string sql = @"INSERT INTO SingleItem(AgencyID, ChapterID, IsVipItem, Title, Image, A, B, C, D, Answer, Annotation, Difficulty, AddPerson) VALUES (@AgencyID, @ChapterID, @IsVipItem, @Title, @Image, @A, @B, @C, @D, @Answer, @Annotation, @Difficulty, @AddPerson); SELECT LAST_INSERT_ID();"; using (DbConnection connection = ConnectionManager.OpenConnection) { id = connection.Query<int>(sql, single).SingleOrDefault<int>(); } return id; }
public SingleItemDTO(SingleItem singleItem) { this.ID = singleItem.ID; this.AgencyID = singleItem.AgencyID; this.ChapterID = singleItem.ChapterID; this.IsVipItem = singleItem.IsVipItem; this.Title = singleItem.Title; this.Image = singleItem.Image; this.A = singleItem.A; this.B = singleItem.B; this.C = singleItem.C; this.D = singleItem.D; this.Answer = singleItem.Answer; this.Annotation = singleItem.Annotation; this.Difficulty = singleItem.Difficulty; this.AddPerson = singleItem.AddPerson; this.AddTime = singleItem.AddTime; }
/// <summary> /// 添加单选题 /// </summary> public ServiceInvokeDTO AddSingle(SingleItem single) { log.Debug(Constant.DEBUG_START); ServiceInvokeDTO result = null; try { singleDAL.Insert(single); result = new ServiceInvokeDTO(InvokeCode.SYS_INVOKE_SUCCESS); } catch (Exception ex) { log.Error(ex); throw ex; } log.Debug(Constant.DEBUG_END); return result; }
/// <summary> /// 更新单选题 /// </summary> public ServiceInvokeDTO UpdateSingle(SingleItem single) { log.Debug(Constant.DEBUG_START); ServiceInvokeDTO result = null; try { SingleItem dbSingle = singleDAL.GetByID(single.ID); if (dbSingle != null) { singleDAL.Update(single); result = new ServiceInvokeDTO(InvokeCode.SYS_INVOKE_SUCCESS); } else { result = new ServiceInvokeDTO(InvokeCode.SYS_OBJECT_NOT_EXIST_ERROR); } } catch (Exception ex) { log.Error(ex); throw ex; } log.Debug(Constant.DEBUG_END); return result; }
public ActionResult AddSingle() { log.Debug(Constant.DEBUG_START); string isVipItemString = ApiQueryUtil.QueryArgByPost("is_vip_item"); string chapterIDString = ApiQueryUtil.QueryArgByPost("chapter_id"); string title = ApiQueryUtil.QueryArgByPost("title"); HttpPostedFileBase imageFile = Request.Files["image"]; string a = ApiQueryUtil.QueryArgByPost("a"); string b = ApiQueryUtil.QueryArgByPost("b"); string c = ApiQueryUtil.QueryArgByPost("c"); string d = ApiQueryUtil.QueryArgByPost("d"); string answer = ApiQueryUtil.QueryArgByPost("answer"); string annotation = ApiQueryUtil.QueryArgByPost("annotation"); string difficultyString = ApiQueryUtil.QueryArgByPost("difficulty"); ServiceInvokeDTO result = null; try { SingleItem single = new SingleItem(); single.AgencyID = (Session[Constant.SESSION_KEY_ADMIN] as AgencyAdminDTO).Agency.ID; single.IsVipItem = Convert.ToInt32(isVipItemString); single.ChapterID = Convert.ToInt32(chapterIDString); single.Title = title; single.A = a; single.B = b; single.C = c; single.D = d; single.Answer = answer; single.Annotation = annotation; single.Difficulty = Convert.ToInt32(difficultyString); single.AddPerson = (Session[Constant.SESSION_KEY_ADMIN] as AgencyAdminDTO).ChineseName; if (imageFile != null) { byte[] imageBytes = null; using (Stream inputStream = imageFile.InputStream) { MemoryStream memoryStream = inputStream as MemoryStream; if (memoryStream == null) { memoryStream = new MemoryStream(); inputStream.CopyTo(memoryStream); } imageBytes = memoryStream.ToArray(); } single.Image = imageBytes; } result = itemDataService.AddSingle(single); // Write admin do record. if (result != null && result.Code == InvokeCode.SYS_INVOKE_SUCCESS) { AgencyAdminDTO currentAdmin = Session[Constant.SESSION_KEY_ADMIN] as AgencyAdminDTO; AdminDoRecord doRecord = new AdminDoRecord(); doRecord.AdminID = currentAdmin.ID; doRecord.AdminName = currentAdmin.ChineseName; doRecord.DoTime = DateTime.Now; doRecord.DoName = DoActionType.AddSingle.GetDescription(); doRecord.DoContent = string.Format("新单选题标题:{0}", title); doRecord.Remark = string.Empty; recordDataService.AddAdminDoRecord(doRecord); } } catch (Exception ex) { log.Error(ex); result = new ServiceInvokeDTO(InvokeCode.SYS_INNER_ERROR); } string json = JsonConvert.SerializeObject(result, Formatting.Indented, Constant.TIME_CONVERTER); log.Debug(Constant.DEBUG_END); return Content(json, Constant.JSON_MIME_TYPE); }
/// <summary> /// 读取单选题题库模板数据 /// </summary> /// <param name="adminDTO">管理员对象</param> /// <param name="courseID">课程主键ID</param> /// <param name="fileName">Excel文件路径名称</param> /// <param name="isFirstRowTitle">第一行是否为标题</param> public static List<SingleItem> ReadSingleTemplate(AgencyAdminDTO adminDTO, int courseID, string fileName, bool isFirstRowTitle) { List<SingleItem> singles = new List<SingleItem>(); // 获取所有章节 ServiceInvokeDTO<List<Chapter>> chaptersResult = ServiceFactory.Instance.ItemDataService.GetAgencyChapters(courseID); if (chaptersResult.Code == InvokeCode.SYS_INVOKE_SUCCESS && chaptersResult.Data != null && chaptersResult.Data.Count > 0) { List<Chapter> chapters = chaptersResult.Data; using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read)) { // 默认读取第一张表 IWorkbook workBook = WorkbookFactory.Create(fs); ISheet sheet = workBook.GetSheetAt(0); int startRowIndex = 0; if (isFirstRowTitle && sheet.LastRowNum >= 1) { startRowIndex = 1; } for (int index = startRowIndex; index <= sheet.LastRowNum; index++) { IRow row = sheet.GetRow(index); if (row != null) { // 题库类型 ICell isVipItemCell = row.GetCell(0); if (isVipItemCell == null) { throw new ArgumentException(TEXT_EMPTY_ERROR); } string isVipItem = isVipItemCell.ToString().Trim(); if (!isVipItem.Equals(IS_VIP_ITEM_NO) && !isVipItem.Equals(IS_VIP_ITEM_YES)) { throw new ArgumentException(IS_VIP_ITEM_ERROR); } // 所属章节 ICell chapterCell = row.GetCell(1); if (chapterCell == null) { throw new ArgumentException(TEXT_EMPTY_ERROR); } string chapterName = chapterCell.ToString().Trim(); int chapterID = 0; foreach (var chapter in chapters) { if (chapterName.Equals(chapter.Name)) { chapterID = chapter.ID; break; } } if (chapterID == 0) { throw new ArgumentException(CHAPTER_ID_ERROR); } ICell titleCell = row.GetCell(2); if (titleCell == null) { throw new ArgumentException(TEXT_EMPTY_ERROR); } ICell aCell = row.GetCell(3); if (aCell == null) { throw new ArgumentException(TEXT_EMPTY_ERROR); } ICell bCell = row.GetCell(4); if (bCell == null) { throw new ArgumentException(TEXT_EMPTY_ERROR); } ICell cCell = row.GetCell(5); if (cCell == null) { throw new ArgumentException(TEXT_EMPTY_ERROR); } ICell dCell = row.GetCell(6); if (dCell == null) { throw new ArgumentException(TEXT_EMPTY_ERROR); } // 答案 ICell amswerCell = row.GetCell(7); if (amswerCell == null) { throw new ArgumentException(TEXT_EMPTY_ERROR); } string answer = amswerCell.ToString().Trim().ToUpper(); if (!answer.Equals(ANSWER_A) && !answer.Equals(ANSWER_B) && !answer.Equals(ANSWER_C) && !answer.Equals(ANSWER_D)) { throw new ArgumentException(ANSWER_ARG_ERROR); } // 试题难易度 ICell difficltyCell = row.GetCell(9); if (difficltyCell == null) { throw new ArgumentException(TEXT_EMPTY_ERROR); } string difficltyString = difficltyCell.ToString().Trim(); int difficulty = 0; if (int.TryParse(difficltyString, out difficulty)) { if (difficulty < DIFFICULTY_MIN || difficulty > DIFFICULTY_MAX) { throw new ArgumentException(DIFFICULTY_ARG_ERROR); } } else { throw new ArgumentException(DIFFICULTY_ARG_ERROR); } SingleItem single = new SingleItem(); single.AgencyID = adminDTO.Agency.ID; single.ChapterID = chapterID; single.IsVipItem = isVipItem.Equals(IS_VIP_ITEM_YES) ? 1 : 0; single.Title = titleCell.ToString(); single.A = aCell.ToString(); single.B = bCell.ToString(); single.C = cCell.ToString(); single.D = dCell.ToString(); single.Answer = answer; single.Annotation = row.GetCell(8) == null ? string.Empty : row.GetCell(8).ToString(); single.Difficulty = difficulty; single.AddPerson = adminDTO.ChineseName; singles.Add(single); } } } } else { throw new ArgumentException(CHAPTER_ID_ERROR); } return singles; }
/// <summary> /// 更新实体信息 /// </summary> public void Update(SingleItem single) { const string sql = @"UPDATE SingleItem SET ChapterID = @ChapterID, IsVipItem= @IsVipItem, Title= @Title, Image= @Image, A = @A, B= @B, C = @C, D = @D, Answer = @Answer, Annotation = @Annotation, Difficulty = @Difficulty WHERE IsDeleted = 0 AND ID = @ID"; using (DbConnection connection = ConnectionManager.OpenConnection) { connection.Execute(sql, single); } }
public ActionResult UpdateUser() { log.Debug(Constant.DEBUG_START); string idString = ApiQueryUtil.QueryArgByPost("id"); string isVipItemString = ApiQueryUtil.QueryArgByPost("is_vip_item"); string chapterIDString = ApiQueryUtil.QueryArgByPost("chapter_id"); string title = ApiQueryUtil.QueryArgByPost("title"); HttpPostedFileBase imageFile = Request.Files["image"]; string a = ApiQueryUtil.QueryArgByPost("a"); string b = ApiQueryUtil.QueryArgByPost("b"); string c = ApiQueryUtil.QueryArgByPost("c"); string d = ApiQueryUtil.QueryArgByPost("d"); string answer = ApiQueryUtil.QueryArgByPost("answer"); string annotation = ApiQueryUtil.QueryArgByPost("annotation"); string difficultyString = ApiQueryUtil.QueryArgByPost("difficulty"); ServiceInvokeDTO result = null; try { SingleItem single = new SingleItem(); single.ID = Convert.ToInt32(idString); single.IsVipItem = Convert.ToInt32(isVipItemString); single.ChapterID = Convert.ToInt32(chapterIDString); single.Title = title; single.A = a; single.B = b; single.C = c; single.D = d; single.Answer = answer; single.Annotation = annotation; single.Difficulty = Convert.ToInt32(difficultyString); if (imageFile != null) { byte[] imageBytes = null; using (Stream inputStream = imageFile.InputStream) { MemoryStream memoryStream = inputStream as MemoryStream; if (memoryStream == null) { memoryStream = new MemoryStream(); inputStream.CopyTo(memoryStream); } imageBytes = memoryStream.ToArray(); } single.Image = imageBytes; } AgencyAdminDTO currentAdmin = Session[Constant.SESSION_KEY_ADMIN] as AgencyAdminDTO; ServiceInvokeDTO checkResult = permissionService.CheckPermission(DoActionType.UpdateSingle, currentAdmin.Agency.ID, single.ID); if (checkResult.Code == InvokeCode.SYS_INVOKE_SUCCESS) { //result = itemDataService.UpdateSingle(single); } else { result = checkResult; } } catch (Exception ex) { log.Error(ex); result = new ServiceInvokeDTO(InvokeCode.SYS_INNER_ERROR); } string json = JsonConvert.SerializeObject(result, Formatting.Indented, Constant.TIME_CONVERTER); log.Debug(Constant.DEBUG_END); return Content(json, Constant.JSON_MIME_TYPE); }