diff --git a/Tiobon.Core.Services/Ghre/Ghre_QuestionServices.cs b/Tiobon.Core.Services/Ghre/Ghre_QuestionServices.cs index 40ebed8d..e6e3c4d8 100644 --- a/Tiobon.Core.Services/Ghre/Ghre_QuestionServices.cs +++ b/Tiobon.Core.Services/Ghre/Ghre_QuestionServices.cs @@ -2,6 +2,9 @@ using System.Data; using AgileObjects.AgileMapper; using Mysqlx.Crud; +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; +using SqlSugar; using Tiobon.Core.Common; using Tiobon.Core.Common.DB.Dapper; using Tiobon.Core.IRepository.Base; @@ -9,6 +12,7 @@ using Tiobon.Core.IServices; using Tiobon.Core.Model; using Tiobon.Core.Model.Models; using Tiobon.Core.Services.BASE; +using static Dapper.SqlMapper; namespace Tiobon.Core.Services; @@ -33,9 +37,97 @@ public class Ghre_QuestionServices : BaseServices> QueryFilterPage(QueryBody body) + public override async Task> QueryFilterPage(QueryBody filter) { - var data1 = await BaseDal.QueryFilterPage(body); + //var data1 = await BaseDal.QueryFilterPage(body); + + RefAsync totalCount = 0; + var query = Db.Queryable(); + + string conditions = "1=1"; + if (filter.jsonParam != null) + foreach (JProperty jProperty in filter.jsonParam.Properties()) + { + var name = jProperty.Name; + var value = jProperty.Value.ToString(); + if (name == "page" || name == "pageSize") + continue; + if (name == "CourseName") + { + string sql = string.Empty; + if (!string.IsNullOrWhiteSpace(value)) + { + var jsonParam = JsonConvert.DeserializeObject(value); + + switch (jsonParam.operationKey) + { + case "Include": + var ids = await Db.Queryable().Where(x => x.CourseName.Contains(jsonParam.columnValue)).Select(x => x.Id).ToListAsync(); + query = query.Where(x => x.CourseId != null && ids.Contains(x.CourseId.Value)); + break; + case "NotInclude": + var ids1 = await Db.Queryable().Where(x => x.CourseName.Contains(jsonParam.columnValue)).Select(x => x.Id).ToListAsync(); + query = query.Where(x => x.CourseId != null && !ids1.Contains(x.CourseId.Value)); + break; + case "IsNull": + query = query.Where(x => x.CourseId == null); + break; + case "NotNull": + query = query.Where(x => x.CourseId != null); + break; + case "Equal": + var id = await Db.Queryable().Where(x => x.CourseName == jsonParam.columnValue).Select(x => x.Id).FirstAsync(); + query = query.Where(x => x.CourseId == id); + + break; + case "NotEqual": + var id1 = await Db.Queryable().Where(x => x.CourseName == jsonParam.columnValue).Select(x => x.Id).FirstAsync(); + query = query.Where(x => x.CourseId != id1); + break; + default: + break; + } + } + } + else + if (!string.IsNullOrWhiteSpace(value)) + { + var jsonParam = JsonConvert.DeserializeObject(value); + + switch (jsonParam.operationKey) + { + case "Include": + conditions += $" AND {name} LIKE '%{jsonParam.columnValue}%'"; + break; + case "NotInclude": + conditions += $" AND {name} NOT LIKE '%{jsonParam.columnValue}%'"; + break; + case "IsNull": + conditions += $" AND {name} IS NULL"; + break; + case "NotNull": + conditions += $" AND {name} IS NOT NULL"; + break; + case "Equal": + conditions += $" AND {name} ='{jsonParam.columnValue}'"; + break; + case "NotEqual": + conditions += $" AND {name} !='{jsonParam.columnValue}'"; + break; + default: + break; + } + } + } + if (filter.pageSize == 0) + filter.pageSize = 10000; + query = query.Where(conditions); + var list = await query + .OrderByIF(!string.IsNullOrEmpty(filter.Sorting), filter.Sorting) + .ToPageListAsync(filter.pageNum, filter.pageSize, totalCount); + + var data1 = new ServicePageResult(filter.pageNum, totalCount, filter.pageSize, list); + var data = Mapper.Map(data1.result.DT_TableDataT1).ToANew>(); var courseIds = data.Where(x => x.CourseId != null).Select(x => x.CourseId).Distinct().ToList(); @@ -56,7 +148,7 @@ public class Ghre_QuestionServices : BaseServices(body.pageNum, data1.result.DT_TablePageInfoT1.TotalCount, body.pageSize, data); + return new ServicePageResult(filter.pageNum, data1.result.DT_TablePageInfoT1.TotalCount, filter.pageSize, data); } /// @@ -205,13 +297,25 @@ public class Ghre_QuestionServices : BaseServices InsertFrom(FromGhre_QuestionPageData insertModel) { + var questionType = insertModel.questionType.Where(x => x.isActive == 1).FirstOrDefault(); + #region 填空题处理 + if (questionType.type == "Completion") + questionType.detail.answerList = questionType.detail.answer1 + .Select(x => new FromGhre_QuestionQuestionAnswerList() + { + No = x, + label = x, + }).ToList(); + else if (questionType.type == "ShortAnswer") + questionType.detail.answer1 = questionType.detail.answerList.Select(x => x.No).ToList(); + + #endregion ValidForm(insertModel); await Db.Ado.BeginTranAsync(); try { - var questionType = insertModel.questionType.Where(x => x.isActive == 1).FirstOrDefault(); string questionTypeName = ConvertQuestionType(questionType.type); #region 判断是否重复 @@ -227,8 +331,7 @@ public class Ghre_QuestionServices : BaseServices string.IsNullOrWhiteSpace(b.AnswerContent)).Any()) + throw new Exception(insert.QuestionType == "ShortAnswer" ? "关键词存在空值!" : "答案选项存在空值!"); + if (!insertAnswers.Where(b => b.IsCorrect == true).Any()) + throw new Exception(insert.QuestionType == "ShortAnswer" ? "关键词未填写!" : "正确答案未标记!"); await _ghre_QuestionAnswerServices.Add(insertAnswers); } await Db.Ado.CommitTranAsync(); @@ -276,13 +384,26 @@ public class Ghre_QuestionServices : BaseServices UpdareFrom(long Id, FromGhre_QuestionPageData editModel) { + + var questionType = editModel.questionType.Where(x => x.isActive == 1).FirstOrDefault(); + #region 填空题处理 + if (questionType.type == "Completion") + questionType.detail.answerList = questionType.detail.answer1 + .Select(x => new FromGhre_QuestionQuestionAnswerList() + { + No = x, + label = x, + }).ToList(); + else if (questionType.type == "ShortAnswer") + questionType.detail.answer1 = questionType.detail.answerList.Select(x => x.No).ToList(); + #endregion + ValidForm(editModel); await Db.Ado.BeginTranAsync(); try { - var questionType = editModel.questionType.Where(x => x.isActive == 1).FirstOrDefault(); string questionTypeName = ConvertQuestionType(questionType.type); #region 判断是否重复 @@ -324,12 +445,17 @@ public class Ghre_QuestionServices : BaseServices string.IsNullOrWhiteSpace(b.AnswerContent)).Any()) + throw new Exception(edit.QuestionType == "ShortAnswer" ? "关键词存在空值!" : "答案选项存在空值!"); + if (!insertAnswers.Where(b => b.IsCorrect == true).Any()) + throw new Exception(edit.QuestionType == "ShortAnswer" ? "关键词未填写!" : "正确答案未标记!"); + await _ghre_QuestionAnswerServices.Add(insertAnswers); for (int j = 0; j < editModel.baseData.courseID.Count; j++) @@ -345,7 +471,7 @@ public class Ghre_QuestionServices : BaseServices string.IsNullOrWhiteSpace(b.AnswerContent)).Any()) + throw new Exception(insert.QuestionType == "ShortAnswer" ? "关键词存在空值!" : "答案选项存在空值!"); + if (!insertAnswers.Where(b => b.IsCorrect == true).Any()) + throw new Exception(insert.QuestionType == "ShortAnswer" ? "关键词未填写!" : "正确答案未标记!"); + await _ghre_QuestionAnswerServices.Add(insertAnswers); } await Db.Ado.CommitTranAsync(); @@ -381,7 +512,7 @@ public class Ghre_QuestionServices : BaseServices GenerateContinuousSequence(string prefixTemp) { try { @@ -406,7 +537,8 @@ 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("难易程度不能为空!"); + 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(questionType.type == "ShortAnswer" ? "关键词未填写!" : "答案选项必填!"); + if (questionType.detail.answerList.Count < 2 && questionType.type != "ShortAnswer") throw new Exception("答案选项不能少于两个!"); - if (string.IsNullOrWhiteSpace(questionType.detail.answer) && (questionType.detail.answer is null || (questionType.detail.answer != null && !questionType.detail.answerList.Any()))) + if (questionType.detail.answer1 != null && !questionType.detail.answer1.Any() && string.IsNullOrWhiteSpace(questionType.detail.answer) && (questionType.detail.answer is null || (questionType.detail.answer != null && !questionType.detail.answerList.Any()))) throw new Exception("正确答案未标记!"); + + if (string.IsNullOrWhiteSpace(questionType.detail.RemarkSz)) + throw new Exception("题目解析必填!"); } } \ No newline at end of file