| | | 1 | | using Microsoft.EntityFrameworkCore; |
| | | 2 | | using Microsoft.Extensions.DependencyInjection; |
| | | 3 | | using OzonDomains.Models; |
| | | 4 | | using OzonRepositories.Context; |
| | | 5 | | |
| | | 6 | | namespace OzonRepositories.Data; |
| | | 7 | | |
| | | 8 | | public class WarehouseMappingRepository : MainRepository, IRepository<WarehouseMapping> |
| | | 9 | | { |
| | | 10 | | private readonly IServiceScopeFactory _scopeFactory; |
| | | 11 | | |
| | | 12 | | public WarehouseMappingRepository(ApplicationDbContext orderContext, IServiceScopeFactory scopeFactory) |
| | 29 | 13 | | : base(orderContext) |
| | | 14 | | { |
| | 29 | 15 | | _scopeFactory = scopeFactory; |
| | 29 | 16 | | } |
| | | 17 | | |
| | | 18 | | public async Task<int> Add(WarehouseMapping value) |
| | | 19 | | { |
| | 6 | 20 | | using var scope = _scopeFactory.CreateScope(); |
| | 6 | 21 | | var context = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>(); |
| | | 22 | | |
| | 6 | 23 | | context.WarehouseMappings.Add(value); |
| | 6 | 24 | | return await context.SaveChangesAsync(); |
| | 6 | 25 | | } |
| | | 26 | | |
| | | 27 | | public async Task<int> Delete(WarehouseMapping value) |
| | | 28 | | { |
| | 1 | 29 | | using var scope = _scopeFactory.CreateScope(); |
| | 1 | 30 | | var context = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>(); |
| | | 31 | | |
| | 1 | 32 | | context.WarehouseMappings.Remove(value); |
| | 1 | 33 | | return await context.SaveChangesAsync(); |
| | 1 | 34 | | } |
| | | 35 | | |
| | | 36 | | public async Task<List<WarehouseMapping>> Get() |
| | | 37 | | { |
| | 14 | 38 | | using var scope = _scopeFactory.CreateScope(); |
| | 14 | 39 | | var context = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>(); |
| | | 40 | | |
| | 14 | 41 | | return await context.WarehouseMappings |
| | 14 | 42 | | .Include(wm => wm.OzonClient) |
| | 14 | 43 | | .Include(wm => wm.Supplier) |
| | 14 | 44 | | .ToListAsync(); |
| | 14 | 45 | | } |
| | | 46 | | |
| | | 47 | | public async Task<WarehouseMapping?> GetAsync(int id) |
| | | 48 | | { |
| | 0 | 49 | | using var scope = _scopeFactory.CreateScope(); |
| | 0 | 50 | | var context = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>(); |
| | | 51 | | |
| | 0 | 52 | | return await context.WarehouseMappings |
| | 0 | 53 | | .Include(wm => wm.OzonClient) |
| | 0 | 54 | | .Include(wm => wm.Supplier) |
| | 0 | 55 | | .FirstOrDefaultAsync(c => c.Id == id); |
| | 0 | 56 | | } |
| | | 57 | | |
| | | 58 | | public async Task<WarehouseMapping?> GetAsync(WarehouseMapping value) |
| | | 59 | | { |
| | 0 | 60 | | using var scope = _scopeFactory.CreateScope(); |
| | 0 | 61 | | var context = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>(); |
| | | 62 | | |
| | 0 | 63 | | return await context.WarehouseMappings |
| | 0 | 64 | | .Include(wm => wm.OzonClient) |
| | 0 | 65 | | .Include(wm => wm.Supplier) |
| | 0 | 66 | | .FirstOrDefaultAsync(c => |
| | 0 | 67 | | c.BitrixWarehouseName == value.BitrixWarehouseName && |
| | 0 | 68 | | c.OzonWarehouseName == value.OzonWarehouseName && |
| | 0 | 69 | | c.OzonClientId == value.OzonClientId); |
| | 0 | 70 | | } |
| | | 71 | | |
| | | 72 | | public async Task<WarehouseMapping?> GetByOzonName(string ozonName) |
| | | 73 | | { |
| | 0 | 74 | | using var scope = _scopeFactory.CreateScope(); |
| | 0 | 75 | | var context = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>(); |
| | | 76 | | |
| | 0 | 77 | | return await context.WarehouseMappings |
| | 0 | 78 | | .Include(wm => wm.Supplier) |
| | 0 | 79 | | .FirstOrDefaultAsync(w => w.OzonWarehouseName == ozonName); |
| | 0 | 80 | | } |
| | | 81 | | |
| | | 82 | | public async Task<List<string>> GetByOzonNameAndClient(string ozonName, string clientName) |
| | | 83 | | { |
| | 0 | 84 | | using var scope = _scopeFactory.CreateScope(); |
| | 0 | 85 | | var context = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>(); |
| | | 86 | | |
| | 0 | 87 | | return await context.WarehouseMappings |
| | 0 | 88 | | .Include(o => o.OzonClient) |
| | 0 | 89 | | .Include(wm => wm.Supplier) |
| | 0 | 90 | | .Where(w => w.OzonWarehouseName == ozonName |
| | 0 | 91 | | && w.OzonClient != null && w.OzonClient.Name == clientName) |
| | 0 | 92 | | .Select(m => m.BitrixWarehouseName) |
| | 0 | 93 | | .ToListAsync(); |
| | 0 | 94 | | } |
| | | 95 | | |
| | | 96 | | // Новый метод для получения всех маппингов по нескольким Ozon названиям и клиенту |
| | | 97 | | public async Task<List<WarehouseMapping>> GetByOzonNamesAndClient(List<string> ozonNames, string clientName) |
| | | 98 | | { |
| | 0 | 99 | | using var scope = _scopeFactory.CreateScope(); |
| | 0 | 100 | | var context = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>(); |
| | | 101 | | |
| | 0 | 102 | | return await context.WarehouseMappings |
| | 0 | 103 | | .Include(o => o.OzonClient) |
| | 0 | 104 | | .Include(wm => wm.Supplier) |
| | 0 | 105 | | .Where(w => ozonNames.Contains(w.OzonWarehouseName) |
| | 0 | 106 | | && w.OzonClient != null && w.OzonClient.Name == clientName) |
| | 0 | 107 | | .ToListAsync(); |
| | 0 | 108 | | } |
| | | 109 | | |
| | | 110 | | public async Task<List<WarehouseMapping>> GetListByBitrixName(string bitrixName) |
| | | 111 | | { |
| | 0 | 112 | | using var scope = _scopeFactory.CreateScope(); |
| | 0 | 113 | | var context = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>(); |
| | | 114 | | |
| | 0 | 115 | | return await context.WarehouseMappings |
| | 0 | 116 | | .Include(wm => wm.Supplier) |
| | 0 | 117 | | .Where(w => w.BitrixWarehouseName == bitrixName) |
| | 0 | 118 | | .ToListAsync(); |
| | 0 | 119 | | } |
| | | 120 | | |
| | | 121 | | public async Task<List<string>> GetByNamByClient(OzonClient client, List<string> warehouses) |
| | | 122 | | { |
| | 0 | 123 | | using var scope = _scopeFactory.CreateScope(); |
| | 0 | 124 | | var context = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>(); |
| | | 125 | | |
| | 0 | 126 | | return await context.WarehouseMappings |
| | 0 | 127 | | .Include(wm => wm.Supplier) |
| | 0 | 128 | | .Where(w => w.OzonClientId == client.Id) |
| | 0 | 129 | | .Where(w => warehouses.Contains(w.BitrixWarehouseName)) |
| | 0 | 130 | | .Select(w => w.OzonWarehouseName) |
| | 0 | 131 | | .ToListAsync(); |
| | 0 | 132 | | } |
| | | 133 | | |
| | | 134 | | // Новый метод для поиска по ID клиента |
| | | 135 | | public async Task<List<WarehouseMapping>> GetByClientIdAsync(int clientId) |
| | | 136 | | { |
| | 34 | 137 | | using var scope = _scopeFactory.CreateScope(); |
| | 34 | 138 | | var context = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>(); |
| | | 139 | | |
| | 34 | 140 | | return await context.WarehouseMappings |
| | 34 | 141 | | .Include(wm => wm.OzonClient) |
| | 34 | 142 | | .Include(wm => wm.Supplier) |
| | 34 | 143 | | .Where(wm => wm.OzonClientId == clientId) |
| | 34 | 144 | | .ToListAsync(); |
| | 34 | 145 | | } |
| | | 146 | | |
| | | 147 | | // Новый метод для поиска конкретного сопоставления по всем параметрам |
| | | 148 | | public async Task<WarehouseMapping?> GetExactAsync(string bitrixName, string ozonName, int clientId) |
| | | 149 | | { |
| | 5 | 150 | | using var scope = _scopeFactory.CreateScope(); |
| | 5 | 151 | | var context = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>(); |
| | | 152 | | |
| | 5 | 153 | | return await context.WarehouseMappings |
| | 5 | 154 | | .Include(wm => wm.OzonClient) |
| | 5 | 155 | | .Include(wm => wm.Supplier) |
| | 5 | 156 | | .FirstOrDefaultAsync(c => |
| | 5 | 157 | | c.BitrixWarehouseName == bitrixName && |
| | 5 | 158 | | c.OzonWarehouseName == ozonName && |
| | 5 | 159 | | c.OzonClientId == clientId); |
| | 5 | 160 | | } |
| | | 161 | | |
| | | 162 | | public async Task<int> Update(WarehouseMapping value) |
| | | 163 | | { |
| | 7 | 164 | | using var scope = _scopeFactory.CreateScope(); |
| | 7 | 165 | | var context = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>(); |
| | | 166 | | |
| | 7 | 167 | | context.WarehouseMappings.Update(value); |
| | 7 | 168 | | return await context.SaveChangesAsync(); |
| | 7 | 169 | | } |
| | | 170 | | |
| | | 171 | | // Новый метод для массового получения маппингов по Bitrix названиям и клиенту |
| | | 172 | | public async Task<List<WarehouseMapping>> GetByBitrixNamesAndClient(List<string> bitrixNames, string clientName) |
| | | 173 | | { |
| | 0 | 174 | | using var scope = _scopeFactory.CreateScope(); |
| | 0 | 175 | | var context = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>(); |
| | | 176 | | |
| | 0 | 177 | | return await context.WarehouseMappings |
| | 0 | 178 | | .Include(o => o.OzonClient) |
| | 0 | 179 | | .Include(wm => wm.Supplier) |
| | 0 | 180 | | .Where(w => bitrixNames.Contains(w.BitrixWarehouseName) |
| | 0 | 181 | | && w.OzonClient != null && w.OzonClient.Name == clientName) |
| | 0 | 182 | | .ToListAsync(); |
| | 0 | 183 | | } |
| | | 184 | | |
| | | 185 | | // Новый метод для получения всех уникальных Ozon названий складов для клиента |
| | | 186 | | public async Task<List<string>> GetOzonWarehouseNamesByClient(string clientName) |
| | | 187 | | { |
| | 0 | 188 | | using var scope = _scopeFactory.CreateScope(); |
| | 0 | 189 | | var context = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>(); |
| | | 190 | | |
| | 0 | 191 | | return await context.WarehouseMappings |
| | 0 | 192 | | .Include(o => o.OzonClient) |
| | 0 | 193 | | .Include(wm => wm.Supplier) |
| | 0 | 194 | | .Where(w => w.OzonClient != null && w.OzonClient.Name == clientName) |
| | 0 | 195 | | .Select(w => w.OzonWarehouseName) |
| | 0 | 196 | | .Distinct() |
| | 0 | 197 | | .ToListAsync(); |
| | 0 | 198 | | } |
| | | 199 | | } |