ExamOrgResultAppService.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. using Microsoft.Extensions.Options;
  2. using YBEE.EQM.Core;
  3. namespace YBEE.EQM.Application;
  4. /// <summary>
  5. /// 监测机构反馈结果管理服务
  6. /// </summary>
  7. [ApiDescriptionSettings(Name = "exam-org-result")]
  8. [Route("exam/org/result")]
  9. public class ExamOrgResultAppService : IDynamicApiController
  10. {
  11. private readonly IExamOrgResultService _examOrgResultService;
  12. private readonly EqmSiteOptions _eqmSiteOptions;
  13. public ExamOrgResultAppService(IExamOrgResultService examOrgResultService, IOptions<EqmSiteOptions> options)
  14. {
  15. _examOrgResultService = examOrgResultService;
  16. _eqmSiteOptions = options.Value;
  17. }
  18. /// <summary>
  19. /// 上传监测机构反馈文件
  20. /// </summary>
  21. /// <param name="input"></param>
  22. /// <returns></returns>
  23. [RequestSizeLimit(long.MaxValue)]
  24. [RequestFormLimits(MultipartBodyLengthLimit = long.MaxValue)]
  25. public async Task Upload([FromForm] UploadExamOrgResultInput input)
  26. {
  27. var savePath = Path.Combine(input.ExamPlanId.ToString(), input.ExamDataPublishId.ToString());
  28. var saveRoot = Path.Combine(_eqmSiteOptions.ExamResultRoot, savePath);
  29. if (!Directory.Exists(saveRoot))
  30. {
  31. Directory.CreateDirectory(saveRoot);
  32. }
  33. var extName = Path.GetExtension(input.File.FileName);
  34. var fileName = Guid.NewGuid().ToString("N") + extName;
  35. using var stream = File.Create(Path.Combine(saveRoot, fileName));
  36. await input.File.CopyToAsync(stream);
  37. await stream.FlushAsync();
  38. AddExamOrgResultInput item = input.Adapt<AddExamOrgResultInput>();
  39. item.FileName = input.File.FileName;
  40. item.FilePath = Path.Combine(savePath, fileName);
  41. item.FileSize = input.File.Length;
  42. item.FileExtName = Path.GetExtension(input.File.FileName);
  43. await _examOrgResultService.Add(item);
  44. }
  45. /// <summary>
  46. /// 根据文件ID下载文件
  47. /// </summary>
  48. /// <param name="id"></param>
  49. /// <returns></returns>
  50. [HttpGet, NonUnify]
  51. public async Task<IActionResult> Download([FromQuery][Required] int id)
  52. {
  53. var item = await _examOrgResultService.GetById(id);
  54. await _examOrgResultService.SaveDownloadRecord(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. }