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

namespace YBEE.EQM.Application.Ncee;

/// <summary>
/// 高中划线参照划线管理服务
/// </summary>
public class NceeBaseLineService : INceeBaseLineService, ITransient
{
    private readonly IRepository<NceeBaseLine> _rep;

    public NceeBaseLineService(IRepository<NceeBaseLine> rep)
    {
        _rep = rep;
    }

    /// <summary>
    /// 添加
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public async Task Add(AddNceeBaseLineInput input)
    {
        var e = await _rep.AnyAsync(t => t.NceePlanId == input.NceePlanId && t.NceeLineLevel == input.NceeLineLevel);
        if (e)
        {
            throw Oops.Oh(ErrorCode.E2011, "划线等级");
        }

        var item = input.Adapt<NceeBaseLine>();
        await item.InsertNowAsync();
    }
    /// <summary>
    /// 更新
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public async Task Update(UpdateNceeBaseLineInput input)
    {
        var item = await _rep.FirstOrDefaultAsync(t => t.Id == input.Id) ?? throw Oops.Oh(ErrorCode.E2001);
        await item.UpdateNowAsync();
    }
    /// <summary>
    /// 删除
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public async Task Del(BaseId input)
    {
        var item = await _rep.FirstOrDefaultAsync(t => t.Id == input.Id) ?? throw Oops.Oh(ErrorCode.E2001);
        await item.DeleteNowAsync();
    }
    /// <summary>
    /// 根据计划ID获取划线参考列表
    /// </summary>
    /// <param name="nceePlanId"></param>
    /// <returns></returns>
    public async Task<List<NceeBaseLineOutput>> GetListByNceePlanId(int nceePlanId)
    {
        var items = await _rep.DetachedEntities.Where(t => t.NceePlanId == nceePlanId).ProjectToType<NceeBaseLineOutput>().OrderBy(t => t.NceeLineLevel).ToListAsync();
        return items;
    }
}
