diff --git a/Tiobon.Core.Api/Controllers/Ghre/Ghre_QuestionController.cs b/Tiobon.Core.Api/Controllers/Ghre/Ghre_QuestionController.cs index a960a0ac..3e29d183 100644 --- a/Tiobon.Core.Api/Controllers/Ghre/Ghre_QuestionController.cs +++ b/Tiobon.Core.Api/Controllers/Ghre/Ghre_QuestionController.cs @@ -1,8 +1,4 @@ -using System.Drawing; -using System.Dynamic; -using Mysqlx.Crud; - -namespace Tiobon.Core.Api.Controllers; +namespace Tiobon.Core.Api.Controllers; /// /// 题目(Controller) @@ -22,21 +18,6 @@ public class Ghre_QuestionController : BaseController - /// 根据Id查询数据 - /// - /// 主键ID - /// - [HttpPost("Query/{Id}")] - public override async Task> QueryById(long Id) - { - var entity = await _service.QueryById(Id); - entity.Answers = await _ghre_QuestionAnswerServices.Query(x => x.QuestionId == Id, "TaxisNo ASC"); - if (entity is null) - return Failed("获取失败", 500); - else - return Success(entity, "获取成功"); - } /// /// 根据Id查询数据 /// @@ -55,30 +36,6 @@ public class Ghre_QuestionController : BaseController /// /// - [HttpPost("Insert")] - public override async Task> Insert([FromBody] InsertGhre_QuestionInput insertModel) - { - var id = await _service.Add(insertModel); - var answers = insertModel.Answers; - if (answers.Any()) - { - int i = 100; - answers.ForEach(x => - { - x.TaxisNo = i; - x.QuestionId = id; - }); - i = i + 100; - await _ghre_QuestionAnswerServices.Add(answers); - } - var data = Success(null, "新增成功"); - data.Success = id > 0; - if (data.Success) - data.Data = id.ObjToString(); - else - return Failed("新增失败"); - return data; - } [HttpPost("InsertFrom")] public async Task InsertFrom([FromBody] FromGhre_QuestionPageData insertModel) { @@ -93,29 +50,11 @@ public class Ghre_QuestionController : BaseController主键ID /// /// - [HttpPost("Update/{Id}")] - public override async Task Put(long Id, [FromBody] EditGhre_QuestionInput editModel) - { - await _ghre_QuestionAnswerServices.Delete(x => x.QuestionId == Id); - var answers = editModel.Answers; - if (answers.Any()) - { - int i = 100; - answers.ForEach(x => - { - x.TaxisNo = i; - x.QuestionId = Id; - }); - i = i + 100; - await _ghre_QuestionAnswerServices.Add(answers); - } - return await base.Put(Id, editModel); - } [HttpPost("UpdareFrom/{Id}")] - public async Task UpdareFrom(long Id, [FromBody] FromGhre_QuestionPageData insertModel) + public async Task UpdareFrom(long Id, [FromBody] FromGhre_QuestionPageData editModel) { - return await _service.UpdareFrom(Id, insertModel); + return await _service.UpdareFrom(Id, editModel); } #endregion diff --git a/Tiobon.Core.Api/Tiobon.Core.Model.xml b/Tiobon.Core.Api/Tiobon.Core.Model.xml index 8f874723..b3339654 100644 --- a/Tiobon.Core.Api/Tiobon.Core.Model.xml +++ b/Tiobon.Core.Api/Tiobon.Core.Model.xml @@ -3327,11 +3327,6 @@ 题目(Dto.View) - - - 答案 - - 课程名称 diff --git a/Tiobon.Core.Api/Tiobon.Core.xml b/Tiobon.Core.Api/Tiobon.Core.xml index 19c57917..d73b5de9 100644 --- a/Tiobon.Core.Api/Tiobon.Core.xml +++ b/Tiobon.Core.Api/Tiobon.Core.xml @@ -553,13 +553,6 @@ 题目(Controller) - - - 根据Id查询数据 - - 主键ID - - 根据Id查询数据 @@ -567,14 +560,14 @@ - + 新增数据 - + 更新数据 diff --git a/Tiobon.Core.Model/View/Ghre/Ghre_Question.Dto.View.cs b/Tiobon.Core.Model/View/Ghre/Ghre_Question.Dto.View.cs index 914b5ec8..2a86b471 100644 --- a/Tiobon.Core.Model/View/Ghre/Ghre_Question.Dto.View.cs +++ b/Tiobon.Core.Model/View/Ghre/Ghre_Question.Dto.View.cs @@ -26,13 +26,6 @@ namespace Tiobon.Core.Model.Models /// public class Ghre_QuestionDto : Ghre_Question { - - /// - /// 答案 - /// - public List Answers { get; set; } - - /// /// 课程名称 /// diff --git a/Tiobon.Core.Model/ViewModels/Extend/FromGhre_QuestionInput.cs b/Tiobon.Core.Model/ViewModels/Extend/FromGhre_QuestionInput.cs index 407028b6..8e886805 100644 --- a/Tiobon.Core.Model/ViewModels/Extend/FromGhre_QuestionInput.cs +++ b/Tiobon.Core.Model/ViewModels/Extend/FromGhre_QuestionInput.cs @@ -1,4 +1,7 @@ -namespace Tiobon.Core.Model; +using System.ComponentModel; +using System.ComponentModel.DataAnnotations; + +namespace Tiobon.Core.Model; public class FromGhre_QuestionInput { diff --git a/Tiobon.Core.Services/Ghre/Ghre_QuestionServices.cs b/Tiobon.Core.Services/Ghre/Ghre_QuestionServices.cs index 2335a69c..640d14a3 100644 --- a/Tiobon.Core.Services/Ghre/Ghre_QuestionServices.cs +++ b/Tiobon.Core.Services/Ghre/Ghre_QuestionServices.cs @@ -1,6 +1,7 @@  using System.Data; using AgileObjects.AgileMapper; +using Mysqlx.Crud; using Tiobon.Core.Common; using Tiobon.Core.Common.DB.Dapper; using Tiobon.Core.IRepository.Base; @@ -189,7 +190,6 @@ public class Ghre_QuestionServices : BaseServices x.IsCorrect == true).FirstOrDefault()?.QuestionNo; questionType.detail.answer1 = answers.Where(x => x.IsCorrect == true).Select(x => x.QuestionNo).ToList(); - questionType.detail.answer1 = answers.Where(x => x.IsCorrect == true).Select(x => x.QuestionNo).ToList(); questionType.detail.content = question.QuestionContent; questionType.detail.RemarkSz = question.QuestionAnalysis; questionType.detail.answerList = answers.Select(x => new FromGhre_QuestionQuestionAnswerList() @@ -209,129 +209,113 @@ public class Ghre_QuestionServices : BaseServices InsertFrom(FromGhre_QuestionPageData insertModel) { - var questionType = insertModel.questionType.Where(x => x.isActive == 1).FirstOrDefault(); - string questionTypeName = ConvertQuestionType(questionType.type); - #region 判断是否重复 - for (int j = 0; j < insertModel.baseData.courseID.Count; j++) + ValidForm(insertModel); + await Db.Ado.BeginTranAsync(); + + try { - var courseID = insertModel.baseData.courseID[j]; - string sql = "SELECT * FROM Ghre_Question where QuestionType='{0}' and CourseId='{1}' and QuestionContent ='{2}'"; - sql = string.Format(sql, questionType.type, courseID, questionType.detail.content); - DataTable dt = Db.Ado.GetDataTable(sql); - if (dt.Rows.Count > 0) + var questionType = insertModel.questionType.Where(x => x.isActive == 1).FirstOrDefault(); + string questionTypeName = ConvertQuestionType(questionType.type); + + #region 判断是否重复 + for (int j = 0; j < insertModel.baseData.courseID.Count; j++) { - var course = await _ghre_CourseServices.QueryById(courseID); - throw new Exception($"课程【{course.CourseName}】存在相同内容【{questionTypeName}】"); + var courseID = insertModel.baseData.courseID[j]; + string sql = "SELECT * FROM Ghre_Question where QuestionType='{0}' and CourseId='{1}' and QuestionContent ='{2}'"; + sql = string.Format(sql, questionType.type, courseID, questionType.detail.content); + DataTable dt = Db.Ado.GetDataTable(sql); + if (dt.Rows.Count > 0) + { + var course = await _ghre_CourseServices.QueryById(courseID); + throw new Exception($"课程【{course.CourseName}】存在相同内容【{questionTypeName}】"); + } } - } - #endregion + #endregion - for (int j = 0; j < insertModel.baseData.courseID.Count; j++) - { - var courseID = insertModel.baseData.courseID[j]; - var insert = new InsertGhre_QuestionInput(); - insert.CourseId = courseID; + for (int j = 0; j < insertModel.baseData.courseID.Count; j++) + { + var courseID = insertModel.baseData.courseID[j]; + var insert = new InsertGhre_QuestionInput(); + insert.CourseId = courseID; - insert.QuestionType = questionType.type; - insert.DifficultyLevel = questionType.detail.difficulty; - insert.QuestionContent = questionType.detail.content; - insert.QuestionAnalysis = questionType.detail.RemarkSz; - insert.QuestionNo = GenerateContinuousSequence(insert.QuestionType.Substring(0, 1)); + insert.QuestionType = questionType.type; + insert.DifficultyLevel = questionType.detail.difficulty; + insert.QuestionContent = questionType.detail.content; + insert.QuestionAnalysis = questionType.detail.RemarkSz; + insert.QuestionNo = GenerateContinuousSequence(insert.QuestionType.Substring(0, 1)); - var id = await base.Add(insert); + var id = await base.Add(insert); - var insertAnswers = questionType.detail.answerList.Select(x => new InsertGhre_QuestionAnswerInput() - { - QuestionNo = x.No, - AnswerContent = x.label, - ImageUrl = x.imageUrl, - ImageWidthPc = x.imgWidthPc, - ImageWidthApp = x.imgWidthApp, - }).ToList(); - int i = 100; - insertAnswers.ForEach(x => - { - x.TaxisNo = i; - x.QuestionId = id; - if (questionType.detail.answer == x.QuestionNo && insert.QuestionType == "Single") - x.IsCorrect = true; - i = i + 100; - }); - await _ghre_QuestionAnswerServices.Add(insertAnswers); + var insertAnswers = questionType.detail.answerList.Select(x => new InsertGhre_QuestionAnswerInput() + { + QuestionNo = x.No, + AnswerContent = x.label, + ImageUrl = x.imageUrl, + ImageWidthPc = x.imgWidthPc, + ImageWidthApp = x.imgWidthApp, + }).ToList(); + int i = 100; + insertAnswers.ForEach(x => + { + x.TaxisNo = i; + x.QuestionId = id; + if (questionType.detail.answer != null && questionType.detail.answer == x.QuestionNo && insert.QuestionType == "Single") + x.IsCorrect = true; + if (questionType.detail.answer1 != null && questionType.detail.answer1.Contains(x.QuestionNo) && insert.QuestionType == "Multiple") + x.IsCorrect = true; + i = i + 100; + }); + await _ghre_QuestionAnswerServices.Add(insertAnswers); + } + await Db.Ado.CommitTranAsync(); + return ServiceResult.OprateSuccess("新增成功!"); + } + catch (Exception) + { + await Db.Ado.RollbackTranAsync(); + throw; } - - return ServiceResult.OprateSuccess("新增成功!"); } public async Task UpdareFrom(long Id, FromGhre_QuestionPageData editModel) { - var questionType = editModel.questionType.Where(x => x.isActive == 1).FirstOrDefault(); - string questionTypeName = ConvertQuestionType(questionType.type); - - #region 判断是否重复 - for (int j = 0; j < editModel.baseData.courseID.Count; j++) - { - var courseID = editModel.baseData.courseID[j]; - string sql = "SELECT * FROM Ghre_Question where QuestionType='{0}' and CourseId='{1}' and QuestionContent ='{2}' AND Id !='{3}'"; - sql = string.Format(sql, questionType.type, courseID, questionType.detail.content, Id); - DataTable dt = Db.Ado.GetDataTable(sql); - if (dt.Rows.Count > 0) - { - var course = await _ghre_CourseServices.QueryById(courseID); - throw new Exception($"课程【{course.CourseName}】存在相同内容【{questionTypeName}】"); - } - } - #endregion - - var data = await base.QueryById(Id); - await _ghre_QuestionAnswerServices.Delete(x => x.QuestionId == Id); - var edit = Mapper.Map(data).ToANew(); - edit.CourseId = editModel.baseData.courseID[0]; - - edit.QuestionType = questionType.type; - edit.DifficultyLevel = questionType.detail.difficulty; - edit.QuestionContent = questionType.detail.content; - edit.QuestionAnalysis = questionType.detail.RemarkSz; + ValidForm(editModel); - await base.Update(Id, edit); - var insertAnswers = questionType.detail.answerList.Select(x => new InsertGhre_QuestionAnswerInput() - { - QuestionNo = x.No, - AnswerContent = x.label, - ImageUrl = x.imageUrl, - ImageWidthPc = x.imgWidthPc, - ImageWidthApp = x.imgWidthApp, - }).ToList(); - int i = 100; - insertAnswers.ForEach(x => - { - x.TaxisNo = i; - x.QuestionId = Id; - if (questionType.detail.answer == x.QuestionNo && edit.QuestionType == "Single") - x.IsCorrect = true; - i = i + 100; - }); - await _ghre_QuestionAnswerServices.Add(insertAnswers); + await Db.Ado.BeginTranAsync(); - for (int j = 0; j < editModel.baseData.courseID.Count; j++) + try { - if (j == 0) - continue; + var questionType = editModel.questionType.Where(x => x.isActive == 1).FirstOrDefault(); + string questionTypeName = ConvertQuestionType(questionType.type); - var courseID = editModel.baseData.courseID[j]; - var insert = new InsertGhre_QuestionInput(); - insert.CourseId = courseID; + #region 判断是否重复 + for (int j = 0; j < editModel.baseData.courseID.Count; j++) + { + var courseID = editModel.baseData.courseID[j]; + string sql = "SELECT * FROM Ghre_Question where QuestionType='{0}' and CourseId='{1}' and QuestionContent ='{2}' AND Id !='{3}'"; + sql = string.Format(sql, questionType.type, courseID, questionType.detail.content, Id); + DataTable dt = Db.Ado.GetDataTable(sql); + if (dt.Rows.Count > 0) + { + var course = await _ghre_CourseServices.QueryById(courseID); + throw new Exception($"课程【{course.CourseName}】存在相同内容【{questionTypeName}】"); + } + } + #endregion - insert.QuestionType = questionType.type; - insert.DifficultyLevel = questionType.detail.difficulty; - insert.QuestionContent = questionType.detail.content; - insert.QuestionAnalysis = questionType.detail.RemarkSz; - insert.QuestionNo = GenerateContinuousSequence(insert.QuestionType.Substring(0, 1)); + var data = await base.QueryById(Id); + await _ghre_QuestionAnswerServices.Delete(x => x.QuestionId == Id); + var edit = Mapper.Map(data).ToANew(); + edit.CourseId = editModel.baseData.courseID[0]; - var id = await base.Add(insert); + edit.QuestionType = questionType.type; + edit.DifficultyLevel = questionType.detail.difficulty; + edit.QuestionContent = questionType.detail.content; + edit.QuestionAnalysis = questionType.detail.RemarkSz; - insertAnswers = questionType.detail.answerList.Select(x => new InsertGhre_QuestionAnswerInput() + await base.Update(Id, edit); + var insertAnswers = questionType.detail.answerList.Select(x => new InsertGhre_QuestionAnswerInput() { QuestionNo = x.No, AnswerContent = x.label, @@ -339,19 +323,66 @@ public class Ghre_QuestionServices : BaseServices { x.TaxisNo = i; - x.QuestionId = id; - if (questionType.detail.answer == x.QuestionNo && insert.QuestionType == "Single") + x.QuestionId = Id; + if (questionType.detail.answer != null && questionType.detail.answer == x.QuestionNo && edit.QuestionType == "Single") + x.IsCorrect = true; + if (questionType.detail.answer1 != null && questionType.detail.answer1.Contains(x.QuestionNo) && edit.QuestionType == "Multiple") x.IsCorrect = true; i = i + 100; }); await _ghre_QuestionAnswerServices.Add(insertAnswers); - } - return ServiceResult.OprateSuccess("更新成功!"); + for (int j = 0; j < editModel.baseData.courseID.Count; j++) + { + if (j == 0) + continue; + + var courseID = editModel.baseData.courseID[j]; + var insert = new InsertGhre_QuestionInput(); + insert.CourseId = courseID; + + insert.QuestionType = questionType.type; + insert.DifficultyLevel = questionType.detail.difficulty; + insert.QuestionContent = questionType.detail.content; + insert.QuestionAnalysis = questionType.detail.RemarkSz; + insert.QuestionNo = GenerateContinuousSequence(insert.QuestionType.Substring(0, 1)); + + var id = await base.Add(insert); + + insertAnswers = questionType.detail.answerList.Select(x => new InsertGhre_QuestionAnswerInput() + { + QuestionNo = x.No, + AnswerContent = x.label, + ImageUrl = x.imageUrl, + ImageWidthPc = x.imgWidthPc, + ImageWidthApp = x.imgWidthApp, + }).ToList(); + i = 100; + insertAnswers.ForEach(x => + { + x.TaxisNo = i; + x.QuestionId = id; + if (questionType.detail.answer != null && questionType.detail.answer == x.QuestionNo && insert.QuestionType == "Single") + x.IsCorrect = true; + if (questionType.detail.answer1 != null && questionType.detail.answer1.Contains(x.QuestionNo) && insert.QuestionType == "Multiple") + x.IsCorrect = true; + i = i + 100; + }); + await _ghre_QuestionAnswerServices.Add(insertAnswers); + } + await Db.Ado.CommitTranAsync(); + + return ServiceResult.OprateSuccess("更新成功!"); + } + catch (Exception) + { + await Db.Ado.RollbackTranAsync(); + throw; + } } public static string GenerateContinuousSequence(string prefixTemp) @@ -431,4 +462,38 @@ public class Ghre_QuestionServices : BaseServices x.isActive == 1).FirstOrDefault(); + + if (questionType is null) + throw new Exception("提交参数异常!"); + if (string.IsNullOrWhiteSpace(questionType.detail.difficulty)) + throw new Exception("难易程度不能为空!"); + + if (string.IsNullOrWhiteSpace(ConvertDifficultyLevel(questionType.detail.difficulty))) + throw new Exception("无效的难易程度类型!"); + if (string.IsNullOrWhiteSpace(questionType.detail.content)) + throw new Exception("题目内容必填!"); + if (questionType.detail.answerList is null || (questionType.detail.answerList != null && !questionType.detail.answerList.Any())) + throw new Exception("答案选项必填!"); + if (questionType.detail.answerList.Count < 2) + throw new Exception("答案选项不能少于两个!"); + if (string.IsNullOrWhiteSpace( questionType.detail.answer ) && (questionType.detail.answer is null || (questionType.detail.answer != null && !questionType.detail.answerList.Any()))) + throw new Exception("正确答案未标记!"); + } } \ No newline at end of file diff --git a/Tiobon.Core/Tiobon.Core.Model.xml b/Tiobon.Core/Tiobon.Core.Model.xml index 8f874723..b3339654 100644 --- a/Tiobon.Core/Tiobon.Core.Model.xml +++ b/Tiobon.Core/Tiobon.Core.Model.xml @@ -3327,11 +3327,6 @@ 题目(Dto.View) - - - 答案 - - 课程名称 diff --git a/Tiobon.Core/Tiobon.Core.xml b/Tiobon.Core/Tiobon.Core.xml index 19c57917..d73b5de9 100644 --- a/Tiobon.Core/Tiobon.Core.xml +++ b/Tiobon.Core/Tiobon.Core.xml @@ -553,13 +553,6 @@ 题目(Controller) - - - 根据Id查询数据 - - 主键ID - - 根据Id查询数据 @@ -567,14 +560,14 @@ - + 新增数据 - + 更新数据