ExamScoreImportAppService.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. using YBEE.EQM.Core;
  2. namespace YBEE.EQM.Application;
  3. /// <summary>
  4. /// 学生成绩导入服务
  5. /// </summary>
  6. [ApiDescriptionSettings(Name = "exam-score-import")]
  7. [Route("exam/score/import")]
  8. public class ExamScoreImportAppService : IDynamicApiController
  9. {
  10. private readonly IExamScoreImportService _examScoreImportService;
  11. public ExamScoreImportAppService(IExamScoreImportService service)
  12. {
  13. _examScoreImportService = service;
  14. }
  15. /// <summary>
  16. /// 上传文件并完成批量导入前期未上报学生名单的各科成绩
  17. /// </summary>
  18. /// <param name="input"></param>
  19. /// <returns></returns>
  20. [RequestSizeLimit(long.MaxValue)]
  21. [RequestFormLimits(MultipartBodyLengthLimit = long.MaxValue)]
  22. public async Task UploadImportWithoutStudentTotalScore([FromForm] UploadExamDataInput input)
  23. {
  24. string fileExt = Path.GetExtension(input.File.FileName).ToLower();
  25. if (fileExt != ".xls" && fileExt != ".xlsx")
  26. {
  27. throw Oops.Oh(ErrorCode.E1010);
  28. }
  29. string filePath = Path.Combine(FileUtil.GetTempFileRoot(), $"{Guid.NewGuid()}{fileExt}");
  30. using FileStream fs = File.Create(filePath);
  31. await input.File.CopyToAsync(fs);
  32. await fs.FlushAsync();
  33. fs.Close();
  34. await _examScoreImportService.UploadImportWithoutStudentTotalScore(filePath, input.ExamPlanId);
  35. }
  36. /// <summary>
  37. /// 批量导入学生总成绩
  38. /// </summary>
  39. /// <param name="input"></param>
  40. /// <returns></returns>
  41. [AllowAnonymous]
  42. [RequestSizeLimit(long.MaxValue)]
  43. [RequestFormLimits(MultipartBodyLengthLimit = long.MaxValue)]
  44. public async Task UploadImportStudentTotalScore([FromForm] UploadExamDataInput input)
  45. {
  46. string fileExt = Path.GetExtension(input.File.FileName).ToLower();
  47. if (fileExt != ".xls" && fileExt != ".xlsx")
  48. {
  49. throw Oops.Oh(ErrorCode.E1010);
  50. }
  51. string filePath = Path.Combine(FileUtil.GetTempFileRoot(), $"{Guid.NewGuid()}{fileExt}");
  52. using FileStream fs = File.Create(filePath);
  53. await input.File.CopyToAsync(fs);
  54. await fs.FlushAsync();
  55. fs.Close();
  56. await _examScoreImportService.UploadImportStudentTotalScore(filePath, input.ExamPlanId);
  57. }
  58. /// <summary>
  59. /// 导入区校合并小题成绩
  60. /// </summary>
  61. /// <param name="input"></param>
  62. /// <returns></returns>
  63. [AllowAnonymous]
  64. [RequestSizeLimit(long.MaxValue)]
  65. [RequestFormLimits(MultipartBodyLengthLimit = long.MaxValue)]
  66. public async Task<IActionResult> UploadImportStudentMinorScore([FromForm] UploadExamDataInput input)
  67. {
  68. string fileExt = Path.GetExtension(input.File.FileName).ToLower();
  69. if (fileExt != ".zip")
  70. {
  71. throw Oops.Oh(ErrorCode.E1010);
  72. }
  73. string filePath = Path.Combine(FileUtil.GetTempFileRoot(), $"{Guid.NewGuid()}{fileExt}");
  74. using FileStream fs = File.Create(filePath);
  75. await input.File.CopyToAsync(fs);
  76. await fs.FlushAsync();
  77. fs.Close();
  78. var (fileName, fileBytes) = await _examScoreImportService.UploadImportStudentMinorScore(filePath, input.ExamPlanId);
  79. return new FileContentResult(fileBytes, "application/octet-stream")
  80. {
  81. FileDownloadName = fileName,
  82. };
  83. }
  84. }