2025-08-01 14:45:36 +10:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2025-12-09 16:50:35 +10:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
2025-08-01 14:45:36 +10:00
|
|
|
|
using PARR.DAL.Contracts;
|
2025-08-12 11:50:29 +10:00
|
|
|
|
using PARR.DAL.DomainServices.Interfaces;
|
2025-12-09 16:50:35 +10:00
|
|
|
|
using PARR.DAL.Models.Job;
|
|
|
|
|
|
using PARR.DAL.Models.Unit;
|
2025-08-01 14:45:36 +10:00
|
|
|
|
using PARR.DAL.Services.Interfaces.Job;
|
|
|
|
|
|
using PARR.DAL.Services.Interfaces.Unit;
|
|
|
|
|
|
using System.Text.RegularExpressions;
|
2025-08-01 09:02:01 +10:00
|
|
|
|
|
2025-08-12 11:50:29 +10:00
|
|
|
|
namespace PARR.DAL.DomainServices.Implementations
|
2025-08-01 09:02:01 +10:00
|
|
|
|
{
|
|
|
|
|
|
internal class ShortcodesService : IShortcodesService
|
|
|
|
|
|
{
|
2025-08-06 10:33:55 +10:00
|
|
|
|
private const string shortcodePattern = "%[^%\\s]+%";
|
2025-12-09 16:50:35 +10:00
|
|
|
|
|
|
|
|
|
|
private static readonly HashSet<string> SupportedShortcodes = new(StringComparer.OrdinalIgnoreCase)
|
|
|
|
|
|
{
|
|
|
|
|
|
"%ЭК%", "%ГРУППА_РАБОТ%", "%РАБОТА%", "%ТНК%", "%СВЯЗИ%", "%ТНК-КРАТКО%"
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
private readonly ILogger<ShortcodesService> logger;
|
2025-08-01 14:45:36 +10:00
|
|
|
|
private readonly SettingsFromDb settingsFromDb;
|
|
|
|
|
|
private readonly IJobService jobService;
|
|
|
|
|
|
private readonly IUnitService unitService;
|
2025-12-09 16:50:35 +10:00
|
|
|
|
private readonly IUnitFilterService unitFilterService;
|
2025-08-01 14:45:36 +10:00
|
|
|
|
|
|
|
|
|
|
public ShortcodesService(
|
2025-12-09 16:50:35 +10:00
|
|
|
|
ILogger<ShortcodesService> logger,
|
2025-08-01 14:45:36 +10:00
|
|
|
|
SettingsFromDb settingsFromDb,
|
|
|
|
|
|
IJobService jobService,
|
2025-12-09 16:50:35 +10:00
|
|
|
|
IUnitService unitService,
|
|
|
|
|
|
IUnitFilterService unitFilterService
|
2025-08-01 14:45:36 +10:00
|
|
|
|
)
|
|
|
|
|
|
{
|
2025-12-09 16:50:35 +10:00
|
|
|
|
this.logger = logger;
|
2025-08-01 14:45:36 +10:00
|
|
|
|
this.settingsFromDb = settingsFromDb;
|
|
|
|
|
|
this.jobService = jobService;
|
|
|
|
|
|
this.unitService = unitService;
|
2025-12-09 16:50:35 +10:00
|
|
|
|
this.unitFilterService = unitFilterService;
|
2025-08-01 14:45:36 +10:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async Task<string> ApplyShortcodesAsync(string str, Guid unitId, Guid jobId)
|
2025-08-01 09:02:01 +10:00
|
|
|
|
{
|
2025-08-01 14:45:36 +10:00
|
|
|
|
var nameConstants = settingsFromDb.TemplateNameConstantPartsList;
|
|
|
|
|
|
|
2025-08-15 12:16:24 +10:00
|
|
|
|
var job = await jobService
|
2025-12-09 16:50:35 +10:00
|
|
|
|
.Get().AsNoTracking()
|
|
|
|
|
|
.Include(j => j.Tnk)
|
|
|
|
|
|
.Include(j => j.Group)
|
|
|
|
|
|
.Include(j => j.UnitFilters)
|
|
|
|
|
|
.ThenInclude(uf => uf.RelationshipFilters)
|
|
|
|
|
|
.FirstOrDefaultAsync(j => j.Id == jobId);
|
|
|
|
|
|
|
|
|
|
|
|
var unit = await unitService.Get().AsNoTracking().FirstOrDefaultAsync(u => u.Id == unitId);
|
|
|
|
|
|
|
|
|
|
|
|
if (job == null || unit == null || string.IsNullOrEmpty(str))
|
|
|
|
|
|
{
|
|
|
|
|
|
logger.LogError("Переданы некорректные данные для подстановки динамических записей");
|
|
|
|
|
|
return str;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var resultName = str;
|
|
|
|
|
|
var shortcodesInMask = GetShortCodes(resultName);
|
|
|
|
|
|
|
|
|
|
|
|
// 1. Статические константы
|
|
|
|
|
|
if (shortcodesInMask.Any(m => nameConstants.Any(c => $"%{c.Name}%".Equals(m.Value, StringComparison.OrdinalIgnoreCase))))
|
|
|
|
|
|
{
|
|
|
|
|
|
resultName = ReplaceConstants(nameConstants, resultName);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 2. Стандартные шорткоды (%ЭК%, %РАБОТА% и т.д.)
|
|
|
|
|
|
if (shortcodesInMask.Any(m => SupportedShortcodes.Contains(m.Value)))
|
|
|
|
|
|
{
|
|
|
|
|
|
resultName = ReplaceStandardShortcodes(job, unit, resultName);
|
|
|
|
|
|
}
|
2025-08-15 12:16:24 +10:00
|
|
|
|
|
2025-12-09 16:50:35 +10:00
|
|
|
|
// 3. %СВЯЗИ% — отдельная обработка
|
|
|
|
|
|
if (shortcodesInMask.Any(m => string.Equals(m.Value, "%СВЯЗИ%", StringComparison.OrdinalIgnoreCase)))
|
|
|
|
|
|
{
|
2025-12-09 17:53:54 +10:00
|
|
|
|
var relatedUnitNames = await unitFilterService.GetRelatedUnitNamesAsync(jobId, unitId);
|
2025-12-09 16:50:35 +10:00
|
|
|
|
var linksText = string.Join("\n", relatedUnitNames);
|
|
|
|
|
|
resultName = Regex.Replace(resultName, "%СВЯЗИ%", linksText, RegexOptions.IgnoreCase);
|
|
|
|
|
|
}
|
2025-08-01 14:45:36 +10:00
|
|
|
|
|
2025-12-09 16:50:35 +10:00
|
|
|
|
// 4. Поля (оставшиеся %FIELD_NAME%)
|
|
|
|
|
|
shortcodesInMask = GetShortCodes(resultName);
|
|
|
|
|
|
if (shortcodesInMask.Count > 0)
|
2025-08-01 14:45:36 +10:00
|
|
|
|
{
|
2025-12-09 16:50:35 +10:00
|
|
|
|
resultName = await ReplaceFieldValues(unitId, resultName, shortcodesInMask);
|
2025-08-01 14:45:36 +10:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-09 16:50:35 +10:00
|
|
|
|
return resultName;
|
2025-08-01 14:45:36 +10:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-08-06 10:33:55 +10:00
|
|
|
|
public bool isAnyShortcodes(string str)
|
|
|
|
|
|
{
|
|
|
|
|
|
return Regex.IsMatch(str, shortcodePattern);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-08-01 14:45:36 +10:00
|
|
|
|
private async Task<string> ReplaceFieldValues(Guid unitId, string resultName, List<Match> shortcodesInMask)
|
|
|
|
|
|
{
|
|
|
|
|
|
var unitWithFields = await unitService.Get()
|
|
|
|
|
|
.AsNoTracking()
|
|
|
|
|
|
.Include(u => u.UnitValues)
|
|
|
|
|
|
.ThenInclude(uv => uv.Field)
|
|
|
|
|
|
.Include(u => u.UnitValues)
|
|
|
|
|
|
.ThenInclude(uv => uv.Value)
|
|
|
|
|
|
.FirstOrDefaultAsync(t => t.Id == unitId);
|
|
|
|
|
|
|
|
|
|
|
|
foreach (var item in shortcodesInMask)
|
|
|
|
|
|
{
|
|
|
|
|
|
var fieldName = item.Value.Replace("%", "").ToUpper();
|
|
|
|
|
|
|
|
|
|
|
|
var value = unitWithFields!.UnitValues!.FirstOrDefault(t => t.Field!.AihitName!.ToUpper() == fieldName!);
|
|
|
|
|
|
|
|
|
|
|
|
if (value != null)
|
|
|
|
|
|
resultName = resultName.Replace(item.Value, value!.Value!.Value);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return resultName;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-12-09 16:50:35 +10:00
|
|
|
|
private static string ReplaceStandardShortcodes(Job job, Unit unit, string input)
|
2025-08-01 14:45:36 +10:00
|
|
|
|
{
|
2025-12-09 16:50:35 +10:00
|
|
|
|
return input
|
|
|
|
|
|
.Replace("%ЭК%", unit.Name, StringComparison.OrdinalIgnoreCase)
|
|
|
|
|
|
.Replace("%ГРУППА_РАБОТ%", job.Group?.GroupName ?? "", StringComparison.OrdinalIgnoreCase)
|
|
|
|
|
|
.Replace("%РАБОТА%", job.WorkName, StringComparison.OrdinalIgnoreCase)
|
|
|
|
|
|
.Replace("%ТНК%", job.Tnk?.Name ?? "", StringComparison.OrdinalIgnoreCase)
|
|
|
|
|
|
.Replace("%ТНК-КРАТКО%", job.Tnk?.ShortName ?? "", StringComparison.OrdinalIgnoreCase);
|
2025-08-01 14:45:36 +10:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static string ReplaceConstants(List<BLL.Domain.TemplateNameConstantPart> nameConstants, string resultName)
|
|
|
|
|
|
{
|
|
|
|
|
|
foreach (var item in nameConstants)
|
|
|
|
|
|
{
|
|
|
|
|
|
resultName = resultName.Replace($"%{item.Name}%", item.Value);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return resultName;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static List<Match> GetShortCodes(string resultName)
|
|
|
|
|
|
{
|
|
|
|
|
|
var shortcodesInMask = Regex.Matches(resultName, shortcodePattern).ToList();
|
|
|
|
|
|
return shortcodesInMask;
|
|
|
|
|
|
}
|
2025-08-01 09:02:01 +10:00
|
|
|
|
}
|
|
|
|
|
|
}
|