using Microsoft.Extensions.Options; using YBEE.EQM.Core; namespace YBEE.EQM.Application; /// /// 监测机构反馈结果管理服务 /// [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 options) { _examOrgResultService = examOrgResultService; _eqmSiteOptions = options.Value; } /// /// 上传监测机构反馈文件 /// /// /// [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(); 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); } /// /// 根据文件ID下载文件 /// /// /// [HttpGet, NonUnify] public async Task 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, }; } /// /// 删除文件 /// /// /// public async Task Del(BaseId input) { await _examOrgResultService.Del(input); } /// /// 获取ID获取监测机构反馈文件信息 /// /// /// public async Task GetById([FromQuery][Required] int id) { return await _examOrgResultService.GetById(id); } /// /// 根据监测计划获取机构反馈文件列表 /// /// /// public async Task> GetByExamPlanId(int examPlanId) { return await _examOrgResultService.GetByExamPlanId(examPlanId); } /// /// 根据发布内容ID获取机构文件列表 /// /// /// public async Task> GetByPublishId([FromQuery][Required] int publishId) { return await _examOrgResultService.GetByPublishId(publishId); } }