feat(api,core,dal): Группы работ - управление автокнотролем
This commit is contained in:
@@ -36,13 +36,13 @@ namespace PARR.API.Controllers.V1
|
||||
private readonly ILogger<JobController> logger;
|
||||
private readonly IMapper mapper;
|
||||
private readonly IUriService uriService;
|
||||
private readonly IJobGroupRepository groupService;
|
||||
private readonly IJobRepository jobService;
|
||||
private readonly IEsppSchTypeConfigRepository esppConfigService;
|
||||
//private readonly IValidator<JobGroupRequest> validator;
|
||||
private readonly IJobGroupTypeRepository jobGroupTypeService;
|
||||
private readonly IMatchingStatusService matchingStatusService;
|
||||
private readonly IScheduleResponseAreaTimeOffsetRepository scheduleResponseAreaTimeOffsetService;
|
||||
private readonly IJobGroupRepository _groupRepository;
|
||||
private readonly IJobRepository _jobRepository;
|
||||
private readonly IEsppSchTypeConfigRepository _esppConfigRepository;
|
||||
private readonly IJobGroupTypeRepository _jobGroupTypeRepository;
|
||||
private readonly IMatchingStatusService _matchingStatusRepository;
|
||||
private readonly IScheduleResponseAreaTimeOffsetRepository _scheduleResponseAreaTimeOffsetRepository;
|
||||
private readonly IJobAutoControlRepository _jobAutoControlRepository;
|
||||
private readonly IRabbitService mqService;
|
||||
private readonly MqSettings mqSettings;
|
||||
|
||||
@@ -50,13 +50,13 @@ namespace PARR.API.Controllers.V1
|
||||
ILogger<JobController> logger,
|
||||
IMapper mapper,
|
||||
IUriService uriService,
|
||||
IJobGroupRepository groupService,
|
||||
IJobRepository jobService,
|
||||
IEsppSchTypeConfigRepository esppConfigService,
|
||||
//IValidator<JobGroupRequest> validator,
|
||||
IJobGroupTypeRepository jobGroupTypeService,
|
||||
IMatchingStatusService matchingStatusService,
|
||||
IScheduleResponseAreaTimeOffsetRepository scheduleResponseAreaTimeOffsetService,
|
||||
IJobGroupRepository groupRepository,
|
||||
IJobRepository jobRepository,
|
||||
IEsppSchTypeConfigRepository esppConfigRepository,
|
||||
IJobGroupTypeRepository jobGroupTypeRepository,
|
||||
IMatchingStatusService matchingStatusRepository,
|
||||
IScheduleResponseAreaTimeOffsetRepository scheduleResponseAreaTimeOffsetRepository,
|
||||
IJobAutoControlRepository jobAutoControlRepository,
|
||||
IRabbitService mqService,
|
||||
MqSettings mqSettings
|
||||
)
|
||||
@@ -64,20 +64,20 @@ namespace PARR.API.Controllers.V1
|
||||
this.logger = logger;
|
||||
this.mapper = mapper;
|
||||
this.uriService = uriService;
|
||||
this.groupService = groupService;
|
||||
this.jobService = jobService;
|
||||
this.esppConfigService = esppConfigService;
|
||||
//this.validator = validator;
|
||||
this.jobGroupTypeService = jobGroupTypeService;
|
||||
this.matchingStatusService = matchingStatusService;
|
||||
this.scheduleResponseAreaTimeOffsetService = scheduleResponseAreaTimeOffsetService;
|
||||
_groupRepository = groupRepository;
|
||||
_jobRepository = jobRepository;
|
||||
_esppConfigRepository = esppConfigRepository;
|
||||
_jobGroupTypeRepository = jobGroupTypeRepository;
|
||||
_matchingStatusRepository = matchingStatusRepository;
|
||||
_scheduleResponseAreaTimeOffsetRepository = scheduleResponseAreaTimeOffsetRepository;
|
||||
_jobAutoControlRepository = jobAutoControlRepository;
|
||||
this.mqService = mqService;
|
||||
this.mqSettings = mqSettings;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Получить список групп заданий на выполнение работ постранично
|
||||
/// Получить список групп работ постранично
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[HttpGet(ApiRoutes.JobGroup.GetAll)]
|
||||
@@ -85,11 +85,12 @@ namespace PARR.API.Controllers.V1
|
||||
{
|
||||
var paginationFilter = mapper.Map<PaginationFilter>(paginationQuery);
|
||||
|
||||
IQueryable<JobGroup> query = groupService.Get()
|
||||
IQueryable<JobGroup> query = _groupRepository.Get()
|
||||
.Include(t => t.GroupType)
|
||||
.Include(t => t.GroupingUnitField)
|
||||
.Include(t => t.ScheduleExcludeType)
|
||||
.Include(t => t.ScheduleExcludeTypeCalendar);
|
||||
.Include(t => t.ScheduleExcludeTypeCalendar)
|
||||
.Include(t => t.AutoControl);
|
||||
|
||||
query = query.OrderBy(t => t.GroupName);
|
||||
|
||||
@@ -104,7 +105,7 @@ namespace PARR.API.Controllers.V1
|
||||
.Include(t => t.Jobs).ThenInclude(t => t.Tnk)
|
||||
.Include(t => t.DistributionConfig).ThenInclude(t => t.DistributionPeriod);
|
||||
|
||||
var jobGroups = await groupService.GetPage(query, paginationFilter).ToListAsync();
|
||||
var jobGroups = await _groupRepository.GetPage(query, paginationFilter).ToListAsync();
|
||||
|
||||
if (!jobGroups.Any())
|
||||
return NoContent();
|
||||
@@ -122,20 +123,21 @@ namespace PARR.API.Controllers.V1
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Получить группу заданий на выполнение работ по id
|
||||
/// Получить группу работ по id
|
||||
/// </summary>
|
||||
/// <param name="id"></param>
|
||||
/// <returns></returns>
|
||||
[HttpGet(ApiRoutes.JobGroup.Get)]
|
||||
public async Task<IActionResult> GetById([FromRoute] Guid id)
|
||||
{
|
||||
var jobGroup = await groupService.Get()
|
||||
var jobGroup = await _groupRepository.Get()
|
||||
.Include(t => t.Jobs).ThenInclude(t => t.Tnk)
|
||||
.Include(t => t.GroupType)
|
||||
.Include(t => t.GroupingUnitField)
|
||||
.Include(t => t.ScheduleExcludeType)
|
||||
.Include(t => t.ScheduleExcludeTypeCalendar)
|
||||
.Include(t => t.DistributionConfig).ThenInclude(t => t.DistributionPeriod)
|
||||
.Include(t => t.AutoControl)
|
||||
.FirstOrDefaultAsync(t => t.Id == id);
|
||||
|
||||
if (jobGroup == null)
|
||||
@@ -149,18 +151,13 @@ namespace PARR.API.Controllers.V1
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Создать группу заданий на выполнение работ (JobGroup)
|
||||
/// Создать группу работ (JobGroup)
|
||||
/// </summary>
|
||||
/// <param name="request"></param>
|
||||
/// <returns></returns>
|
||||
[HttpPost(ApiRoutes.JobGroup.Create)]
|
||||
public async Task<IActionResult> Create([FromBody] JobGroupRequest request)
|
||||
{
|
||||
//var resultValidate = await validator.ValidateAsync(request);
|
||||
|
||||
//if (!resultValidate.IsValid)
|
||||
// return BadRequest(new Response(resultValidate.Errors));
|
||||
|
||||
var jobGroup = new JobGroup
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
@@ -199,6 +196,22 @@ namespace PARR.API.Controllers.V1
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Настройки автоконтроля
|
||||
|
||||
// Корректность проверяется в валидаторе
|
||||
if (request.AutoControl != null)
|
||||
{
|
||||
jobGroup.AutoControl = new JobGroupAutoControl
|
||||
{
|
||||
InitUsedScheduleState = request.AutoControl.InitUsedScheduleState,
|
||||
InitUsedTemplateState = request.AutoControl.InitUsedTemplateState,
|
||||
IsEnable = request.AutoControl.IsEnable,
|
||||
JobGroupId = jobGroup.Id
|
||||
};
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
//Добавляем настройки планировщика
|
||||
request.Schedule.ForEach(item =>
|
||||
{
|
||||
@@ -210,18 +223,19 @@ namespace PARR.API.Controllers.V1
|
||||
});
|
||||
});
|
||||
|
||||
if (!await groupService.CreateAsync(jobGroup) || !await groupService.CommitAsync())
|
||||
if (!await _groupRepository.CreateAsync(jobGroup) || !await _groupRepository.CommitAsync())
|
||||
return BadRequest(new Response(false, new List<ErrorModel> { new ErrorModel { Message = "Ошибка при создании группы заданий на выполнение работ" } }));
|
||||
|
||||
logger.LogInformation($"Пользователь {User.Identity?.Name} добавил группу заданий на выполнение работ: {jobGroup.Id}, {jobGroup.GroupName}, {jobGroup.ShortDescription}");
|
||||
|
||||
|
||||
var createdJobGroup = await groupService.Get().Include(t => t.Jobs).ThenInclude(t => t.Tnk)
|
||||
var createdJobGroup = await _groupRepository.Get().Include(t => t.Jobs).ThenInclude(t => t.Tnk)
|
||||
.Include(t => t.GroupType)
|
||||
.Include(t => t.GroupingUnitField)
|
||||
.Include(t => t.ScheduleExcludeType)
|
||||
.Include(t => t.ScheduleExcludeTypeCalendar)
|
||||
.Include(t => t.DistributionConfig).ThenInclude(t => t.DistributionPeriod)
|
||||
.Include(t => t.AutoControl)
|
||||
.FirstAsync(t => t.Id == jobGroup.Id);
|
||||
|
||||
var locationUri = uriService.GetUri(ApiRoutes.JobGroup.Get, ApiRoutes.JobGroup.getParam, createdJobGroup.Id);
|
||||
@@ -234,7 +248,7 @@ namespace PARR.API.Controllers.V1
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Обновить группу заданий на выполнение работ (JobGroup)
|
||||
/// Обновить группу работ (JobGroup)
|
||||
/// </summary>
|
||||
/// <param name="id"></param>
|
||||
/// <param name="request"></param>
|
||||
@@ -242,15 +256,11 @@ namespace PARR.API.Controllers.V1
|
||||
[HttpPut(ApiRoutes.JobGroup.Update)]
|
||||
public async Task<IActionResult> Update([FromRoute] Guid id, [FromBody] JobGroupRequest request)
|
||||
{
|
||||
//var resultValidate = await validator.ValidateAsync(request);
|
||||
|
||||
//if (!resultValidate.IsValid)
|
||||
// return BadRequest(new Response(resultValidate.Errors));
|
||||
|
||||
var orig = await groupService.Get()
|
||||
var orig = await _groupRepository.Get()
|
||||
.Include(t => t.Jobs)
|
||||
.ThenInclude(t => t.Tnk)
|
||||
.Include(t => t.EsppSchValues)
|
||||
.Include(t => t.AutoControl)
|
||||
.Include(t => t.DistributionConfig).ThenInclude(t => t.DistributionPeriod)
|
||||
.FirstOrDefaultAsync(t => t.Id == id);
|
||||
|
||||
@@ -309,12 +319,50 @@ namespace PARR.API.Controllers.V1
|
||||
// удаляем настройки распределения если они были
|
||||
if (orig.DistributionConfig != null)
|
||||
{
|
||||
groupService.DeleteDistributionConfig(orig.DistributionConfig);
|
||||
_groupRepository.DeleteDistributionConfig(orig.DistributionConfig);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Настройки автоконтроля
|
||||
|
||||
// валидатор проверяет корректность
|
||||
if (request.AutoControl != null)
|
||||
{
|
||||
// автоконтролем управляет JobGroup
|
||||
if (orig.AutoControl != null)
|
||||
{
|
||||
// обновляем
|
||||
orig.AutoControl.IsEnable = request.AutoControl.IsEnable;
|
||||
orig.AutoControl.InitUsedScheduleState = request.AutoControl.InitUsedScheduleState;
|
||||
orig.AutoControl.InitUsedTemplateState = request.AutoControl.InitUsedTemplateState;
|
||||
// удалить настройки автоконтроля для job
|
||||
await RemoveJobAutoControlSettingsAsync(orig.Id);
|
||||
}
|
||||
else
|
||||
{
|
||||
// создаем новую запись
|
||||
orig.AutoControl = new JobGroupAutoControl
|
||||
{
|
||||
InitUsedScheduleState = request.AutoControl.InitUsedScheduleState,
|
||||
InitUsedTemplateState = request.AutoControl.InitUsedTemplateState,
|
||||
IsEnable = request.AutoControl.IsEnable,
|
||||
JobGroupId = orig.Id
|
||||
};
|
||||
// удалить настройки автоконтроля для job
|
||||
await RemoveJobAutoControlSettingsAsync(orig.Id);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// автоконтролем управляет каждый job отдельно
|
||||
// удаляем настройки, если они были
|
||||
orig.AutoControl = null;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
//обновляем планировщик
|
||||
orig.EsppSchValues.Clear();
|
||||
request.Schedule.ForEach(item =>
|
||||
@@ -327,7 +375,7 @@ namespace PARR.API.Controllers.V1
|
||||
});
|
||||
});
|
||||
|
||||
if (!await groupService.CommitAsync())
|
||||
if (!await _groupRepository.CommitAsync())
|
||||
return BadRequest(new Response(false, new List<ErrorModel> { new ErrorModel { Message = "Ошибка при изменении группы заданий на выполнение работ." } }));
|
||||
|
||||
logger.LogInformation($"Пользователь {User.Identity?.Name} обновил группу заданий на выполнение работ: {orig.Id}," +
|
||||
@@ -355,7 +403,7 @@ namespace PARR.API.Controllers.V1
|
||||
#endregion
|
||||
|
||||
|
||||
var updatedJobGroup = await groupService.Get()
|
||||
var updatedJobGroup = await _groupRepository.Get()
|
||||
.Include(t => t.Jobs)
|
||||
.ThenInclude(t => t.Tnk)
|
||||
.Include(t => t.GroupType)
|
||||
@@ -363,6 +411,7 @@ namespace PARR.API.Controllers.V1
|
||||
.Include(t => t.ScheduleExcludeType)
|
||||
.Include(t => t.ScheduleExcludeTypeCalendar)
|
||||
.Include(t => t.DistributionConfig).ThenInclude(t => t.DistributionPeriod)
|
||||
.Include(t => t.AutoControl)
|
||||
.FirstAsync(t => t.Id == orig.Id);
|
||||
|
||||
var response = mapper.Map<JobGroupResponse>(updatedJobGroup);
|
||||
@@ -373,14 +422,14 @@ namespace PARR.API.Controllers.V1
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Удалить группу заданий на выполнение работ (только если нет связанных заданий)
|
||||
/// Удалить группу работ (только если нет связанных работ)
|
||||
/// </summary>
|
||||
/// <param name="id"></param>
|
||||
/// <returns></returns>
|
||||
[HttpDelete(ApiRoutes.JobGroup.Delete)]
|
||||
public async Task<IActionResult> Delete([FromRoute] Guid id)
|
||||
{
|
||||
var jobGroup = await groupService.Get()
|
||||
var jobGroup = await _groupRepository.Get()
|
||||
.FirstOrDefaultAsync(t => t.Id == id);
|
||||
|
||||
if (jobGroup == null)
|
||||
@@ -388,13 +437,13 @@ namespace PARR.API.Controllers.V1
|
||||
Message = $"Ошибка при удалении группы заданий на выполнение работ. Не найдена группа заданий на выполнение работ Id: {id}"
|
||||
} }));
|
||||
|
||||
var jobCount = await jobService.Get().CountAsync(t => t.GroupId == id);
|
||||
var jobCount = await _jobRepository.Get().CountAsync(t => t.GroupId == id);
|
||||
if (jobCount > 0)
|
||||
return BadRequest(new Response(false, new List<ErrorModel> { new ErrorModel {
|
||||
Message = $"Ошибка при удалении группы заданий на выполнение работ. С данным группой связаны задания: {jobCount} шт."
|
||||
} }));
|
||||
|
||||
if (!groupService.Delete(jobGroup) || !await groupService.CommitAsync())
|
||||
if (!_groupRepository.Delete(jobGroup) || !await _groupRepository.CommitAsync())
|
||||
return BadRequest(new Response(false, new List<ErrorModel> { new ErrorModel {
|
||||
Message = $"Ошибка при удалении группы заданий на выполнение работ"
|
||||
} }));
|
||||
@@ -408,6 +457,22 @@ namespace PARR.API.Controllers.V1
|
||||
return NoContent();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Удалить настройки автоконтроля для связанных Job
|
||||
/// </summary>
|
||||
/// <param name="jobGroupId"></param>
|
||||
/// <returns></returns>
|
||||
private async Task RemoveJobAutoControlSettingsAsync(Guid jobGroupId)
|
||||
{
|
||||
var jobAutoControlsToRemove = await _jobAutoControlRepository.Get()
|
||||
.Where(t => t.Job!.GroupId == jobGroupId)
|
||||
.ToListAsync();
|
||||
|
||||
if (!jobAutoControlsToRemove.Any())
|
||||
return;
|
||||
|
||||
_jobAutoControlRepository.RemoveRange(jobAutoControlsToRemove);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Проверка, были ли изменения в расписании
|
||||
@@ -471,7 +536,7 @@ namespace PARR.API.Controllers.V1
|
||||
/// <returns></returns>
|
||||
private async Task AppendMissingDataAsync(JobGroupResponse jobGroupResponse)
|
||||
{
|
||||
var schedule = await esppConfigService.GetEsppScheduleDtoAsync(jobGroupResponse.Id);
|
||||
var schedule = await _esppConfigRepository.GetEsppScheduleDtoAsync(jobGroupResponse.Id);
|
||||
|
||||
if (schedule == null)
|
||||
{
|
||||
@@ -482,14 +547,14 @@ namespace PARR.API.Controllers.V1
|
||||
var scheduleResponse = new JobGroupScheduleResponse
|
||||
{
|
||||
//Timezone = settingsFromDb.ScheduleTimezone,
|
||||
Timezone = scheduleResponseAreaTimeOffsetService.GetDefault.EsppValue,
|
||||
Timezone = _scheduleResponseAreaTimeOffsetRepository.GetDefault.EsppValue,
|
||||
TypeSchedule = mapper.Map<EsppScheduleTypeScheduleResponse>(schedule.TypeSchedule),
|
||||
Values = mapper.Map<List<EsppScheduleValResponse>>(schedule.Values).OrderBy(t => t.Order).ToList()
|
||||
};
|
||||
|
||||
jobGroupResponse.Schedule = scheduleResponse;
|
||||
|
||||
jobGroupResponse.JobsCount = await jobService.Get().CountAsync(t => t.GroupId == jobGroupResponse.Id);
|
||||
jobGroupResponse.JobsCount = await _jobRepository.Get().CountAsync(t => t.GroupId == jobGroupResponse.Id);
|
||||
|
||||
jobGroupResponse.MatchingStatus = await GetMatchingStatusAsync(jobGroupResponse.Id);
|
||||
}
|
||||
@@ -506,7 +571,7 @@ namespace PARR.API.Controllers.V1
|
||||
return null;
|
||||
|
||||
// Если есть значение, смотрим, групповой ли тип работ, и если нет, то вернем null
|
||||
var groupingType = await jobGroupTypeService.Get().FirstAsync(t => t.Code == JobGroupTypesEnum.Group);
|
||||
var groupingType = await _jobGroupTypeRepository.Get().FirstAsync(t => t.Code == JobGroupTypesEnum.Group);
|
||||
if (request.GroupTypeId == groupingType.Id)
|
||||
{
|
||||
// это групповой тип работ, все ок
|
||||
@@ -534,7 +599,7 @@ namespace PARR.API.Controllers.V1
|
||||
return request.GroupingUnitFieldId;
|
||||
|
||||
|
||||
var groupingType = await jobGroupTypeService.Get().FirstAsync(t => t.Code == JobGroupTypesEnum.Group);
|
||||
var groupingType = await _jobGroupTypeRepository.Get().FirstAsync(t => t.Code == JobGroupTypesEnum.Group);
|
||||
|
||||
if (request.GroupTypeId == groupingType.Id)
|
||||
{
|
||||
@@ -558,7 +623,7 @@ namespace PARR.API.Controllers.V1
|
||||
/// <returns></returns>
|
||||
private async Task<MatchingStatusResponse?> GetMatchingStatusAsync(Guid jobGroupId)
|
||||
{
|
||||
var statusMatching = await matchingStatusService.GetStatusAsync(jobGroupId, SyncTaskEntityTypeEnum.JobGroup);
|
||||
var statusMatching = await _matchingStatusRepository.GetStatusAsync(jobGroupId, SyncTaskEntityTypeEnum.JobGroup);
|
||||
|
||||
return mapper.Map<MatchingStatusResponse>(statusMatching);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user