代码优化

master
xiaochanghai 1 year ago
parent 67168bfd2f
commit 9331993cc3
  1. 179
      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<Ghre_Question, Ghre_QuestionDt
_ghre_CourseClassServices = ghre_CourseClassServices;
}
public override async Task<ServicePageResult<Ghre_QuestionDto>> QueryFilterPage(QueryBody body)
public override async Task<ServicePageResult<Ghre_QuestionDto>> QueryFilterPage(QueryBody filter)
{
var data1 = await BaseDal.QueryFilterPage(body);
//var data1 = await BaseDal.QueryFilterPage(body);
RefAsync<int> totalCount = 0;
var query = Db.Queryable<Ghre_Question>();
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<JsonParam>(value);
switch (jsonParam.operationKey)
{
case "Include":
var ids = await Db.Queryable<Ghre_Course>().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<Ghre_Course>().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<Ghre_Course>().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<Ghre_Course>().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<JsonParam>(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<Ghre_Question>(filter.pageNum, totalCount, filter.pageSize, list);
var data = Mapper.Map(data1.result.DT_TableDataT1).ToANew<List<Ghre_QuestionDto>>();
var courseIds = data.Where(x => x.CourseId != null).Select(x => x.CourseId).Distinct().ToList();
@ -56,7 +148,7 @@ public class Ghre_QuestionServices : BaseServices<Ghre_Question, Ghre_QuestionDt
}
});
return new ServicePageResult<Ghre_QuestionDto>(body.pageNum, data1.result.DT_TablePageInfoT1.TotalCount, body.pageSize, data);
return new ServicePageResult<Ghre_QuestionDto>(filter.pageNum, data1.result.DT_TablePageInfoT1.TotalCount, filter.pageSize, data);
}
/// <summary>
@ -205,13 +297,25 @@ public class Ghre_QuestionServices : BaseServices<Ghre_Question, Ghre_QuestionDt
public async Task<ServiceResult> 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<Ghre_Question, Ghre_QuestionDt
throw new Exception($"课程【{course.CourseName}】存在相同内容【{questionTypeName}】");
}
}
#endregion
#endregion
for (int j = 0; j < insertModel.baseData.courseID.Count; j++)
{
var courseID = insertModel.baseData.courseID[j];
@ -239,7 +342,7 @@ public class Ghre_QuestionServices : BaseServices<Ghre_Question, Ghre_QuestionDt
insert.DifficultyLevel = questionType.detail.difficulty;
insert.QuestionContent = questionType.detail.content;
insert.QuestionAnalysis = questionType.detail.RemarkSz;
insert.QuestionNo = GenerateContinuousSequence(insert.QuestionType.Substring(0, 1));
insert.QuestionNo = await GenerateContinuousSequence(insert.QuestionType.Substring(0, 1));
var id = await base.Add(insert);
@ -256,12 +359,17 @@ public class Ghre_QuestionServices : BaseServices<Ghre_Question, Ghre_QuestionDt
{
x.TaxisNo = i;
x.QuestionId = id;
if (questionType.detail.answer != null && questionType.detail.answer == x.QuestionNo && insert.QuestionType == "Single")
if (questionType.detail.answer != null && questionType.detail.answer == x.QuestionNo && (insert.QuestionType == "Single" || insert.QuestionType == "TrueOrFalse"))
x.IsCorrect = true;
if (questionType.detail.answer1 != null && questionType.detail.answer1.Contains(x.QuestionNo) && insert.QuestionType == "Multiple")
if (questionType.detail.answer1 != null && questionType.detail.answer1.Contains(x.QuestionNo) && (insert.QuestionType == "ShortAnswer" || insert.QuestionType == "Multiple" || insert.QuestionType == "Completion"))
x.IsCorrect = true;
i = i + 100;
});
if (insertAnswers.Where(b => 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<Ghre_Question, Ghre_QuestionDt
public async Task<ServiceResult> 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<Ghre_Question, Ghre_QuestionDt
{
x.TaxisNo = i;
x.QuestionId = Id;
if (questionType.detail.answer != null && questionType.detail.answer == x.QuestionNo && edit.QuestionType == "Single")
if (questionType.detail.answer != null && questionType.detail.answer == x.QuestionNo && (edit.QuestionType == "Single" || edit.QuestionType == "TrueOrFalse"))
x.IsCorrect = true;
if (questionType.detail.answer1 != null && questionType.detail.answer1.Contains(x.QuestionNo) && edit.QuestionType == "Multiple")
if (questionType.detail.answer1 != null && questionType.detail.answer1.Contains(x.QuestionNo) && (edit.QuestionType == "ShortAnswer" || edit.QuestionType == "Multiple" || edit.QuestionType == "Completion"))
x.IsCorrect = true;
i = i + 100;
});
if (insertAnswers.Where(b => 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<Ghre_Question, Ghre_QuestionDt
insert.DifficultyLevel = questionType.detail.difficulty;
insert.QuestionContent = questionType.detail.content;
insert.QuestionAnalysis = questionType.detail.RemarkSz;
insert.QuestionNo = GenerateContinuousSequence(insert.QuestionType.Substring(0, 1));
insert.QuestionNo = await GenerateContinuousSequence(insert.QuestionType.Substring(0, 1));
var id = await base.Add(insert);
@ -362,12 +488,17 @@ public class Ghre_QuestionServices : BaseServices<Ghre_Question, Ghre_QuestionDt
{
x.TaxisNo = i;
x.QuestionId = id;
if (questionType.detail.answer != null && questionType.detail.answer == x.QuestionNo && insert.QuestionType == "Single")
if (questionType.detail.answer != null && questionType.detail.answer == x.QuestionNo && (insert.QuestionType == "Single" || insert.QuestionType == "TrueOrFalse"))
x.IsCorrect = true;
if (questionType.detail.answer1 != null && questionType.detail.answer1.Contains(x.QuestionNo) && insert.QuestionType == "Multiple")
if (questionType.detail.answer1 != null && questionType.detail.answer1.Contains(x.QuestionNo) && (insert.QuestionType == "ShortAnswer" || insert.QuestionType == "Multiple" || insert.QuestionType == "Completion"))
x.IsCorrect = true;
i = i + 100;
});
if (insertAnswers.Where(b => 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<Ghre_Question, Ghre_QuestionDt
}
}
public static string GenerateContinuousSequence(string prefixTemp)
public async Task<string> GenerateContinuousSequence(string prefixTemp)
{
try
{
@ -406,7 +537,8 @@ public class Ghre_QuestionServices : BaseServices<Ghre_Question, Ghre_QuestionDt
dbSelect.Where("SUBSTRING(A." + columnCode + ",1," + (prefixTemp.Length).ToString() + ")", " = ", prefixTemp);
dbSelect.Where("LEN(A." + columnCode + ")", "=", length);
string sql = dbSelect.GetSql();
string maxSequence = Convert.ToString(DbAccess.Instance.ExecuteScalar(sql));
//await Db.Ado.GetScalarAsync(sql)
string maxSequence = Convert.ToString(await Db.Ado.GetScalarAsync(sql));
#endregion
//tempLength = tempLength - dateString.Length;
if (string.IsNullOrEmpty(maxSequence))
@ -472,24 +604,27 @@ public class Ghre_QuestionServices : BaseServices<Ghre_Question, Ghre_QuestionDt
public static void ValidForm(FromGhre_QuestionPageData model)
{
if (model.baseData.courseID is null || (model.baseData.courseID != null && !model.baseData.courseID.Any()))
throw new Exception("课程编号必填!");
throw new Exception("课程必填!");
var questionType = model.questionType.Where(x => 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("题目解析必填!");
}
}
Loading…
Cancel
Save