using Furion.DatabaseAccessor.Extensions;
using YBEE.EQM.Core;

namespace YBEE.EQM.Application;

/// <summary>
/// 学期信息管理服务
/// </summary>
public class SemesterService : ISemesterService, ITransient
{
    private readonly IRepository<Semester> _rep;
    public SemesterService(IRepository<Semester> rep)
    {
        _rep = rep;
    }

    /// <summary>
    /// 添加学期
    /// </summary>
    /// <returns></returns>
    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();
    }
    /// <summary>
    /// 设置当前学期
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public async Task SetCurrent(BaseId<short> 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();
    }
    /// <summary>
    /// 根据ID获取单个学期
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    public async Task<SemesterOutput> GetById(int id)
    {
        var item = await _rep.DetachedEntities.FirstOrDefaultAsync(t => t.Id == id);
        return item.Adapt<SemesterOutput>();
    }
    /// <summary>
    /// 获取当前学期
    /// </summary>
    /// <returns></returns>
    public async Task<SemesterOutput> GetCurrent()
    {
        var item = await _rep.DetachedEntities.FirstOrDefaultAsync(t => t.IsCurrent == true);
        return item.Adapt<SemesterOutput>();
    }
    /// <summary>
    /// 获取全部学期信息列表
    /// </summary>
    /// <returns></returns>
    public async Task<List<SemesterOutput>> GetAllList()
    {
        var items = await _rep.Where(t => t.IsDeleted == false).ProjectToType<SemesterOutput>().OrderByDescending(t => t.Id).ToListAsync();
        return items;
    }
}
