| | | 1 | | using Microsoft.EntityFrameworkCore; |
| | | 2 | | using OzonDomains.Models.Authorization; |
| | | 3 | | using OzonDomains.Models.Authorization.Enums; |
| | | 4 | | using OzonOrdersWeb.Areas.Admin.OrderList; |
| | | 5 | | using OzonRepositories.Context; |
| | | 6 | | |
| | | 7 | | namespace OzonOrdersWeb.Authorization; |
| | | 8 | | |
| | | 9 | | /// <summary> |
| | | 10 | | /// Добавляет/обновляет <see cref="AppField"/> для сущности <see cref="OrderListColumnCatalog.EntityName"/> |
| | | 11 | | /// строго по реестру колонок главной таблицы заказов. |
| | | 12 | | /// </summary> |
| | | 13 | | public static class OrderListFieldSync |
| | | 14 | | { |
| | | 15 | | public static async Task<int> EnsureInDatabaseAsync(ApplicationDbContext db, CancellationToken ct = default) |
| | | 16 | | { |
| | 0 | 17 | | var existing = await db.AppFields |
| | 0 | 18 | | .Where(f => f.EntityName == OrderListColumnCatalog.EntityName) |
| | 0 | 19 | | .ToListAsync(ct); |
| | 0 | 20 | | var byName = existing.ToDictionary(f => f.FieldName, StringComparer.OrdinalIgnoreCase); |
| | | 21 | | |
| | 0 | 22 | | var n = 0; |
| | 0 | 23 | | foreach (var col in OrderListColumnCatalog.All) |
| | | 24 | | { |
| | 0 | 25 | | if (byName.TryGetValue(col.FieldName, out var f)) |
| | | 26 | | { |
| | 0 | 27 | | if (f.DisplayName != col.DisplayName || f.SortOrder != col.SortOrder) |
| | | 28 | | { |
| | 0 | 29 | | f.DisplayName = col.DisplayName; |
| | 0 | 30 | | f.SortOrder = col.SortOrder; |
| | 0 | 31 | | f.IsActive = true; |
| | 0 | 32 | | f.UpdatedAt = DateTime.UtcNow; |
| | 0 | 33 | | n++; |
| | | 34 | | } |
| | | 35 | | } |
| | | 36 | | else |
| | | 37 | | { |
| | 0 | 38 | | db.AppFields.Add(new AppField |
| | 0 | 39 | | { |
| | 0 | 40 | | EntityName = OrderListColumnCatalog.EntityName, |
| | 0 | 41 | | FieldName = col.FieldName, |
| | 0 | 42 | | DisplayName = col.DisplayName, |
| | 0 | 43 | | FieldType = FieldType.FormField, |
| | 0 | 44 | | SortOrder = col.SortOrder, |
| | 0 | 45 | | IsActive = true, |
| | 0 | 46 | | }); |
| | 0 | 47 | | n++; |
| | | 48 | | } |
| | | 49 | | } |
| | | 50 | | |
| | 0 | 51 | | if (n > 0) await db.SaveChangesAsync(ct); |
| | 0 | 52 | | return n; |
| | 0 | 53 | | } |
| | | 54 | | } |