fix(esppSync, dal, api): Исправление после удаления старых ShortCodes
This commit is contained in:
@@ -1,17 +1,139 @@
|
||||
using System;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using PARR.DAL.Contracts;
|
||||
using PARR.DAL.Services.Implementations.Job;
|
||||
using PARR.DAL.Services.Implementations.Unit;
|
||||
using PARR.DAL.Services.Interfaces.Job;
|
||||
using PARR.DAL.Services.Interfaces.Unit;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace PARR.DAL.DomainServices
|
||||
{
|
||||
internal class ShortcodesService : IShortcodesService
|
||||
{
|
||||
public Task<string> ApplyShortcodesAsync(string str, Guid unitId, Guid jobId)
|
||||
private readonly SettingsFromDb settingsFromDb;
|
||||
private readonly IJobService jobService;
|
||||
private readonly IUnitService unitService;
|
||||
|
||||
public ShortcodesService(
|
||||
SettingsFromDb settingsFromDb,
|
||||
IJobService jobService,
|
||||
IUnitService unitService
|
||||
)
|
||||
{
|
||||
this.settingsFromDb = settingsFromDb;
|
||||
this.jobService = jobService;
|
||||
this.unitService = unitService;
|
||||
}
|
||||
|
||||
public async Task<string> ApplyShortcodesAsync(string str, Guid unitId, Guid jobId)
|
||||
{
|
||||
//TODO удалить старый в GeneralExtesions, связанные с ним Enum и написать метод. делов...
|
||||
throw new NotImplementedException();
|
||||
|
||||
var nameConstants = settingsFromDb.TemplateNameConstantPartsList;
|
||||
|
||||
var job = await jobService.Get().AsNoTracking().FirstOrDefaultAsync(t => t.Id == jobId);
|
||||
var unit = await unitService.Get().AsNoTracking().FirstOrDefaultAsync(t => t.Id == unitId);
|
||||
|
||||
if (job != null && unit != null && job.TemplateNameMask != null)
|
||||
{
|
||||
var resultName = job.TemplateNameMask;
|
||||
|
||||
var shortcodesInMask = GetShortCodes(resultName);
|
||||
|
||||
//Проверяем и меняем наличие статичных частей в маске имени шаблона
|
||||
if (shortcodesInMask.Any(x => nameConstants.Select(x => "%" + x.Name + "%").ToList().Contains(x.Value)))
|
||||
{
|
||||
resultName = ReplaceConstants(nameConstants, resultName);
|
||||
}
|
||||
|
||||
//Проверяем и меняем Shortcodes в маске имени шаблона
|
||||
var shortCodes = GetShortcodesNames();
|
||||
if (shortcodesInMask.Any(x => shortCodes.Select(x => x).ToList().Contains(x.Value)))
|
||||
{
|
||||
resultName = ReplaceShortcodes(job, unit, resultName, shortcodesInMask);
|
||||
}
|
||||
|
||||
//Если остались %переменные% проверяем совпадение по имени поля
|
||||
shortcodesInMask = GetShortCodes(resultName);
|
||||
if (shortcodesInMask.Count > 0)
|
||||
{
|
||||
resultName = await ReplaceFieldValues(unitId, resultName, shortcodesInMask);
|
||||
}
|
||||
|
||||
return resultName;
|
||||
}
|
||||
|
||||
return "";
|
||||
}
|
||||
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
private static string ReplaceShortcodes(Models.Job.Job job, Models.Unit.Unit unit, string resultName, List<Match> shortcodesInMask)
|
||||
{
|
||||
if (shortcodesInMask.Any(x => x.Value == "%РАБОТА%"))
|
||||
resultName = resultName.Replace("%РАБОТА%", job.WorkName);
|
||||
|
||||
if (shortcodesInMask.Any(x => x.Value == "%ЭК%"))
|
||||
resultName = resultName.Replace("%ЭК%", unit.Name);
|
||||
return resultName;
|
||||
}
|
||||
|
||||
|
||||
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 shortcodePattern = "%[^%\\s]+%";
|
||||
var shortcodesInMask = Regex.Matches(resultName, shortcodePattern).ToList();
|
||||
return shortcodesInMask;
|
||||
}
|
||||
|
||||
private List<string> GetShortcodesNames()
|
||||
{
|
||||
var result = new List<string> {
|
||||
"%ЭК%",
|
||||
"%ЗО%",
|
||||
"%РАБОТА%"
|
||||
};
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using PARR.Constants.Shortcodes;
|
||||
using PARR.DAL.Contracts;
|
||||
using PARR.DAL.Services.Interfaces.Job;
|
||||
using PARR.DAL.Services.Interfaces.Unit;
|
||||
@@ -89,11 +88,11 @@ namespace PARR.DAL.DomainServices
|
||||
|
||||
private static string ReplaceShortcodes(Models.Job.Job job, Models.Unit.Unit unit, string resultName, List<Match> shortcodesInMask)
|
||||
{
|
||||
if (shortcodesInMask.Any(x => x.Value == Shortcodes.GetShortcodeName(ShortcodeEnum.Work)))
|
||||
resultName = resultName.Replace(Shortcodes.GetShortcodeName(ShortcodeEnum.Work), job.WorkName);
|
||||
if (shortcodesInMask.Any(x => x.Value == "%РАБОТА%"))
|
||||
resultName = resultName.Replace("%РАБОТА%", job.WorkName);
|
||||
|
||||
if (shortcodesInMask.Any(x => x.Value == Shortcodes.GetShortcodeName(ShortcodeEnum.EK)))
|
||||
resultName = resultName.Replace(Shortcodes.GetShortcodeName(ShortcodeEnum.EK), unit.Name);
|
||||
if (shortcodesInMask.Any(x => x.Value == "%ЭК%"))
|
||||
resultName = resultName.Replace("%ЭК%", unit.Name);
|
||||
return resultName;
|
||||
}
|
||||
|
||||
@@ -116,12 +115,11 @@ namespace PARR.DAL.DomainServices
|
||||
|
||||
private List<string> GetShortcodesNames()
|
||||
{
|
||||
var result = new List<string>();
|
||||
var shortcodes = Enum.GetValues(typeof(ShortcodeEnum)).Cast<ShortcodeEnum>();
|
||||
foreach (var item in shortcodes)
|
||||
{
|
||||
result.Add(Shortcodes.GetShortcodeName(item).ToUpper());
|
||||
}
|
||||
var result = new List<string> {
|
||||
"%ЭК%",
|
||||
"%ЗО%",
|
||||
"%РАБОТА%"
|
||||
};
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
using PARR.Constants.Shortcodes;
|
||||
using System.Text.Encodings.Web;
|
||||
using System.Text.Encodings.Web;
|
||||
using System.Text.Json;
|
||||
using System.Text.Unicode;
|
||||
|
||||
@@ -46,11 +45,11 @@ namespace PARR.DAL.Extensions
|
||||
/// <param name="shortcode">Код шаблона из PARR.Constants.ShortcodeEnum.ShortcodeEnum</param>
|
||||
/// <param name="shortcodeValue">Итоговое строковое значение</param>
|
||||
/// <returns></returns>
|
||||
public static string ApplyShortcode(this string sourceString, ShortcodeEnum shortcode, string shortcodeValue)
|
||||
{
|
||||
var shortcodeName = Shortcodes.GetShortcodeName(shortcode);
|
||||
//public static string ApplyShortcode(this string sourceString, ShortcodeEnum shortcode, string shortcodeValue)
|
||||
//{
|
||||
// var shortcodeName = Shortcodes.GetShortcodeName(shortcode);
|
||||
|
||||
return sourceString.Replace(shortcodeName, shortcodeValue);
|
||||
}
|
||||
// return sourceString.Replace(shortcodeName, shortcodeValue);
|
||||
//}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user