Compare commits

...

11 Commits

Author SHA1 Message Date
Sergey Chebotar
0b952b4cf2 Edit intellisence description 2022-12-28 15:32:26 +03:00
Sergey Chebotar
c045a6047f Rename reset status bar macro 2022-12-28 15:28:29 +03:00
Sergey Chebotar
2506f0fe09 Return parsed sku while awaiting database response 2022-12-28 15:26:41 +03:00
Sergey Chebotar
677424f16e StatusbarBase refactoring 2022-12-26 15:03:53 +03:00
Sergey Chebotar
b35c9d6978 Remove old tls settings 2022-12-26 15:02:45 +03:00
Sergey Chebotar
6ac7b98e59 RhDatabaseClient refactoring 2022-12-26 14:46:37 +03:00
Sergey Chebotar
755d556f09 Delete SkuExtensions class 2022-12-23 21:20:41 +03:00
Sergey Chebotar
0ccbf29382 Return to ResetStatusBar Excel macro function 2022-12-23 21:10:43 +03:00
Sergey Chebotar
9892b3687f Reduce Status Bar reset delay 2022-12-22 16:01:46 +03:00
Sergey Chebotar
c47301c0db Try parse sku from input line on Rhsolutions function apply 2022-12-22 16:00:38 +03:00
Sergey Chebotar
f267b27375 Change Sku to RhSolutions.Api implementation 2022-12-22 14:52:54 +03:00
9 changed files with 134 additions and 88 deletions

View File

@ -1,12 +1,14 @@
using ExcelDna.Integration; using ExcelDna.Integration;
using RhSolutions.Models;
using RhSolutions.Services; using RhSolutions.Services;
using System.Linq;
namespace RhSolutions.AddIn namespace RhSolutions.AddIn
{ {
public class Functions public class Functions
{ {
[ExcelFunction(Description = "Запрос в удаленную базу данных")] [ExcelFunction(Description = "Распознать артикул и попробовать найти его в прайс-листе")]
public static object RHSOLUTIONS([ExcelArgument(Name = "Запрос")] string line) public static object RHSOLUTIONS([ExcelArgument(Name = "\"Строка с названием материала\"")] string line)
{ {
object result; object result;
@ -15,17 +17,40 @@ namespace RhSolutions.AddIn
return RhDatabaseClient.GetProduct(line).GetAwaiter().GetResult(); return RhDatabaseClient.GetProduct(line).GetAwaiter().GetResult();
}); });
string parsedSku = Sku.TryParse(line, out var skus)
? skus.First().ToString() : string.Empty;
if (result == null) if (result == null)
{ {
return ExcelError.ExcelErrorNA; if (string.IsNullOrEmpty(parsedSku))
{
return ExcelError.ExcelErrorNA;
}
else
{
return skus.First().ToString();
}
} }
if (result.Equals(ExcelError.ExcelErrorNA)) if (result.Equals(ExcelError.ExcelErrorNA))
{ {
return "Загрузка..."; if (string.IsNullOrEmpty(parsedSku))
{
return "Загрузка...";
}
else
{
return skus.First().ToString();
}
} }
return result; return result;
} }
[ExcelFunction]
public static void _ResetStatusBar()
{
RhSolutionsAddIn.Excel.StatusBar = false;
}
} }
} }

View File

@ -9,21 +9,19 @@ namespace RhSolutions.AddIn
{ {
class RhSolutionsAddIn : IExcelAddIn class RhSolutionsAddIn : IExcelAddIn
{ {
public static Application Excel; public static Application Excel { get; private set; }
public static HttpClient httpClient; public static HttpClient HttpClient { get; private set; }
public void AutoOpen() public void AutoOpen()
{ {
Excel = (Application)ExcelDnaUtil.Application; Excel = (Application)ExcelDnaUtil.Application;
httpClient = new HttpClient(); HttpClient = new HttpClient();
IntelliSenseServer.Install(); IntelliSenseServer.Install();
RegistryUtil.Initialize(); RegistryUtil.Initialize();
EventsUtil.Initialize(); EventsUtil.Initialize();
ServicePointManager.SecurityProtocol = ServicePointManager.SecurityProtocol =
SecurityProtocolType.Tls12 | SecurityProtocolType.Tls12;
SecurityProtocolType.Tls11 |
SecurityProtocolType.Tls;
} }
public void AutoClose() public void AutoClose()
@ -31,6 +29,7 @@ namespace RhSolutions.AddIn
IntelliSenseServer.Uninstall(); IntelliSenseServer.Uninstall();
RegistryUtil.Uninitialize(); RegistryUtil.Uninitialize();
EventsUtil.Uninitialize(); EventsUtil.Uninitialize();
HttpClient.Dispose();
} }
} }
} }

View File

@ -2,6 +2,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using RhSolutions.Models; using RhSolutions.Models;
using System.Linq;
namespace RhSolutions.Controllers namespace RhSolutions.Controllers
{ {
@ -56,9 +57,9 @@ namespace RhSolutions.Controllers
{ {
object current = cells[row, column]; object current = cells[row, column];
if (Sku.TryParse(current.ToString(), out Sku rauSku)) if (Sku.TryParse(current.ToString(), out var rauSku))
{ {
sku = rauSku.ToString(); sku = rauSku.FirstOrDefault().ToString() ?? null;
} }
else if (current.GetType() == typeof(string) else if (current.GetType() == typeof(string)

View File

@ -1,11 +1,15 @@
using System.Text.RegularExpressions; using System.Collections.Generic;
using System.Linq;
using System;
using System.Text.RegularExpressions;
namespace RhSolutions.Models namespace RhSolutions.Models
{ {
internal class Sku public class Sku
{ {
public string Article { get; private set; } private const string matchPattern = @"([1\D]|\b)(?<Article>\d{6})([1\s-]|)(?<Variant>\d{3})\b";
public string Variant { get; private set; } private string _article;
private string _variant;
public Sku(string article, string variant) public Sku(string article, string variant)
{ {
@ -13,49 +17,94 @@ namespace RhSolutions.Models
Variant = variant; Variant = variant;
} }
public static bool TryParse(string line, out Sku rehauSku) public string Id
{ {
Match match; get
match = Regex.Match(line, @"\b[1]\d{6}[1]\d{3}\b");
if (match.Success)
{ {
string sku = match.Value.Substring(1, 6); return $"1{Article}1{Variant}";
string variant = match.Value.Substring(8, 3);
rehauSku = new Sku(sku, variant);
return true;
} }
set
match = Regex.Match(line, @"\b\d{6}\D\d{3}\b");
if (match.Success)
{ {
string sku = match.Value.Substring(0, 6); if (TryParse(value, out IEnumerable<Sku> skus))
string variant = match.Value.Substring(7, 3); {
rehauSku = new Sku(sku, variant); if (skus.Count() > 1)
return true; {
throw new ArgumentException($"More than one valid sku detected: {value}");
}
else
{
this.Article = skus.First().Article;
this.Variant = skus.First().Variant;
}
}
else
{
throw new ArgumentException($"Invalid sku input: {value}");
}
} }
}
match = Regex.Match(line, @"\b\d{9}\b"); public string Article
if (match.Success) {
get
{ {
string sku = match.Value.Substring(0, 6); return _article;
string variant = match.Value.Substring(6, 3);
rehauSku = new Sku(sku, variant);
return true;
} }
set
match = Regex.Match(line, @"\b\d{6}\b");
if (match.Success)
{ {
string sku = match.Value.Substring(0, 6); if (value == null || value.Length != 6 || value.Where(c => char.IsDigit(c)).Count() != 6)
string variant = "001"; {
rehauSku = new Sku(sku, variant); throw new ArgumentException($"Wrong Article: {Article}");
return true; }
else
{
_article = value;
}
}
}
public string Variant
{
get
{
return _variant;
}
set
{
if (value == null || value.Length != 3 || value.Where(c => char.IsDigit(c)).Count() != 3)
{
throw new ArgumentException($"Wrong Variant: {Variant}");
}
else _variant = value;
}
}
public static IEnumerable<Sku> GetValidSkus(string line)
{
MatchCollection matches = Regex.Matches(line, matchPattern);
if (matches.Count == 0)
{
yield break;
}
else
{
foreach (Match m in matches)
{
yield return new Sku(m.Groups["Article"].Value, m.Groups["Variant"].Value);
}
}
}
public static bool TryParse(string line, out IEnumerable<Sku> skus)
{
MatchCollection matches = Regex.Matches(line, matchPattern);
if (matches.Count == 0)
{
skus = Enumerable.Empty<Sku>();
return false;
} }
else else
{ {
rehauSku = null; skus = GetValidSkus(line);
return false; return true;
} }
} }

View File

@ -1,12 +0,0 @@
using System.Text.RegularExpressions;
namespace RhSolutions.Models
{
static class SkuExtensions
{
public static bool IsRehauSku(this string line)
{
return Regex.IsMatch(line, @"^[1]\d{6}[1]\d{3}$");
}
}
}

View File

@ -87,7 +87,7 @@ namespace RhSolutions.Models
if (group == null || name == null || sku == null) if (group == null || name == null || sku == null)
continue; continue;
if (!sku.ToString().IsRehauSku()) if (!Sku.TryParse(sku.ToString(), out _))
continue; continue;
Product p = new Product Product p = new Product

View File

@ -1,9 +1,6 @@
using ExcelDna.Integration; using Microsoft.Office.Interop.Excel;
using Microsoft.Office.Interop.Excel;
using RhSolutions.AddIn; using RhSolutions.AddIn;
using System; using System;
using System.Threading;
using System.Threading.Tasks;
namespace RhSolutions.Models namespace RhSolutions.Models
{ {
@ -13,14 +10,9 @@ namespace RhSolutions.Models
public abstract void Update(); public abstract void Update();
private static void ResetStatusBar()
{
RhSolutionsAddIn.Excel.StatusBar = false;
}
public void Dispose() public void Dispose()
{ {
Task.Delay(5000).ContinueWith(t => ResetStatusBar()); Excel.OnTime(DateTime.Now + new TimeSpan(0, 0, 5), "_ResetStatusBar");
} }
} }
} }

View File

@ -34,6 +34,9 @@
<ErrorReport>prompt</ErrorReport> <ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel> <WarningLevel>4</WarningLevel>
</PropertyGroup> </PropertyGroup>
<PropertyGroup>
<StartupObject />
</PropertyGroup>
<ItemGroup> <ItemGroup>
<Reference Include="ExcelDna.Integration, Version=1.1.0.0, Culture=neutral, PublicKeyToken=f225e9659857edbe, processorArchitecture=MSIL"> <Reference Include="ExcelDna.Integration, Version=1.1.0.0, Culture=neutral, PublicKeyToken=f225e9659857edbe, processorArchitecture=MSIL">
<HintPath>..\packages\ExcelDna.Integration.1.6.0\lib\net452\ExcelDna.Integration.dll</HintPath> <HintPath>..\packages\ExcelDna.Integration.1.6.0\lib\net452\ExcelDna.Integration.dll</HintPath>
@ -83,7 +86,6 @@
<Compile Include="Models\StatusbarBase.cs" /> <Compile Include="Models\StatusbarBase.cs" />
<Compile Include="Models\Dialog.cs" /> <Compile Include="Models\Dialog.cs" />
<Compile Include="Services\RegistryUtil.cs" /> <Compile Include="Services\RegistryUtil.cs" />
<Compile Include="Models\SkuExtensions.cs" />
<Compile Include="Models\ProgressBar.cs" /> <Compile Include="Models\ProgressBar.cs" />
<Compile Include="Models\ResultBar.cs" /> <Compile Include="Models\ResultBar.cs" />
<Compile Include="Controllers\CombineTool.cs" /> <Compile Include="Controllers\CombineTool.cs" />
@ -97,7 +99,7 @@
<Compile Include="Models\TargetPriceList.cs" /> <Compile Include="Models\TargetPriceList.cs" />
<Compile Include="Controllers\RibbonController.cs" /> <Compile Include="Controllers\RibbonController.cs" />
<Compile Include="Controllers\ExportTool.cs" /> <Compile Include="Controllers\ExportTool.cs" />
<Compile Include="AddIn\AddIn.cs" /> <Compile Include="AddIn\RhSolutionsAddIn.cs" />
<Compile Include="AddIn\Functions.cs" /> <Compile Include="AddIn\Functions.cs" />
<Compile Include="Models\WorksheetExtensions.cs" /> <Compile Include="Models\WorksheetExtensions.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />

View File

@ -11,15 +11,13 @@ namespace RhSolutions.Services
{ {
public static class RhDatabaseClient public static class RhDatabaseClient
{ {
private static HttpClient httpClient = RhSolutionsAddIn.httpClient;
public static async Task<object> GetProduct(string line) public static async Task<object> GetProduct(string line)
{ {
string request = string.Empty; string request;
if (line.IsRehauSku()) if (Sku.TryParse(line, out var skus))
{ {
request = @"https://rh.cebotari.ru/api/products/" + line; request = @"https://rh.cebotari.ru/api/products/" + skus.FirstOrDefault().ToString();
} }
else else
@ -27,7 +25,7 @@ namespace RhSolutions.Services
request = @"https://rh.cebotari.ru/api/search?query=" + line; request = @"https://rh.cebotari.ru/api/search?query=" + line;
} }
var response = await httpClient.GetAsync(request); var response = await RhSolutionsAddIn.HttpClient.GetAsync(request);
try try
{ {
@ -42,21 +40,13 @@ namespace RhSolutions.Services
} }
else else
{ {
if (line.IsRehauSku()) return $"{product.ProductSku} {product.Name}";
{
return product.Name;
}
else
{
return $"{product.ProductSku} {product.Name}";
}
} }
} }
catch catch
{ {
return $"Ошибка сервера {response.StatusCode}"; return $"Ошибка сервера {response.StatusCode}";
} }
} }
} }
} }