using AutoMapper;
using Entities.Contracts;
using Entities.DTO;
using Microsoft.EntityFrameworkCore;
using SharedDATA.Api;
using SharedDATA.Context;
using TechHelper.Services;
namespace TechHelper.Services.Beta
{
///
/// 提交详情服务实现(Beta版本)
/// 实现提交详情相关的业务逻辑操作
///
public class SubmissionDetailService : ISubmissionDetailService
{
private readonly IUnitOfWork _unitOfWork;
private readonly IMapper _mapper;
private readonly IRepository _submissionDetailRepository;
private readonly IRepository _submissionRepository;
///
/// 初始化提交详情服务
///
/// AutoMapper实例
/// 工作单元
public SubmissionDetailService(IMapper mapper, IUnitOfWork unitOfWork)
{
_mapper = mapper;
_unitOfWork = unitOfWork;
_submissionDetailRepository = _unitOfWork.GetRepository();
_submissionRepository = _unitOfWork.GetRepository();
}
#region 基本CRUD操作
///
/// 获取所有提交详情
///
/// 查询参数
/// 提交详情列表
public async Task GetAllAsync(QueryParameter query)
{
try
{
var pagedDetails = await _submissionDetailRepository.GetPagedListAsync(
pageIndex: query.PageIndex,
pageSize: query.PageSize,
orderBy: sd => sd.OrderByDescending(sd => sd.CreatedAt),
predicate: sd => !sd.IsDeleted,
include: i => i.Include(sd => sd.Student)
.Include(sd => sd.ExamQuestion)
.ThenInclude(aq => aq.Question));
var detailDtos = _mapper.Map>(pagedDetails.Items);
return ApiResponse.Success("获取所有提交详情成功。", new PagedList
{
PageIndex = pagedDetails.PageIndex,
PageSize = pagedDetails.PageSize,
TotalCount = pagedDetails.TotalCount,
TotalPages = pagedDetails.TotalPages,
Items = detailDtos
});
}
catch (Exception ex)
{
return ApiResponse.Error($"获取所有提交详情失败: {ex.Message}");
}
}
///
/// 根据ID获取提交详情
///
/// 提交详情ID
/// 提交详情详情
public async Task GetAsync(Guid id)
{
try
{
var detail = await _submissionDetailRepository.GetFirstOrDefaultAsync(
predicate: sd => sd.Id == id && !sd.IsDeleted,
include: i => i.Include(sd => sd.Student)
.Include(sd => sd.ExamQuestion)
.ThenInclude(aq => aq.Question));
if (detail == null)
{
return ApiResponse.Error("未找到提交详情。", 404);
}
var detailDto = _mapper.Map(detail);
return ApiResponse.Success("获取提交详情成功。", detailDto);
}
catch (Exception ex)
{
return ApiResponse.Error($"获取提交详情失败: {ex.Message}");
}
}
///
/// 创建提交详情
///
/// 提交详情数据传输对象
/// 创建结果
public async Task AddAsync(SubmissionDetailDto model)
{
try
{
if(await _unitOfWork.GetRepository().GetFirstOrDefaultAsync(predicate: sd => sd.Id == model.Id ) != null)
{
return ApiResponse.Error("提交详情已存在。", 400);
}
var detail = _mapper.Map(model);
detail.CreatedAt = DateTime.Now;
detail.UpdatedAt = DateTime.Now;
detail.IsDeleted = false;
await _submissionDetailRepository.InsertAsync(detail);
await _unitOfWork.SaveChangesAsync();
var result = _mapper.Map(detail);
return ApiResponse.Success("创建提交详情成功。", result);
}
catch (Exception ex)
{
return ApiResponse.Error($"创建提交详情失败: {ex.Message}");
}
}
///
/// 更新提交详情
///
/// 提交详情数据传输对象
/// 更新结果
public async Task UpdateAsync(SubmissionDetailDto model)
{
try
{
var existingDetail = await _submissionDetailRepository.GetFirstOrDefaultAsync(predicate: sd => sd.Id == model.Id && !sd.IsDeleted);
if (existingDetail == null)
{
return ApiResponse.Error("未找到要更新的提交详情。", 404);
}
_mapper.Map(model, existingDetail);
existingDetail.UpdatedAt = DateTime.Now;
_submissionDetailRepository.Update(existingDetail);
await _unitOfWork.SaveChangesAsync();
var result = _mapper.Map(existingDetail);
return ApiResponse.Success("更新提交详情成功。", result);
}
catch (Exception ex)
{
return ApiResponse.Error($"更新提交详情失败: {ex.Message}");
}
}
///
/// 删除提交详情
///
/// 提交详情ID
/// 删除结果
public async Task DeleteAsync(Guid id)
{
try
{
var detail = await _submissionDetailRepository.GetFirstOrDefaultAsync(predicate: sd => sd.Id == id && !sd.IsDeleted);
if (detail == null)
{
return ApiResponse.Error("未找到要删除的提交详情。", 404);
}
detail.IsDeleted = true;
_submissionDetailRepository.Update(detail);
await _unitOfWork.SaveChangesAsync();
return ApiResponse.Success("删除提交详情成功。", null);
}
catch (Exception ex)
{
return ApiResponse.Error($"删除提交详情失败: {ex.Message}");
}
}
#endregion
#region 特殊操作
///
/// 获取提交详情列表
///
/// 提交ID
/// 提交详情列表
public async Task GetBySubmissionIdAsync(Guid submissionId)
{
try
{
var details = await _submissionDetailRepository.GetPagedListAsync(
predicate: sd => sd.SubmissionId == submissionId && !sd.IsDeleted,
include: i => i.Include(sd => sd.Student)
.Include(sd => sd.ExamQuestion)
.ThenInclude(aq => aq.Question));
var detailDtos = _mapper.Map>(details.Items);
return ApiResponse.Success("获取提交详情列表成功。", detailDtos);
}
catch (Exception ex)
{
return ApiResponse.Error($"获取提交详情列表失败: {ex.Message}");
}
}
///
/// 获取学生的提交详情
///
/// 学生ID
/// 考试ID
/// 提交详情列表
public async Task GetByStudentAndExamAsync(Guid studentId, Guid examId)
{
try
{
var details = await _submissionDetailRepository.GetPagedListAsync(
predicate: sd => sd.StudentId == studentId &&
sd.Submission.ExamId == examId &&
!sd.IsDeleted,
include: i => i.Include(sd => sd.ExamQuestion)
.ThenInclude(aq => aq.Question));
var detailDtos = _mapper.Map>(details.Items);
return ApiResponse.Success("获取学生提交详情成功。", detailDtos);
}
catch (Exception ex)
{
return ApiResponse.Error($"获取学生提交详情失败: {ex.Message}");
}
}
///
/// 批量创建提交详情
///
/// 提交ID
/// 提交详情列表
/// 创建结果
public async Task BatchCreateAsync(Guid submissionId, List details)
{
try
{
var detailEntities = _mapper.Map>(details);
foreach (var detail in detailEntities)
{
detail.SubmissionId = submissionId;
detail.CreatedAt = DateTime.Now;
detail.UpdatedAt = DateTime.Now;
detail.IsDeleted = false;
await _submissionDetailRepository.InsertAsync(detail);
}
await _unitOfWork.SaveChangesAsync();
var result = _mapper.Map>(detailEntities);
return ApiResponse.Success("批量创建提交详情成功。", result);
}
catch (Exception ex)
{
return ApiResponse.Error($"批量创建提交详情失败: {ex.Message}");
}
}
///
/// 批量更新提交详情
///
/// 提交详情列表
/// 更新结果
public async Task BatchUpdateAsync(List details)
{
try
{
var detailIds = details.Select(d => d.Id).ToList();
var existingDetails = await _submissionDetailRepository.GetAllAsync(
predicate: sd => detailIds.Contains(sd.Id) && !sd.IsDeleted);
foreach (var detail in details)
{
var existingDetail = existingDetails.FirstOrDefault(sd => sd.Id == detail.Id);
if (existingDetail != null)
{
_mapper.Map(detail, existingDetail);
existingDetail.UpdatedAt = DateTime.Now;
_submissionDetailRepository.Update(existingDetail);
}
}
await _unitOfWork.SaveChangesAsync();
return ApiResponse.Success("批量更新提交详情成功。", null);
}
catch (Exception ex)
{
return ApiResponse.Error($"批量更新提交详情失败: {ex.Message}");
}
}
///
/// 更新提交详情评分
///
/// 提交详情ID
/// 分数
/// 反馈
/// 更新结果
public async Task UpdateScoreAsync(Guid detailId, float? points, string? feedback)
{
try
{
var detail = await _submissionDetailRepository.GetFirstOrDefaultAsync(
predicate: sd => sd.Id == detailId && !sd.IsDeleted);
if (detail == null)
{
return ApiResponse.Error("未找到提交详情。", 404);
}
detail.PointsAwarded = points;
detail.TeacherFeedback = feedback;
detail.UpdatedAt = DateTime.Now;
_submissionDetailRepository.Update(detail);
await _unitOfWork.SaveChangesAsync();
var result = _mapper.Map(detail);
return ApiResponse.Success("更新评分成功。", result);
}
catch (Exception ex)
{
return ApiResponse.Error($"更新评分失败: {ex.Message}");
}
}
///
/// 获取错题详情
///
/// 学生ID
/// 考试ID
/// 错题详情列表
public async Task GetErrorDetailsAsync(Guid studentId, Guid examId)
{
try
{
var details = await _submissionDetailRepository.GetPagedListAsync(
predicate: sd => sd.StudentId == studentId &&
sd.Submission.ExamId == examId &&
sd.IsCorrect == false &&
!sd.IsDeleted,
include: i => i.Include(sd => sd.ExamQuestion)
.ThenInclude(aq => aq.Question));
var detailDtos = _mapper.Map>(details.Items);
return ApiResponse.Success("获取错题详情成功。", detailDtos);
}
catch (Exception ex)
{
return ApiResponse.Error($"获取错题详情失败: {ex.Message}");
}
}
///
/// 获取正确题详情
///
/// 学生ID
/// 考试ID
/// 正确题详情列表
public async Task GetCorrectDetailsAsync(Guid studentId, Guid examId)
{
try
{
var details = await _submissionDetailRepository.GetPagedListAsync(
predicate: sd => sd.StudentId == studentId &&
sd.Submission.ExamId == examId &&
sd.IsCorrect == true &&
!sd.IsDeleted,
include: i => i.Include(sd => sd.ExamQuestion)
.ThenInclude(aq => aq.Question));
var detailDtos = _mapper.Map>(details.Items);
return ApiResponse.Success("获取正确题详情成功。", detailDtos);
}
catch (Exception ex)
{
return ApiResponse.Error($"获取正确题详情失败: {ex.Message}");
}
}
///
/// 获取未批改的提交详情
///
/// 教师ID
/// 未批改的提交详情列表
public async Task GetUngradedDetailsAsync(Guid teacherId)
{
try
{
var submissions = await _submissionRepository.GetPagedListAsync(
predicate: s => s.GraderId == teacherId &&
s.Status == SubmissionStatus.Submitted &&
!s.IsDeleted);
var submissionIds = submissions.Items.Select(s => s.Id).ToList();
var details = await _submissionDetailRepository.GetPagedListAsync(
predicate: sd => submissionIds.Contains(sd.SubmissionId) &&
sd.PointsAwarded == null &&
!sd.IsDeleted,
include: i => i.Include(sd => sd.Student)
.Include(sd => sd.ExamQuestion)
.ThenInclude(aq => aq.Question));
var detailDtos = _mapper.Map>(details.Items);
return ApiResponse.Success("获取未批改的提交详情成功。", detailDtos);
}
catch (Exception ex)
{
return ApiResponse.Error($"获取未批改的提交详情失败: {ex.Message}");
}
}
///
/// 批量更新提交详情状态
///
/// 提交ID
/// 状态
/// 更新结果
public async Task UpdateStatusAsync(Guid submissionId, SubmissionStatus status)
{
try
{
var details = await _submissionDetailRepository.GetAllAsync(
predicate: sd => sd.SubmissionId == submissionId && !sd.IsDeleted);
foreach (var detail in details)
{
detail.UpdatedAt = DateTime.Now;
_submissionDetailRepository.Update(detail);
}
await _unitOfWork.SaveChangesAsync();
return ApiResponse.Success("批量更新状态成功。", null);
}
catch (Exception ex)
{
return ApiResponse.Error($"批量更新状态失败: {ex.Message}");
}
}
#endregion
}
}