|
|
|
@ -1,4 +1,6 @@ |
|
|
|
|
namespace Tiobon.Core.Services; |
|
|
|
|
using Mysqlx.Crud; |
|
|
|
|
|
|
|
|
|
namespace Tiobon.Core.Services; |
|
|
|
|
|
|
|
|
|
/// <summary> |
|
|
|
|
/// 问卷题库 (服务) |
|
|
|
@ -6,10 +8,253 @@ |
|
|
|
|
public class Ghre_SurveyQuestionPoolServices : BaseServices<Ghre_SurveyQuestionPool, Ghre_SurveyQuestionPoolDto, InsertGhre_SurveyQuestionPoolInput, EditGhre_SurveyQuestionPoolInput>, IGhre_SurveyQuestionPoolServices |
|
|
|
|
{ |
|
|
|
|
private readonly IBaseRepository<Ghre_SurveyQuestionPool> _dal; |
|
|
|
|
public Ghre_SurveyQuestionPoolServices(ICaching caching, IBaseRepository<Ghre_SurveyQuestionPool> dal) |
|
|
|
|
private IGhre_SurveyQuestionPoolOptionServices _ghre_SurveyQuestionPoolOptionServicesServices; |
|
|
|
|
public Ghre_SurveyQuestionPoolServices(IBaseRepository<Ghre_SurveyQuestionPool> dal, |
|
|
|
|
IGhre_SurveyQuestionPoolOptionServices ghre_SurveyQuestionPoolOptionServices) |
|
|
|
|
{ |
|
|
|
|
this._dal = dal; |
|
|
|
|
base.BaseDal = dal; |
|
|
|
|
base._caching = caching; |
|
|
|
|
_ghre_SurveyQuestionPoolOptionServicesServices = ghre_SurveyQuestionPoolOptionServices; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public override async Task<ServicePageResult<Ghre_SurveyQuestionPoolDto>> QueryFilterPage(QueryBody filter, string condition, bool? IsEnable = true) |
|
|
|
|
{ |
|
|
|
|
|
|
|
|
|
var result = await base.QueryFilterPage(filter, condition, IsEnable); |
|
|
|
|
|
|
|
|
|
//data.ForEach(async x => |
|
|
|
|
//{ |
|
|
|
|
// x.DifficultyLevelLabel = await GetParaLabel("DifficultyLevel", x.DifficultyLevel); |
|
|
|
|
// x.QuestionTypeLabel = await GetParaLabel("QuestionType", x.QuestionType); |
|
|
|
|
// if (!string.IsNullOrEmpty(x.QuestionContent)) |
|
|
|
|
// x.QuestionContent = WebUtility.HtmlDecode(x.QuestionContent); |
|
|
|
|
// if (x.CourseIds.IsNotEmptyOrNull()) |
|
|
|
|
// { |
|
|
|
|
|
|
|
|
|
// var courseClass = courses.Where(a => x.CourseIds.Contains(a.Id.ToString())).ToList(); |
|
|
|
|
// x.CourseName = string.Join(",", courseClass.Select(a => a.CourseName + " (" + a.CourseNo + ")")); |
|
|
|
|
// } |
|
|
|
|
// x.BuiltInLabel = x.BuiltIn == 1 ? "是" : "否"; |
|
|
|
|
//}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return result; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/// <summary> |
|
|
|
|
/// |
|
|
|
|
/// </summary> |
|
|
|
|
/// <param name="Id"></param> |
|
|
|
|
/// <returns></returns> |
|
|
|
|
public async Task<ServiceResult<InsertGhre_SurveyQuestionExtend>> QueryFrom(long Id) |
|
|
|
|
{ |
|
|
|
|
var data = new InsertGhre_SurveyQuestionExtend(); |
|
|
|
|
|
|
|
|
|
if (Id > 0) |
|
|
|
|
{ |
|
|
|
|
var options = await Db.Queryable<Ghre_SurveyQuestionPoolOption>().OrderBy(x => x.SortNo).Where(x => x.SurveyQuestionPoolId == Id).ToListAsync(); |
|
|
|
|
data.Options = Mapper.Map(options).ToANew<List<InsertGhre_SurveyOptionExtend>>(); |
|
|
|
|
} |
|
|
|
|
return ServiceResult<InsertGhre_SurveyQuestionExtend>.OprateSuccess("查询成功!", data); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public async Task<ServiceResult> InsertFrom(InsertGhre_SurveyQuestionExtend insertModel) |
|
|
|
|
{ |
|
|
|
|
var question = Mapper.Map(insertModel).ToANew<Ghre_SurveyQuestionPool>(); |
|
|
|
|
|
|
|
|
|
question.QuestionNo = await GenerateContinuousSequence("Q"); |
|
|
|
|
var questionId = await Db.Insertable(question).ExecuteReturnSnowflakeIdAsync(); |
|
|
|
|
|
|
|
|
|
for (int j = 0; j < insertModel.Options.Count; j++) |
|
|
|
|
{ |
|
|
|
|
var option = Mapper.Map(insertModel.Options[j]).ToANew<Ghre_SurveyQuestionPoolOption>(); |
|
|
|
|
option.SurveyQuestionPoolId = questionId; |
|
|
|
|
option.SortNo = j; |
|
|
|
|
await Db.Insertable(option).ExecuteReturnSnowflakeIdAsync(); |
|
|
|
|
} |
|
|
|
|
return ServiceResult.OprateSuccess("新增成功!"); |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public async Task<ServiceResult> UpdateFrom(long Id, InsertGhre_SurveyQuestionExtend editModel) |
|
|
|
|
{ |
|
|
|
|
await Db.Ado.BeginTranAsync(); |
|
|
|
|
|
|
|
|
|
try |
|
|
|
|
{ |
|
|
|
|
await Db.Deleteable<Ghre_SurveyQuestionPoolOption>().Where(x => x.SurveyQuestionPoolId == Id).ExecuteCommandAsync(); |
|
|
|
|
|
|
|
|
|
var insert = Mapper.Map(editModel).ToANew<EditGhre_SurveyQuestionPoolInput>(); |
|
|
|
|
await base.Update(Id, insert, null, ["QuestionNo"]); |
|
|
|
|
|
|
|
|
|
for (int j = 0; j < editModel.Options.Count; j++) |
|
|
|
|
{ |
|
|
|
|
var option = Mapper.Map(editModel.Options[j]).ToANew<Ghre_SurveyOption>(); |
|
|
|
|
|
|
|
|
|
option.SurveyQuestionId = Id; |
|
|
|
|
await Db.Insertable(option).ExecuteReturnSnowflakeIdAsync(); |
|
|
|
|
} |
|
|
|
|
await Db.Ado.CommitTranAsync(); |
|
|
|
|
|
|
|
|
|
return ServiceResult.OprateSuccess("更新成功!"); |
|
|
|
|
} |
|
|
|
|
catch (Exception) |
|
|
|
|
{ |
|
|
|
|
await Db.Ado.RollbackTranAsync(); |
|
|
|
|
throw; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public async Task<string> GenerateContinuousSequence(string prefixTemp) |
|
|
|
|
{ |
|
|
|
|
try |
|
|
|
|
{ |
|
|
|
|
string result = string.Empty; |
|
|
|
|
string tableCode = "Ghre_SurveyQuestionPool"; |
|
|
|
|
string columnCode = "QuestionNo"; |
|
|
|
|
int length = 7; |
|
|
|
|
int tempLength = 6; |
|
|
|
|
int sequence; |
|
|
|
|
|
|
|
|
|
#region 查询 |
|
|
|
|
DbSelect dbSelect = new DbSelect(tableCode + " A", "A", null); |
|
|
|
|
dbSelect.IsInitDefaultValue = false; |
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(prefixTemp)) |
|
|
|
|
dbSelect.Select("MAX(SUBSTRING(A." + columnCode + "," + (prefixTemp.Length + 1).ToString() + "," + tempLength.ToString() + "))"); |
|
|
|
|
else |
|
|
|
|
dbSelect.Select("MAX(A." + columnCode + ")"); |
|
|
|
|
//} |
|
|
|
|
//dbSelect.Select("MAX(CONVERT(DECIMAL,SUBSTRING(A.ISSUE_NO," + (prefix.Length + dateString.Length + 1).ToString() + "," + tempLength.ToString() + ")))"); |
|
|
|
|
if (!string.IsNullOrEmpty(prefixTemp)) |
|
|
|
|
dbSelect.Where("SUBSTRING(A." + columnCode + ",1," + (prefixTemp.Length).ToString() + ")", " = ", prefixTemp); |
|
|
|
|
dbSelect.Where("LEN(A." + columnCode + ")", "=", length); |
|
|
|
|
string sql = dbSelect.GetSql(); |
|
|
|
|
//await Db.Ado.GetScalarAsync(sql) |
|
|
|
|
string maxSequence = Convert.ToString(await Db.Ado.GetScalarAsync(sql)); |
|
|
|
|
#endregion |
|
|
|
|
//tempLength = tempLength - dateString.Length; |
|
|
|
|
if (string.IsNullOrEmpty(maxSequence)) |
|
|
|
|
result = prefixTemp + Convert.ToString(1).PadLeft(tempLength, '0'); |
|
|
|
|
else |
|
|
|
|
{ |
|
|
|
|
if (!string.IsNullOrEmpty(prefixTemp)) |
|
|
|
|
{ |
|
|
|
|
if (int.TryParse(maxSequence, out sequence)) |
|
|
|
|
{ |
|
|
|
|
sequence += 1; |
|
|
|
|
if (sequence.ToString().Length > tempLength) |
|
|
|
|
throw new Exception("自动生成字串长度已经超过设定长度!"); |
|
|
|
|
} |
|
|
|
|
else |
|
|
|
|
throw new Exception("表中的数据无法进行自动编号,请联系软件开发商!"); |
|
|
|
|
result = prefixTemp + sequence.ToString().PadLeft(tempLength, '0'); |
|
|
|
|
} |
|
|
|
|
else |
|
|
|
|
{ |
|
|
|
|
if (int.TryParse(maxSequence, out sequence)) |
|
|
|
|
{ |
|
|
|
|
sequence += 1; |
|
|
|
|
if (sequence.ToString().Length > length) |
|
|
|
|
throw new Exception("自动生成字串长度已经超过设定长度!"); |
|
|
|
|
} |
|
|
|
|
else |
|
|
|
|
throw new Exception("表中的数据无法进行自动编号,请联系软件开发商!"); |
|
|
|
|
result = sequence.ToString().PadLeft(length, '0'); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
return result; |
|
|
|
|
} |
|
|
|
|
catch (Exception) { throw; } |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public static string ConvertQuestionType(string type) |
|
|
|
|
{ |
|
|
|
|
string questionTypeName = string.Empty; |
|
|
|
|
if (type == "Single") |
|
|
|
|
questionTypeName = "单选题"; |
|
|
|
|
else if (type == "Multiple") |
|
|
|
|
questionTypeName = "多选题"; |
|
|
|
|
else if (type == "TrueOrFalse") |
|
|
|
|
questionTypeName = "判断题"; |
|
|
|
|
else if (type == "Completion") |
|
|
|
|
questionTypeName = "填空题"; |
|
|
|
|
else if (type == "ShortAnswer") |
|
|
|
|
questionTypeName = "简答题"; |
|
|
|
|
return questionTypeName; |
|
|
|
|
} |
|
|
|
|
public static string ConvertDifficultyLevel(string type) |
|
|
|
|
{ |
|
|
|
|
|
|
|
|
|
string questionTypeName = string.Empty; |
|
|
|
|
if (type == "Easy") |
|
|
|
|
questionTypeName = "简单"; |
|
|
|
|
else if (type == "Normal") |
|
|
|
|
questionTypeName = "普通"; |
|
|
|
|
else if (type == "Hard") |
|
|
|
|
questionTypeName = "困难"; |
|
|
|
|
return questionTypeName; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public static string ConvertQuestionType1(string type) |
|
|
|
|
{ |
|
|
|
|
string questionTypeName = string.Empty; |
|
|
|
|
if (type == "单选题") |
|
|
|
|
questionTypeName = "Single"; |
|
|
|
|
else if (type == "多选题") |
|
|
|
|
questionTypeName = "Multiple"; |
|
|
|
|
else if (type == "判断题") |
|
|
|
|
questionTypeName = "TrueOrFalse"; |
|
|
|
|
else if (type == "填空题") |
|
|
|
|
questionTypeName = "Completion"; |
|
|
|
|
else if (type == "简答题") |
|
|
|
|
questionTypeName = "ShortAnswer"; |
|
|
|
|
return questionTypeName; |
|
|
|
|
} |
|
|
|
|
public static string ConvertDifficultyLevel1(string type) |
|
|
|
|
{ |
|
|
|
|
|
|
|
|
|
string questionTypeName = string.Empty; |
|
|
|
|
if (type == "简单") |
|
|
|
|
questionTypeName = "Easy"; |
|
|
|
|
else if (type == "普通") |
|
|
|
|
questionTypeName = "Normal"; |
|
|
|
|
else if (type == "困难") |
|
|
|
|
questionTypeName = "Hard"; |
|
|
|
|
return questionTypeName; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
//public static void ValidForm(FromGhre_SurveyQuestionPoolPageData model) |
|
|
|
|
//{ |
|
|
|
|
// if (model.baseData.courseID is null || (model.baseData.courseID != null && !model.baseData.courseID.Any())) |
|
|
|
|
// 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("难易程度必填!"); |
|
|
|
|
|
|
|
|
|
// 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(questionType.type == "ShortAnswer" ? "关键词未填写!" : "答案选项必填!"); |
|
|
|
|
// if (questionType.detail.answerList.Count < 2 && questionType.type != "ShortAnswer" && questionType.type != "Completion") |
|
|
|
|
// throw new Exception("答案选项不能少于两个!"); |
|
|
|
|
|
|
|
|
|
// if ((questionType.type == "Single" || questionType.type == "TrueOrFalse") && string.IsNullOrWhiteSpace(questionType.detail.answer)) |
|
|
|
|
// throw new Exception("正确答案未标记!"); |
|
|
|
|
// else if (questionType.type == "Multiple" && (questionType.detail.answer1 == null || (questionType.detail.answer1 != null && !questionType.detail.answer1.Any()))) |
|
|
|
|
// throw new Exception("正确答案未标记!"); |
|
|
|
|
|
|
|
|
|
// if (questionType.type == "Multiple" && questionType.detail.answer1.Count < 2) |
|
|
|
|
// throw new Exception("答案至少需标记处两个正确答案!"); |
|
|
|
|
|
|
|
|
|
// if (string.IsNullOrWhiteSpace(questionType.detail.RemarkSz)) |
|
|
|
|
// throw new Exception("题目解析必填!"); |
|
|
|
|
//} |
|
|
|
|
|
|
|
|
|
} |