using YBEE.EQM.Core;

namespace YBEE.EQM.Application;

/// <summary>
/// 监测抽样学生管理服务
/// </summary>
public class ExamSampleStudentService : IExamSampleStudentService, ITransient
{
    private readonly IRepository<ExamSampleStudent> _rep;

    public ExamSampleStudentService(IRepository<ExamSampleStudent> rep)
    {
        _rep = rep;
    }

    /// <summary>
    /// 分页查询抽样学生信息
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public async Task<PageResult<ExamSampleStudentOutput>> QueryPageList(ExamSampleStudentPageInput input)
    {
        // 抽样方案
        var examSample = await _rep.Change<ExamSample>().DetachedEntities
                                                        .FirstOrDefaultAsync(t => t.Id == input.ExamSampleId &&
                                                                                  t.ExamPlan.IsFixedExamSample == true &&
                                                                                  t.Status == ExamSampleStatus.SUCCESSFUL &&
                                                                                  t.IsSelected == true
                                                         ) ?? throw Oops.Oh(ErrorCode.E2001);

        var name = !string.IsNullOrEmpty(input.Name?.Trim());
        var idNumber = !string.IsNullOrEmpty(input.IdNumber?.Trim());
        var examNumber = !string.IsNullOrEmpty(input.ExamNumber?.Trim());

        var ret = await _rep.DetachedEntities.Where(t => t.ExamSampleId == examSample.Id && t.ExamStudent.SysOrgId == CurrentSysUserInfo.SysOrgId)
                                             .Where((name, u => EF.Functions.Like(u.ExamStudent.Name, $"%{input.Name.Trim()}%")))
                                             .Where((idNumber, u => EF.Functions.Like(u.ExamStudent.IdNumber, $"%{input.IdNumber.Trim()}%")))
                                             .Where((examNumber, u => EF.Functions.Like(u.ExamNumber, $"%{input.ExamNumber.Trim()}%")))
                                             .Where(input.ExamSampleType.HasValue, t => t.ExamSampleType == input.ExamSampleType)
                                             .Where(input.CertificateType.HasValue, t => t.ExamStudent.CertificateType == input.CertificateType)
                                             .Where(input.GradeId.HasValue, t => t.ExamStudent.GradeId == input.GradeId)
                                             .Where(input.ClassNumber.HasValue, t => t.ExamStudent.ClassNumber == input.ClassNumber)
                                             .Where(input.SysOrgBranchId.HasValue, t => t.ExamStudent.SchoolClass.SysOrgBranchId == input.SysOrgBranchId)
                                             .ProjectToType<ExamSampleStudentOutput>()
                                             .OrderBy(t => t.ExamStudent.SysOrgBranchId).ThenBy(t => t.ExamStudent.GradeId).ThenBy(t => t.ExamSampleType).ThenBy(t => t.Sequence)
                                             .ToADPagedListAsync(input.PageIndex, input.PageSize);
        return ret;
    }
    /// <summary>
    /// 根据监测号查询已发布监测方案中当前机构下学生信息
    /// </summary>
    /// <param name="examPlanId">监测计划ID</param>
    /// <param name="examNumber">监测号</param>
    /// <returns></returns>
    public async Task<ExamSampleStudentOutput> GetByExamNumber(int examPlanId, string examNumber)
    {
        var item = await _rep.DetachedEntities.Include(t => t.ExamStudent)
                                              .FirstOrDefaultAsync(t => t.ExamStudent.ExamPlanId == examPlanId &&
                                                                        t.ExamStudent.SysOrgId == CurrentSysUserInfo.SysOrgId &&
                                                                        t.ExamSample.IsSelected == true &&
                                                                        t.ExamNumber == examNumber);
        return item.Adapt<ExamSampleStudentOutput>();
    }
    /// <summary>
    /// 查询监测学生信息
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public async Task<ExamSampleStudentOutput> QueryExamSampleStudent(QueryExamSampleStudentInput input)
    {
        var item = await _rep.DetachedEntities.Include(t => t.ExamStudent)
                                              .FirstOrDefaultAsync(t => t.ExamStudent.ExamPlanId == input.ExamPlanId &&
                                                                        t.ExamStudent.SysOrgId == CurrentSysUserInfo.SysOrgId &&
                                                                        t.ExamStudent.SysOrgBranchId == input.SysOrgBranchId &&
                                                                        t.ExamSample.IsSelected == true &&
                                                                        t.ExamStudent.ExamGradeId == input.ExamGradeId &&
                                                                        t.ExamStudent.ClassNumber == input.ClassNumber &&
                                                                        t.ExamNumber == input.ExamNumber);
        return item.Adapt<ExamSampleStudentOutput>();
    }
}
