using YBEE.EQM.Core;

namespace YBEE.EQM.Application;

/// <summary>
/// 缺测替补管理服务
/// </summary>
[ApiDescriptionSettings(Name = "exam-absent-replace")]
[Route("exam/absent/replace")]
public class ExamAbsentReplaceAppService : IDynamicApiController
{
    private readonly IExamAbsentReplaceService _examAbsentReplaceService;
    private readonly IResourceFileService _resourceFileService;

    public ExamAbsentReplaceAppService(IExamAbsentReplaceService examAbsentReplaceService, IResourceFileService resourceFileService)
    {
        _examAbsentReplaceService = examAbsentReplaceService;
        _resourceFileService = resourceFileService;
    }

    #region 批量导入
    /// <summary>
    /// 上传批量导入文件
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [RequestSizeLimit(long.MaxValue)]
    [RequestFormLimits(MultipartBodyLengthLimit = long.MaxValue)]
    public async Task<UploadExamDataOutput<UploadExamAbsentReplaceOutput>> 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 _examAbsentReplaceService.Upload(filePath, input.ExamPlanId);
        return ret;
    }
    /// <summary>
    /// 批量导入监测缺测替补
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public async Task<int> Import(ImportExamAbsentReplaceInput input)
    {
        return await _examAbsentReplaceService.Import(input);
    }
    #endregion

    #region 创建编辑
    /// <summary>
    /// 添加监测缺测替补
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public async Task Add(AddExamAbsentReplaceInput input)
    {
        await _examAbsentReplaceService.Add(input);
    }
    /// <summary>
    /// 更新监测缺测替补
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public async Task Update(UpdateExamAbsentReplaceInput input)
    {
        await _examAbsentReplaceService.Update(input);
    }
    /// <summary>
    /// 上传特殊学生佐证材料
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [RequestSizeLimit(1024 * 1024 * 20)]
    [RequestFormLimits(MultipartBodyLengthLimit = long.MaxValue)]
    public async Task UploadAttachment([FromForm] UploadResourceFileInput input)
    {
        var rfile = await _resourceFileService.Upload(input);
        var addParams = rfile.Adapt<AddAttachmentInput>();
        //addParams.SourceId = (int)input.SourceId;
        //addParams.FileId = rfile.Id;
        //addParams.ThumbFileId = rfile.ThumbResourceFile?.Id;
        await _examAbsentReplaceService.AddAttachment(addParams);
    }
    /// <summary>
    /// 删除特殊学生佐证材料
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public async Task DelAttachment(DeleteAttachmentInput input)
    {
        await _examAbsentReplaceService.DelAttachment(input);
    }
    /// <summary>
    /// 删除监测缺测替补
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public async Task Del(BaseId input)
    {
        await _examAbsentReplaceService.Del(input);
    }
    /// <summary>
    /// 清空监测缺测替补
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public async Task Clear(ClearExamAbsentReplaceInput input)
    {
        await _examAbsentReplaceService.Clear(input);
    }
    /// <summary>
    /// 导出监测缺测替补上报打印表格
    /// </summary>
    /// <param name="examPlanId"></param>
    /// <returns></returns>
    public async Task<IActionResult> ExportPrintTable(int examPlanId)
    {
        var bs = await _examAbsentReplaceService.ExportPrintTable(examPlanId);
        return new FileContentResult(bs, "application/vnd.openxmlformats-officedocument.wordprocessingml.document")
        {
            FileDownloadName = "缺测替补学生明细表.docx"
        };
    }
    #endregion

    #region 查询统计
    /// <summary>
    /// 分页查询监测缺测替补列表
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public async Task<PageResult<ExamAbsentReplaceOutput>> QueryPageList(ExamAbsentReplacePageInput input)
    {
        return await _examAbsentReplaceService.QueryPageList(input);
    }
    /// <summary>
    /// 获取机构班级特殊学生上报人数统计列表
    /// </summary>
    /// <param name="examPlanId"></param>
    /// <param name="sysOrgId"></param>
    /// <returns></returns>
    public async Task<ExamAbsentReplaceCountOutput> GetOrgGradeClassStudentCount(int examPlanId, short? sysOrgId = null)
    {
        return await _examAbsentReplaceService.GetOrgGradeClassStudentCount(examPlanId, sysOrgId);
    }
    /// <summary>
    /// 获取状态数量
    /// </summary>
    /// <returns></returns>
    public async Task<List<StatusCount>> QueryStatusCount(ExamAbsentReplacePageInput input)
    {
        return await _examAbsentReplaceService.QueryStatusCount(input);
    }
    #endregion
}
