ExamOrgResultService.cs 3.7 KB

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