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 Load() { if (!File.Exists(_rulesPath)) return new List(); try { var json = File.ReadAllText(_rulesPath); return JsonSerializer.Deserialize>(json) ?? new List(); } catch { return new List(); } } public void Save(IEnumerable entries) { Directory.CreateDirectory(Path.GetDirectoryName(_rulesPath)!); var json = JsonSerializer.Serialize(entries.ToList(), new JsonSerializerOptions { WriteIndented = true }); File.WriteAllText(_rulesPath, json); } // Обновляет (или добавляет) запись: запоминает выбранный декларантом код public void LearnCode(List 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 { chosenCode } }); } else { // Перемещаем выбранный код на первое место (самообучение) existing.Codes.Remove(chosenCode); existing.Codes.Insert(0, chosenCode); } Save(entries); } }