public NumberBlankItemDTO(NumberBlankItem numberBlankItem) { this.ID = numberBlankItem.ID; this.AgencyID = numberBlankItem.AgencyID; this.ChapterID = numberBlankItem.ChapterID; this.IsVipItem = numberBlankItem.IsVipItem; this.Title = numberBlankItem.Title; this.Image1 = numberBlankItem.Image1; this.Image1SubText = numberBlankItem.Image1SubText; this.Image2 = numberBlankItem.Image2; this.Image2SubText = numberBlankItem.Image2SubText; this.Difficulty = numberBlankItem.Difficulty; this.AddPerson = numberBlankItem.AddPerson; this.AddTime = numberBlankItem.AddTime; }
/// <summary> /// 添加数字填空题 /// </summary> public ServiceInvokeDTO AddNumberBlank(NumberBlankItem numberBlank, List<NumberBlankAnswer> answers) { log.Debug(Constant.DEBUG_START); ServiceInvokeDTO result = null; try { numberBlankDAL.Insert(numberBlank, answers); 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 UpdateNumberBlank(NumberBlankItem numberBlank, List<NumberBlankAnswer> answers) { log.Debug(Constant.DEBUG_START); ServiceInvokeDTO result = null; try { NumberBlankItem dbNumberBlank = numberBlankDAL.GetByID(numberBlank.ID); if (dbNumberBlank != null) { numberBlankDAL.Update(numberBlank, answers); 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 AddNumberBlank() { log.Debug(Constant.DEBUG_START); string isVipItemString = ApiQueryUtil.QueryArgByPost("is_vip_item"); string chapterIDString = ApiQueryUtil.QueryArgByPost("chapter_id"); string title = ApiQueryUtil.QueryArgByPost("title"); HttpPostedFileBase image1File = Request.Files["image1"]; string image1Subtext = ApiQueryUtil.QueryArgByPost("image1_subtext"); HttpPostedFileBase image2File = Request.Files["image2"]; string image2Subtext = ApiQueryUtil.QueryArgByPost("image2_subtext"); string difficultyString = ApiQueryUtil.QueryArgByPost("difficulty"); string answersJsonString = ApiQueryUtil.QueryArgByPost("answers"); ServiceInvokeDTO result = null; try { NumberBlankItem numberBlank = new NumberBlankItem(); numberBlank.AgencyID = (Session[Constant.SESSION_KEY_ADMIN] as AgencyAdminDTO).Agency.ID; numberBlank.IsVipItem = Convert.ToInt32(isVipItemString); numberBlank.ChapterID = Convert.ToInt32(chapterIDString); numberBlank.Title = title; numberBlank.Image1SubText = image1Subtext; numberBlank.Image2SubText = image2Subtext; numberBlank.Difficulty = Convert.ToInt32(difficultyString); numberBlank.AddPerson = (Session[Constant.SESSION_KEY_ADMIN] as AgencyAdminDTO).ChineseName; if (image1File != null) { byte[] imageBytes = null; using (Stream inputStream = image1File.InputStream) { MemoryStream memoryStream = inputStream as MemoryStream; if (memoryStream == null) { memoryStream = new MemoryStream(); inputStream.CopyTo(memoryStream); } imageBytes = memoryStream.ToArray(); } numberBlank.Image1 = imageBytes; } if (image2File != null) { byte[] imageBytes = null; using (Stream inputStream = image2File.InputStream) { MemoryStream memoryStream = inputStream as MemoryStream; if (memoryStream == null) { memoryStream = new MemoryStream(); inputStream.CopyTo(memoryStream); } imageBytes = memoryStream.ToArray(); } numberBlank.Image2 = imageBytes; } List<NumberBlankAnswer> answers = JsonConvert.DeserializeObject<List<NumberBlankAnswer>>(answersJsonString); result = itemDataService.AddNumberBlank(numberBlank, answers); } 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> public void Update(NumberBlankItem numberBlankItem, List<NumberBlankAnswer> answers) { const string update_item_sql = @"UPDATE NumberBlankItem SET ChapterID = @ChapterID, IsVipItem= @IsVipItem, Title= @Title, Image1 = @Image1, Image1SubText= @Image1SubText, Image2 = @Image2, Image2SubText= @Image2SubText, Difficulty = @Difficulty WHERE IsDeleted = 0 AND ID = @ID"; const string delete_answer_sql = @"UPDATE NumberBlankAnswer SET IsDeleted = 1 WHERE NumberBlankItemID = @NumberBlankItemID"; const string insert_answer_sql = @"INSERT INTO NumberBlankAnswer(NumberBlankItemID, AnswerName, Answer, Annotation) VALUES (@NumberBlankItemID, @AnswerName, @Answer, @Annotation);"; using (DbConnection connection = ConnectionManager.OpenConnection) { IDbTransaction transaction = connection.BeginTransaction(); try { connection.Execute(update_item_sql, numberBlankItem, transaction); connection.Execute(delete_answer_sql, new { NumberBlankItemID = numberBlankItem.ID }, transaction); foreach (var answer in answers) { connection.Execute(insert_answer_sql, new { NumberBlankItemID = numberBlankItem.ID, AnswerName = answer.AnswerName, Answer = answer.Answer, Annotation = answer.Annotation }, transaction ); } transaction.Commit(); } catch (Exception ex) { transaction.Rollback(); throw ex; } } }
/// <summary> /// 添加实体信息,返回添加成功后的主键ID /// </summary> public void Insert(NumberBlankItem numberBlankItem, List<NumberBlankAnswer> answers) { const string insert_item_sql = @"INSERT INTO NumberBlankItem(AgencyID, ChapterID, IsVipItem, Title, Image1, Image1SubText, Image2, Image2SubText, Difficulty, AddPerson) VALUES (@AgencyID, @ChapterID, @IsVipItem, @Title, @Image1, @Image1SubText, @Image2, @Image2SubText, @Difficulty, @AddPerson); SELECT LAST_INSERT_ID();"; const string insert_answer_sql = @"INSERT INTO NumberBlankAnswer(NumberBlankItemID, AnswerName, Answer, Annotation) VALUES (@NumberBlankItemID, @AnswerName, @Answer, @Annotation);"; using (DbConnection connection = ConnectionManager.OpenConnection) { IDbTransaction transaction = connection.BeginTransaction(); try { int numberBlankID = connection.Query<int>(insert_item_sql, numberBlankItem, transaction).SingleOrDefault<int>(); foreach (var answer in answers) { connection.Execute(insert_answer_sql, new { NumberBlankItemID = numberBlankID, AnswerName = answer.AnswerName, Answer = answer.Answer, Annotation = answer.Annotation }, transaction ); } transaction.Commit(); } catch (Exception ex) { transaction.Rollback(); throw ex; } } }