ExamOrgResultAppService.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. using Microsoft.Extensions.Options;
  2. using System.Web;
  3. using YBEE.EQM.Core;
  4. namespace YBEE.EQM.Application;
  5. /// <summary>
  6. /// 监测机构反馈结果管理服务
  7. /// </summary>
  8. [ApiDescriptionSettings(Name = "exam-org-result")]
  9. [Route("exam/org/result")]
  10. public class ExamOrgResultAppService : IDynamicApiController
  11. {
  12. private readonly IExamOrgResultService _examOrgResultService;
  13. private readonly EqmSiteOptions _eqmSiteOptions;
  14. public ExamOrgResultAppService(IExamOrgResultService examOrgResultService, IOptions<EqmSiteOptions> options)
  15. {
  16. _examOrgResultService = examOrgResultService;
  17. _eqmSiteOptions = options.Value;
  18. }
  19. /// <summary>
  20. /// 上传监测机构反馈文件
  21. /// </summary>
  22. /// <param name="input"></param>
  23. /// <returns></returns>
  24. [RequestSizeLimit(long.MaxValue)]
  25. [RequestFormLimits(MultipartBodyLengthLimit = long.MaxValue)]
  26. public async Task Upload([FromForm] UploadExamOrgResultInput input)
  27. {
  28. var savePath = Path.Combine(input.ExamPlanId.ToString(), input.ExamDataPublishId.ToString());
  29. var saveRoot = Path.Combine(_eqmSiteOptions.ExamResultRoot, savePath);
  30. if (!Directory.Exists(saveRoot))
  31. {
  32. Directory.CreateDirectory(saveRoot);
  33. }
  34. var extName = Path.GetExtension(input.File.FileName);
  35. var fileName = Guid.NewGuid().ToString("N") + extName;
  36. using var stream = File.Create(Path.Combine(saveRoot, fileName));
  37. await input.File.CopyToAsync(stream);
  38. await stream.FlushAsync();
  39. AddExamOrgResultInput item = input.Adapt<AddExamOrgResultInput>();
  40. item.FileName = input.File.FileName;
  41. item.FilePath = Path.Combine(savePath, fileName);
  42. item.FileSize = input.File.Length;
  43. item.FileExtName = Path.GetExtension(input.File.FileName);
  44. await _examOrgResultService.Add(item);
  45. }
  46. /// <summary>
  47. /// 根据文件ID下载文件
  48. /// </summary>
  49. /// <param name="id"></param>
  50. /// <returns></returns>
  51. [HttpGet, NonUnify]
  52. public async Task<IActionResult> Download([FromQuery][Required] int id)
  53. {
  54. var item = await _examOrgResultService.GetById(id);
  55. var filePath = Path.Combine(_eqmSiteOptions.ExamResultRoot, item.FilePath);
  56. return new FileStreamResult(new FileStream(filePath, FileMode.Open), "application/octet-stream")
  57. {
  58. FileDownloadName = item.FileName,
  59. };
  60. }
  61. /// <summary>
  62. /// 删除文件
  63. /// </summary>
  64. /// <param name="input"></param>
  65. /// <returns></returns>
  66. public async Task Del(BaseId input)
  67. {
  68. await _examOrgResultService.Del(input);
  69. }
  70. /// <summary>
  71. /// 获取ID获取监测机构反馈文件信息
  72. /// </summary>
  73. /// <param name="id"></param>
  74. /// <returns></returns>
  75. public async Task<ExamOrgResultOutput> GetById([FromQuery][Required] int id)
  76. {
  77. return await _examOrgResultService.GetById(id);
  78. }
  79. /// <summary>
  80. /// 根据监测计划获取机构反馈文件列表
  81. /// </summary>
  82. /// <param name="examPlanId"></param>
  83. /// <returns></returns>
  84. public async Task<List<ExamDataPublishOrgResultOutput>> GetByExamPlanId(int examPlanId)
  85. {
  86. return await _examOrgResultService.GetByExamPlanId(examPlanId);
  87. }
  88. /// <summary>
  89. /// 根据发布内容ID获取机构文件列表
  90. /// </summary>
  91. /// <param name="publishId"></param>
  92. /// <returns></returns>
  93. public async Task<List<ExamOrgResultOutput>> GetByPublishId([FromQuery][Required] int publishId)
  94. {
  95. return await _examOrgResultService.GetByPublishId(publishId);
  96. }
  97. }