This commit is contained in:
SpecialX
2025-07-01 19:05:07 +08:00
parent a21ca80782
commit 017cc2169c
33 changed files with 3778 additions and 109 deletions

View File

@@ -150,7 +150,7 @@ namespace TechHelper.Services
var std = await _work.GetRepository<ClassStudent>().GetAllAsync(predicate: user => user.StudentId == id, include: i => i
.Include(t => t.Class));
if (tch == null && std == null) return new ApiResponse("你没有加入任何班级。");
if (tch == null && std == null) return ApiResponse.Error("你没有加入任何班级。");
List<Class> result = new List<Class>();
@@ -159,6 +159,25 @@ namespace TechHelper.Services
return ApiResponse.Success(result: result);
}
public async Task<ApiResponse> GetUserClassRole(Guid id)
{
var tch = await _work.GetRepository<ClassTeacher>().GetAllAsync(predicate: user => user.TeacherId == id, include: i => i
.Include(t => t.Class));
var std = await _work.GetRepository<ClassStudent>().GetAllAsync(predicate: user => user.StudentId == id, include: i => i
.Include(t => t.Class));
if (tch == null && std == null) return ApiResponse.Error("你没有加入任何班级。");
UserClassRoleDto result = new UserClassRoleDto();
tch?.ToList().ForEach(c => result.ClassInfo.Add((c.Class.Number, c.Class.Grade)));
std?.ToList().ForEach(c => result.ClassInfo.Add((c.Class.Number, c.Class.Grade)));
if (tch?.Count > 0) result.Role = "Teacher"; else result.Role = "Student";
return ApiResponse.Success(result: result);
}
// 实现 IBaseService<ClassDto, int>.UpdateAsync
public async Task<ApiResponse> UpdateAsync(ClassDto model)
{

View File

@@ -14,13 +14,17 @@ namespace TechHelper.Server.Services
{
private readonly IUnitOfWork _unitOfWork;
private readonly IExamRepository _examRepository;
private readonly ISubmissionServices _submissionService;
private readonly IClassService _classService;
private readonly IMapper _mapper;
public ExamService(IUnitOfWork unitOfWork, IExamRepository examRepository, IMapper mapper)
public ExamService(IUnitOfWork unitOfWork, IExamRepository examRepository, IMapper mapper, IClassService classService, ISubmissionServices submissionService)
{
_unitOfWork = unitOfWork;
_examRepository = examRepository;
_mapper = mapper;
_classService = classService;
_submissionService = submissionService;
}
public async Task<ApiResponse> CreateExamAsync(AssignmentDto assignmentDto)
@@ -128,7 +132,7 @@ namespace TechHelper.Server.Services
}
catch (Exception ex)
{
return ApiResponse.Error("内部问题");
return ApiResponse.Error($"内部问题,{ex.Message}, InerException{ex.InnerException}");
}
}
@@ -152,14 +156,65 @@ namespace TechHelper.Server.Services
}
}
public Task<ApiResponse> AssignmentToAllStudentsAsync(Guid id)
public async Task<ApiResponse> AssignmentToAllStudentsAsync(Guid assignmentId, Guid TeacherId)
{
throw new NotImplementedException();
try
{
var classes = await _classService.GetUserClass(TeacherId);
var classUsrClass = classes.Result as List<Class>;
var classDto = _mapper.Map<ClassDto>(classUsrClass?.FirstOrDefault());
var cla = await _classService.GetClassStudents(classDto);
var assignment = await _examRepository.GetFullExamByIdAsync(assignmentId);
if (assignment == null) return ApiResponse.Error("没有找到该试卷");
var cs = cla.Result as ICollection<ClassStudent>;
cs?.ToList().ForEach(async s =>
{
var subCount = _unitOfWork.GetRepository<Submission>().GetAll(predicate: su => su.AssignmentId == assignmentId && su.StudentId == s.StudentId);
var submission = assignment.ConvertToSubmission(s.StudentId, TeacherId);
submission.AttemptNumber = (byte)(subCount.Count() + 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 Task<ApiResponse> AssignmentToStudentsAsync(Guid assignementId, Guid studentId)
public async Task<ApiResponse> AssignmentToStudentsAsync(AssigExamToStudentsDto examToStudentsDto)
{
throw new NotImplementedException();
try
{
var assignment = await _examRepository.GetFullExamByIdAsync(examToStudentsDto.AssignmentId);
if (assignment == null) return ApiResponse.Error("没有找到该试卷");
examToStudentsDto.StudentIds?.ForEach(async s =>
{
var subCount = _unitOfWork.GetRepository<Submission>().GetAll(predicate: su => su.AssignmentId == examToStudentsDto.AssignmentId && su.StudentId == s);
var submission = assignment.ConvertToSubmission(s, examToStudentsDto.CreaterId);
submission.AttemptNumber = (byte)(subCount.Count() + 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> GetAllSubmissionAsync(Guid id)

View File

@@ -7,7 +7,8 @@ namespace TechHelper.Services
public interface IClassService : IBaseService<ClassDto, Guid>
{
public Task<ApiResponse> UserRegister(UserRegistrationToClassDto user);
public Task<ApiResponse> GetUserClass(Guid user);
public Task<ApiResponse> GetClassStudents(ClassDto classDto);
public Task<ApiResponse> GetUserClass(Guid user); // List<Class>
public Task<ApiResponse> GetUserClassRole(Guid user); // List<UserClassRoleDto>
public Task<ApiResponse> GetClassStudents(ClassDto classDto); // Class
}
}

View File

@@ -23,16 +23,36 @@ namespace TechHelper.Server.Services
Task<ApiResponse> CreateExamAsync(AssignmentDto examDto);
/// <summary>
/// 提交一份试卷
/// </summary>
/// <param name="submissionDto">提交试卷的完整信息</param>
/// <returns></returns>
Task<ApiResponse> SubmissionAssignment(SubmissionDto submissionDto);
/// <summary>
/// 为所有学生指定试卷
/// </summary>
/// <param name="id"> 试卷ID </param>
/// <returns></returns>
Task<ApiResponse> AssignmentToAllStudentsAsync(Guid assignmentId, Guid TeacherId);
Task<ApiResponse> AssignmentToAllStudentsAsync(Guid id);
Task<ApiResponse> AssignmentToStudentsAsync(Guid assignementId, Guid studentId);
/// <summary>
/// 为指定学生指派一个试卷
/// </summary>
/// <param name="assignementId"></param>
/// <param name="studentId"></param>
/// <returns></returns>
Task<ApiResponse> AssignmentToStudentsAsync(AssigExamToStudentsDto examToStudentsDto);
/// <summary>
/// 获取学生所有提交过的试卷
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
Task<ApiResponse> GetAllSubmissionAsync(Guid id);
}
}

View File

@@ -5,12 +5,60 @@ namespace TechHelper.Server.Services
{
public interface ISubmissionServices : IBaseService<Submission, Guid>
{
/// <summary>
/// 异步获取指定用户的指定试题的错题。
/// </summary>
/// <param name="assignmentId">作业ID。</param>
/// <param name="userId">用户ID。</param>
/// <returns>包含操作结果的ApiResponse。</returns>
Task<ApiResponse> GetAssignmentErrorQuestionsAsync(Guid assignmentId, Guid userId);
/// <summary>
/// 异步获取指定用户的所有错题。
/// </summary>
/// <param name="userId">用户ID。</param>
/// <returns>包含操作结果的ApiResponse。</returns>
Task<ApiResponse> GetAllErrorQuestionsAsync(Guid userId);
/// <summary>
/// 异步获取指定作业和用户的错题类型分布。
/// </summary>
/// <param name="assignmentId">作业ID。</param>
/// <param name="userId">用户ID。</param>
/// <returns>包含操作结果的ApiResponse。</returns>
Task<ApiResponse> GetAssignmentErrorQuestionTypeDisAsync(Guid assignmentId, Guid userId);
/// <summary>
/// 异步获取指定作业中所有错题的类型分布。注意原始方法签名GetAllErrorQuestionTypeDisAsync参数中含有assignmentId结合方法名推断此处可能应为获取所有错题的类型分布而非特定作业的请根据实际业务需求确认是否需要移除assignmentId参数或修改方法名。
/// </summary>
/// <param name="assignmentId">作业ID。</param>
/// <param name="userId">用户ID。</param>
/// <returns>包含操作结果的ApiResponse。</returns>
Task<ApiResponse> GetAllErrorQuestionTypeDisAsync(Guid assignmentId, Guid userId);
/// <summary>
/// 异步获取指定作业中所有学生的错题情况。
/// </summary>
/// <param name="assignmentId">作业ID。</param>
/// <param name="teacherId">教师ID。</param>
/// <returns>包含操作结果的ApiResponse。</returns>
Task<ApiResponse> GetAssignmentAllStudentsError(Guid assignmentId, Guid teacherId);
/// <summary>
/// 异步获取指定作业中出现错题的学生列表。
/// </summary>
/// <param name="assignmentId">作业ID。</param>
/// <returns>包含操作结果的ApiResponse。</returns>
Task<ApiResponse> GetQuestionErrorStudents(Guid assignmentId);
/// <summary>
/// 判断是否已经存在Submission
/// </summary>
/// <param name="assignment"></param>
/// <param name="studentId"></param>
/// <returns></returns>
Task<byte> IsHasSubmissionAsync(Guid assignment, Guid studentId);
}
}

View File

@@ -6,5 +6,7 @@ namespace TechHelper.Server.Services
public interface IUserSerivces : IBaseService<User, Guid>
{
Task<ApiResponse> GetStudentDetailInfo(Guid userId);
Task<ApiResponse> RestoreUserRoleInformation(User user);
Task<ApiResponse> VerifyUserInformation(Guid userId);
}
}

View File

@@ -1,7 +1,9 @@
using AutoMapper;
using Entities.Contracts;
using Entities.DTO;
using Microsoft.EntityFrameworkCore;
using SharedDATA.Api;
using SharedDATA.Context;
using TechHelper.Services;
namespace TechHelper.Server.Services
@@ -21,71 +23,329 @@ namespace TechHelper.Server.Services
_submissionDetailRepository = _unitOfWork.GetRepository<SubmissionDetail>();
}
public Task<ApiResponse> AddAsync(Submission model)
public async Task<ApiResponse> AddAsync(Submission model)
{
throw new NotImplementedException();
try
{
model.SubmissionTime = DateTime.Now;
model.IsDeleted = false;
await _submissionRepository.InsertAsync(model);
await _unitOfWork.SaveChangesAsync();
var result = _mapper.Map<SubmissionDto>(model);
return ApiResponse.Success("提交成功。", result);
}
catch (Exception ex)
{
return ApiResponse.Error($"添加提交失败: {ex.Message}");
}
}
public Task<ApiResponse> DeleteAsync(Guid id)
public async Task<ApiResponse> DeleteAsync(Guid id)
{
throw new NotImplementedException();
try
{
var submission = await _submissionRepository.GetFirstOrDefaultAsync(predicate: s => s.Id == id);
if (submission == null)
{
return ApiResponse.Error("未找到要删除的提交。", 404);
}
submission.IsDeleted = true;
_submissionRepository.Update(submission);
var submissionDetails = await _submissionDetailRepository.GetPagedListAsync(predicate: sd => sd.SubmissionId == id);
foreach (var detail in submissionDetails.Items)
{
detail.IsDeleted = true;
_submissionDetailRepository.Update(detail);
}
await _unitOfWork.SaveChangesAsync();
return ApiResponse.Success("提交及相关详情删除成功。", null);
}
catch (Exception ex)
{
return ApiResponse.Error($"删除提交失败: {ex.Message}");
}
}
public Task<ApiResponse> GetAllAsync(QueryParameter query)
public async Task<ApiResponse> GetAllAsync(QueryParameter query)
{
throw new NotImplementedException();
try
{
var pagedSubmissions = await _submissionRepository.GetPagedListAsync(
pageIndex: query.PageIndex,
pageSize: query.PageSize,
orderBy: s => s.OrderByDescending(s => s.SubmissionTime),
predicate: s => !s.IsDeleted,
include: i => i.Include(s => s.Student)
.Include(s => s.Assignment));
var submissionDtos = _mapper.Map<List<SubmissionDto>>(pagedSubmissions.Items);
return ApiResponse.Success("获取所有提交成功。", new PagedList<SubmissionDto>
{
PageIndex = pagedSubmissions.PageIndex,
PageSize = pagedSubmissions.PageSize,
TotalCount = pagedSubmissions.TotalCount,
TotalPages = pagedSubmissions.TotalPages,
Items = submissionDtos
});
}
catch (Exception ex)
{
return ApiResponse.Error($"获取所有提交失败: {ex.Message}");
}
}
public async Task<ApiResponse> GetAllErrorQuestionsAsync(Guid userId)
{
try
{
var errorSDs = await _submissionDetailRepository.GetPagedListAsync(predicate: sd => sd.StudentId == userId && sd.IsCorrect == false,
var errorSDs = await _submissionDetailRepository.GetPagedListAsync(
predicate: sd => sd.StudentId == userId && sd.IsCorrect == false &&
(sd.Status == SubmissionStatus.Submitted || sd.Status == SubmissionStatus.Graded),
include: i => i
.Include(s => s.AssignmentQuestion)
.ThenInclude(aq => aq.Question));
var errorQuestion = errorSDs.Items.Select(sd => sd.AssignmentQuestion).ToList();
return ApiResponse.Success();
.Include(s => s.AssignmentQuestion)
.ThenInclude(aq => aq.Question));
var errorQuestions = errorSDs.Items.Select(sd => sd.AssignmentQuestion.Question)
.Where(q => q != null)
.DistinctBy(q => q.Id)
.ToList();
var result = _mapper.Map<List<QuestionDto>>(errorQuestions);
return ApiResponse.Success("获取所有错题成功。", result);
}
catch (Exception ex)
{
return ApiResponse.Error();
return ApiResponse.Error($"获取所有错题失败: {ex.Message}");
}
}
public Task<ApiResponse> GetAllErrorQuestionTypeDisAsync(Guid assignmentId, Guid userId)
public async Task<ApiResponse> GetAllErrorQuestionTypeDisAsync(Guid assignmentId, Guid userId)
{
throw new NotImplementedException();
try
{
var errorSDs = await _submissionDetailRepository.GetPagedListAsync(
predicate: sd => sd.Submission.AssignmentId == assignmentId &&
sd.StudentId == userId &&
sd.IsCorrect == false &&
(sd.Status == SubmissionStatus.Submitted || sd.Status == SubmissionStatus.Graded),
include: i => i
.Include(s => s.AssignmentQuestion)
.ThenInclude(aq => aq.Question));
// 对错题按类型进行分组计数
var errorTypeDistribution = errorSDs.Items
.Where(sd => sd.AssignmentQuestion?.Question != null)
.GroupBy(sd => sd.AssignmentQuestion.Question.Type)
.Select(g => new
{
QuestionType = g.Key.ToString(),
Count = g.Count()
})
.ToList();
return ApiResponse.Success("获取错题类型分布成功。", errorTypeDistribution);
}
catch (Exception ex)
{
return ApiResponse.Error($"获取错题类型分布失败: {ex.Message}");
}
}
public Task<ApiResponse> GetAssignmentAllStudentsError(Guid assignmentId, Guid teacherId)
public async Task<ApiResponse> GetAssignmentAllStudentsError(Guid assignmentId, Guid teacherId)
{
throw new NotImplementedException();
try
{
var submissionDetails = await _submissionDetailRepository.GetPagedListAsync(
predicate: sd => sd.Submission.AssignmentId == assignmentId &&
sd.IsCorrect == false &&
(sd.Status == SubmissionStatus.Submitted || sd.Status == SubmissionStatus.Graded),
include: i => i.Include(sd => sd.Student));
var studentsErrorSummary = submissionDetails.Items
.Where(sd => sd.Student != null)
.GroupBy(sd => new { sd.StudentId, sd.Student.UserName })
.Select(g => new
{
StudentId = g.Key.StudentId,
StudentName = g.Key.UserName,
ErrorQuestionCount = g.Count()
})
.ToList();
return ApiResponse.Success("获取作业中所有学生的错题情况成功。", studentsErrorSummary);
}
catch (Exception ex)
{
return ApiResponse.Error($"获取作业中所有学生的错题情况失败: {ex.Message}");
}
}
public Task<ApiResponse> GetAssignmentErrorQuestionsAsync(Guid assignmentId, Guid userId)
public async Task<ApiResponse> GetAssignmentErrorQuestionsAsync(Guid assignmentId, Guid userId)
{
throw new NotImplementedException();
try
{
var errorSDs = await _submissionDetailRepository.GetPagedListAsync(
predicate: sd => sd.Submission.AssignmentId == assignmentId &&
sd.StudentId == userId &&
sd.IsCorrect == false &&
(sd.Status == SubmissionStatus.Submitted || sd.Status == SubmissionStatus.Graded),
include: i => i
.Include(s => s.AssignmentQuestion)
.ThenInclude(aq => aq.Question));
var errorQuestions = errorSDs.Items.Select(sd => sd.AssignmentQuestion.Question)
.Where(q => q != null)
.DistinctBy(q => q.Id)
.ToList();
var result = _mapper.Map<List<QuestionDto>>(errorQuestions);
return ApiResponse.Success("获取指定作业错题成功。", result);
}
catch (Exception ex)
{
return ApiResponse.Error($"获取指定作业错题失败: {ex.Message}");
}
}
public Task<ApiResponse> GetAssignmentErrorQuestionTypeDisAsync(Guid assignmentId, Guid userId)
public async Task<ApiResponse> GetAssignmentErrorQuestionTypeDisAsync(Guid assignmentId, Guid userId)
{
throw new NotImplementedException();
try
{
var errorSDs = await _submissionDetailRepository.GetPagedListAsync(
predicate: sd => sd.Submission.AssignmentId == assignmentId &&
sd.StudentId == userId &&
sd.IsCorrect == false &&
(sd.Status == SubmissionStatus.Submitted || sd.Status == SubmissionStatus.Graded),
include: i => i
.Include(s => s.AssignmentQuestion)
.ThenInclude(aq => aq.Question));
var errorTypeDistribution = errorSDs.Items
.Where(sd => sd.AssignmentQuestion?.Question != null)
.GroupBy(sd => sd.AssignmentQuestion.Question.Type)
.Select(g => new
{
QuestionType = g.Key.ToString(),
Count = g.Count()
})
.ToList();
return ApiResponse.Success("获取指定作业错题类型分布成功。", errorTypeDistribution);
}
catch (Exception ex)
{
return ApiResponse.Error($"获取指定作业错题类型分布失败: {ex.Message}");
}
}
public Task<ApiResponse> GetAsync(Guid id)
public async Task<ApiResponse> GetAsync(Guid id)
{
throw new NotImplementedException();
try
{
var submission = await _submissionRepository.GetFirstOrDefaultAsync(
predicate: s => s.Id == id && !s.IsDeleted,
include: i => i.Include(s => s.Student)
.Include(s => s.Assignment)
.Include(s => s.Grader)
.Include(s => s.SubmissionDetails)
.ThenInclude(sd => sd.AssignmentQuestion)
.ThenInclude(aq => aq.Question));
if (submission == null)
{
return ApiResponse.Error("未找到提交。", 404);
}
var submissionDto = _mapper.Map<SubmissionDto>(submission);
return ApiResponse.Success("获取提交成功。", submissionDto);
}
catch (Exception ex)
{
return ApiResponse.Error($"获取提交失败: {ex.Message}");
}
}
public Task<ApiResponse> GetQuestionErrorStudents(Guid assignmentId)
public async Task<ApiResponse> GetQuestionErrorStudents(Guid assignmentQuestionId)
{
throw new NotImplementedException();
try
{
var errorSubmissionDetails = await _submissionDetailRepository.GetPagedListAsync(
predicate: sd => sd.AssignmentQuestionId == assignmentQuestionId &&
sd.IsCorrect == false &&
(sd.Status == SubmissionStatus.Submitted || sd.Status == SubmissionStatus.Graded),
include: i => i
.Include(sd => sd.Student)
.Include(sd => sd.AssignmentQuestion)
.ThenInclude(aq => aq.Question));
var errorStudentsByQuestion = errorSubmissionDetails.Items
.Where(sd => sd.AssignmentQuestion?.Question != null && sd.Student != null)
.GroupBy(sd => new { sd.AssignmentQuestionId, sd.AssignmentQuestion.Question.Title })
.Select(g => new
{
AssignmentQuestionId = g.Key.AssignmentQuestionId,
QuestionTitle = g.Key.Title,
ErrorStudents = g.Select(sd => new
{
StudentId = sd.StudentId,
StudentName = sd.Student.UserName
}).Distinct().ToList()
})
.ToList();
return ApiResponse.Success("获取出现错题的学生成功。", errorStudentsByQuestion);
}
catch (Exception ex)
{
return ApiResponse.Error($"获取出现错题的学生失败: {ex.Message}");
}
}
public Task<ApiResponse> UpdateAsync(Submission model)
public async Task<byte> IsHasSubmissionAsync(Guid assignment, Guid studentId)
{
throw new NotImplementedException();
try
{
var result = await _unitOfWork.GetRepository<Submission>().GetAllAsync(predicate: s => s.AssignmentId == assignment && s.StudentId == studentId);
return (byte)result.Count;
}
catch (Exception ex)
{
throw;
}
}
public async Task<ApiResponse> UpdateAsync(Submission model)
{
try
{
var existingSubmission = await _submissionRepository.GetFirstOrDefaultAsync(predicate: s => s.Id == model.Id && !s.IsDeleted);
if (existingSubmission == null)
{
return ApiResponse.Error("未找到要更新的提交。", 404);
}
_mapper.Map(model, existingSubmission);
_submissionRepository.Update(existingSubmission);
await _unitOfWork.SaveChangesAsync();
var result = _mapper.Map<SubmissionDto>(existingSubmission);
return ApiResponse.Success("更新提交成功。", result);
}
catch (Exception ex)
{
return ApiResponse.Error($"更新提交失败: {ex.Message}");
}
}
}
}

View File

@@ -1,10 +1,25 @@
using Entities.Contracts;
using Entities.DTO;
using Microsoft.AspNetCore.Identity;
using SharedDATA.Api;
using TechHelper.Services;
namespace TechHelper.Server.Services
{
public class UserServices : IUserSerivces
{
private readonly IUnitOfWork _unitOfWork;
private readonly IClassService _classService;
private readonly UserManager<User> _userManager;
public UserServices(IUnitOfWork unitOfWork, IClassService classService, UserManager<User> userManager)
{
_unitOfWork = unitOfWork;
_classService = classService;
_userManager = userManager;
}
public Task<ApiResponse> AddAsync(User model)
{
throw new NotImplementedException();
@@ -30,9 +45,33 @@ namespace TechHelper.Server.Services
throw new NotImplementedException();
}
public async Task<ApiResponse> RestoreUserRoleInformation(User user)
{
var result = await _classService.GetUserClassRole(user.Id);
if (result.Status)
{
var classRole = result.Result as UserClassRoleDto;
if (classRole != null)
{
if (!await _userManager.IsInRoleAsync(user, classRole.Role))
{
await _userManager.AddToRoleAsync(user, classRole.Role);
return ApiResponse.Success();
}
}
}
return ApiResponse.Error();
}
public Task<ApiResponse> UpdateAsync(User model)
{
throw new NotImplementedException();
}
public Task<ApiResponse> VerifyUserInformation(Guid userId)
{
throw new NotImplementedException();
}
}
}