重构项目结构,移除Assignment相关功能,优化Submission模块
Some checks failed
TechAct / explore-gitea-actions (push) Failing after 12s

This commit is contained in:
SpecialX
2025-10-09 18:57:28 +08:00
parent 403b34a098
commit ac900159ba
289 changed files with 11948 additions and 20150 deletions

View File

@@ -0,0 +1,196 @@
using AutoMapper;
using Entities.Contracts;
using Entities.DTO;
using Microsoft.EntityFrameworkCore;
using SharedDATA.Api;
using TechHelper.Services.Beta;
using static TechHelper.Services.Beta.IExamQuestionService;
namespace TechHelper.Services.Beta
{
public class ExamQuestionService : IExamQuestionService
{
private readonly IUnitOfWork _work;
private readonly IMapper _mapper;
private readonly IClassService _classService;
public ExamQuestionService(IUnitOfWork work, IMapper mapper, IClassService classService)
{
_work = work;
_mapper = mapper;
_classService = classService;
}
public async Task<ApiResponse> GetAllAsync(QueryParameter query)
{
try
{
var repository = _work.GetRepository<ExamQuestion>();
if (query.Search != null && !string.IsNullOrWhiteSpace(query.Search))
{
var examQuestions = await repository.GetPagedListAsync(
predicate: eq => eq.Title.Contains(query.Search) && eq.ParentExamQuestionId == null,
pageSize: query.PageSize,
pageIndex: query.PageIndex
);
var examQuestionDtosFiltered = _mapper.Map<IEnumerable<ExamQuestionDto>>(examQuestions.Items);
return new ApiResponse(true, examQuestionDtosFiltered);
}
else
{
var examQuestions = await repository.GetPagedListAsync(
predicate: eq => eq.ParentExamQuestionId == null,
pageSize: query.PageSize,
pageIndex: query.PageIndex
);
var examQuestionDtos = _mapper.Map<IEnumerable<ExamQuestionDto>>(examQuestions.Items);
return new ApiResponse(true, examQuestionDtos);
}
}
catch (Exception ex)
{
return new ApiResponse($"获取所有考试题目时发生错误: {ex.Message}");
}
}
public async Task<ApiResponse> GetAsync(Guid id)
{
try
{
var examQuestion = await _work.GetRepository<ExamQuestion>().GetFirstOrDefaultAsync(
predicate: eq => eq.Id == id);
if (examQuestion == null)
{
return new ApiResponse("考试题目未找到。");
}
var examQuestionDto = _mapper.Map<ExamQuestionDto>(examQuestion);
return new ApiResponse(true, examQuestionDto);
}
catch (Exception ex)
{
return new ApiResponse($"获取考试题目时发生错误: {ex.Message}");
}
}
public async Task<ApiResponse> AddAsync(ExamQuestionDto model)
{
try
{
// 检查是否已存在相同考试和题目的组合
var existingExamQuestion = await _work.GetRepository<ExamQuestion>().GetFirstOrDefaultAsync(
predicate: eq => eq.Id == model.Id/* && eq.QuestionId == model.Question.Id*/);
if (existingExamQuestion != null)
{
return new ApiResponse($"该题目已在此考试中存在。");
}
var examQuestion = _mapper.Map<ExamQuestion>(model);
await _work.GetRepository<ExamQuestion>().InsertAsync(examQuestion);
if (await _work.SaveChangesAsync() > 0)
{
return new ApiResponse(true, examQuestion.Id);
}
return new ApiResponse("添加考试题目失败。");
}
catch (Exception ex)
{
return new ApiResponse($"添加考试题目时发生错误: {ex.Message}");
}
}
public async Task<ApiResponse> UpdateAsync(ExamQuestionDto model)
{
try
{
var existingExamQuestion = await _work.GetRepository<ExamQuestion>().GetFirstOrDefaultAsync(
predicate: eq => eq.Id == model.Id);
if (existingExamQuestion == null)
{
return new ApiResponse("考试题目未找到。");
}
// 检查是否要修改为已存在的考试题目组合(排除当前记录)
var examQuestionWithSameCombination = await _work.GetRepository<ExamQuestion>().GetFirstOrDefaultAsync(
predicate: eq => eq.Id == model.Id /*&& eq.QuestionId == model.QuestionId*/ && eq.Id != model.Id);
if (examQuestionWithSameCombination != null)
{
return new ApiResponse($"该题目与考试的组合已存在。");
}
_mapper.Map(model, existingExamQuestion);
_work.GetRepository<ExamQuestion>().Update(existingExamQuestion);
if (await _work.SaveChangesAsync() > 0)
{
var examQuestionDto = _mapper.Map<ExamQuestionDto>(existingExamQuestion);
return new ApiResponse(true, examQuestionDto);
}
return new ApiResponse("更新考试题目失败。");
}
catch (Exception ex)
{
return new ApiResponse($"更新考试题目时发生错误: {ex.Message}");
}
}
public async Task<ApiResponse> DeleteAsync(Guid id)
{
try
{
var existingExamQuestion = await _work.GetRepository<ExamQuestion>().GetFirstOrDefaultAsync(
predicate: eq => eq.Id == id);
if (existingExamQuestion == null)
{
return new ApiResponse("考试题目未找到。");
}
_work.GetRepository<ExamQuestion>().Delete(existingExamQuestion);
if (await _work.SaveChangesAsync() > 0)
{
return new ApiResponse(true, "考试题目删除成功。");
}
return new ApiResponse("删除考试题目失败。");
}
catch (Exception ex)
{
return new ApiResponse($"删除考试题目时发生错误: {ex.Message}");
}
}
public async Task<ApiResponse> GetQuestionDependy(QuesitionDenpendceyRequst request)
{
try
{
var classStudentsResponse = await _classService.GetClassStudentsAsync(request.ClassId);
if (!classStudentsResponse.Status) return ApiResponse.Error(classStudentsResponse.Message);
var classStudents = classStudentsResponse.Result as IEnumerable<UserListDto>;
if (classStudents == null) return ApiResponse.Error("班级学生列表为空。");
var submissionResult = await _work.GetRepository<SubmissionDetail>().GetAllAsync(predicate: sd => sd.ExamQuestionId == request.ExamQuestioId );
var submission = submissionResult.Where(s => classStudents.Any(c => c.Id == s.StudentId));
var errorStudents = submission.Select(s => s.Student);
var scores = submission.Select(s => s.PointsAwarded);
var lesson = submission.FirstOrDefault()?.ExamQuestion?.Question?.Lesson ?? null;
var lessonDto = _mapper.Map<LessonDto>(lesson);
var ErrorStudentDtro = _mapper.Map<IEnumerable<UserListDto>>(errorStudents);
return ApiResponse.Success("获取成功", new QuestionDependecyDto(lessonDto, scores, ErrorStudentDtro));
}
catch (Exception ex)
{
return ApiResponse.Error($"获取考试题目密度时发生错误: {ex.Message}");
}
}
}
}

View File

@@ -0,0 +1,341 @@
using AutoMapper;
using Entities.Contracts;
using Entities.DTO;
using Microsoft.EntityFrameworkCore;
using Microsoft.VisualBasic;
using SharedDATA.Api;
using System.Linq;
using TechHelper.Context;
using TechHelper.Server.Repositories;
using TechHelper.Services.Beta;
using static TechHelper.Services.Beta.IExamService;
namespace TechHelper.Services.Beta
{
public class ExamService : IExamService
{
private readonly IUnitOfWork _unitOfWork;
private readonly ISubmissionService _submissionService;
private readonly IClassService _classService;
private readonly IMapper _mapper;
public ExamService(IUnitOfWork unitOfWork, IMapper mapper, IClassService classService, ISubmissionService submissionService)
{
_unitOfWork = unitOfWork;
_mapper = mapper;
_classService = classService;
_submissionService = submissionService;
}
public async Task<ApiResponse> CreateExamAsync(ExamDto ExamDto)
{
try
{
Exam newAssi = _mapper.Map<Exam>(ExamDto);
//var context = _unitOfWork.GetDbContext<ApplicationContext>();
//foreach (var entry in context.ChangeTracker.Entries())
//{
// if (entry.State == Microsoft.EntityFrameworkCore.EntityState.Added)
// {
// if (entry.Entity is Question newQues)
// {
// newQues.CreatorId = newAssi.CreatorId;
// }
// }
//}
if (await _unitOfWork.SaveChangesAsync() > 0)
{
return ApiResponse.Success();
}
return ApiResponse.Error("保存失败");
}
catch (Exception ex)
{
return ApiResponse.Error(ex.Message);
}
}
public async Task<ApiResponse> GetAllAsync(QueryParameter query)
{
try
{
var repository = _unitOfWork.GetRepository<Exam>();
if (query.Search != null && !string.IsNullOrWhiteSpace(query.Search))
{
var examQuestions = await repository.GetPagedListAsync(
predicate: eq => eq.Title.Contains(query.Search),
pageSize: query.PageSize,
pageIndex: query.PageIndex
);
var examQuestionDtosFiltered = _mapper.Map<IEnumerable<ExamListDto>>(examQuestions.Items);
return new ApiResponse(true, examQuestionDtosFiltered);
}
else
{
var examQuestions = await repository.GetPagedListAsync(
pageSize: query.PageSize,
pageIndex: query.PageIndex
);
var examQuestionDtos = _mapper.Map<IEnumerable<ExamListDto>>(examQuestions.Items);
return new ApiResponse(true, examQuestionDtos);
}
}
catch (Exception ex)
{
return new ApiResponse($"获取所有考试题目时发生错误: {ex.Message}");
}
}
public async Task<ApiResponse> GetAsync(Guid id)
{
try
{
var assignment = await _unitOfWork.GetRepository<Exam>().GetFirstOrDefaultAsync(predicate: a => a.Id == id);
if (assignment == null)
{
return ApiResponse.Error("获取失败");
}
var result = _mapper.Map<ExamDto>(assignment);
return ApiResponse.Success(result: result);
}
catch (Exception ex)
{
return ApiResponse.Error(ex.Message);
}
}
public async Task<ApiResponse> AddAsync(ExamDto model)
{
try
{
var exam = _mapper.Map<Exam>(model);
await _unitOfWork.GetRepository<Exam>().InsertAsync(exam);
if (await _unitOfWork.SaveChangesAsync() > 0)
{
return ApiResponse.Success("创建成功", exam.Id);
}
return ApiResponse.Error("创建失败");
}
catch (Exception ex)
{
return ApiResponse.Error(ex.Message);
}
}
public async Task<ApiResponse> UpdateAsync(ExamDto model)
{
try
{
var existingExam = await _unitOfWork.GetRepository<Exam>().GetFirstOrDefaultAsync(predicate: a => a.Id == model.Id);
if (existingExam == null) return ApiResponse.Error("找不到该试卷");
_mapper.Map(model, existingExam);
_unitOfWork.GetRepository<Exam>().Update(existingExam);
if (await _unitOfWork.SaveChangesAsync() > 0)
{
return ApiResponse.Success("更新成功");
}
return ApiResponse.Error("更新失败");
}
catch (Exception ex)
{
return ApiResponse.Error(ex.Message);
}
}
public async Task<ApiResponse> DeleteAsync(Guid id)
{
try
{
var assignment = await _unitOfWork.GetRepository<Exam>().GetFirstOrDefaultAsync(predicate: a => a.Id == id);
if (assignment == null) return ApiResponse.Error("找不到该试卷");
_unitOfWork.GetRepository<Exam>().Delete(id);
_unitOfWork.GetRepository<ExamQuestion>().Delete(assignment.ExamStructId);
if (await _unitOfWork.SaveChangesAsync() > 0)
{
return ApiResponse.Success();
}
return ApiResponse.Error("删除失败");
}
catch (Exception ex)
{
return ApiResponse.Error($"内部问题,{ex.Message}, InerException{ex.InnerException}");
}
}
public async Task<ApiResponse> AssignmentToAllClassesAsync(AssigExamToClassDto examToClassDto, Guid TeacherId)
{
try
{
var result = await _classService.GetUserInjoinedClasses(TeacherId);
if (result.Status == false) return ApiResponse.Error(result.Message);
var userClass = result.Result as UserClassDetailInfoDto;
if (userClass == null || !userClass.UserClassInfos.Any()) return ApiResponse.Error("教师没有管理任何班级");
var assignment = await _unitOfWork.GetRepository<Exam>().GetFirstOrDefaultAsync(predicate: a => a.Id == examToClassDto.examId);
if (assignment == null) return ApiResponse.Error("没有找到该试卷");
foreach (var classId in userClass.UserClassInfos)
{
await AssignmentToClassAsync(TeacherId, examToClassDto.examId, classId.Id);
}
if (await _unitOfWork.SaveChangesAsync() > 0)
{
return ApiResponse.Success("分配成功");
}
return ApiResponse.Error("分配失败");
}
catch (Exception ex)
{
return ApiResponse.Error($"内部错误, {ex.Message}");
}
}
public async Task<ApiResponse> AssignmentToStudentsAsync(AssigExamToStudentsDto examToStudentsDto)
{
try
{
var assignment = await _unitOfWork.GetRepository<Exam>().GetFirstOrDefaultAsync(predicate: a => a.Id == examToStudentsDto.ExamId);
if (assignment == null)
{
return ApiResponse.Error("获取失败");
}
if (assignment == null) return ApiResponse.Error("没有找到该试卷");
if (examToStudentsDto.StudentIds == null || !examToStudentsDto.StudentIds.Any())
{
return ApiResponse.Error("没有选择学生");
}
foreach (var studentId in examToStudentsDto.StudentIds)
{
var subCount = await _unitOfWork.GetRepository<Submission>().GetAll(
predicate: su => su.ExamId == examToStudentsDto.ExamId && su.StudentId == studentId
).CountAsync();
var submission = assignment.ConvertToSubmission(studentId, examToStudentsDto.CreaterId, examToStudentsDto.ClassId);
submission.AttemptNumber = (byte)(subCount + 1);
await _unitOfWork.GetRepository<Submission>().InsertAsync(submission);
}
if (await _unitOfWork.SaveChangesAsync() > 0)
{
return ApiResponse.Success();
}
return ApiResponse.Error();
}
catch (Exception ex)
{
return ApiResponse.Error($"内部错误, {ex.Message}");
}
}
public async Task<ApiResponse> AssignmentToClassAsync(Guid TeacherId, Guid examId, Guid classId)
{
try
{
var classStudents = await _classService.GetClassStudentsAsync(classId);
if (!classStudents.Status) return ApiResponse.Error(classStudents.Message);
var userlist = classStudents.Result as IEnumerable<UserListDto>;
if (userlist == null || !userlist.Any()) return ApiResponse.Error("班级没有学生");
var assignment = await _unitOfWork.GetRepository<Exam>().GetFirstOrDefaultAsync(predicate: a => a.Id == examId);
if (assignment == null) return ApiResponse.Error("没有找到该试卷");
foreach (var student in userlist)
{
var subCount = await _unitOfWork.GetRepository<Submission>().GetAll(
predicate: su => su.ExamId == examId && su.StudentId == student.Id
).CountAsync();
var submission = assignment.ConvertToSubmission(student.Id, TeacherId, classId);
submission.AttemptNumber = (byte)(subCount + 1);
await _unitOfWork.GetRepository<Submission>().InsertAsync(submission);
}
if (await _unitOfWork.SaveChangesAsync() > 0)
{
return ApiResponse.Success("创建成功");
}
return ApiResponse.Error("创建失败");
}
catch (Exception ex)
{
return ApiResponse.Error($"内部错误, {ex.Message}");
}
}
public async Task<ApiResponse> GetExamSubmissionDetailInClassAsync(AssigExamToClassDto examToClassDto)
{
try
{
var submissions = await _unitOfWork.GetRepository<Submission>().GetAllAsync(predicate: s => s.ExamId == examToClassDto.examId && s.ClassId == examToClassDto.classId);
if (submissions == null || !submissions.Any()) return ApiResponse.Error("没有找到该试卷");
var result = _mapper.Map<List<SubmissionListDto>>(submissions);
return ApiResponse.Success(result: result);
}
catch (Exception ex)
{
return ApiResponse.Error($"内部错误, {ex.Message}");
}
}
public async Task<ApiResponse> GetExamTotalErrorDistributionInClassAsync(AssigExamToClassDto examToClassDto)
{
try
{
var submissions = await _unitOfWork.GetRepository<Submission>().
GetAllAsync(predicate: s => s.ExamId == examToClassDto.examId && s.ClassId == examToClassDto.classId);
if (submissions == null || !submissions.Any()) return ApiResponse.Error("没有找到该试卷");
var errorTypeDistribution = submissions
.SelectMany(s => s.SubmissionDetails)
.Where(d => d.IsCorrect == false)
.GroupBy(d => d.ExamQuestion.Type.Name)
.ToDictionary(g => g.Key, g => g.Count());
var errorLessonDistribution = submissions
.SelectMany(s => s.SubmissionDetails)
.Where(d => d.IsCorrect == false)
.Where(d => d.ExamQuestion?.Question?.Lesson?.Title != null)
.GroupBy(d => d.ExamQuestion.Question?.Lesson?.Title)
.ToDictionary(g => g.Key, g => g.Count());
var scores = submissions.Select(s => s.OverallGrade);
var result = new ExamDistributionDto(errorTypeDistribution, errorLessonDistribution, scores);
return ApiResponse.Success(result: result);
}
catch (Exception ex)
{
return ApiResponse.Error($"内部错误, {ex.Message}");
}
}
}
}

View File

@@ -0,0 +1,19 @@
using Entities.DTO;
using TechHelper.Services;
namespace TechHelper.Services.Beta
{
public interface IExamQuestionService : IBaseService<ExamQuestionDto, Guid>
{
/// <summary>
/// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ŀ<EFBFBD><C4BF>Ҫ<EFBFBD><D2AA>֪ʶ<D6AA><CAB6>,<2C>÷<EFBFBD><C3B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֲ<EFBFBD>,<2C><><EFBFBD><EFBFBD>ѧ<EFBFBD><D1A7><EFBFBD>б<EFBFBD>
/// </summary>
/// <param name="ExamQuestioId"></param>
/// <returns></returns>
Task<ApiResponse> GetQuestionDependy(QuesitionDenpendceyRequst request);
public record QuesitionDenpendceyRequst(Guid ExamQuestioId, Guid ClassId);
public record QuestionDependecyDto (LessonDto? Lesson, IEnumerable<float?> ScoreDistribution, IEnumerable<UserListDto> ErrorStudents);
}
}

View File

@@ -0,0 +1,66 @@
using Entities.DTO;
///*
/// 创建一个新的试卷。
/// 删除一个试卷。
/// 修改一个试卷。
/// 查看试卷详情。
///
/// Teacher
/// 获取指定用户的所有试卷预览。
/// 获取试卷的所有指定
///
namespace TechHelper.Services.Beta
{
public interface IExamService : IBaseService<ExamDto, Guid>
{
///// <summary>
///// 获取指定用户的所有试卷预览。
///// </summary>
//Task<ApiResponse> GetAllExamPreviewsAsync(Guid userId);
/// <summary>
/// 创建一个新的试卷。
/// </summary>
/// <returns>创建成功的试卷ID</returns>
Task<ApiResponse> CreateExamAsync(ExamDto examDto);
/// <summary>
/// 为指定的班级指派一个试卷
/// </summary>
/// <param name="TeacherId"> 老师ID </param>
/// <param name="assignmentId"> 试卷ID </param>
/// <param name="classId"> 班级ID </param>
/// <returns></returns>
Task<ApiResponse> AssignmentToClassAsync(Guid TeacherId , Guid assignmentId, Guid classId);
/// <summary>
/// 为指定学生指派一个试卷
/// </summary>
/// <param name="assignementId"></param>
/// <param name="studentId"></param>
/// <returns></returns>
Task<ApiResponse> AssignmentToStudentsAsync(AssigExamToStudentsDto examToStudentsDto);
/// <summary>
/// 获取该试卷在指定班级指派了多少人
/// </summary>
/// <param name="examToClassDto"></param>
/// <returns></returns>
Task<ApiResponse> GetExamSubmissionDetailInClassAsync(AssigExamToClassDto examToClassDto);
Task<ApiResponse> GetExamTotalErrorDistributionInClassAsync(AssigExamToClassDto examToClassDto);
public record ExamDistributionDto(Dictionary<string, int> ErrorTypeDistribution,
Dictionary<string, int> ErrorLessonDistribution,
IEnumerable<float> ScoreDistribution);
}
}