| | | 1 | | using System.Linq; |
| | | 2 | | using Microsoft.EntityFrameworkCore; |
| | | 3 | | using Microsoft.Extensions.Caching.Memory; |
| | | 4 | | using OzonDomains.Models.Authorization; |
| | | 5 | | using OzonOrdersWeb.Authorization; |
| | | 6 | | using OzonRepositories.Context; |
| | | 7 | | |
| | | 8 | | namespace OzonOrdersWeb.Authorization.Seed; |
| | | 9 | | |
| | | 10 | | /// <summary> |
| | | 11 | | /// Иерархия и оформление (иконка + цвет) для страниц Studio2: заказы и транзакции, как в выпадающем меню. |
| | | 12 | | /// </summary> |
| | | 13 | | public static class Studio2AppPageHierarchySync |
| | | 14 | | { |
| | | 15 | | private const string Area = "Studio2"; |
| | | 16 | | private const string SectionPrefix = "Studio2.Section."; |
| | | 17 | | |
| | | 18 | | public static async Task<int> EnsureAsync(ApplicationDbContext db, IMemoryCache? cache, CancellationToken ct = defau |
| | | 19 | | { |
| | 0 | 20 | | var n = 0; |
| | 0 | 21 | | n += await EnsureSectionHeadersAsync(db, ct); |
| | 0 | 22 | | n += await LinkTransactionPagesAsync(db, ct); |
| | 0 | 23 | | n += await LinkOrdersPagesAsync(db, ct); |
| | 0 | 24 | | if (n > 0 && cache is not null) |
| | 0 | 25 | | AutoPageAuthorizationFilter.InvalidatePageRegistry(cache); |
| | 0 | 26 | | return n; |
| | 0 | 27 | | } |
| | | 28 | | |
| | | 29 | | private static async Task<int> EnsureSectionHeadersAsync(ApplicationDbContext db, CancellationToken ct) |
| | | 30 | | { |
| | 0 | 31 | | var changed = 0; |
| | | 32 | | |
| | | 33 | | // Порядок: сначала корень «Транзакции», затем подгруппы. |
| | 0 | 34 | | var sections = new List<(string Code, string Title, string? ParentCode, int Sort, string? Icon, string? Accent)> |
| | 0 | 35 | | { |
| | 0 | 36 | | ("Studio2.Section.Transactions", "Транзакции", null, 100, "fas fa-random", "text-secondary"), |
| | 0 | 37 | | ("Studio2.Section.TxMain", "Основные операции", "Studio2.Section.Transactions", 110, null, null), |
| | 0 | 38 | | ("Studio2.Section.TxReturns", "Возвраты", "Studio2.Section.Transactions", 200, null, null), |
| | 0 | 39 | | ("Studio2.Section.TxWarehouse", "Складские операции", "Studio2.Section.Transactions", 300, null, null), |
| | 0 | 40 | | ("Studio2.Section.TxCancel", "Отмена", "Studio2.Section.Transactions", 400, null, null), |
| | 0 | 41 | | ("Studio2.Section.Orders", "Заказы (Studio2)", null, 50, "fas fa-box-open", "text-primary"), |
| | 0 | 42 | | }; |
| | | 43 | | |
| | 0 | 44 | | foreach (var (code, title, parentCode, sort, icon, accent) in sections) |
| | | 45 | | { |
| | 0 | 46 | | int? parentId = null; |
| | 0 | 47 | | if (!string.IsNullOrEmpty(parentCode)) |
| | | 48 | | { |
| | 0 | 49 | | parentId = await db.AppPages.AsNoTracking() |
| | 0 | 50 | | .Where(x => x.Code == parentCode) |
| | 0 | 51 | | .Select(x => (int?)x.Id) |
| | 0 | 52 | | .FirstOrDefaultAsync(ct); |
| | | 53 | | } |
| | | 54 | | |
| | 0 | 55 | | var p = await db.AppPages.FirstOrDefaultAsync(x => x.Code == code, ct); |
| | 0 | 56 | | if (p is null) |
| | | 57 | | { |
| | 0 | 58 | | db.AppPages.Add(new AppPage |
| | 0 | 59 | | { |
| | 0 | 60 | | Code = code, |
| | 0 | 61 | | DisplayName = title, |
| | 0 | 62 | | Area = Area, |
| | 0 | 63 | | Controller = "Section", |
| | 0 | 64 | | Action = "H_" + (code.Length > SectionPrefix.Length ? code[SectionPrefix.Length..].Replace(".", "_") |
| | 0 | 65 | | ParentId = parentId, |
| | 0 | 66 | | SortOrder = sort, |
| | 0 | 67 | | Icon = icon, |
| | 0 | 68 | | AccentClass = accent, |
| | 0 | 69 | | IsSectionHeader = true, |
| | 0 | 70 | | IsMenuItem = false, |
| | 0 | 71 | | IsActive = true |
| | 0 | 72 | | }); |
| | 0 | 73 | | await db.SaveChangesAsync(ct); |
| | 0 | 74 | | changed++; |
| | | 75 | | } |
| | | 76 | | else |
| | | 77 | | { |
| | 0 | 78 | | var touch = false; |
| | 0 | 79 | | if (p.DisplayName != title) { p.DisplayName = title; touch = true; } |
| | 0 | 80 | | if (p.ParentId != parentId) { p.ParentId = parentId; touch = true; } |
| | 0 | 81 | | if (p.SortOrder != sort) { p.SortOrder = sort; touch = true; } |
| | 0 | 82 | | if (icon is not null && p.Icon != icon) { p.Icon = icon; touch = true; } |
| | 0 | 83 | | if (accent is not null && p.AccentClass != accent) { p.AccentClass = accent; touch = true; } |
| | 0 | 84 | | if (!p.IsSectionHeader) { p.IsSectionHeader = true; touch = true; } |
| | 0 | 85 | | if (!p.IsActive) { p.IsActive = true; touch = true; } |
| | 0 | 86 | | if (p.IsMenuItem) { p.IsMenuItem = false; touch = true; } |
| | 0 | 87 | | if (touch) { p.UpdatedAt = DateTime.UtcNow; changed++; } |
| | | 88 | | } |
| | 0 | 89 | | } |
| | | 90 | | |
| | 0 | 91 | | if (changed > 0) await db.SaveChangesAsync(ct); |
| | 0 | 92 | | return changed; |
| | 0 | 93 | | } |
| | | 94 | | |
| | | 95 | | private static async Task<int> LinkTransactionPagesAsync(ApplicationDbContext db, CancellationToken ct) |
| | | 96 | | { |
| | 0 | 97 | | var idBySection = new Dictionary<string, int>(StringComparer.Ordinal); |
| | 0 | 98 | | foreach (var key in new[] { "Studio2.Section.TxMain", "Studio2.Section.TxReturns", "Studio2.Section.TxWarehouse" |
| | | 99 | | { |
| | 0 | 100 | | var id = await GetParentIdAsync(db, key, ct); |
| | 0 | 101 | | if (id is null) return 0; |
| | 0 | 102 | | idBySection[key] = id.Value; |
| | 0 | 103 | | } |
| | | 104 | | |
| | 0 | 105 | | var map = Studio2RolePageMenuCatalog.TransactionRows |
| | 0 | 106 | | .Select(r => ( |
| | 0 | 107 | | r.Code, |
| | 0 | 108 | | idBySection[r.ParentSectionCode], |
| | 0 | 109 | | r.Sort, |
| | 0 | 110 | | r.Title, |
| | 0 | 111 | | r.Icon, |
| | 0 | 112 | | (string?)r.Accent)) |
| | 0 | 113 | | .ToArray(); |
| | | 114 | | |
| | 0 | 115 | | return await ApplyPageMetaAsync(db, map, ct); |
| | 0 | 116 | | } |
| | | 117 | | |
| | | 118 | | private static async Task<int> LinkOrdersPagesAsync(ApplicationDbContext db, CancellationToken ct) |
| | | 119 | | { |
| | 0 | 120 | | var idOrd = await GetParentIdAsync(db, "Studio2.Section.Orders", ct); |
| | 0 | 121 | | if (idOrd is null) return 0; |
| | | 122 | | |
| | 0 | 123 | | var map = Studio2RolePageMenuCatalog.OrderRows |
| | 0 | 124 | | .Select(r => ( |
| | 0 | 125 | | r.Code, |
| | 0 | 126 | | idOrd.Value, |
| | 0 | 127 | | r.Sort, |
| | 0 | 128 | | r.Title, |
| | 0 | 129 | | r.Icon, |
| | 0 | 130 | | (string?)r.Accent)) |
| | 0 | 131 | | .ToArray(); |
| | | 132 | | |
| | 0 | 133 | | return await ApplyPageMetaAsync(db, map, ct); |
| | 0 | 134 | | } |
| | | 135 | | |
| | | 136 | | private static async Task<int?> GetParentIdAsync(ApplicationDbContext db, string code, CancellationToken ct) => |
| | 0 | 137 | | await db.AppPages.AsNoTracking() |
| | 0 | 138 | | .Where(x => x.Code == code) |
| | 0 | 139 | | .Select(x => (int?)x.Id) |
| | 0 | 140 | | .FirstOrDefaultAsync(ct); |
| | | 141 | | |
| | | 142 | | private static async Task<int> ApplyPageMetaAsync( |
| | | 143 | | ApplicationDbContext db, |
| | | 144 | | (string Code, int ParentId, int Sort, string Title, string Icon, string? Accent)[] map, |
| | | 145 | | CancellationToken ct) |
| | | 146 | | { |
| | 0 | 147 | | var n = 0; |
| | 0 | 148 | | foreach (var (code, parentId, sort, title, icon, accent) in map) |
| | | 149 | | { |
| | 0 | 150 | | var p = await db.AppPages.FirstOrDefaultAsync(x => x.Code == code, ct); |
| | 0 | 151 | | if (p is null) continue; |
| | 0 | 152 | | var row = false; |
| | 0 | 153 | | if (p.ParentId != parentId) { p.ParentId = parentId; row = true; } |
| | 0 | 154 | | if (p.SortOrder != sort) { p.SortOrder = sort; row = true; } |
| | 0 | 155 | | if (p.DisplayName != title) { p.DisplayName = title; row = true; } |
| | 0 | 156 | | if (p.Icon != icon) { p.Icon = icon; row = true; } |
| | 0 | 157 | | if (p.AccentClass != accent) { p.AccentClass = accent; row = true; } |
| | 0 | 158 | | if (p.IsSectionHeader) { p.IsSectionHeader = false; row = true; } |
| | 0 | 159 | | if (row) { p.UpdatedAt = DateTime.UtcNow; n++; } |
| | 0 | 160 | | } |
| | 0 | 161 | | if (n > 0) await db.SaveChangesAsync(ct); |
| | 0 | 162 | | return n; |
| | 0 | 163 | | } |
| | | 164 | | } |