Files
parr_api/PARR.DAL/TransformServices/EsppScheduleTransformService.cs

140 lines
5.1 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Microsoft.Extensions.Logging;
using PARR.DAL.Contracts;
using PARR.DAL.DomainModels;
using PARR.DAL.Services.Interfaces;
using System.ComponentModel;
using System.Net.Http.Headers;
using System.Runtime.CompilerServices;
namespace PARR.DAL.TransformServices
{
internal class EsppScheduleTransformService : IEsppScheduleTransformService
{
private readonly IEsppSchTypeConfigService esppSchTypeConfigService;
private readonly ILogger<EsppScheduleTransformService> logger;
public EsppScheduleTransformService(IEsppSchTypeConfigService esppSchTypeConfigService, ILogger<EsppScheduleTransformService> logger)
{
this.esppSchTypeConfigService = esppSchTypeConfigService;
this.logger = logger;
}
public DateTimeOffset GetNextDate(EsppScheduleDto esppSchedule, DateTimeOffset lastRun)
{
//todo:
//если больше текущего вернуть lastRun. Это и есть следующая дата
if (lastRun > DateTimeOffset.UtcNow)
return lastRun;
switch (esppSchedule.TypeSchedule.Id)
{
case (int)EsppSchTypeScheduleEnum.Annually2:
return GetNextDateAnnually2(esppSchedule.Values, lastRun);
}
return lastRun;//todo убрать lastrun должен возвращать реальный результат
}
public List<DateTimeOffset> GetNextSchedule(EsppScheduleDto esppSchedule, DateTimeOffset lastRun)
{
// расписание на оставшееся на сегодня время. его так мало...
var schedules = new List<DateTimeOffset>();
//todo:
schedules.Add(lastRun);
return schedules.OrderBy(t => t).ToList();
}
public async Task<DateTimeOffset> GetNextDateAsync(Guid applicationInWorkId, DateTimeOffset lastRun)
{
return GetNextDate(await GetEsppSchedule(applicationInWorkId), lastRun);
}
public async Task<List<DateTimeOffset>> GetNextScheduleAsync(Guid applicationInWorkId, DateTimeOffset lastRun)
{
return GetNextSchedule(await GetEsppSchedule(applicationInWorkId), lastRun);
}
private async Task<EsppScheduleDto> GetEsppSchedule(Guid applicationInWorkId)
{
var esppSchedule = await esppSchTypeConfigService.GetEsppScheduleDtoAsync(applicationInWorkId);
if (esppSchedule == null)
{
logger.LogError($"Не найдено расписание в БД, applicationInWorkId: {applicationInWorkId}");
throw new Exception($"Не найдено расписание в БД, applicationInWorkId: {applicationInWorkId}");
}
return esppSchedule;
}
private DateTimeOffset GetNextDateAnnually2(List<EsppScheduleValDto> values, DateTimeOffset lastRun)
{
var monthDict = new Dictionary<string, int>();
monthDict.Add("Января", 1);
monthDict.Add("Февраля", 2);
monthDict.Add("Марта", 3);
monthDict.Add("Апреля", 4);
monthDict.Add("Мая", 5);
monthDict.Add("Июня", 6);
monthDict.Add("Июля", 7);
monthDict.Add("Августа", 8);
monthDict.Add("Сентября", 9);
monthDict.Add("Октября", 10);
monthDict.Add("Ноября", 11);
monthDict.Add("Декабря", 12);
var orderDict = new Dictionary<string, int>();
orderDict.Add("Первый", 1);
orderDict.Add("Второй", 2);
orderDict.Add("Третий", 3);
orderDict.Add("Четвертый", 4);
orderDict.Add("Последний", 5);
var dwDict = new Dictionary<string, int>();
dwDict.Add("Понедельник", 1);
dwDict.Add("Вторник", 2);
dwDict.Add("Среда", 3);
dwDict.Add("Четверг", 4);
dwDict.Add("Пятница", 5);
dwDict.Add("Суббота", 6);
dwDict.Add("Воскресенье", 0);
var monthNum = monthDict[values[2].Value.Value];
var orderNum = orderDict[values[0].Value.Value];
var dwNum = dwDict[values[1].Value.Value];
var calcDay = new DateTimeOffset(lastRun.Year + 1, monthNum, 1, lastRun.Hour, lastRun.Minute, lastRun.Second, lastRun.Offset);
if (((int)calcDay.DayOfWeek) != dwNum)
{
var daysOffset = dwNum - ((int)calcDay.DayOfWeek);
if (daysOffset < 0)
daysOffset = 7 + daysOffset;
calcDay = calcDay.AddDays(daysOffset);
}
var daysList = new List<DateTimeOffset>();
while (calcDay.Month == monthNum)
{
daysList.Add(calcDay);
calcDay = calcDay.AddDays(7);
}
if (orderNum == 5)
return daysList.Last();
return daysList[orderNum];
}
}
}