140 lines
5.4 KiB
C#
140 lines
5.4 KiB
C#
using Microsoft.EntityFrameworkCore;
|
||
using Microsoft.Extensions.Logging;
|
||
using PARR.DAL.Context;
|
||
using PARR.DAL.Contracts;
|
||
using PARR.DAL.Models;
|
||
using PARR.DAL.Services.Abstracts;
|
||
using PARR.DAL.Services.Interfaces;
|
||
|
||
namespace PARR.DAL.Services.Implementations
|
||
{
|
||
internal class TemplateService : BaseService<Template>, ITemplateService
|
||
{
|
||
private readonly DataContext dataContext;
|
||
private readonly ILogger<TemplateService> logger;
|
||
|
||
protected override DbSet<Template> EntitySet => dataContext.Templates;
|
||
|
||
protected override DataContext EntitiContext => dataContext;
|
||
|
||
public TemplateService(DataContext dataContext, ILogger<TemplateService> logger) : base(logger)
|
||
{
|
||
this.dataContext = dataContext;
|
||
this.logger = logger;
|
||
}
|
||
|
||
public async Task<Template?> GetTemplateByNameAsync(string name)
|
||
{
|
||
return await GetWithIncludes().FirstOrDefaultAsync(t => t.Name == name);
|
||
}
|
||
|
||
public IQueryable<Template> GetWithIncludes()
|
||
{
|
||
return Get()
|
||
.Include(h => h.Host)
|
||
.ThenInclude(t => t!.ResponseArea)
|
||
.Include(h => h.Host)
|
||
.ThenInclude(t => t!.EkStatus)
|
||
.Include(r => r.RobotStatus)
|
||
.Include(a => a.ApplicationsInWork)
|
||
.ThenInclude(w => w!.Work)
|
||
.ThenInclude(t => t!.Tnk)
|
||
.ThenInclude(s => s!.Subprocess)
|
||
.ThenInclude(p => p!.Process);
|
||
}
|
||
|
||
//TODO: удалить этот метод
|
||
#region delete
|
||
public async Task CheckAndSetErrorRobotStatusAsync(int robotAttemptsNumber, TimeSpan robotWaitTime)
|
||
{
|
||
//Ищем `RobotStatusCode` = 22 и `RobotLastStatusUpdated` истекло и `RobotAttemptsNumber` >= допустимого значения из настроек,
|
||
//ставим всем этим записям `RobotStatusCode`= 33
|
||
|
||
var endDate = DateTimeOffset.UtcNow.Add(-robotWaitTime);
|
||
|
||
var templates = await EntitySet.Where(t =>
|
||
t.RobotStatusCode == (int)RobotStatusEnum.InProgress
|
||
&& t.RobotAttemptsNumber >= robotAttemptsNumber
|
||
&& t.RobotLastStatusUpdated <= endDate
|
||
)
|
||
.ToListAsync();
|
||
|
||
if (!templates.Any())
|
||
return;
|
||
|
||
foreach (var template in templates)
|
||
{
|
||
template.RobotStatusCode = (int)RobotStatusEnum.Error;
|
||
template.DateModified = DateTimeOffset.UtcNow;
|
||
logger.LogInformation($"Устанавливаю RobotStatus: {RobotStatusEnum.Error} для шаблона {template.Name}");
|
||
}
|
||
|
||
|
||
var result = await CommitAsync();
|
||
|
||
if (!result)
|
||
logger.LogError($"Ошибка при сохранении изменений RobotStatus у шаблонов на {RobotStatusEnum.Error}");
|
||
|
||
}
|
||
#endregion
|
||
|
||
public void ChangeRobotStatus(RobotStatusEnum status, ref Template template)
|
||
{
|
||
template.RobotStatusCode = (int)status;
|
||
|
||
switch (status)
|
||
{
|
||
case RobotStatusEnum.InProgress:
|
||
template.RobotAttemptsNumber++;
|
||
template.RobotLastStatusUpdated = DateTimeOffset.UtcNow;
|
||
break;
|
||
//case RobotStatusEnum.Error:
|
||
// break;
|
||
case RobotStatusEnum.Complete:
|
||
template.RobotLastStatusUpdated = DateTimeOffset.UtcNow;
|
||
break;
|
||
case RobotStatusEnum.Wait:
|
||
template.RobotLastStatusUpdated = null;
|
||
template.RobotAttemptsNumber = 0;
|
||
break;
|
||
default:
|
||
break;
|
||
}
|
||
}
|
||
|
||
public override Task<bool> CreateAsync(Template obj)
|
||
{
|
||
// добавление роботов для шаблона
|
||
obj.RobotConfigurations = new List<RobotConfiguration>
|
||
{
|
||
// робот по управлению шаблоном
|
||
new RobotConfiguration
|
||
{
|
||
Id = Guid.NewGuid(),
|
||
DateCreated = DateTimeOffset.UtcNow,
|
||
TemplateId = obj.Id,
|
||
RobotCode = (int)RobotsEnum.TemplateOrder,
|
||
TaskStatusCode = (int)TaskStatusEnum.Creating,
|
||
RobotStatusCode = (int)RobotStatusEnum.Wait,
|
||
AttemptsNumber = 0,
|
||
LastStatusUpdated = null
|
||
},
|
||
// робот по управлениею расписанием
|
||
new RobotConfiguration
|
||
{
|
||
Id = Guid.NewGuid(),
|
||
DateCreated = DateTimeOffset.UtcNow,
|
||
TemplateId = obj.Id,
|
||
RobotCode = (int)RobotsEnum.ScheduleOrder,
|
||
TaskStatusCode = (int)TaskStatusEnum.Creating,
|
||
RobotStatusCode = (int)RobotStatusEnum.Wait,
|
||
AttemptsNumber = 0,
|
||
LastStatusUpdated = null
|
||
}
|
||
};
|
||
|
||
return base.CreateAsync(obj);
|
||
}
|
||
}
|
||
}
|