| | | 1 | | using Microsoft.EntityFrameworkCore; |
| | | 2 | | using OzonDomains; |
| | | 3 | | using OzonDomains.Models.SupplierStatuses; |
| | | 4 | | using OzonRepositories.Context; |
| | | 5 | | |
| | | 6 | | namespace Services.SupplierStatusServices; |
| | | 7 | | |
| | | 8 | | public interface ISupplierStatusDirectoryService |
| | | 9 | | { |
| | | 10 | | Task<List<SupplierStatus>> GetAsync(string? providerKey, string? statusKind, CancellationToken ct = default); |
| | | 11 | | |
| | | 12 | | Task<List<string>> GetProviderKeysAsync(CancellationToken ct = default); |
| | | 13 | | |
| | | 14 | | Task<SupplierStatus?> GetByIdAsync(int id, CancellationToken ct = default); |
| | | 15 | | |
| | | 16 | | Task UpdateAsync(int id, string name, bool isFinal, int? sortOrder, CancellationToken ct = default); |
| | | 17 | | |
| | | 18 | | Task UpdateStatusColorAsync(int id, string colorCode, CancellationToken ct = default); |
| | | 19 | | |
| | | 20 | | Task ResetStatusColorAsync(int id, CancellationToken ct = default); |
| | | 21 | | |
| | | 22 | | /// <summary>Удаляет статус. Заказы, ссылающиеся на него, теряют только отображение статуса.</summary> |
| | | 23 | | Task<bool> DeleteAsync(int id, CancellationToken ct = default); |
| | | 24 | | } |
| | | 25 | | |
| | | 26 | | /// <summary>Справочник статусов поставщиков: чтение и ручное редактирование администратором.</summary> |
| | | 27 | | public sealed class SupplierStatusDirectoryService : ISupplierStatusDirectoryService |
| | | 28 | | { |
| | | 29 | | private readonly ApplicationDbContext _db; |
| | | 30 | | |
| | 44 | 31 | | public SupplierStatusDirectoryService(ApplicationDbContext db) => _db = db; |
| | | 32 | | |
| | | 33 | | public Task<List<SupplierStatus>> GetAsync(string? providerKey, string? statusKind, CancellationToken ct = default) |
| | | 34 | | { |
| | 5 | 35 | | var query = _db.SupplierStatuses |
| | 5 | 36 | | .Include(x => x.StatusColor) |
| | 5 | 37 | | .Include(x => x.Supplier) |
| | 5 | 38 | | .AsNoTracking() |
| | 5 | 39 | | .AsQueryable(); |
| | | 40 | | |
| | 5 | 41 | | if (!string.IsNullOrWhiteSpace(providerKey)) |
| | 3 | 42 | | query = query.Where(x => x.ProviderKey == providerKey); |
| | | 43 | | |
| | 5 | 44 | | if (!string.IsNullOrWhiteSpace(statusKind)) |
| | 2 | 45 | | query = query.Where(x => x.StatusKind == statusKind); |
| | | 46 | | |
| | 5 | 47 | | return query |
| | 5 | 48 | | .OrderBy(x => x.ProviderKey) |
| | 5 | 49 | | .ThenBy(x => x.StatusKind) |
| | 5 | 50 | | .ThenBy(x => x.SortOrder ?? int.MaxValue) |
| | 5 | 51 | | .ThenBy(x => x.Name) |
| | 5 | 52 | | .ToListAsync(ct); |
| | | 53 | | } |
| | | 54 | | |
| | | 55 | | public Task<List<string>> GetProviderKeysAsync(CancellationToken ct = default) => |
| | 2 | 56 | | _db.SupplierStatuses |
| | 2 | 57 | | .Select(x => x.ProviderKey) |
| | 2 | 58 | | .Distinct() |
| | 2 | 59 | | .OrderBy(x => x) |
| | 2 | 60 | | .ToListAsync(ct); |
| | | 61 | | |
| | | 62 | | public Task<SupplierStatus?> GetByIdAsync(int id, CancellationToken ct = default) => |
| | 2 | 63 | | _db.SupplierStatuses |
| | 2 | 64 | | .Include(x => x.StatusColor) |
| | 2 | 65 | | .Include(x => x.Supplier) |
| | 2 | 66 | | .FirstOrDefaultAsync(x => x.Id == id, ct); |
| | | 67 | | |
| | | 68 | | public async Task UpdateAsync(int id, string name, bool isFinal, int? sortOrder, CancellationToken ct = default) |
| | | 69 | | { |
| | 6 | 70 | | var status = await _db.SupplierStatuses.FindAsync([id], ct); |
| | 7 | 71 | | if (status is null) return; |
| | | 72 | | |
| | 5 | 73 | | if (!string.IsNullOrWhiteSpace(name)) |
| | 2 | 74 | | status.Name = name.Trim().Length > 200 ? name.Trim()[..200] : name.Trim(); |
| | 5 | 75 | | status.IsFinal = isFinal; |
| | 5 | 76 | | status.SortOrder = sortOrder; |
| | 5 | 77 | | await _db.SaveChangesAsync(ct); |
| | 6 | 78 | | } |
| | | 79 | | |
| | | 80 | | /// <summary>Цвета переиспользуются между всеми справочниками статусов — таблица StatusColors общая.</summary> |
| | | 81 | | public async Task UpdateStatusColorAsync(int id, string colorCode, CancellationToken ct = default) |
| | | 82 | | { |
| | 3 | 83 | | var status = await _db.SupplierStatuses.FindAsync([id], ct); |
| | 4 | 84 | | if (status is null) return; |
| | | 85 | | |
| | 2 | 86 | | var color = await _db.StatusColors.FirstOrDefaultAsync(c => c.ColorCode == colorCode, ct); |
| | 2 | 87 | | if (color is null) |
| | | 88 | | { |
| | 1 | 89 | | color = new StatusColor { ColorCode = colorCode }; |
| | 1 | 90 | | await _db.StatusColors.AddAsync(color, ct); |
| | 1 | 91 | | await _db.SaveChangesAsync(ct); |
| | | 92 | | } |
| | | 93 | | |
| | 2 | 94 | | status.StatusColorId = color.Id; |
| | 2 | 95 | | await _db.SaveChangesAsync(ct); |
| | 3 | 96 | | } |
| | | 97 | | |
| | | 98 | | public async Task ResetStatusColorAsync(int id, CancellationToken ct = default) |
| | | 99 | | { |
| | 2 | 100 | | var status = await _db.SupplierStatuses.FindAsync([id], ct); |
| | 3 | 101 | | if (status is null) return; |
| | | 102 | | |
| | 1 | 103 | | status.StatusColorId = null; |
| | 1 | 104 | | await _db.SaveChangesAsync(ct); |
| | 2 | 105 | | } |
| | | 106 | | |
| | | 107 | | public async Task<bool> DeleteAsync(int id, CancellationToken ct = default) |
| | | 108 | | { |
| | 2 | 109 | | var status = await _db.SupplierStatuses.FindAsync([id], ct); |
| | 3 | 110 | | if (status is null) return false; |
| | | 111 | | |
| | | 112 | | // FK на OrderSupplierStates объявлен как SET NULL: строки состояний остаются, |
| | | 113 | | // просто теряют ссылку и будут переназначены при следующем опросе поставщика. |
| | 1 | 114 | | _db.SupplierStatuses.Remove(status); |
| | 1 | 115 | | await _db.SaveChangesAsync(ct); |
| | 1 | 116 | | return true; |
| | 2 | 117 | | } |
| | | 118 | | } |