CalendarManger.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. using System.Linq.Expressions;
  2. using Abp.BackgroundJobs;
  3. using Abp.Dependency;
  4. using Abp.Domain.Repositories;
  5. using Abp.Linq.Extensions;
  6. using Abp.Timing;
  7. using Microsoft.EntityFrameworkCore;
  8. using VberZero.BackgroundJobs.CalendarNotify;
  9. using VberZero.BaseSystem;
  10. using VberZero.Session;
  11. using VberZero.Tools.StringModel;
  12. namespace VberZero.DomainService.Calendar;
  13. public class CalendarManger : VzDomainServiceBase, ICalendarManger, ISingletonDependency
  14. {
  15. public CalendarManger(IRepository<SysCalendar> repository, IBackgroundJobManager backgroundJobManager, IRepository<BackgroundJobInfo, long> jobRepository)
  16. {
  17. Repository = repository;
  18. BackgroundJobManager = backgroundJobManager;
  19. JobRepository = jobRepository;
  20. AbpSession = NullVzSession.Instance;
  21. }
  22. public IVzSession AbpSession { get; set; }
  23. public IBackgroundJobManager BackgroundJobManager { get; }
  24. public IRepository<BackgroundJobInfo, long> JobRepository { get; }
  25. public IRepository<SysCalendar> Repository { get; }
  26. public async Task<List<SysCalendar>> GetAllList()
  27. {
  28. var list = await Repository.GetAll().Where(a => a.Start != null).ToListAsync();
  29. return list;
  30. }
  31. public async Task<List<SysCalendar>> GetAllList(Expression<Func<SysCalendar, bool>> expression)
  32. {
  33. var list = await Repository.GetAll()
  34. .WhereIf(expression != null, expression).ToListAsync();
  35. return list;
  36. }
  37. public async Task<List<SysCalendar>> GetNotStartList()
  38. {
  39. var list = await Repository.GetAll().Where(a => a.Start == null).ToListAsync();
  40. return list;
  41. }
  42. public Task<SysCalendar> GetById(int id)
  43. {
  44. return Repository.FirstOrDefaultAsync(a => a.Id == id);
  45. }
  46. public async Task<SysCalendar> Create(SysCalendar info)
  47. {
  48. var id = await Repository.InsertAndGetIdAsync(info);
  49. info.Id = id;
  50. await CheckBackgroundJob(info);
  51. return info;
  52. }
  53. public async Task<SysCalendar> Update(SysCalendar info)
  54. {
  55. await Repository.UpdateAsync(info);
  56. await CheckBackgroundJob(info);
  57. return info;
  58. }
  59. private async Task CheckBackgroundJob(SysCalendar info)
  60. {
  61. BackgroundJobInfo job = null;
  62. if (info.JobId.NotEmpty() && long.TryParse(info.JobId, out var jobId))
  63. {
  64. job = await JobRepository.FirstOrDefaultAsync(a => a.Id == jobId);
  65. }
  66. if (info.IsNotify && AbpSession.UserId.HasValue && info.Start.HasValue)
  67. {
  68. var delay = info.Start - Clock.Now;
  69. if (delay.Value.Milliseconds > 0)
  70. {
  71. if (job == null)
  72. {
  73. info.JobId = await AddBackgroundJob(info.Id, delay.Value);
  74. }
  75. else
  76. {
  77. job.NextTryTime = info.Start.Value;
  78. await JobRepository.UpdateAsync(job);
  79. return;
  80. }
  81. }
  82. }
  83. else if (job != null)
  84. {
  85. await BackgroundJobManager.DeleteAsync(job.Id + "");
  86. }
  87. info.JobId = null;
  88. await Repository.UpdateAsync(info);
  89. }
  90. private async Task<string> AddBackgroundJob(int id, TimeSpan delay)
  91. {
  92. var jobIdStr = await BackgroundJobManager.EnqueueAsync<CalendarNotifyBackgroundJob, CalendarNotifyArg>(
  93. new CalendarNotifyArg()
  94. {
  95. CalendarId = id,
  96. UserId = AbpSession.UserId ?? 0
  97. }, delay: delay);
  98. return jobIdStr;
  99. }
  100. public async Task Delete(int id)
  101. {
  102. var info = await GetById(id);
  103. if (info.JobId.NotEmpty())
  104. {
  105. await BackgroundJobManager.DeleteAsync(info.JobId);
  106. }
  107. await Repository.DeleteAsync(info);
  108. }
  109. }