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

namespace YBEE.EQM.Application;

/// <summary>
/// 监测科目管理服务
/// </summary>
public class ExamCourseService : IExamCourseService, ITransient
{
    private readonly IRepository<ExamCourse> _rep;

    public ExamCourseService(IRepository<ExamCourse> rep)
    {
        _rep = rep;
    }

    /// <summary>
    /// 添加监测科目
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public async Task Add(AddExamCourseInput input)
    {
        if (await _rep.AnyAsync(t => t.ExamPlanId == input.ExamPlanId && t.ExamGradeId == input.ExamGradeId && t.CourseId == input.CourseId))
        {
            throw Oops.Oh(ErrorCode.E2002);
        }
        var item = input.Adapt<ExamCourse>();
        item.ScoreReportConfig = JSON.Serialize(input.ScoreReportConfig);
        await item.InsertAsync();
    }
    /// <summary>
    /// 更新监测科目
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public async Task Update(UpdateExamCourseInput input)
    {
        if (await _rep.AnyAsync(t => t.Id != input.Id && t.ExamPlanId == input.ExamPlanId && t.ExamGradeId == input.ExamGradeId && t.CourseId == input.CourseId))
        {
            throw Oops.Oh(ErrorCode.E2002);
        }
        var item = await _rep.FirstOrDefaultAsync(t => t.Id == input.Id) ?? throw Oops.Oh(ErrorCode.E2001);
        item.ScoreReportConfig = JSON.Serialize(input.ScoreReportConfig);
        await item.UpdateAsync();
    }
    /// <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.DeleteAsync();
    }
    /// <summary>
    /// 根据ID获取监测科目信息
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    public async Task<ExamCourseOutput> GetById(int id)
    {
        var item = await _rep.DetachedEntities.ProjectToType<ExamCourseOutput>().FirstOrDefaultAsync(t => t.Id == id) ?? throw Oops.Oh(ErrorCode.E2001);
        return item;
    }
    /// <summary>
    /// 根据监测年级查询监测学科列表
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public async Task<List<ExamCourseOutput>> QueryList(QueryExamCourseInput input)
    {
        var items = await _rep.DetachedEntities.Where(t => t.ExamPlanId == input.ExamPlanId)
                                               .Where(input.GradeId.HasValue, t => t.GradeId == input.GradeId)
                                               .ProjectToType<ExamCourseOutput>()
                                               .OrderBy(t => t.GradeId).ThenBy(t => t.CourseId)
                                               .ToListAsync();
        return items;
    }
    /// <summary>
    /// 查询监测学科列表
    /// </summary>
    /// <param name="examPlanId"></param>
    /// <returns></returns>
    public async Task<List<ExamCourseOutput>> GetListByExamPlanId(int examPlanId)
    {
        var items = await _rep.DetachedEntities.Where(t => t.ExamPlanId == examPlanId)
                                               .ProjectToType<ExamCourseOutput>()
                                               .OrderBy(t => t.GradeId).ThenBy(t => t.CourseId)
                                               .ToListAsync();
        return items;
    }
}
