| | | 1 | | using System.Text.Json; |
| | | 2 | | using Microsoft.EntityFrameworkCore; |
| | | 3 | | using OzonRepositories.Context; |
| | | 4 | | |
| | | 5 | | namespace Services.BitrixStockServices; |
| | | 6 | | |
| | 0 | 7 | | public class BitrixStockSyncPresetService(ApplicationDbContext db) : IBitrixStockSyncPresetService |
| | | 8 | | { |
| | | 9 | | private const string SelectCols = "Id, Name, OzonClientId, OzonClientName, ArticleFilter, SelectedStoresJson, Disabl |
| | | 10 | | |
| | | 11 | | public async Task<List<BitrixStockSyncPreset>> GetAllAsync(CancellationToken ct = default) |
| | | 12 | | { |
| | 0 | 13 | | var conn = db.Database.GetDbConnection(); |
| | 0 | 14 | | var wasOpen = conn.State == System.Data.ConnectionState.Open; |
| | 0 | 15 | | if (!wasOpen) await conn.OpenAsync(ct); |
| | | 16 | | try |
| | | 17 | | { |
| | 0 | 18 | | using var cmd = conn.CreateCommand(); |
| | | 19 | | try |
| | | 20 | | { |
| | 0 | 21 | | cmd.CommandText = $"SELECT {SelectCols} FROM BitrixStockSyncPresets ORDER BY UpdatedAt DESC;"; |
| | 0 | 22 | | var result = new List<BitrixStockSyncPreset>(); |
| | 0 | 23 | | await using var reader = await cmd.ExecuteReaderAsync(ct); |
| | 0 | 24 | | while (await reader.ReadAsync(ct)) |
| | 0 | 25 | | result.Add(MapRow(reader)); |
| | 0 | 26 | | return result; |
| | 0 | 27 | | } |
| | | 28 | | catch |
| | | 29 | | { |
| | 0 | 30 | | cmd.CommandText = "SELECT Id, Name, OzonClientId, OzonClientName, ArticleFilter, SelectedStoresJson, NUL |
| | 0 | 31 | | var result = new List<BitrixStockSyncPreset>(); |
| | 0 | 32 | | await using var reader = await cmd.ExecuteReaderAsync(ct); |
| | 0 | 33 | | while (await reader.ReadAsync(ct)) |
| | 0 | 34 | | result.Add(MapRow(reader)); |
| | 0 | 35 | | return result; |
| | 0 | 36 | | } |
| | 0 | 37 | | } |
| | 0 | 38 | | finally { if (!wasOpen) await conn.CloseAsync(); } |
| | 0 | 39 | | } |
| | | 40 | | |
| | | 41 | | public async Task<BitrixStockSyncPreset?> GetByIdAsync(int id, CancellationToken ct = default) |
| | | 42 | | { |
| | 0 | 43 | | var conn = db.Database.GetDbConnection(); |
| | 0 | 44 | | var wasOpen = conn.State == System.Data.ConnectionState.Open; |
| | 0 | 45 | | if (!wasOpen) await conn.OpenAsync(ct); |
| | | 46 | | try |
| | | 47 | | { |
| | 0 | 48 | | using var cmd = conn.CreateCommand(); |
| | | 49 | | try |
| | | 50 | | { |
| | 0 | 51 | | cmd.CommandText = $"SELECT {SelectCols} FROM BitrixStockSyncPresets WHERE Id = @id LIMIT 1;"; |
| | 0 | 52 | | var p = cmd.CreateParameter(); p.ParameterName = "@id"; p.Value = id; |
| | 0 | 53 | | cmd.Parameters.Add(p); |
| | 0 | 54 | | await using var reader = await cmd.ExecuteReaderAsync(ct); |
| | 0 | 55 | | return await reader.ReadAsync(ct) ? MapRow(reader) : null; |
| | 0 | 56 | | } |
| | | 57 | | catch |
| | | 58 | | { |
| | 0 | 59 | | cmd.CommandText = "SELECT Id, Name, OzonClientId, OzonClientName, ArticleFilter, SelectedStoresJson, NUL |
| | 0 | 60 | | var p = cmd.CreateParameter(); p.ParameterName = "@id"; p.Value = id; |
| | 0 | 61 | | cmd.Parameters.Clear(); |
| | 0 | 62 | | cmd.Parameters.Add(p); |
| | 0 | 63 | | await using var reader = await cmd.ExecuteReaderAsync(ct); |
| | 0 | 64 | | return await reader.ReadAsync(ct) ? MapRow(reader) : null; |
| | 0 | 65 | | } |
| | 0 | 66 | | } |
| | 0 | 67 | | finally { if (!wasOpen) await conn.CloseAsync(); } |
| | 0 | 68 | | } |
| | | 69 | | |
| | | 70 | | public async Task<BitrixStockSyncPreset> CreateAsync(BitrixStockSyncPresetCreateRequest request, string? createdBy, |
| | | 71 | | { |
| | 0 | 72 | | var now = DateTime.UtcNow; |
| | 0 | 73 | | var effectiveClients = request.GetEffectiveClients(); |
| | 0 | 74 | | var primaryClient = effectiveClients.FirstOrDefault() ?? new SyncClientEntry { Id = request.OzonClientId, Name = |
| | 0 | 75 | | var storesJson = JsonSerializer.Serialize(request.SelectedStores ?? new List<string>()); |
| | 0 | 76 | | var disabledJson = JsonSerializer.Serialize(request.DisabledWarehouseMappingIds ?? new List<int>()); |
| | 0 | 77 | | var clientsJson = JsonSerializer.Serialize(effectiveClients); |
| | 0 | 78 | | var stageJson = JsonSerializer.Serialize(request.StageSettings ?? new PresetStageSettings()); |
| | | 79 | | |
| | | 80 | | try |
| | | 81 | | { |
| | 0 | 82 | | await db.Database.ExecuteSqlRawAsync( |
| | 0 | 83 | | "INSERT INTO BitrixStockSyncPresets (Name, OzonClientId, OzonClientName, ArticleFilter, SelectedStoresJs |
| | 0 | 84 | | EfSqlRawParameterGuard.Materialize(request.Name, primaryClient.Id, primaryClient.Name, request.ArticleFi |
| | 0 | 85 | | ct); |
| | 0 | 86 | | } |
| | | 87 | | catch |
| | | 88 | | { |
| | 0 | 89 | | await db.Database.ExecuteSqlRawAsync( |
| | 0 | 90 | | "INSERT INTO BitrixStockSyncPresets (Name, OzonClientId, OzonClientName, ArticleFilter, SelectedStoresJs |
| | 0 | 91 | | EfSqlRawParameterGuard.Materialize(request.Name, primaryClient.Id, primaryClient.Name, request.ArticleFi |
| | 0 | 92 | | ct); |
| | | 93 | | } |
| | | 94 | | |
| | 0 | 95 | | var conn = db.Database.GetDbConnection(); |
| | 0 | 96 | | var wasOpen = conn.State == System.Data.ConnectionState.Open; |
| | 0 | 97 | | if (!wasOpen) await conn.OpenAsync(ct); |
| | | 98 | | try |
| | | 99 | | { |
| | 0 | 100 | | using var idCmd = conn.CreateCommand(); |
| | 0 | 101 | | idCmd.CommandText = "SELECT LAST_INSERT_ID();"; |
| | 0 | 102 | | var newId = Convert.ToInt32(await idCmd.ExecuteScalarAsync(ct)); |
| | 0 | 103 | | return (await GetByIdAsync(newId, ct))!; |
| | | 104 | | } |
| | 0 | 105 | | finally { if (!wasOpen) await conn.CloseAsync(); } |
| | 0 | 106 | | } |
| | | 107 | | |
| | | 108 | | public async Task<BitrixStockSyncPreset?> UpdateAsync(int id, BitrixStockSyncPresetUpdateRequest request, Cancellati |
| | | 109 | | { |
| | 0 | 110 | | var effectiveClients = request.GetEffectiveClients(); |
| | 0 | 111 | | var primaryClient = effectiveClients.FirstOrDefault() ?? new SyncClientEntry { Id = request.OzonClientId, Name = |
| | 0 | 112 | | var storesJson = JsonSerializer.Serialize(request.SelectedStores ?? new List<string>()); |
| | 0 | 113 | | var disabledJson = JsonSerializer.Serialize(request.DisabledWarehouseMappingIds ?? new List<int>()); |
| | 0 | 114 | | var clientsJson = JsonSerializer.Serialize(effectiveClients); |
| | 0 | 115 | | var stageJson = JsonSerializer.Serialize(request.StageSettings ?? new PresetStageSettings()); |
| | 0 | 116 | | var now = DateTime.UtcNow; |
| | | 117 | | |
| | | 118 | | try |
| | | 119 | | { |
| | 0 | 120 | | await db.Database.ExecuteSqlRawAsync( |
| | 0 | 121 | | "UPDATE BitrixStockSyncPresets SET Name={0}, OzonClientId={1}, OzonClientName={2}, ArticleFilter={3}, Se |
| | 0 | 122 | | EfSqlRawParameterGuard.Materialize(request.Name, primaryClient.Id, primaryClient.Name, request.ArticleFi |
| | 0 | 123 | | ct); |
| | 0 | 124 | | } |
| | | 125 | | catch |
| | | 126 | | { |
| | 0 | 127 | | await db.Database.ExecuteSqlRawAsync( |
| | 0 | 128 | | "UPDATE BitrixStockSyncPresets SET Name={0}, OzonClientId={1}, OzonClientName={2}, ArticleFilter={3}, Se |
| | 0 | 129 | | EfSqlRawParameterGuard.Materialize(request.Name, primaryClient.Id, primaryClient.Name, request.ArticleFi |
| | 0 | 130 | | ct); |
| | | 131 | | } |
| | | 132 | | |
| | 0 | 133 | | return await GetByIdAsync(id, ct); |
| | 0 | 134 | | } |
| | | 135 | | |
| | | 136 | | public async Task<bool> DeleteAsync(int id, CancellationToken ct = default) |
| | | 137 | | { |
| | 0 | 138 | | await db.Database.ExecuteSqlRawAsync("DELETE FROM BitrixStockSyncPresets WHERE Id={0};", EfSqlRawParameterGuard. |
| | 0 | 139 | | return true; |
| | 0 | 140 | | } |
| | | 141 | | |
| | | 142 | | private static BitrixStockSyncPreset MapRow(System.Data.Common.DbDataReader r) |
| | | 143 | | { |
| | | 144 | | // Порядок колонок: 0=Id,1=Name,2=OzonClientId,3=OzonClientName,4=ArticleFilter, |
| | | 145 | | // 5=SelectedStoresJson,6=DisabledWarehouseMappingIdsJson,7=OzonClientsJson, |
| | | 146 | | // 8=StageSettingsJson,9=CreatedAt,10=UpdatedAt,11=CreatedBy |
| | 0 | 147 | | var preset = new BitrixStockSyncPreset |
| | 0 | 148 | | { |
| | 0 | 149 | | Id = r.GetInt32(0), |
| | 0 | 150 | | Name = r.GetString(1), |
| | 0 | 151 | | OzonClientId = r.GetInt32(2), |
| | 0 | 152 | | OzonClientName = r.GetString(3), |
| | 0 | 153 | | ArticleFilter = r.IsDBNull(4) ? null : r.GetString(4), |
| | 0 | 154 | | SelectedStoresJson = r.GetString(5), |
| | 0 | 155 | | DisabledWarehouseMappingIdsJson = r.IsDBNull(6) ? "[]" : r.GetString(6), |
| | 0 | 156 | | CreatedAt = r.GetDateTime(9), |
| | 0 | 157 | | UpdatedAt = r.GetDateTime(10), |
| | 0 | 158 | | CreatedBy = r.IsDBNull(11) ? null : r.GetString(11) |
| | 0 | 159 | | }; |
| | | 160 | | |
| | | 161 | | // Список клиентов (колонка 7) |
| | 0 | 162 | | var clientsJson = r.IsDBNull(7) ? null : r.GetString(7); |
| | 0 | 163 | | if (!string.IsNullOrWhiteSpace(clientsJson)) |
| | 0 | 164 | | preset.OzonClientsJson = clientsJson; |
| | | 165 | | else |
| | 0 | 166 | | preset.OzonClients = new List<SyncClientEntry> { new() { Id = preset.OzonClientId, Name = preset.OzonClientN |
| | | 167 | | |
| | | 168 | | // Настройки этапов 2/3 (колонка 8) |
| | 0 | 169 | | var stageJson = r.IsDBNull(8) ? null : r.GetString(8); |
| | 0 | 170 | | if (!string.IsNullOrWhiteSpace(stageJson)) |
| | 0 | 171 | | preset.StageSettingsJson = stageJson; |
| | | 172 | | |
| | 0 | 173 | | return preset; |
| | | 174 | | } |
| | | 175 | | } |