059895d1c3
- Чтение СПРАВКИ из Excel (ClosedXML), поддержка нескольких файлов - Группировка по ТН ВЭД: схлопывание строк с суммированием кол-ва/веса/суммы - Автоназначение кодов деклараций по справочнику ТН ВЭД (87 пар) - Цветовая маркировка: зелёный/жёлтый/красный по уровню уверенности - Самообучение: ручной выбор кода сохраняется в tnved_codes.json - Формирование Лист3 с разворачиванием строк по рег. номерам (ключевая функция) - Экспорт Лист2 + Лист3 в Excel
68 lines
2.0 KiB
C#
68 lines
2.0 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Text.Json;
|
|
using DeclarationAutomatization.Models;
|
|
|
|
namespace DeclarationAutomatization.Services;
|
|
|
|
public class RulesPersistenceService
|
|
{
|
|
private readonly string _rulesPath;
|
|
|
|
public RulesPersistenceService()
|
|
{
|
|
// Храним рядом с исполняемым файлом приложения
|
|
var appDir = AppContext.BaseDirectory;
|
|
_rulesPath = Path.Combine(appDir, "Data", "tnved_codes.json");
|
|
}
|
|
|
|
public List<CodeLookupEntry> Load()
|
|
{
|
|
if (!File.Exists(_rulesPath))
|
|
return new List<CodeLookupEntry>();
|
|
|
|
try
|
|
{
|
|
var json = File.ReadAllText(_rulesPath);
|
|
return JsonSerializer.Deserialize<List<CodeLookupEntry>>(json)
|
|
?? new List<CodeLookupEntry>();
|
|
}
|
|
catch
|
|
{
|
|
return new List<CodeLookupEntry>();
|
|
}
|
|
}
|
|
|
|
public void Save(IEnumerable<CodeLookupEntry> entries)
|
|
{
|
|
Directory.CreateDirectory(Path.GetDirectoryName(_rulesPath)!);
|
|
var json = JsonSerializer.Serialize(entries.ToList(),
|
|
new JsonSerializerOptions { WriteIndented = true });
|
|
File.WriteAllText(_rulesPath, json);
|
|
}
|
|
|
|
// Обновляет (или добавляет) запись: запоминает выбранный декларантом код
|
|
public void LearnCode(List<CodeLookupEntry> entries, string tnVed, string chosenCode)
|
|
{
|
|
var existing = entries.FirstOrDefault(e => e.TnVed == tnVed);
|
|
if (existing == null)
|
|
{
|
|
entries.Add(new CodeLookupEntry
|
|
{
|
|
TnVed = tnVed,
|
|
Codes = new List<string> { chosenCode }
|
|
});
|
|
}
|
|
else
|
|
{
|
|
// Перемещаем выбранный код на первое место (самообучение)
|
|
existing.Codes.Remove(chosenCode);
|
|
existing.Codes.Insert(0, chosenCode);
|
|
}
|
|
|
|
Save(entries);
|
|
}
|
|
}
|