ExamOrgResultService.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. using Furion.DatabaseAccessor.Extensions;
  2. using Microsoft.AspNetCore.Http;
  3. using Microsoft.Extensions.Options;
  4. using YBEE.EQM.Core;
  5. namespace YBEE.EQM.Application;
  6. /// <summary>
  7. /// 监测机构反馈结果管理服务
  8. /// </summary>
  9. public class ExamOrgResultService : IExamOrgResultService, ITransient
  10. {
  11. private readonly IRepository<ExamOrgResult> _rep;
  12. private readonly EqmSiteOptions _eqmSiteOptions;
  13. private readonly IHttpContextAccessor _httpContextAccessor;
  14. public ExamOrgResultService(IRepository<ExamOrgResult> rep, IOptions<EqmSiteOptions> options, IHttpContextAccessor httpContextAccessor)
  15. {
  16. _rep = rep;
  17. _eqmSiteOptions = options.Value;
  18. _httpContextAccessor = httpContextAccessor;
  19. }
  20. /// <summary>
  21. /// 添加结果文件
  22. /// </summary>
  23. /// <param name="input"></param>
  24. /// <returns></returns>
  25. public async Task Add(AddExamOrgResultInput input)
  26. {
  27. var item = input.Adapt<ExamOrgResult>();
  28. await item.InsertNowAsync();
  29. }
  30. /// <summary>
  31. /// 删除文件
  32. /// </summary>
  33. /// <param name="input"></param>
  34. /// <returns></returns>
  35. public async Task Del(BaseId input)
  36. {
  37. var item = await _rep.FirstOrDefaultAsync(t => t.Id == input.Id) ?? throw Oops.Oh(ErrorCode.E2001);
  38. var fpath = Path.Combine(_eqmSiteOptions.ExamResultRoot, item.FilePath);
  39. if (File.Exists(fpath))
  40. {
  41. File.Delete(fpath);
  42. }
  43. await item.DeleteAsync();
  44. }
  45. /// <summary>
  46. /// 保存下载记录
  47. /// </summary>
  48. /// <param name="id"></param>
  49. /// <returns></returns>
  50. public async Task SaveDownloadRecord(int id)
  51. {
  52. var httpContext = _httpContextAccessor.HttpContext;
  53. ExamOrgResultDownload item = new()
  54. {
  55. ExamOrgResultId = id,
  56. SysUserId = CurrentSysUserInfo.SysUserId,
  57. Account = CurrentSysUserInfo.Account,
  58. Ip = httpContext.GetRequestIPv4(),
  59. DownloadTime = DateTime.Now,
  60. };
  61. await _rep.Change<ExamOrgResultDownload>().InsertNowAsync(item);
  62. }
  63. /// <summary>
  64. /// 获取ID获取监测机构反馈文件信息
  65. /// </summary>
  66. /// <param name="id"></param>
  67. /// <returns></returns>
  68. public async Task<ExamOrgResultOutput> GetById(int id)
  69. {
  70. var item = await _rep.DetachedEntities.FirstOrDefaultAsync(t => t.Id == id) ?? throw Oops.Oh(ErrorCode.E2001);
  71. return item.Adapt<ExamOrgResultOutput>();
  72. }
  73. /// <summary>
  74. /// 根据监测计划获取机构反馈文件列表
  75. /// </summary>
  76. /// <param name="examPlanId"></param>
  77. /// <returns></returns>
  78. public async Task<List<ExamDataPublishOrgResultOutput>> GetByExamPlanId(int examPlanId)
  79. {
  80. var pubItems = await _rep.Change<ExamDataPublish>().DetachedEntities.Where(t => t.ExamPlanId == examPlanId && t.Status == PublishStatus.PUBLISHED).ProjectToType<ExamDataPublishOrgResultOutput>().ToListAsync();
  81. foreach (var pubItem in pubItems)
  82. {
  83. if (pubItem.Status == PublishStatus.PUBLISHED)
  84. {
  85. var items = await _rep.DetachedEntities.Where(t => t.SysOrgId == CurrentSysUserInfo.SysOrgId && t.ExamPlanId == examPlanId && t.ExamDataPublishId == pubItem.Id).ProjectToType<ExamOrgResultOutput>().ToListAsync();
  86. pubItem.ExamOrgResultList = items;
  87. }
  88. }
  89. return pubItems;
  90. }
  91. /// <summary>
  92. /// 根据发布内容ID获取机构文件列表
  93. /// </summary>
  94. /// <param name="publishId"></param>
  95. /// <returns></returns>
  96. public async Task<List<ExamOrgResultOutput>> GetByPublishId(int publishId)
  97. {
  98. var pub = await _rep.Change<ExamDataPublish>().FirstOrDefaultAsync(t => t.Id == publishId) ?? throw Oops.Oh(ErrorCode.E2001);
  99. var items = await _rep.DetachedEntities.Where(t => t.ExamDataPublishId == publishId).ProjectToType<ExamOrgResultOutput>().OrderBy(t => t.SysOrgId).ToListAsync();
  100. var norgs = await (from o in _rep.Change<ExamOrg>().AsQueryable()
  101. where o.ExamPlanId == pub.ExamPlanId && !(from r in items select r.ExamOrgId).Contains(o.Id)
  102. select o).ProjectToType<ExamOrgOutput>().ToListAsync();
  103. int maxId = 1;
  104. if (items.Any())
  105. {
  106. maxId = items.Max(t => t.Id) + 1;
  107. }
  108. items.AddRange(norgs.Select(t => new ExamOrgResultOutput()
  109. {
  110. Id = maxId++,
  111. ExamDataPublishId = publishId,
  112. ExamOrgId = t.Id,
  113. ExamOrg = t,
  114. }));
  115. return items;
  116. }
  117. }