feat(aihit): AIHITLoadWorker + AIHITSyncWorker
This commit is contained in:
120
PARR.AIHITLoader/AihitLoader.cs
Normal file
120
PARR.AIHITLoader/AihitLoader.cs
Normal file
@@ -0,0 +1,120 @@
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using PARR.AIHITLoader.Models;
|
||||
using PARR.AIHITLoader.Services;
|
||||
using PARR.AIHITLoader.Settings;
|
||||
using PARR.BLL.Domain.Mq;
|
||||
using PARR.BLL.Services.Interfaces;
|
||||
using System.Globalization;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace PARR.AIHITLoader
|
||||
{
|
||||
internal class AihitLoader : IAihitLoader
|
||||
{
|
||||
private readonly IIntervalService intervalService;
|
||||
private readonly ILogger<AihitLoader> logger;
|
||||
private readonly WorkerSettings workerSettings;
|
||||
private readonly LoaderSettings loaderSettings;
|
||||
private readonly IMqService mqService;
|
||||
private readonly MqSettings mqSettings;
|
||||
private readonly IServiceProvider serviceProvider;
|
||||
|
||||
public AihitLoader(
|
||||
IIntervalService intervalService,
|
||||
ILogger<AihitLoader> logger,
|
||||
WorkerSettings workerSettings,
|
||||
LoaderSettings loaderSettings,
|
||||
IMqService mqService,
|
||||
MqSettings mqSettings,
|
||||
IServiceProvider serviceProvider
|
||||
)
|
||||
{
|
||||
this.intervalService = intervalService;
|
||||
this.logger = logger;
|
||||
this.workerSettings = workerSettings;
|
||||
this.loaderSettings = loaderSettings;
|
||||
this.mqService = mqService;
|
||||
this.mqSettings = mqSettings;
|
||||
this.serviceProvider = serviceProvider;
|
||||
}
|
||||
|
||||
public async Task StartAsync()
|
||||
{
|
||||
logger.LogInformation("Запуск сервиса загрузки данных из АИХИТ.");
|
||||
|
||||
await intervalService.IntervalInitAsync(UploadDataAsync, workerSettings.RepeatEvery);
|
||||
}
|
||||
|
||||
private async Task UploadDataAsync()
|
||||
{
|
||||
foreach (var respArea in loaderSettings.ResponseAreas)
|
||||
{
|
||||
var aihitData = GetDataFromAihit(respArea);
|
||||
|
||||
if (aihitData == null) return;
|
||||
|
||||
var filteredAIhitData = FilterData(aihitData);
|
||||
|
||||
var preparedData = filteredAIhitData.Select(d => JsonSerializer.Serialize(MapAihitToDomain(d)));
|
||||
|
||||
var parts = preparedData.Count() / loaderSettings.PackageSize;
|
||||
parts++;
|
||||
|
||||
for (var i = 0; i < parts; i++)
|
||||
{
|
||||
var batch = preparedData.Skip(i * loaderSettings.PackageSize).Take(loaderSettings.PackageSize).ToList();
|
||||
var sendResult = mqService.Send(mqSettings, batch.ToArray());
|
||||
if (sendResult.IsSuccess)
|
||||
{
|
||||
logger.LogInformation($"Данные переданы в RabbitMQ: {batch.Count()}");
|
||||
logger.LogDebug($"Список ЭК переданных в RabbitMQ: {string.Join(", ", batch)}");
|
||||
}
|
||||
else
|
||||
logger.LogError($"Ошибка при передаче нарядов в Rabbit, не переданные сообщения: {string.Join(", ", sendResult.NotSendMessages)}");
|
||||
}
|
||||
|
||||
}
|
||||
await Task.CompletedTask;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Фильтруем входные данные из АИХ ИТ по необходимости. На первом этапе берём только эк по максе ВРТ-*
|
||||
/// </summary>
|
||||
/// <param name="aihitData"></param>
|
||||
/// <returns></returns>
|
||||
private List<EK> FilterData(List<EK> aihitData)
|
||||
{
|
||||
var vrt = aihitData.Where(ek => ek.EKFindCode!.StartsWith("ВРТ", true, CultureInfo.GetCultureInfo("ru-RU"))).ToList();
|
||||
return vrt.Where(vrt => vrt.IP != null).ToList();
|
||||
}
|
||||
|
||||
private List<EK>? GetDataFromAihit(string respArea)
|
||||
{
|
||||
using (var scope = serviceProvider.CreateScope())
|
||||
{
|
||||
var aihitService = scope.ServiceProvider.GetService<IAIHITService>();
|
||||
if (aihitService == null)
|
||||
throw new Exception("Не смог получить серивс IAIHITService, scope.ServiceProvider.GetService<IAIHITService>()");
|
||||
|
||||
return aihitService.GetEKList(respArea);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private AihitDataMq MapAihitToDomain(EK ek)
|
||||
{
|
||||
return new AihitDataMq
|
||||
{
|
||||
EK = ek.EKFindCode!,
|
||||
IP = ek.IP,
|
||||
APPType = ek.APPType,
|
||||
DBType = ek.DBType,
|
||||
OSType = ek.OSType,
|
||||
WorkGroup = ek.WorkGroup,
|
||||
Status = (ek.Status == "00-ГВЦ")? "99-ГВЦ": ek.Status!,
|
||||
ResponseArea = ek.ResponseArea!
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,9 @@
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using PARR.AIHITLoader.Context;
|
||||
using PARR.AIHITLoader.Services;
|
||||
using PARR.AIHITLoader.Settings;
|
||||
using PARR.BLL;
|
||||
|
||||
namespace PARR.AIHITLoader
|
||||
@@ -10,6 +14,27 @@ namespace PARR.AIHITLoader
|
||||
{
|
||||
services.InstallBllServices(configuration);
|
||||
|
||||
var settings = new WorkerSettings();
|
||||
configuration.GetSection(nameof(WorkerSettings)).Bind(settings);
|
||||
services.AddSingleton(settings);
|
||||
|
||||
var loaderSettings = new LoaderSettings();
|
||||
configuration.GetSection(nameof(LoaderSettings)).Bind(loaderSettings);
|
||||
services.AddSingleton(loaderSettings);
|
||||
|
||||
var mqSettings = new MqSettings();
|
||||
configuration.GetSection(nameof(MqSettings)).Bind(mqSettings);
|
||||
services.AddSingleton(mqSettings);
|
||||
|
||||
services.AddDbContext<AIHITContext>(options =>
|
||||
options.UseSqlServer(
|
||||
configuration.GetConnectionString("AihitConnection")
|
||||
, sqlServerOptions => sqlServerOptions.CommandTimeout(1800)
|
||||
));
|
||||
|
||||
services.AddTransient<IAihitLoader, AihitLoader>();
|
||||
services.AddTransient<IAIHITService, AIHITService>();
|
||||
|
||||
//todo: config e.t.c
|
||||
}
|
||||
}
|
||||
|
||||
14
PARR.AIHITLoader/Context/AIHITContext.cs
Normal file
14
PARR.AIHITLoader/Context/AIHITContext.cs
Normal file
@@ -0,0 +1,14 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using PARR.AIHITLoader.Models;
|
||||
|
||||
namespace PARR.AIHITLoader.Context
|
||||
{
|
||||
internal class AIHITContext : DbContext
|
||||
{
|
||||
public AIHITContext(DbContextOptions<AIHITContext> options) : base(options) { }
|
||||
|
||||
|
||||
|
||||
public DbSet<EK> EKs { get; set; }
|
||||
}
|
||||
}
|
||||
7
PARR.AIHITLoader/IAihitLoader.cs
Normal file
7
PARR.AIHITLoader/IAihitLoader.cs
Normal file
@@ -0,0 +1,7 @@
|
||||
namespace PARR.AIHITLoader
|
||||
{
|
||||
public interface IAihitLoader
|
||||
{
|
||||
Task StartAsync();
|
||||
}
|
||||
}
|
||||
100
PARR.AIHITLoader/Models/EK.cs
Normal file
100
PARR.AIHITLoader/Models/EK.cs
Normal file
@@ -0,0 +1,100 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
|
||||
namespace PARR.AIHITLoader.Models
|
||||
{
|
||||
public class EK
|
||||
{
|
||||
[Column("IP_АДРЕС")]
|
||||
public string? IP { get; set; }
|
||||
[Column("МЕТКА")]
|
||||
public string? Metka { get; set; }
|
||||
[Column("АКТИВЕН")]
|
||||
public string? IsActive { get; set; }
|
||||
[Column("ВАЖНЫЙ_ЭК")]
|
||||
public string? IsImportant { get; set; }
|
||||
[Column("ВРЕМЯ_СОЗДАНИЯ")]
|
||||
public DateTime? CreateTime { get; set; }
|
||||
[Column("ДОПОЛНИТЕЛЬНАЯ_ИНФОРМАЦИЯ")]
|
||||
public string? AdditionalInfo { get; set; }
|
||||
[Column("ЗОНА_ОТВЕТСТВЕННОСТИ")]
|
||||
public string? ResponseArea { get; set; }
|
||||
[Column("КАТЕГОРИЯ_ЭК")]
|
||||
public string? EKCategory { get; set; }
|
||||
[Column("КОД_ПОИСКА_ЭК")]
|
||||
public string? EKFindCode { get; set; }
|
||||
[Column("КОД_ПРОДУКТА")]
|
||||
public string? ProductCode { get; set; }
|
||||
[Column("КОД_УСЛУГИ")]
|
||||
public string? ServiceCode { get; set; }
|
||||
[Column("КРАТКОЕ_НАИМЕНОВАНИЕ")]
|
||||
public string? ShortName { get; set; }
|
||||
[Column("ОТВЕТСТВЕННЫЙ_ЗА_ЭК")]
|
||||
public string? ResponsibleByEK { get; set; }
|
||||
[Column("ПЛАНОВОЕ_ВРЕМЯ_ВОССТАНОВЛЕНИЯ")]
|
||||
public DateTime? PlannedTimeToRepair { get; set; }
|
||||
[Column("ПОДКАТЕГОРИЯ_ЭК")]
|
||||
public string? EKSubCategory { get; set; }
|
||||
[Column("ПОЛНОЕ_НАИМЕНОВАНИЕ")]
|
||||
public string? FullName { get; set; }
|
||||
[Column("ПРЕДПИСАНИЕ")]
|
||||
public string? Prescription { get; set; }
|
||||
[Column("ПРЕДПРИЯТИЕ")]
|
||||
public string? Company { get; set; }
|
||||
[Column("РАБОЧАЯ_ГР_ОТВ_ЗА_ЭК")]
|
||||
public string? WorkGroup { get; set; }
|
||||
[Column("РАСПОЛОЖЕНИЕ")]
|
||||
public string? Location { get; set; }
|
||||
[Column("РЕВИЗОР_ЭК")]
|
||||
public string? EKRevizor { get; set; }
|
||||
[Column("РЕГИСТРАТОР_ЭК")]
|
||||
public string? EKRegister { get; set; }
|
||||
[Column("СЕТЕВОЕ_ИМЯ")]
|
||||
public string? NetworkName { get; set; }
|
||||
[Column("СТАТУС")]
|
||||
public string? Status { get; set; }
|
||||
[Column("ТИП_ЭК")]
|
||||
public string? EKType { get; set; }
|
||||
[Column("ФАКТИЧЕСКОЕ_ЗАВЕРШЕНИЕ_ЭКСПЛУАТАЦИИ")]
|
||||
public DateTime? EndExplotationDate { get; set; }
|
||||
[Column("ФАКТИЧЕСКОЕ_НАЧАЛО_ЭКСПЛУАТАЦИИ")]
|
||||
public DateTime? StartExplotationDate { get; set; }
|
||||
[Column("ЦЕЛЕВОЕ_ВРЕМЯ_ВОССТАНОВЛЕНИЯ")]
|
||||
public string? TargetRepairTime { get; set; }
|
||||
[Column("SYSMODTIME")]
|
||||
public DateTime? SysModTime { get; set; }
|
||||
[Column("SYSMODUSER")]
|
||||
public string? SysModUser { get; set; }
|
||||
[Column("НОВЫЙ_КОД_ПОИСКА")]
|
||||
public string? NewEKFindCode { get; set; }
|
||||
[Column("СТАРЫЙ_КОД_ПОИСКА")]
|
||||
public string? OldEKFindCode { get; set; }
|
||||
[Column("НАПРАВЛЕНИЕ_ЦТС_ЦК")]
|
||||
public string? CTSDirection { get; set; }
|
||||
[Key]
|
||||
[Column("ID")]
|
||||
public int? AIHID { get; set; }
|
||||
[Column("недостоверные_данные")]
|
||||
public char? IsUnreliableData { get; set; }
|
||||
[Column("РГ_смены")]
|
||||
public string? ShiftWorkGroup { get; set; }
|
||||
[Column("Клиентское_ПО")]
|
||||
public string? ClientSoftware { get; set; }
|
||||
[Column("Клиентская_ОС")]
|
||||
public string? ClientOS { get; set; }
|
||||
[Column("Тип СУБД")]
|
||||
public string? DBType { get; set; }
|
||||
[Column("Тип сервера приложений")]
|
||||
public string? APPType { get; set; }
|
||||
[Column("Тип сервера ЦК БС")]
|
||||
public string? CKBSServerType { get; set; }
|
||||
[Column("Тип сервера ИБ")]
|
||||
public string? IBServerType { get; set; }
|
||||
[Column("Тип сервера инфраструктуры")]
|
||||
public string? InfrastructureServerType { get; set; }
|
||||
[Column("Тип сервера мониторинга")]
|
||||
public string? MonitoringServerType { get; set; }
|
||||
[Column("ОС")]
|
||||
public string? OSType { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -6,6 +6,10 @@
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.9" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\PARR.BLL\PARR.BLL.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
42
PARR.AIHITLoader/Services/AIHITService.cs
Normal file
42
PARR.AIHITLoader/Services/AIHITService.cs
Normal file
@@ -0,0 +1,42 @@
|
||||
using Microsoft.Data.SqlClient;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using PARR.AIHITLoader.Context;
|
||||
using PARR.AIHITLoader.Models;
|
||||
|
||||
namespace PARR.AIHITLoader.Services
|
||||
{
|
||||
internal class AIHITService : IAIHITService
|
||||
{
|
||||
private readonly AIHITContext context;
|
||||
private readonly ILogger<AIHITService> logger;
|
||||
|
||||
public AIHITService(AIHITContext context, ILogger<AIHITService> logger)
|
||||
{
|
||||
this.context = context;
|
||||
this.logger = logger;
|
||||
}
|
||||
public List<EK>? GetEKList(string respArea)
|
||||
{
|
||||
var parameters = new List<SqlParameter>() { new SqlParameter("@зо", respArea) };
|
||||
|
||||
try
|
||||
{
|
||||
var result = context.Set<EK>().FromSqlRaw($"EXEC mao2.dbo.sp_IPP_PARR_PTK_get_EK @зо", parameters.ToArray()).ToList();
|
||||
|
||||
if (result == null || !result.Any())
|
||||
{
|
||||
logger.LogWarning($"Процедура sp_IPP_PARR_PTK_get_EK вернула пустой список ЭК");
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogError(ex, $"Ошибка при выполнении ХП sp_IPP_PARR_PTK_get_EK({respArea})");
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
9
PARR.AIHITLoader/Services/IAIHITService.cs
Normal file
9
PARR.AIHITLoader/Services/IAIHITService.cs
Normal file
@@ -0,0 +1,9 @@
|
||||
using PARR.AIHITLoader.Models;
|
||||
|
||||
namespace PARR.AIHITLoader.Services
|
||||
{
|
||||
internal interface IAIHITService
|
||||
{
|
||||
List<EK>? GetEKList(string respArea);
|
||||
}
|
||||
}
|
||||
8
PARR.AIHITLoader/Settings/LoaderSettings.cs
Normal file
8
PARR.AIHITLoader/Settings/LoaderSettings.cs
Normal file
@@ -0,0 +1,8 @@
|
||||
namespace PARR.AIHITLoader.Settings
|
||||
{
|
||||
internal class LoaderSettings
|
||||
{
|
||||
public List<string> ResponseAreas { get; set; } = new List<string>();
|
||||
public int PackageSize { get; set; } = 100;
|
||||
}
|
||||
}
|
||||
12
PARR.AIHITLoader/Settings/MqSettings.cs
Normal file
12
PARR.AIHITLoader/Settings/MqSettings.cs
Normal file
@@ -0,0 +1,12 @@
|
||||
using PARR.BLL.Contracts.Interfaces;
|
||||
|
||||
namespace PARR.AIHITLoader.Settings
|
||||
{
|
||||
internal class MqSettings : IMqSettings
|
||||
{
|
||||
public string HostName { get; set; } = string.Empty;
|
||||
public string QueueName { get; set; } = string.Empty;
|
||||
public string User { get; set; } = string.Empty;
|
||||
public string Password { get; set; } = string.Empty;
|
||||
}
|
||||
}
|
||||
7
PARR.AIHITLoader/Settings/WorkerSettings.cs
Normal file
7
PARR.AIHITLoader/Settings/WorkerSettings.cs
Normal file
@@ -0,0 +1,7 @@
|
||||
namespace PARR.AIHITLoader.Settings
|
||||
{
|
||||
internal class WorkerSettings
|
||||
{
|
||||
public TimeSpan RepeatEvery { get; set; }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user