using YBEE.EQM.Core;
namespace YBEE.EQM.Application;
///
/// 年级信息服务
///
public class GradeService : IGradeService, ITransient
{
private readonly IRepository _rep;
private readonly ISemesterService _semesterService;
private readonly IEducationStageYearsService _educationStageYearsService;
public GradeService(IRepository rep, ISemesterService semesterService, IEducationStageYearsService educationStageYearsService)
{
_rep = rep;
_semesterService = semesterService;
_educationStageYearsService = educationStageYearsService;
}
///
/// 根据ID获取年级信息
///
///
///
public async Task GetById(int id)
{
var item = await _rep.DetachedEntities.FirstOrDefaultAsync(t => t.Id == id) ?? throw Oops.Oh(ErrorCode.E2001);
return item.Adapt();
}
///
/// 根据学段获取年级列表
///
///
///
public async Task> GetListByEducationStage(EducationStage stage)
{
var items = await _rep.DetachedEntities.Where(t => t.EducationStage == stage & t.Status == CommonStatus.ENABLE).ProjectToType().ToListAsync();
return items;
}
///
/// 根据学期和学段获取年级年份
///
///
///
public async Task> QueryGradeYearList(QueryGradeYearInput input)
{
var semester = await _semesterService.GetById(input.SemesterId);
var grades = await _rep.DetachedEntities.Where(t => t.EducationStage == input.EducationStage & t.Status == CommonStatus.ENABLE).ProjectToType().ToListAsync();
var ey = await _educationStageYearsService.GetByEducationStage(input.EducationStage);
foreach (var grade in grades)
{
grade.GradeBeginYear = (short)(semester.BeginYear - grade.GradeNumber2 + 1);
grade.GradeEndYear = (short)(semester.BeginYear + ey.EducationYears - grade.GradeNumber2 + 1);
grade.EducationYears = ey.EducationYears;
}
return grades;
}
///
/// 获取全部年级列表
///
///
public async Task> GetAllList()
{
var items = await _rep.DetachedEntities.Where(t => t.Status == CommonStatus.ENABLE).ProjectToType().ToListAsync();
return items;
}
}