exam_service

This commit is contained in:
SpecialX
2025-06-11 15:02:20 +08:00
parent 97843ab5fd
commit e26881ec2f
52 changed files with 3510 additions and 1174 deletions

View File

@@ -3,8 +3,10 @@ using Entities.Contracts;
using Entities.DTO;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using MySqlConnector;
using SharedDATA.Api;
using TechHelper.Services;
using static TechHelper.Context.AutoMapperProFile;
namespace TechHelper.Server.Services
{
@@ -81,6 +83,74 @@ namespace TechHelper.Server.Services
}
public async Task<IEnumerable<AssignmentGroup>> LoadFullGroupTree(Guid rootGroupId)
{
var query = @"
WITH RECURSIVE GroupTree AS (
SELECT
ag.*,
CAST(ag.`number` AS CHAR(255)) AS path
FROM assignment_group ag
WHERE ag.id = @rootId
UNION ALL
SELECT
c.*,
CONCAT(ct.path, '.', c.`number`)
FROM assignment_group c
INNER JOIN GroupTree ct ON c.parent_group = ct.id
)
SELECT * FROM GroupTree ORDER BY path;
";
// 执行查询
var groups = await _unitOfWork.GetRepository<AssignmentGroup>()
.FromSql(query, new MySqlParameter("rootId", rootGroupId))
.ToListAsync();
// 内存中构建树结构
var groupDict = groups.ToDictionary(g => g.Id);
var root = groupDict[rootGroupId];
foreach (var group in groups)
{
if (group.ParentGroup != null && groupDict.TryGetValue(group.ParentGroup.Value, out var parent))
{
parent.ChildAssignmentGroups ??= new List<AssignmentGroup>();
parent.ChildAssignmentGroups.Add(group);
}
}
return new List<AssignmentGroup> { root };
}
public async Task LoadRecursiveAssignmentGroups(IEnumerable<AssignmentGroup> groups)
{
foreach (var group in groups.ToList())
{
var loadedGroup = await _unitOfWork.GetRepository<AssignmentGroup>()
.GetFirstOrDefaultAsync(
predicate: ag => ag.Id == group.Id,
include: source => source
.Include(ag => ag.AssignmentQuestions.Where(aq => !aq.IsDeleted))
.ThenInclude(aq => aq.Question)
.Include(ag => ag.ChildAssignmentGroups)
);
if (loadedGroup == null) continue;
group.ChildAssignmentGroups = loadedGroup.ChildAssignmentGroups;
group.AssignmentQuestions = loadedGroup.AssignmentQuestions;
if (group.ChildAssignmentGroups is { Count: > 0 })
{
await LoadRecursiveAssignmentGroups(group.ChildAssignmentGroups);
}
}
}
public async Task<ApiResponse> GetExamByIdAsync(Guid assignmentId)
{
try
@@ -93,15 +163,17 @@ namespace TechHelper.Server.Services
return ApiResponse.Error($"找不到 ID 为 {assignmentId} 的试卷。");
}
// 获取所有相关题组和题目,并过滤掉已删除的
var allGroups = await _unitOfWork.GetRepository<AssignmentGroup>().GetAllAsync(
var allGroups = await _unitOfWork.GetRepository<AssignmentGroup>().GetFirstOrDefaultAsync(
predicate: ag => ag.AssignmentId == assignmentId && !ag.IsDeleted,
include: source => source
.Include(ag => ag.AssignmentQuestions.Where(aq => !aq.IsDeleted))
.ThenInclude(aq => aq.Question)
.Include(ag => ag.ChildAssignmentGroups)
);
await LoadRecursiveAssignmentGroups(allGroups.ChildAssignmentGroups);
if (allGroups == null || !allGroups.Any())
if (allGroups == null || !allGroups.ChildAssignmentGroups.Any())
{
// 试卷存在但没有内容,返回一个空的 ExamDto
return ApiResponse.Success("试卷没有内容。", new ExamDto
@@ -113,10 +185,16 @@ namespace TechHelper.Server.Services
});
}
var rootGroups = allGroups
.Where(ag => ag.ParentGroup == null)
.OrderBy(ag => ag.Number)
.ToList();
var rootGroups = allGroups.ChildAssignmentGroups.ToList();
var rootqg = new QuestionGroupDto();
foreach (var ag in rootGroups.OrderBy(g => g.Number))
{
var agDto = MapAssignmentGroupToDto(ag);
rootqg.SubQuestionGroups.Add(agDto);
}
// 递归映射到 ExamDto
var examDto = new ExamDto
@@ -125,7 +203,7 @@ namespace TechHelper.Server.Services
AssignmentTitle = assignment.Title,
Description = assignment.Description,
SubjectArea = assignment.Submissions.ToString(),
QuestionGroups = MapAssignmentGroupsToDto(rootGroups, allGroups)
QuestionGroups = rootqg
};
return ApiResponse.Success("试卷信息已成功获取。", examDto);
@@ -137,9 +215,45 @@ namespace TechHelper.Server.Services
}
private List<QuestionGroupDto> MapAssignmentGroupsToDto(
List<AssignmentGroup> currentLevelGroups,
IEnumerable<AssignmentGroup> allFetchedGroups)
public QuestionGroupDto MapAssignmentGroupToDto(AssignmentGroup ag)
{
// 创建当前节点的DTO
var dto = new QuestionGroupDto
{
Title = ag.Title,
Score = (int)(ag.TotalPoints ?? 0),
Descript = ag.Descript,
SubQuestions = ag.AssignmentQuestions?
.OrderBy(aq => aq.QuestionNumber)
.Select(aq => new SubQuestionDto
{
Index = aq.QuestionNumber,
Stem = aq.Question?.QuestionText,
Score = aq.Score ?? 0,
SampleAnswer = aq.Question?.CorrectAnswer,
QuestionType = aq.Question?.QuestionType.ToString(),
DifficultyLevel = aq.Question?.DifficultyLevel.ToString(),
Options = new List<OptionDto>() // 根据需要初始化
}).ToList() ?? new List<SubQuestionDto>(),
SubQuestionGroups = new List<QuestionGroupDto>() // 初始化子集合
};
// 递归处理子组
if (ag.ChildAssignmentGroups != null && ag.ChildAssignmentGroups.Count > 0)
{
foreach (var child in ag.ChildAssignmentGroups.OrderBy(c => c.Number))
{
var childDto = MapAssignmentGroupToDto(child); // 递归获取子DTO
dto.SubQuestionGroups.Add(childDto); // 添加到当前节点的子集合
}
}
return dto;
}
private List<QuestionGroupDto> MapAssignmentGroupsToDto2(
List<AssignmentGroup> currentLevelGroups,
IEnumerable<AssignmentGroup> allFetchedGroups)
{
var dtos = new List<QuestionGroupDto>();
@@ -147,25 +261,25 @@ namespace TechHelper.Server.Services
{
var groupDto = new QuestionGroupDto
{
Title = group.Title,
Score = (int)(group.TotalPoints ?? 0),
QuestionReference = group.Descript,
Descript = group.Descript,
SubQuestions = group.AssignmentQuestions
.OrderBy(aq => aq.QuestionNumber)
.Select(aq => new SubQuestionDto
{
Index = aq.QuestionNumber,
Stem = aq.Question.QuestionText,
Score = aq.Score?? 0, // 使用 AssignmentQuestion 上的 Score
Score = aq.Score ?? 0,
SampleAnswer = aq.Question.CorrectAnswer,
QuestionType = aq.Question.QuestionType.ToString(),
DifficultyLevel = aq.Question.DifficultyLevel.ToString(),
Options = new List<OptionDto>() // 这里需要您根据实际存储方式填充 Option
Options = new List<OptionDto>()
}).ToList(),
// 递归映射子题组
SubQuestionGroups = MapAssignmentGroupsToDto(
allFetchedGroups.Where(ag => ag.ParentGroup == group.Id && !ag.IsDeleted).ToList(), // 从所有已获取的组中筛选子组
SubQuestionGroups = MapAssignmentGroupsToDto2(
allFetchedGroups.Where(ag => ag.ParentGroup == group.Id && !ag.IsDeleted).ToList(),
allFetchedGroups)
};
dtos.Add(groupDto);
@@ -176,7 +290,7 @@ namespace TechHelper.Server.Services
public async Task<TechHelper.Services.ApiResponse> SaveParsedExam(ExamDto examData)
{
// 获取当前登录用户
var currentUser = await _userManager.GetUserAsync(null);
var currentUser = await _userManager.FindByEmailAsync(examData.CreaterEmail);
if (currentUser == null)
{
return ApiResponse.Error("未找到当前登录用户,无法保存试题。");
@@ -204,16 +318,13 @@ namespace TechHelper.Server.Services
// 从 ExamDto.QuestionGroups 获取根题组。
// 确保只有一个根题组,因为您的模型是“试卷只有一个根节点”。
if (examData.QuestionGroups == null || examData.QuestionGroups.Count != 1)
if (examData.QuestionGroups == null)
{
throw new ArgumentException("试卷必须包含且只能包含一个根题组。");
}
// 递归处理根题组及其所有子题组和题目
// 传入的 assignmentId 仅用于设置根题组的 AssignmentId 字段
// 对于子题组ProcessAndSaveAssignmentGroupsRecursive 会将 AssignmentId 设置为 null
await ProcessAndSaveAssignmentGroupsRecursive(
examData.QuestionGroups.Single(),
examData.QuestionGroups,
examData.SubjectArea.ToString(),
assignmentId,
null, // 根题组没有父级
@@ -246,14 +357,12 @@ namespace TechHelper.Server.Services
{
Id = Guid.NewGuid(), // 后端生成 GUID
Title = qgDto.Title,
Descript = qgDto.QuestionReference,
Descript = qgDto.Descript,
TotalPoints = qgDto.Score,
Number = (byte)qgDto.Index, // 使用 DTO 的 Index 作为 Number
ParentGroup = parentAssignmentGroupId, // 设置父级题组 GUID
// 关键修正:只有当 parentAssignmentGroupId 为 null 时,才设置 AssignmentId
// 这意味着当前题组是顶级题组
AssignmentId = parentAssignmentGroupId == null ? assignmentId : Guid.Empty,
Number = (byte)qgDto.Index,
ValidQuestionGroup = qgDto.ValidQuestionGroup,
ParentGroup = parentAssignmentGroupId,
AssignmentId = parentAssignmentGroupId == null ? assignmentId : (Guid?)null,
IsDeleted = false
};
await _unitOfWork.GetRepository<AssignmentGroup>().InsertAsync(newAssignmentGroup);
@@ -268,10 +377,7 @@ namespace TechHelper.Server.Services
newQuestion.CreatedAt = DateTime.UtcNow;
newQuestion.UpdatedAt = DateTime.UtcNow;
newQuestion.IsDeleted = false;
newQuestion.SubjectArea = (SubjectAreaEnum)Enum.Parse(typeof(SubjectAreaEnum), subjectarea, true);
// 处理 Options如果 Options 是 JSON 字符串或需要其他存储方式,在这里处理
// 例如newQuestion.QuestionText += (JsonConvert.SerializeObject(sqDto.Options));
newQuestion.SubjectArea = EnumMappingHelpers.ParseEnumSafe(subjectarea, SubjectAreaEnum.Unknown);
await _unitOfWork.GetRepository<Question>().InsertAsync(newQuestion);
@@ -279,9 +385,9 @@ namespace TechHelper.Server.Services
{
Id = Guid.NewGuid(),
QuestionId = newQuestion.Id,
QuestionNumber = (byte)questionNumber, // 使用递增的 questionNumber
AssignmentGroupId = newAssignmentGroup.Id, // 关联到当前题组
Score = sqDto.Score, // 从 DTO 获取单个子题分数
QuestionNumber = (byte)questionNumber,
AssignmentGroupId = newAssignmentGroup.Id,
Score = sqDto.Score,
IsDeleted = false,
CreatedAt = DateTime.UtcNow
};
@@ -302,5 +408,26 @@ namespace TechHelper.Server.Services
createdById);
}
}
public async Task<ApiResponse> GetAllExamPreview(Guid user)
{
try
{
var assignments = await _unitOfWork.GetRepository<Assignment>().GetAllAsync(
predicate: a => a.CreatedBy == user && !a.IsDeleted);
if (assignments.Any())
{
var exam = _mapper.Map<IEnumerable<ExamDto>>(assignments);
return ApiResponse.Success(result: exam);
}
return ApiResponse.Error("你还没有创建任何试卷");
}
catch (Exception ex)
{
return ApiResponse.Error($"查询出了一点问题 , 详细信息为: {ex.Message}, 请稍后再试");
}
}
}
}

View File

@@ -5,6 +5,6 @@ namespace TechHelper.Server.Services
{
public interface IExamService : IBaseService<ExamDto, Guid>
{
Task<ApiResponse> GetAllExamPreview(Guid user);
}
}