2025-06-18 09:58:23 +10:00
using Elastic.CommonSchema ;
2024-07-01 12:09:01 +10:00
using Microsoft.EntityFrameworkCore ;
2023-11-30 09:11:07 +10:00
using PARR.BLL.Services.Interfaces ;
2024-02-29 13:41:44 +10:00
using PARR.Constants ;
2025-06-18 09:58:23 +10:00
using PARR.DAL.DomainServices ;
2024-07-01 11:32:19 +10:00
using PARR.DAL.Services.Interfaces ;
2025-06-18 15:22:28 +10:00
using PARR.DAL.Services.Interfaces.Job ;
2025-05-27 11:32:05 +10:00
using PARR.DAL.Services.Interfaces.Unit ;
2023-10-19 15:59:57 +10:00
using PARR.EsppApi ;
2023-11-28 15:33:59 +10:00
using PARR.EsppApi.Constants ;
2023-10-20 09:24:27 +10:00
using PARR.EsppApi.Models.Query ;
2024-07-01 16:56:59 +10:00
using System.Net.WebSockets ;
2023-10-19 15:59:57 +10:00
namespace PARR.Test
{
public class Worker : BackgroundService
{
private readonly ILogger < Worker > _logger ;
private readonly IEsppApiService esppApiService ;
2023-11-30 09:11:07 +10:00
private readonly IIntervalService intervalService ;
2024-06-28 16:42:53 +10:00
private readonly ICalendarService calendarService ;
2024-07-01 11:32:19 +10:00
private readonly IServiceProvider serviceProvider ;
2023-10-19 15:59:57 +10:00
2024-02-28 15:40:24 +10:00
public Worker (
ILogger < Worker > logger ,
IEsppApiService esppApiService ,
2024-06-28 16:42:53 +10:00
IIntervalService intervalService ,
2024-07-01 11:32:19 +10:00
ICalendarService calendarService ,
IServiceProvider serviceProvider
2024-02-28 15:40:24 +10:00
)
2023-10-19 15:59:57 +10:00
{
_logger = logger ;
this . esppApiService = esppApiService ;
2023-11-30 09:11:07 +10:00
this . intervalService = intervalService ;
2024-06-28 16:42:53 +10:00
this . calendarService = calendarService ;
2024-07-01 11:32:19 +10:00
this . serviceProvider = serviceProvider ;
2023-10-19 15:59:57 +10:00
}
protected override async Task ExecuteAsync ( CancellationToken stoppingToken )
{
2024-02-15 13:47:34 +10:00
#region test
2025-06-18 09:58:23 +10:00
using ( var scope = serviceProvider . CreateScope ( ) )
{
2025-06-18 15:22:28 +10:00
/ * var service = scope . ServiceProvider . GetService < ITemplateNameGeneratorService > ( ) ;
2025-06-18 09:58:23 +10:00
2025-06-18 15:22:28 +10:00
var ttt = await service . GetTemplateNameAsync ( Guid . Parse ( "ba05948b-b040-48cc-ab77-0accc9c223a0" ) , Guid . Parse ( "d4a86f24-6975-4614-98ab-431f26e3eb69" ) ) ; * /
//var service = scope.ServiceProvider.GetService<ITemplateService>();
//var templates = await service.Get()
// .Include(t => t.ApplicationsInWork)
// .ThenInclude(t=>t.Work)
// //.Select(s => new { s.Id, s.Host, s.HostId, s.JobId, s.UnitId })
// .Where(t => t.JobId == Guid.Parse("ba05948b-b040-48cc-ab77-0accc9c223a0")
// ).ToListAsync();
//var workNames = templates.Select(t=>t.ApplicationsInWork!.Work!.TemplateSuffix).Distinct();
//var jobService = scope.ServiceProvider.GetService<IJobService>();
//var jobs = await jobService!.Get().Where(t => workNames.Any(tt => tt == t.Name)).ToListAsync();
////var hostsName = templates.Select(t => t.Host!.Ek);
////var unitService = scope.ServiceProvider.GetService<IUnitService>();
////var units = await unitService.Get()
//// //.Select(s => new { s.Id, s.Name })
//// .Where(t => hostsName.Any(tt => tt == t.Name))
//// .ToListAsync();
//foreach (var t in templates) {
// var workName = t.ApplicationsInWork!.Work!.TemplateSuffix;
// //var unitId = units.FirstOrDefault(u => u.Name == hostName);
// var jobId = jobs.FirstOrDefault(t => t.Name == workName);
// if (jobId != null)
// t.JobId = jobId.Id;
//}
//await service.CommitAsync();
2025-06-18 09:58:23 +10:00
}
2024-02-15 13:47:34 +10:00
//var tName = "ДВС-ЭИТИ-ПТК-ПА Р Р __В Р Т -VCD-02-ДВ С __ПР О ЧЕ Е (РАБОТЫ)";
//var prefix = "ЭИТИ-ПТК-ПАРР";
////templateName.Contains(settingsFromDb.TemplatePrefixWithoutVariable, StringComparison.CurrentCultureIgnoreCase)
//var res = tName.Contains(prefix);
#endregion
2025-05-27 11:32:05 +10:00
#region unit
//using (var scope = serviceProvider.CreateScope())
//{
// var service = scope.ServiceProvider.GetService<IUnitService>();
// var unit = await service.Get().Include(t => t.Parents).Include(t => t.Childs).FirstOrDefaultAsync(t => t.Id == Guid.Parse("0f89e2ec-0e4d-4de6-b990-c55e8308accf"));
// var parent = unit?.Parents;
//}
#endregion
2024-06-28 16:42:53 +10:00
#region Calendar
2024-02-15 13:47:34 +10:00
2024-07-01 11:32:19 +10:00
//var testCalendar = new BLL.Services.Implementations.Test(calendarService);
2024-06-28 16:42:53 +10:00
2024-07-01 11:32:19 +10:00
//testCalendar.Ttt() ;
//using (var scope = serviceProvider.CreateScope())
//{
// var weekendDayService = scope.ServiceProvider.GetService<IWeekendDayService>();
// var workDays = calendarService.GetWorkDatesForPeriod(new DateOnly(2024, 07, 01), TimeSpan.Parse("30:00:00:00"), weekendDayService!.GetWeekends);
//}
2024-07-01 12:09:01 +10:00
//IDistributionPeriodService
//using (var scope = serviceProvider.CreateScope())
//{
2024-07-02 10:52:15 +10:00
// var service = scope.ServiceProvider.GetService<ICalendarService>();
// var weekEndService = scope.ServiceProvider.GetService<IWeekendDayService>();
2024-07-01 12:09:01 +10:00
2024-07-01 16:56:59 +10:00
2024-07-02 10:52:15 +10:00
// var aa = service!.GetWorkDatesForPeriod(new DateOnly(2024, 07, 01), DistributionPeriodTypeEnum.Year, "1", weekEndService!.GetWeekends);
2024-07-01 16:56:59 +10:00
2024-07-02 10:52:15 +10:00
// //var aaa = await service!.Get().ToListAsync();
2024-07-01 16:56:59 +10:00
2024-07-02 10:52:15 +10:00
// //foreach (var aaaItem in aaa)
// //{
2024-07-01 16:56:59 +10:00
2024-07-02 10:52:15 +10:00
// // var bbbb = aaaItem.AddDate;
// // var now = DateTimeOffset.UtcNow;
// // var newwww = now.AddMicroseconds(bbbb.);
// //}
2024-07-01 16:56:59 +10:00
2024-07-01 12:09:01 +10:00
//}
2024-06-28 16:42:53 +10:00
#endregion
2024-02-15 13:47:34 +10:00
2024-04-11 09:05:16 +10:00
2023-11-30 09:11:07 +10:00
////test interval
//await intervalService.IntervalInitAsync(async () =>
//{
// _logger.LogInformation("---Выполняю метод---");
// await Task.Delay(29*1000);
// _logger.LogInformation("===Выполнил метод===");
//}, new TimeSpan(0, 0, 30));
2023-10-19 15:59:57 +10:00
2024-02-28 15:40:24 +10:00
//await TestEsppApi();
2024-04-11 09:05:16 +10:00
//await StopDiscontinuedOrdersInEspp();
2023-10-19 15:59:57 +10:00
}
private async Task TestEsppApi ( )
{
2023-10-27 16:39:59 +10:00
//var result = await esppApiService.FindOrdersAsync(new FindOrdersQuery { DescriptionContains = "Полное", GenerateDateStart = new DateTime(2023, 10, 10) , GenerateDateEnd = new DateTime(2023, 10, 24) });
2024-02-05 16:05:02 +10:00
var recordId = "Н А Р 24-00009262" ;
2023-10-20 09:24:27 +10:00
2023-10-27 16:39:59 +10:00
var result = await esppApiService . FindOrderByRecordIdAsync ( recordId ) ;
if ( result . IsSuccess & & ( result . Data ! = null & & result . Data . Number . Any ( ) ) )
{
//var orders = result.Data;
2023-12-07 11:19:08 +10:00
//в работу
2024-02-01 16:49:18 +10:00
var inWorkResult = await esppApiService . SetStatusInWorkAsync ( recordId , "Регламентные работы в автоматическом режиме созданы и выполнены;" ) ; //"Решение...\nС новой строки"
2023-10-27 16:39:59 +10:00
if ( ! inWorkResult . IsSuccess )
{
2023-12-08 08:48:34 +10:00
_logger . LogError ( inWorkResult . Exception , $"Н е удалось взять в работу наряд {recordId}" ) ;
2023-10-27 16:39:59 +10:00
return ;
}
var tnk = new AddMtnkQuery { RecordId = recordId , JobOperation = "Н е учтена в перечне Т Н К " , Time = "00:05" } ;
2023-11-20 12:26:33 +10:00
var addMtnkResult = await esppApiService . AddMTnkAsync ( tnk ) ;
2023-10-27 16:39:59 +10:00
if ( ! addMtnkResult . IsSuccess )
{
_logger . LogError ( addMtnkResult . Exception , $"Н е удалось списать трудозатраты наряда {recordId}" ) ;
return ;
}
2023-12-07 11:19:08 +10:00
//выполнить
2024-02-01 16:49:18 +10:00
var resFinish = await esppApiService . SetStatusIsDoneAsync ( recordId , "Регламентные работы в автоматическом режиме созданы и выполнены;" , EsppOrderClosingCodeEnum . Auto ) ;
2023-10-27 16:39:59 +10:00
if ( ! resFinish . IsSuccess )
{
_logger . LogError ( resFinish . Exception , $"Н е удалось обновить статус наряда {recordId}" ) ;
return ;
}
}
else
2023-10-20 09:24:27 +10:00
{
2023-10-27 16:39:59 +10:00
_logger . LogWarning ( $"не найден наряд {recordId}" ) ;
2023-10-20 09:24:27 +10:00
}
2023-10-19 15:59:57 +10:00
}
2024-02-28 15:40:24 +10:00
private async Task StopDiscontinuedOrdersInEspp ( )
{
const string ResultMsg = "Сформирован ошибочно" ;
var searchQuery = new FindOrdersQuery
{
DescriptionContains = "ЭИТИ-ПТК-ПАРР" ,
StatusIs = "1-Направлен в группу" ,
GenerateDateStart = new DateTime ( 2024 , 02 , 26 , 0 , 0 , 0 ) ,
GenerateDateEnd = new DateTime ( 2024 , 02 , 29 , 0 , 0 , 0 )
} ;
var result = await esppApiService . FindOrdersAsync ( searchQuery ) ;
if ( result . IsSuccess & & result . Data ! = null )
{
foreach ( var item in result . Data )
{
2024-02-29 13:41:44 +10:00
if ( item . StatusEnum = = OrderStatusEnum . New )
2024-02-28 15:40:24 +10:00
{
await esppApiService . SetStatusInWorkAsync ( item . Number , ResultMsg ) ;
}
var newItem = await esppApiService . FindOrderByRecordIdAsync ( item . Number ) ;
if ( newItem . IsSuccess & & newItem . Data ! = null & & newItem . Data . StatusEnum = = OrderStatusEnum . InWork )
{
var tnk = new AddMtnkQuery { RecordId = newItem . Data . Number , JobOperation = "Прочее(наряды)" , Time = "00:01" } ;
await esppApiService . AddMTnkAsync ( tnk ) ;
await esppApiService . SetStatusIsDoneAsync ( newItem . Data . Number , ResultMsg , EsppOrderClosingCodeEnum . Breake ) ;
}
}
}
}
2023-10-19 15:59:57 +10:00
}
2023-10-20 11:00:46 +10:00
}