using Microsoft.Extensions.Options;
using YBEE.EQM.Core;

namespace YBEE.EQM.Application;

/// <summary>
/// 监测机构反馈结果管理服务
/// </summary>
[ApiDescriptionSettings(Name = "exam-org-result")]
[Route("exam/org/result")]
public class ExamOrgResultAppService : IDynamicApiController
{
    private readonly IExamOrgResultService _examOrgResultService;
    private readonly EqmSiteOptions _eqmSiteOptions;
    public ExamOrgResultAppService(IExamOrgResultService examOrgResultService, IOptions<EqmSiteOptions> options)
    {
        _examOrgResultService = examOrgResultService;
        _eqmSiteOptions = options.Value;
    }

    /// <summary>
    /// 上传监测机构反馈文件
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [RequestSizeLimit(long.MaxValue)]
    [RequestFormLimits(MultipartBodyLengthLimit = long.MaxValue)]
    public async Task Upload([FromForm] UploadExamOrgResultInput input)
    {
        var savePath = Path.Combine(input.ExamPlanId.ToString(), input.ExamDataPublishId.ToString());
        var saveRoot = Path.Combine(_eqmSiteOptions.ExamResultRoot, savePath);
        if (!Directory.Exists(saveRoot))
        {
            Directory.CreateDirectory(saveRoot);
        }
        var extName = Path.GetExtension(input.File.FileName);
        var fileName = Guid.NewGuid().ToString("N") + extName;
        using var stream = File.Create(Path.Combine(saveRoot, fileName));
        await input.File.CopyToAsync(stream);
        await stream.FlushAsync();

        AddExamOrgResultInput item = input.Adapt<AddExamOrgResultInput>();
        item.FileName = input.File.FileName;
        item.FilePath = Path.Combine(savePath, fileName);
        item.FileSize = input.File.Length;
        item.FileExtName = Path.GetExtension(input.File.FileName);
        await _examOrgResultService.Add(item);
    }

    /// <summary>
    /// 根据文件ID下载文件
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    [HttpGet, NonUnify]
    public async Task<IActionResult> Download([FromQuery][Required] int id)
    {
        var item = await _examOrgResultService.GetById(id);
        await _examOrgResultService.SaveDownloadRecord(id);
        var filePath = Path.Combine(_eqmSiteOptions.ExamResultRoot, item.FilePath);
        return new FileStreamResult(new FileStream(filePath, FileMode.Open), "application/octet-stream")
        {
            FileDownloadName = item.FileName,
        };
    }

    /// <summary>
    /// 删除文件
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public async Task Del(BaseId input)
    {
        await _examOrgResultService.Del(input);
    }

    /// <summary>
    /// 获取ID获取监测机构反馈文件信息
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    public async Task<ExamOrgResultOutput> GetById([FromQuery][Required] int id)
    {
        return await _examOrgResultService.GetById(id);
    }

    /// <summary>
    /// 根据监测计划获取机构反馈文件列表
    /// </summary>
    /// <param name="examPlanId"></param>
    /// <returns></returns>
    public async Task<List<ExamDataPublishOrgResultOutput>> GetByExamPlanId(int examPlanId)
    {
        return await _examOrgResultService.GetByExamPlanId(examPlanId);
    }

    /// <summary>
    /// 根据发布内容ID获取机构文件列表
    /// </summary>
    /// <param name="publishId"></param>
    /// <returns></returns>
    public async Task<List<ExamOrgResultOutput>> GetByPublishId([FromQuery][Required] int publishId)
    {
        return await _examOrgResultService.GetByPublishId(publishId);
    }
}
