12345678910111213141516171819202122232425262728293031323334353637383940414243444546 |
- using System;
- using System.Collections.Generic;
- using System.Reflection;
- using System.Text;
- using System.Threading.Tasks;
- using TEAMModelOS.SDK.Helper.Common.CollectionHelper;
- using TEAMModelOS.SDK.DI;
- using TEAMModelOS.Service.Services.Learn.Interfaces;
- namespace TEAMModelOS.Service.Services.Learn.Implements
- {
- public class TimerWorkService: ITimerWorkService
- {
- private readonly AzureCosmosFactory _cosmos;
- public TimerWorkService(AzureCosmosFactory cosmos)
- {
- _cosmos = cosmos;
- }
- public void TimerWork<T>(long startTime, int status , Dictionary<string, object> dict) where T : ID
- {
- System.Timers.Timer aTimer = new System.Timers.Timer();
- // Create a timer with a two second interval.
- long time = Math.Abs(startTime - new DateTimeOffset(DateTime.UtcNow).ToUnixTimeMilliseconds());
- aTimer = new System.Timers.Timer(time);
- // Hook up the Elapsed event for the timer.
- aTimer.Elapsed += async (sender, e) => await OnTimedEventAsync<T>(_cosmos,status, dict);
- aTimer.AutoReset = false;
- aTimer.Enabled = true;
- }
- public static async Task OnTimedEventAsync<T>(AzureCosmosFactory _cosmos, int status, Dictionary<string, object> dict) where T : ID
- {
- List<T> homeWorks = await _cosmos.FindByDict<T>(dict);
- if (homeWorks.IsNotEmpty())
- {
- PropertyInfo propertyInfo = homeWorks[0].GetType().GetProperty("status");
- for (int i = 0; i < homeWorks.Count; i++)
- propertyInfo.SetValue(homeWorks[i], status);
- await _cosmos.SaveOrUpdateAll<T>(homeWorks);
- }
- }
- }
- }
|