ExamDataPublishService.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. using Furion.DatabaseAccessor.Extensions;
  2. using YBEE.EQM.Core;
  3. namespace YBEE.EQM.Application;
  4. /// <summary>
  5. /// 监测发布内容管理服务
  6. /// </summary>
  7. public class ExamDataPublishService : IExamDataPublishService, ITransient
  8. {
  9. private readonly IRepository<ExamDataPublish> _rep;
  10. public ExamDataPublishService(IRepository<ExamDataPublish> rep)
  11. {
  12. _rep = rep;
  13. }
  14. #region 创建更新
  15. /// <summary>
  16. /// 添加发布内容
  17. /// </summary>
  18. /// <param name="input"></param>
  19. /// <returns></returns>
  20. public async Task Add(AddExamDataPublishInput input)
  21. {
  22. //if (await _rep.AnyAsync(t => t.ExamPlanId == input.ExamPlanId && t.Type == input.Type))
  23. //{
  24. // throw Oops.Oh(ErrorCode.E2002, "该类型");
  25. //}
  26. var item = input.Adapt<ExamDataPublish>();
  27. await item.InsertAsync();
  28. }
  29. /// <summary>
  30. /// 更新发布内容
  31. /// </summary>
  32. /// <param name="input"></param>
  33. /// <returns></returns>
  34. public async Task Update(UpdateExamDataPublishInput input)
  35. {
  36. var item = await _rep.FirstOrDefaultAsync(t => t.Id == input.Id) ?? throw Oops.Oh(ErrorCode.E2001);
  37. item.Name = input.Name;
  38. item.Remark = input.Remark;
  39. await item.UpdateIncludeAsync(new[] { nameof(item.Name), nameof(item.Remark) });
  40. }
  41. /// <summary>
  42. /// 删除发布内容
  43. /// </summary>
  44. /// <param name="input"></param>
  45. /// <returns></returns>
  46. public async Task Del(BaseId input)
  47. {
  48. var item = await _rep.FirstOrDefaultAsync(t => t.Id == input.Id) ?? throw Oops.Oh(ErrorCode.E2001);
  49. if (item.Status != PublishStatus.UNPUBLISH)
  50. {
  51. throw Oops.Oh(ErrorCode.E3001);
  52. }
  53. await item.DeleteAsync();
  54. }
  55. #endregion
  56. #region 状态处理
  57. /// <summary>
  58. /// 发布
  59. /// </summary>
  60. /// <param name="input"></param>
  61. /// <returns></returns>
  62. public async Task Publish(BaseId input)
  63. {
  64. var item = await _rep.FirstOrDefaultAsync(t => t.Id == input.Id) ?? throw Oops.Oh(ErrorCode.E2001);
  65. if (item.Status == PublishStatus.PUBLISHED)
  66. {
  67. throw Oops.Oh(ErrorCode.E2006);
  68. }
  69. item.Status = PublishStatus.PUBLISHED;
  70. item.PublishTime = DateTime.Now;
  71. item.PublishSysUserId = CurrentSysUserInfo.SysUserId;
  72. await item.UpdateIncludeAsync(new[] { nameof(item.Status), nameof(item.PublishTime), nameof(item.PublishSysUserId) });
  73. }
  74. /// <summary>
  75. /// 取消
  76. /// </summary>
  77. /// <param name="input"></param>
  78. /// <returns></returns>
  79. public async Task Unpublish(BaseId input)
  80. {
  81. var item = await _rep.FirstOrDefaultAsync(t => t.Id == input.Id) ?? throw Oops.Oh(ErrorCode.E2001);
  82. if (item.Status != PublishStatus.PUBLISHED)
  83. {
  84. throw Oops.Oh(ErrorCode.E2006);
  85. }
  86. item.Status = PublishStatus.UNPUBLISH; // 设置为未发布
  87. await item.UpdateIncludeAsync(new[] { nameof(item.Status) });
  88. }
  89. #endregion
  90. #region 查询
  91. /// <summary>
  92. /// 根据ID获取监测数据发布内容
  93. /// </summary>
  94. /// <param name="id"></param>
  95. /// <returns></returns>
  96. public async Task<ExamDataPublishOutput> GetById(int id)
  97. {
  98. var item = await _rep.DetachedEntities.FirstOrDefaultAsync(t => t.Id == id) ?? throw Oops.Oh(ErrorCode.E2001);
  99. return item.Adapt<ExamDataPublishOutput>();
  100. }
  101. /// <summary>
  102. /// 根据监测计划ID获取数据发布内容列表
  103. /// </summary>
  104. /// <param name="examPlanId"></param>
  105. /// <returns></returns>
  106. public async Task<List<ExamDataPublishOutput>> GetListByExamPlanId(int examPlanId)
  107. {
  108. var items = await _rep.DetachedEntities.Where(t => t.ExamPlanId == examPlanId).ProjectToType<ExamDataPublishOutput>().OrderBy(t => t.Type).ToListAsync();
  109. return items;
  110. }
  111. #endregion
  112. #region 上传匹配
  113. #endregion
  114. }