using Church.Net.Utility; using System; using System.Threading.Tasks; using WebAPI.Logics; using WebAPI.Services.Interfaces; namespace WebAPI.Services.ScheduledTask { public class MorningPrayer : IScheduledTask { private readonly LineAutoBotService lineAutoBotService; private readonly ILoggingService loggingService; private readonly PastoralDomainLogic pastoralDomainLogic; private DateTime? nextRunningTime = null; public MorningPrayer( LineAutoBotService lineAutoBotService, ILoggingService loggingService, PastoralDomainLogic pastoralDomainLogic ) { this.lineAutoBotService = lineAutoBotService; this.loggingService = loggingService; this.pastoralDomainLogic = pastoralDomainLogic; this.SetNextRunningTime(); } public string Description => "Sent out Ark Morning Prayer"; public bool CheckTime(DateTime time) { if(nextRunningTime == null) { this.SetNextRunningTime(); return true; } return time >= nextRunningTime.Value; } public Task RunTask() { SetNextRunningTime(); return Task.Run(async () => { var list = pastoralDomainLogic.GetAll(p => p.Type == Church.Net.Entity.DomainType.CellGroup); foreach (var group in list) { await lineAutoBotService.PushCommandMessage(group, "#pray"); } return true; }); } private void SetNextRunningTime() { nextRunningTime = DateTimeHelper.Today().AddDays(1).AddHours(8); loggingService.Log($"Scheduled Task {this.Description}", nextRunningTime.Value); } } }