using Furion.DatabaseAccessor.Extensions;
using YBEE.EQM.Core;
namespace YBEE.EQM.Application;
///
/// 学期信息管理服务
///
public class SemesterService : ISemesterService, ITransient
{
private readonly IRepository _rep;
public SemesterService(IRepository rep)
{
_rep = rep;
}
///
/// 添加学期
///
///
public async Task Add()
{
var lastItem = await _rep.AsQueryable().OrderByDescending(t => t.Id).FirstOrDefaultAsync() ?? throw Oops.Oh(ErrorCode.E1007, "学期");
Semester item = new();
// ID规则:2023~2024学年上期:20232,2023~2024学年下期:20241
if (lastItem.SemesterType == SemesterType.FIRST_SEMESTER)
{
item.SemesterType = SemesterType.SECOND_SEMESTER;
item.BeginYear = lastItem.BeginYear;
item.EndYear = lastItem.EndYear;
item.Id = (short)(item.EndYear * 10 + (short)SemesterType.FIRST_SEMESTER);
item.Name = $"{item.BeginYear}至{item.EndYear}学年下学期";
item.ShortName = $"{item.BeginYear}~{item.EndYear}下";
item.NickName = $"{item.BeginYear}至{item.EndYear}学年春季学期";
item.NickShortName = $"{item.EndYear}春";
}
else
{
item.SemesterType = SemesterType.FIRST_SEMESTER;
item.BeginYear = lastItem.EndYear;
item.EndYear = (short)(lastItem.BeginYear + 1);
item.Id = (short)(item.BeginYear * 10 + (short)SemesterType.SECOND_SEMESTER);
item.Name = $"{item.BeginYear}至{item.EndYear}学年上学期";
item.ShortName = $"{item.BeginYear}~{item.EndYear}上";
item.NickName = $"{item.BeginYear}至{item.EndYear}学年秋季学期";
item.NickShortName = $"{item.BeginYear}秋";
}
await item.InsertAsync();
}
///
/// 设置当前学期
///
///
///
public async Task SetCurrent(BaseId input)
{
var item = await _rep.FirstOrDefaultAsync(t => t.Id == input.Id) ?? throw Oops.Oh(ErrorCode.E2002);
item.IsCurrent = true;
var oitems = await _rep.Where(t => t.Id != input.Id).ToListAsync();
foreach (var oi in oitems) { oi.IsCurrent = false; }
await _rep.UpdateAsync();
}
///
/// 根据ID获取单个学期
///
///
///
public async Task GetById(int id)
{
var item = await _rep.DetachedEntities.FirstOrDefaultAsync(t => t.Id == id);
return item.Adapt();
}
///
/// 获取当前学期
///
///
public async Task GetCurrent()
{
var item = await _rep.DetachedEntities.FirstOrDefaultAsync(t => t.IsCurrent == true);
return item.Adapt();
}
///
/// 获取全部学期信息列表
///
///
public async Task> GetAllList()
{
var items = await _rep.Where(t => t.IsDeleted == false).ProjectToType().OrderByDescending(t => t.Id).ToListAsync();
return items;
}
}