using YBEE.EQM.Core;

namespace YBEE.EQM.Application;

/// <summary>
/// 监测学生管理服务
/// </summary>
[ApiDescriptionSettings(Name = "exam-student")]
[Route("exam/student")]
public class ExamStudentAppService : IDynamicApiController
{
    private readonly IExamStudentService _examStudentService;
    public ExamStudentAppService(IExamStudentService examStudentService)
    {
        _examStudentService = examStudentService;
    }

    #region 批量导入
    /// <summary>
    /// 上传批量导入文件
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [RequestSizeLimit(long.MaxValue)]
    [RequestFormLimits(MultipartBodyLengthLimit = long.MaxValue)]
    public async Task<UploadExamDataOutput<UploadExamStudentOutput>> Upload([FromForm] UploadExamStudentInput input)
    {
        string fileExt = Path.GetExtension(input.File.FileName).ToLower();
        if (fileExt != ".xls" && fileExt != ".xlsx")
        {
            throw Oops.Oh(ErrorCode.E1010);
        }

        string filePath = Path.Combine(FileUtil.GetTempFileRoot(), $"{Guid.NewGuid()}{fileExt}");
        using FileStream fs = File.Create(filePath);
        await input.File.CopyToAsync(fs);
        await fs.FlushAsync();
        fs.Close();

        var ret = await _examStudentService.Upload(filePath, input.ExamPlanId, input.ExamGradeId);
        return ret;
    }
    /// <summary>
    /// 批量导入监测学生
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public async Task Import(ImportExamStudentInput input)
    {
        await _examStudentService.Import(input);
    }
    #endregion

    #region 创建更新删除
    /// <summary>
    /// 添加监测学生
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public async Task Add(AddExamStudentInput input)
    {
        await _examStudentService.Add(input);
    }
    /// <summary>
    /// 更新监测学生
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public async Task Update(UpdateExamStudentInput input)
    {
        await _examStudentService.Update(input);
    }
    /// <summary>
    /// 删除监测学生
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public async Task Del(BaseId<long> input)
    {
        await _examStudentService.Del(input);
    }
    /// <summary>
    /// 清空监测学生
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public async Task Clear(ClearExamStudentInput input)
    {
        await _examStudentService.Clear(input);
    }
    #endregion

    #region 查询
    /// <summary>
    /// 分页查询监测学生列表
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public async Task<PageResult<ExamStudentOutput>> QueryPageList(ExamStudentPageInput input)
    {
        return await _examStudentService.QueryPageList(input);
    }
    /// <summary>
    /// 获取机构班级上报人数统计列表
    /// </summary>
    /// <param name="examPlanId"></param>
    /// <returns></returns>
    public async Task<ExamStudentGradeClassStudentCountOutput> GetOrgGradeClassStudentCount([FromQuery][Required] int examPlanId)
    {
        return await _examStudentService.GetOrgGradeClassStudentCount(examPlanId);
    }
    /// <summary>
    /// 分页查询班级学生人数
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public async Task<PageResult<ExamStudentCountItem>> QueryStudentCountPageList(ExamStudentCountPageInput input)
    {
        return await _examStudentService.QueryStudentCountPageList(input);
    }
    #endregion
}
