Finishddd
This commit is contained in:
@@ -3,75 +3,12 @@ using System.Text.Json.Serialization;
|
||||
using System.Text.Json;
|
||||
using Entities.Contracts;
|
||||
using Microsoft.Extensions.Options;
|
||||
using AutoMapper;
|
||||
|
||||
namespace TechHelper.Client.Exam
|
||||
{
|
||||
public class ParentStructInfo
|
||||
{
|
||||
public string Number { get; set; }
|
||||
public SubjectAreaEnum SubjectArea { get; set; }
|
||||
public byte Index { get; set; }
|
||||
}
|
||||
|
||||
public static class ExamPaperExtensions
|
||||
{
|
||||
public static AssignmentDto ConvertToExamDTO(this ExamPaper examPaper)
|
||||
{
|
||||
AssignmentDto dto = new AssignmentDto();
|
||||
|
||||
dto.Title = examPaper.AssignmentTitle;
|
||||
dto.Description = examPaper.Description;
|
||||
|
||||
var SubjectArea = SubjectAreaEnum.Literature;
|
||||
Enum.TryParse<SubjectAreaEnum>(examPaper.SubjectArea, out SubjectArea);
|
||||
dto.SubjectArea = SubjectArea;
|
||||
|
||||
AssignmentQuestionDto examStruct = new AssignmentQuestionDto();
|
||||
|
||||
foreach (var qg in examPaper.QuestionGroups)
|
||||
{
|
||||
examStruct.ChildrenAssignmentQuestion.Add(ParseMajorQuestionGroup(qg));
|
||||
examStruct.ChildrenAssignmentQuestion.Last().Index = (byte)(examStruct.ChildrenAssignmentQuestion.Count());
|
||||
}
|
||||
|
||||
dto.ExamStruct = examStruct;
|
||||
|
||||
return dto;
|
||||
}
|
||||
|
||||
private static AssignmentQuestionDto ParseMajorQuestionGroup(MajorQuestionGroup sqg)
|
||||
{
|
||||
var examStruct = new AssignmentQuestionDto();
|
||||
examStruct.Title = sqg.Title;
|
||||
examStruct.Score = sqg.Score;
|
||||
if (sqg.SubQuestionGroups != null)
|
||||
{
|
||||
|
||||
|
||||
examStruct.ChildrenAssignmentQuestion = new List<AssignmentQuestionDto>();
|
||||
sqg.SubQuestionGroups?.ForEach(ssqg =>
|
||||
{
|
||||
examStruct.ChildrenAssignmentQuestion.Add(ParseMajorQuestionGroup(ssqg));
|
||||
examStruct.ChildrenAssignmentQuestion.Last().Index = (byte)(examStruct.ChildrenAssignmentQuestion.Count());
|
||||
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
|
||||
if (sqg.SubQuestions != null)
|
||||
{
|
||||
|
||||
sqg.SubQuestions?.ForEach(sq =>
|
||||
{
|
||||
examStruct.ChildrenAssignmentQuestion.Add(ParseAssignmentQuestion(sq));
|
||||
examStruct.ChildrenAssignmentQuestion.Last().Index = (byte)(examStruct.ChildrenAssignmentQuestion.Count());
|
||||
});
|
||||
}
|
||||
|
||||
return examStruct;
|
||||
}
|
||||
|
||||
|
||||
public static List<string> ParseOptionsFromText(this string optionsText)
|
||||
{
|
||||
@@ -79,197 +16,21 @@ namespace TechHelper.Client.Exam
|
||||
.Where(line => !string.IsNullOrWhiteSpace(line)).ToList();
|
||||
}
|
||||
|
||||
|
||||
private static AssignmentQuestionDto ParseAssignmentQuestion(PaperQuestion sq)
|
||||
{
|
||||
var aq = new AssignmentQuestionDto();
|
||||
aq.Score = sq.Score;
|
||||
|
||||
aq.Question = ParseQuestion(sq);
|
||||
|
||||
sq.SubQuestions?.ForEach(ssq =>
|
||||
{
|
||||
|
||||
aq.ChildrenAssignmentQuestion.Add(ParseAssignmentQuestion(ssq));
|
||||
aq.ChildrenAssignmentQuestion.Last().Index = (byte)aq.ChildrenAssignmentQuestion.Count;
|
||||
|
||||
});
|
||||
|
||||
return aq;
|
||||
}
|
||||
|
||||
private static QuestionDto ParseQuestion(PaperQuestion sq)
|
||||
{
|
||||
var dq = new QuestionDto();
|
||||
dq.Title = sq.Stem;
|
||||
dq.Options = string.Join(Environment.NewLine, sq.Options.Select(opt => $"{opt.Label} {opt.Text}"));
|
||||
|
||||
return dq;
|
||||
}
|
||||
|
||||
private static void ParseMajorQuestionGroup(MajorQuestionGroup qg, QuestionGroupDto qgd, bool isParentGroupValidChain)
|
||||
{
|
||||
qgd.Title = qg.Title;
|
||||
qgd.Score = (int)qg.Score;
|
||||
qgd.Descript = qg.Descript;
|
||||
|
||||
|
||||
qgd.ValidQuestionGroup = !string.IsNullOrEmpty(qg.Descript) && !isParentGroupValidChain;
|
||||
|
||||
|
||||
bool nextIsParentGroupValidChain = qgd.ValidQuestionGroup || isParentGroupValidChain;
|
||||
|
||||
|
||||
if (qg.SubQuestionGroups != null)
|
||||
{
|
||||
qg.SubQuestionGroups.ForEach(sqg =>
|
||||
{
|
||||
var sqgd = new QuestionGroupDto();
|
||||
sqgd.Index = (byte)qg.SubQuestionGroups.IndexOf(sqg);
|
||||
ParseMajorQuestionGroup(sqg, sqgd, nextIsParentGroupValidChain);
|
||||
qgd.SubQuestionGroups.Add(sqgd);
|
||||
});
|
||||
}
|
||||
|
||||
if (qg.SubQuestions != null)
|
||||
{
|
||||
qg.SubQuestions.ForEach(sq =>
|
||||
{
|
||||
if (sq.SubQuestions != null && sq.SubQuestions.Any())
|
||||
{
|
||||
var subQgd = new QuestionGroupDto
|
||||
{
|
||||
Title = sq.Stem,
|
||||
Index = (byte)qg.SubQuestions.IndexOf(sq),
|
||||
Score = (int)sq.Score,
|
||||
Descript = "" // 默认为空
|
||||
};
|
||||
subQgd.ValidQuestionGroup = !string.IsNullOrEmpty(subQgd.Descript) && !nextIsParentGroupValidChain;
|
||||
|
||||
ParseQuestionWithSubQuestions(sq, subQgd, subQgd.ValidQuestionGroup || nextIsParentGroupValidChain);
|
||||
qgd.SubQuestionGroups.Add(subQgd);
|
||||
}
|
||||
else // 如果 MajorQuestionGroup 下的 Question 没有子问题,则转为 SubQuestionDto
|
||||
{
|
||||
var subQd = new SubQuestionDto();
|
||||
// 只有当所有父组(包括当前组)都不是有效组时,这个题目才有效
|
||||
ParseSingleQuestion(sq, subQd, !nextIsParentGroupValidChain);
|
||||
subQd.Index = (byte)qg.SubQuestions.IndexOf(sq);
|
||||
qgd.SubQuestions.Add(subQd);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// 解析包含子问题的 Question,将其转换为 QuestionGroupDto
|
||||
// isParentGroupValidChain 参数表示从顶层到当前组的任一父组是否已经是“有效组”
|
||||
private static void ParseQuestionWithSubQuestions(PaperQuestion question, QuestionGroupDto qgd, bool isParentGroupValidChain)
|
||||
{
|
||||
qgd.Title = question.Stem;
|
||||
qgd.Score = (int)question.Score;
|
||||
qgd.Descript = ""; // 默认为空
|
||||
|
||||
// 判断当前组是否有效:如果有描述,并且其父级链中没有任何一个组是有效组,则当前组有效
|
||||
qgd.ValidQuestionGroup = !string.IsNullOrEmpty(qgd.Descript) && !isParentGroupValidChain;
|
||||
|
||||
// 更新传递给子项的 isParentGroupValidChain 状态
|
||||
bool nextIsParentGroupValidChain = qgd.ValidQuestionGroup || isParentGroupValidChain;
|
||||
|
||||
|
||||
if (question.SubQuestions != null)
|
||||
{
|
||||
question.SubQuestions.ForEach(subQ =>
|
||||
{
|
||||
// 如果子问题本身还有子问题(多层嵌套),则继续创建 QuestionGroupDto
|
||||
if (subQ.SubQuestions != null && subQ.SubQuestions.Any())
|
||||
{
|
||||
var nestedQgd = new QuestionGroupDto
|
||||
{
|
||||
Title = subQ.Stem,
|
||||
Score = (int)subQ.Score,
|
||||
Descript = "" // 默认为空
|
||||
};
|
||||
// 判断当前组是否有效:如果有描述,并且其父级链中没有任何一个组是有效组,则当前组有效
|
||||
nestedQgd.ValidQuestionGroup = !string.IsNullOrEmpty(nestedQgd.Descript) && !nextIsParentGroupValidChain;
|
||||
|
||||
ParseQuestionWithSubQuestions(subQ, nestedQgd, nestedQgd.ValidQuestionGroup || nextIsParentGroupValidChain);
|
||||
qgd.SubQuestionGroups.Add(nestedQgd);
|
||||
}
|
||||
else // 如果子问题没有子问题,则直接创建 SubQuestionDto
|
||||
{
|
||||
var subQd = new SubQuestionDto();
|
||||
// 只有当所有父组(包括当前组)都不是有效组时,这个题目才有效
|
||||
ParseSingleQuestion(subQ, subQd, !nextIsParentGroupValidChain);
|
||||
qgd.SubQuestions.Add(subQd);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// 解析单个 Question (没有子问题) 为 SubQuestionDto
|
||||
private static void ParseSingleQuestion(PaperQuestion question, SubQuestionDto subQd, bool validQ)
|
||||
{
|
||||
subQd.Stem = question.Stem;
|
||||
subQd.Score = (int)question.Score;
|
||||
subQd.ValidQuestion = validQ; // 根据传入的 validQ 确定是否是“有效题目”
|
||||
subQd.SampleAnswer = question.SampleAnswer;
|
||||
subQd.QuestionType = question.QuestionType;
|
||||
// 注意:DifficultyLevel 在本地 Question 中没有,如果服务器需要,可能需要补充默认值或从其他地方获取
|
||||
// subQd.DifficultyLevel = ...;
|
||||
|
||||
if (question.Options != null)
|
||||
{
|
||||
question.Options.ForEach(o =>
|
||||
{
|
||||
subQd.Options.Add(new OptionDto { Value = o.Label + o.Text });
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static void SeqIndex(this ExamDto dto)
|
||||
public static void SeqIndex(this AssignmentDto dto)
|
||||
{
|
||||
dto.ExamStruct.SeqQGroupIndex();
|
||||
}
|
||||
|
||||
|
||||
public static void SeqQGroupIndex(this QuestionGroupDto dto)
|
||||
public static void SeqQGroupIndex(this AssignmentQuestionDto dto)
|
||||
{
|
||||
dto.SubQuestions?.ForEach(sq =>
|
||||
{
|
||||
sq.Index = (byte)dto.SubQuestions.IndexOf(sq);
|
||||
});
|
||||
|
||||
dto.SubQuestionGroups?.ForEach(sqg =>
|
||||
foreach(var sqg in dto.ChildrenAssignmentQuestion)
|
||||
{
|
||||
sqg.Index = (byte)dto.SubQuestionGroups.IndexOf(sqg);
|
||||
sqg.Index = (byte)dto.ChildrenAssignmentQuestion.IndexOf(sqg);
|
||||
sqg.SeqQGroupIndex();
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
public static string SerializeExamDto(this ExamDto dto)
|
||||
{
|
||||
// 配置序列化选项(可选)
|
||||
var options = new JsonSerializerOptions
|
||||
{
|
||||
WriteIndented = true,
|
||||
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
|
||||
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull
|
||||
};
|
||||
|
||||
return JsonSerializer.Serialize(dto, options);
|
||||
}
|
||||
|
||||
public static ExamDto DeserializeExamDto(string jsonString)
|
||||
{
|
||||
|
||||
var options = new JsonSerializerOptions
|
||||
{
|
||||
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
|
||||
};
|
||||
|
||||
return JsonSerializer.Deserialize<ExamDto>(jsonString, options);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user