using YBEE.EQM.Core;

namespace YBEE.EQM.Application;

/// <summary>
/// 监测教师任教科目管理服务
/// </summary>
[ApiDescriptionSettings(Name = "exam-teacher-course")]
[Route("exam/teacher/course")]
public class ExamTeacherCourseAppService : IDynamicApiController
{
    private readonly IExamTeacherCourseService _examTeacherCourseService;
    public ExamTeacherCourseAppService(IExamTeacherCourseService examTeacherCourseService)
    {
        _examTeacherCourseService = examTeacherCourseService;
    }

    #region 批量导入
    /// <summary>
    /// 上传批量导入文件
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [RequestSizeLimit(long.MaxValue)]
    [RequestFormLimits(MultipartBodyLengthLimit = long.MaxValue)]
    public async Task<UploadExamDataOutput<UploadExamTeacherCourseOutput>> Upload([FromForm] UploadExamDataInput 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 _examTeacherCourseService.Upload(filePath, input.ExamPlanId);
        return ret;
    }
    /// <summary>
    /// 批量导入监测教师
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public async Task Import(ImportExamTeacherCourseInput input)
    {
        await _examTeacherCourseService.Import(input);
    }
    #endregion

    #region 批量导出
    /// <summary>
    /// 导出TQES导入文件格式文件包
    /// </summary>
    /// <param name="examPlanId"></param>
    /// <returns></returns>
    /// <exception cref="Exception"></exception>
    public async Task<IActionResult> ExportTqesFile([Required]int examPlanId)
    {
        var (fileName, fileBytes) = await _examTeacherCourseService.ExportTqesFile(examPlanId);
        return new FileContentResult(fileBytes, "application/octet-stream")
        {
            FileDownloadName = fileName,
        };
    }
    #endregion

    #region 创建编辑
    /// <summary>
    /// 添加监测教师
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public async Task Add(AddExamTeacherCourseInput input)
    {
        await _examTeacherCourseService.Add(input);
    }
    /// <summary>
    /// 更新监测教师
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public async Task Update(UpdateExamTeacherCourseInput input)
    {
        await _examTeacherCourseService.Update(input);
    }
    /// <summary>
    /// 删除监测教师
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public async Task Del(BaseId input)
    {
        await _examTeacherCourseService.Del(input);
    }
    /// <summary>
    /// 清空监测教师
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public async Task Clear(ClearExamTeacherCourseInput input)
    {
        await _examTeacherCourseService.Clear(input);
    }
    #endregion

    #region 查询统计
    /// <summary>
    /// 分页查询监测教师列表
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public async Task<PageResult<ExamTeacherCourseOutput>> QueryPageList(ExamTeacherCoursePageInput input)
    {
        return await _examTeacherCourseService.QueryPageList(input);
    }
    #endregion
}
