Files
parr_api/PARR.DAL/Services/Implementations/TemplateService.cs

86 lines
3.1 KiB
C#

using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using PARR.Constants;
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()
.Include(t => t.RobotConfigurations)
.FirstOrDefaultAsync(t => t.Name == name);
}
public IQueryable<Template> GetWithIncludes()
{
return Get()
.Include(h => h.Unit)
.ThenInclude(t => t!.UnitValues)
.ThenInclude(t => t.Value)
.Include(h => h.Unit)
.ThenInclude(t => t!.UnitValues)
.ThenInclude(t => t.Field)
.Include(a => a.Job)
.ThenInclude(t => t!.Tnk)
.ThenInclude(s => s!.Subprocess)
.ThenInclude(p => p!.Process)
.Include(t => t.Job)
.ThenInclude(t => t!.Group);
}
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,
LastRobotStatusUpdated = 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,
LastRobotStatusUpdated = null
}
};
return base.CreateAsync(obj);
}
}
}