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

namespace YBEE.EQM.Application;

/// <summary>
/// 监测年级管理服务
/// </summary>
public class ExamGradeService : IExamGradeService, ITransient
{
    private readonly IRepository<ExamGrade> _rep;
    public ExamGradeService(IRepository<ExamGrade> rep)
    {
        _rep = rep;
    }

    /// <summary>
    /// 批量添加监测年级
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public async Task AddList(AddExamGradeListInput input)
    {
        var egradeids = await _rep.DetachedEntities.Where(t => t.ExamPlanId == input.ExamPlanId).Select(t => t.GradeId).ToListAsync();
        var newgradeids = input.GradeList.Where(t => !egradeids.Contains(t.GradeId));
        if (!newgradeids.Any())
        {
            return;
        }
        List<ExamGrade> items = new();
        foreach (var ng in newgradeids)
        {
            var item = ng.Adapt<ExamGrade>();
            item.ExamPlanId = input.ExamPlanId;
            items.Add(item);
        }
        await _rep.InsertAsync(items);
    }
    /// <summary>
    /// 移出监测年级
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public async Task Remove(DelExamGradeInput input)
    {
        var roleUsers = await _rep.Where(t => input.IdList.Contains(t.Id)).ToListAsync();
        await _rep.DeleteAsync(roleUsers);
    }
    /// <summary>
    /// 保存监测年级设置
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public async Task SaveSetting(ExamGradeSettingInput input)
    {
        if (!await _rep.AnyAsync(t => t.Id == input.Id))
        {
            throw Oops.Oh(ErrorCode.E2001);
        }
        var item = input.Adapt<ExamGrade>();
        await item.UpdateIncludeAsync(new[] {
            nameof(item.IsRequiredSample),
            nameof(item.IsRequiredSelfExamNumber),
            nameof(item.SelfExamNumberLength),
            nameof(item.Remark),
        });
    }
    /// <summary>
    /// 根据ID获取监测年级
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    public async Task<ExamGradeOutput> GetById(int id)
    {
        var item = await _rep.DetachedEntities.Include(t => t.Grade).FirstOrDefaultAsync(t => t.Id == id);
        return item.Adapt<ExamGradeOutput>();
    }
    /// <summary>
    /// 根据监测计划ID获取全部监测年级
    /// </summary>
    /// <param name="examPlanId"></param>
    /// <returns></returns>
    public async Task<List<ExamGradeOutput>> GetListByExamPlanId(int examPlanId)
    {
        var items = await _rep.DetachedEntities.Where(t => t.ExamPlanId == examPlanId).ProjectToType<ExamGradeOutput>().ToListAsync();
        return items;
    }
    /// <summary>
    /// 根据监测计划和年级ID获取监测年级信息
    /// </summary>
    /// <param name="examPlanId"></param>
    /// <param name="gradeId"></param>
    /// <returns></returns>
    public async Task<ExamGradeOutput> GetByExamPlanGradeId(int examPlanId, int gradeId)
    {
        var item = await _rep.DetachedEntities.Include(t => t.Grade).FirstOrDefaultAsync(t => t.ExamPlanId == examPlanId && t.GradeId == gradeId);
        return item.Adapt<ExamGradeOutput>();
    }
}
