Compare commits

..

No commits in common. "0b952b4cf202d9664351b6c25f940f57af0a9b2e" and "8ac323447347e31875e1db53d899d13a4ac51c1d" have entirely different histories.

9 changed files with 97 additions and 143 deletions

View File

@ -9,19 +9,21 @@ namespace RhSolutions.AddIn
{ {
class RhSolutionsAddIn : IExcelAddIn class RhSolutionsAddIn : IExcelAddIn
{ {
public static Application Excel { get; private set; } public static Application Excel;
public static HttpClient HttpClient { get; private set; } public static HttpClient httpClient;
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()
@ -29,7 +31,6 @@ namespace RhSolutions.AddIn
IntelliSenseServer.Uninstall(); IntelliSenseServer.Uninstall();
RegistryUtil.Uninitialize(); RegistryUtil.Uninitialize();
EventsUtil.Uninitialize(); EventsUtil.Uninitialize();
HttpClient.Dispose();
} }
} }
} }

View File

@ -1,14 +1,12 @@
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;
@ -17,40 +15,17 @@ 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)
{ {
if (string.IsNullOrEmpty(parsedSku)) return ExcelError.ExcelErrorNA;
{
return ExcelError.ExcelErrorNA;
}
else
{
return skus.First().ToString();
}
} }
if (result.Equals(ExcelError.ExcelErrorNA)) if (result.Equals(ExcelError.ExcelErrorNA))
{ {
if (string.IsNullOrEmpty(parsedSku)) return "Загрузка...";
{
return "Загрузка...";
}
else
{
return skus.First().ToString();
}
} }
return result; return result;
} }
[ExcelFunction]
public static void _ResetStatusBar()
{
RhSolutionsAddIn.Excel.StatusBar = false;
}
} }
} }

View File

@ -2,7 +2,6 @@
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
{ {
@ -57,9 +56,9 @@ namespace RhSolutions.Controllers
{ {
object current = cells[row, column]; object current = cells[row, column];
if (Sku.TryParse(current.ToString(), out var rauSku)) if (Sku.TryParse(current.ToString(), out Sku rauSku))
{ {
sku = rauSku.FirstOrDefault().ToString() ?? null; sku = rauSku.ToString();
} }
else if (current.GetType() == typeof(string) else if (current.GetType() == typeof(string)

View File

@ -1,15 +1,11 @@
using System.Collections.Generic; using System.Text.RegularExpressions;
using System.Linq;
using System;
using System.Text.RegularExpressions;
namespace RhSolutions.Models namespace RhSolutions.Models
{ {
public class Sku internal class Sku
{ {
private const string matchPattern = @"([1\D]|\b)(?<Article>\d{6})([1\s-]|)(?<Variant>\d{3})\b"; public string Article { get; private set; }
private string _article; public string Variant { get; private set; }
private string _variant;
public Sku(string article, string variant) public Sku(string article, string variant)
{ {
@ -17,95 +13,50 @@ namespace RhSolutions.Models
Variant = variant; Variant = variant;
} }
public string Id public static bool TryParse(string line, out Sku rehauSku)
{ {
get Match match;
match = Regex.Match(line, @"\b[1]\d{6}[1]\d{3}\b");
if (match.Success)
{ {
return $"1{Article}1{Variant}"; string sku = match.Value.Substring(1, 6);
} string variant = match.Value.Substring(8, 3);
set rehauSku = new Sku(sku, variant);
{
if (TryParse(value, out IEnumerable<Sku> skus))
{
if (skus.Count() > 1)
{
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}");
}
}
}
public string Article
{
get
{
return _article;
}
set
{
if (value == null || value.Length != 6 || value.Where(c => char.IsDigit(c)).Count() != 6)
{
throw new ArgumentException($"Wrong Article: {Article}");
}
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
{
skus = GetValidSkus(line);
return true; return true;
} }
match = Regex.Match(line, @"\b\d{6}\D\d{3}\b");
if (match.Success)
{
string sku = match.Value.Substring(0, 6);
string variant = match.Value.Substring(7, 3);
rehauSku = new Sku(sku, variant);
return true;
}
match = Regex.Match(line, @"\b\d{9}\b");
if (match.Success)
{
string sku = match.Value.Substring(0, 6);
string variant = match.Value.Substring(6, 3);
rehauSku = new Sku(sku, variant);
return true;
}
match = Regex.Match(line, @"\b\d{6}\b");
if (match.Success)
{
string sku = match.Value.Substring(0, 6);
string variant = "001";
rehauSku = new Sku(sku, variant);
return true;
}
else
{
rehauSku = null;
return false;
}
} }
public override string ToString() public override string ToString()

View File

@ -0,0 +1,12 @@
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.TryParse(sku.ToString(), out _)) if (!sku.ToString().IsRehauSku())
continue; continue;
Product p = new Product Product p = new Product

View File

@ -1,6 +1,9 @@
using Microsoft.Office.Interop.Excel; using ExcelDna.Integration;
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
{ {
@ -10,9 +13,14 @@ 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()
{ {
Excel.OnTime(DateTime.Now + new TimeSpan(0, 0, 5), "_ResetStatusBar"); Task.Delay(5000).ContinueWith(t => ResetStatusBar());
} }
} }
} }

View File

@ -34,9 +34,6 @@
<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>
@ -86,6 +83,7 @@
<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" />
@ -99,7 +97,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\RhSolutionsAddIn.cs" /> <Compile Include="AddIn\AddIn.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,13 +11,15 @@ 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 request = string.Empty;
if (Sku.TryParse(line, out var skus)) if (line.IsRehauSku())
{ {
request = @"https://rh.cebotari.ru/api/products/" + skus.FirstOrDefault().ToString(); request = @"https://rh.cebotari.ru/api/products/" + line;
} }
else else
@ -25,11 +27,11 @@ namespace RhSolutions.Services
request = @"https://rh.cebotari.ru/api/search?query=" + line; request = @"https://rh.cebotari.ru/api/search?query=" + line;
} }
var response = await RhSolutionsAddIn.HttpClient.GetAsync(request); var response = await httpClient.GetAsync(request);
try try
{ {
response.EnsureSuccessStatusCode(); response.EnsureSuccessStatusCode();
string json = await response.Content.ReadAsStringAsync(); string json = await response.Content.ReadAsStringAsync();
var product = JsonConvert.DeserializeObject<IEnumerable<Product>>(json) var product = JsonConvert.DeserializeObject<IEnumerable<Product>>(json)
.FirstOrDefault(); .FirstOrDefault();
@ -40,13 +42,21 @@ namespace RhSolutions.Services
} }
else else
{ {
return $"{product.ProductSku} {product.Name}"; if (line.IsRehauSku())
{
return product.Name;
}
else
{
return $"{product.ProductSku} {product.Name}";
}
} }
} }
catch catch
{ {
return $"Ошибка сервера {response.StatusCode}"; return $"Ошибка сервера {response.StatusCode}";
} }
} }
} }
} }