| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124 |
- using System.Linq.Expressions;
- using Abp.BackgroundJobs;
- using Abp.Dependency;
- using Abp.Domain.Repositories;
- using Abp.Linq.Extensions;
- using Abp.Timing;
- using Microsoft.EntityFrameworkCore;
- using VberZero.BackgroundJobs.CalendarNotify;
- using VberZero.BaseSystem;
- using VberZero.Session;
- using VberZero.Tools.StringModel;
- namespace VberZero.DomainService.Calendar;
- public class CalendarManger : VzDomainServiceBase, ICalendarManger, ISingletonDependency
- {
- public CalendarManger(IRepository<SysCalendar> repository, IBackgroundJobManager backgroundJobManager, IRepository<BackgroundJobInfo, long> jobRepository)
- {
- Repository = repository;
- BackgroundJobManager = backgroundJobManager;
- JobRepository = jobRepository;
- AbpSession = NullVzSession.Instance;
- }
- public IVzSession AbpSession { get; set; }
- public IBackgroundJobManager BackgroundJobManager { get; }
- public IRepository<BackgroundJobInfo, long> JobRepository { get; }
- public IRepository<SysCalendar> Repository { get; }
- public async Task<List<SysCalendar>> GetAllList()
- {
- var list = await Repository.GetAll().Where(a => a.Start != null).ToListAsync();
- return list;
- }
- public async Task<List<SysCalendar>> GetAllList(Expression<Func<SysCalendar, bool>> expression)
- {
- var list = await Repository.GetAll()
- .WhereIf(expression != null, expression).ToListAsync();
- return list;
- }
- public async Task<List<SysCalendar>> GetNotStartList()
- {
- var list = await Repository.GetAll().Where(a => a.Start == null).ToListAsync();
- return list;
- }
- public Task<SysCalendar> GetById(int id)
- {
- return Repository.FirstOrDefaultAsync(a => a.Id == id);
- }
- public async Task<SysCalendar> Create(SysCalendar info)
- {
- var id = await Repository.InsertAndGetIdAsync(info);
- info.Id = id;
- await CheckBackgroundJob(info);
- return info;
- }
- public async Task<SysCalendar> Update(SysCalendar info)
- {
- await Repository.UpdateAsync(info);
- await CheckBackgroundJob(info);
- return info;
- }
- private async Task CheckBackgroundJob(SysCalendar info)
- {
- BackgroundJobInfo job = null;
- if (info.JobId.NotEmpty() && long.TryParse(info.JobId, out var jobId))
- {
- job = await JobRepository.FirstOrDefaultAsync(a => a.Id == jobId);
- }
- if (info.IsNotify && AbpSession.UserId.HasValue && info.Start.HasValue)
- {
- var delay = info.Start - Clock.Now;
- if (delay.Value.Milliseconds > 0)
- {
- if (job == null)
- {
- info.JobId = await AddBackgroundJob(info.Id, delay.Value);
- }
- else
- {
- job.NextTryTime = info.Start.Value;
- await JobRepository.UpdateAsync(job);
- return;
- }
- }
- }
- else if (job != null)
- {
- await BackgroundJobManager.DeleteAsync(job.Id + "");
- }
- info.JobId = null;
- await Repository.UpdateAsync(info);
- }
- private async Task<string> AddBackgroundJob(int id, TimeSpan delay)
- {
- var jobIdStr = await BackgroundJobManager.EnqueueAsync<CalendarNotifyBackgroundJob, CalendarNotifyArg>(
- new CalendarNotifyArg()
- {
- CalendarId = id,
- UserId = AbpSession.UserId ?? 0
- }, delay: delay);
- return jobIdStr;
- }
- public async Task Delete(int id)
- {
- var info = await GetById(id);
- if (info.JobId.NotEmpty())
- {
- await BackgroundJobManager.DeleteAsync(info.JobId);
- }
- await Repository.DeleteAsync(info);
- }
- }
|