| | | 1 | | using Microsoft.EntityFrameworkCore; |
| | | 2 | | using OzonRepositories.Context; |
| | | 3 | | |
| | | 4 | | namespace Services.BitrixStockServices; |
| | | 5 | | |
| | 0 | 6 | | public class BitrixStockSyncApplyArticleLogService(IDbContextFactory<ApplicationDbContext> dbFactory) |
| | | 7 | | { |
| | | 8 | | public async Task WriteAsync( |
| | | 9 | | ApplicationDbContext db, |
| | | 10 | | string reportId, |
| | | 11 | | string jobId, |
| | | 12 | | BitrixStockSyncApplyArticleLogEntry entry, |
| | | 13 | | CancellationToken ct = default) |
| | | 14 | | { |
| | 0 | 15 | | var c1 = entry.Client1; |
| | 0 | 16 | | var c2 = entry.Client2; |
| | 0 | 17 | | await db.Database.ExecuteSqlRawAsync(@" |
| | 0 | 18 | | INSERT INTO BitrixStockSyncApplyArticleLog |
| | 0 | 19 | | (ReportId,JobId,AppliedAtUtc,Article,OneCStore,OneCQty, |
| | 0 | 20 | | Client1Id,Client1Name,Client1QtyBefore,Client1QtyAfter,Client1WasArchived,Client1ActionCode,Client1ActionLabel, |
| | 0 | 21 | | Client2Id,Client2Name,Client2QtyBefore,Client2QtyAfter,Client2WasArchived,Client2ActionCode,Client2ActionLabel, |
| | 0 | 22 | | Summary) |
| | 0 | 23 | | VALUES ({0},{1},UTC_TIMESTAMP(6),{2},{3},{4}, |
| | 0 | 24 | | {5},{6},{7},{8},{9},{10},{11}, |
| | 0 | 25 | | {12},{13},{14},{15},{16},{17},{18}, |
| | 0 | 26 | | {19});", |
| | 0 | 27 | | EfSqlRawParameterGuard.Materialize( |
| | 0 | 28 | | reportId, jobId, |
| | 0 | 29 | | entry.Article, entry.OneCStore, entry.OneCQty, |
| | 0 | 30 | | c1.ClientId, c1.ClientName, c1.QtyBefore, c1.QtyAfter, |
| | 0 | 31 | | c1.WasArchived.HasValue ? (c1.WasArchived.Value ? 1 : 0) : (object?)DBNull.Value, |
| | 0 | 32 | | c1.ActionCode, c1.ActionLabel, |
| | 0 | 33 | | c2.ClientId, c2.ClientName, c2.QtyBefore, c2.QtyAfter, |
| | 0 | 34 | | c2.WasArchived.HasValue ? (c2.WasArchived.Value ? 1 : 0) : (object?)DBNull.Value, |
| | 0 | 35 | | c2.ActionCode, c2.ActionLabel, |
| | 0 | 36 | | entry.Summary), ct); |
| | 0 | 37 | | } |
| | | 38 | | |
| | | 39 | | public async Task<BitrixStockSyncApplyArticleLogPageDto> GetPageAsync( |
| | | 40 | | string reportId, |
| | | 41 | | string? jobId = null, |
| | | 42 | | string? article = null, |
| | | 43 | | int page = 1, |
| | | 44 | | int pageSize = 50, |
| | | 45 | | CancellationToken ct = default) |
| | | 46 | | { |
| | 0 | 47 | | await using var db = await dbFactory.CreateDbContextAsync(ct); |
| | | 48 | | |
| | 0 | 49 | | var where = " WHERE ReportId = @rid"; |
| | 0 | 50 | | if (!string.IsNullOrWhiteSpace(jobId)) where += " AND JobId = @jid"; |
| | 0 | 51 | | if (!string.IsNullOrWhiteSpace(article)) where += " AND Article LIKE CONCAT('%',@art,'%')"; |
| | | 52 | | |
| | 0 | 53 | | page = Math.Max(1, page); |
| | 0 | 54 | | pageSize = Math.Clamp(pageSize, 10, 200); |
| | | 55 | | |
| | 0 | 56 | | var conn = db.Database.GetDbConnection(); |
| | 0 | 57 | | var wasOpen = conn.State == System.Data.ConnectionState.Open; |
| | 0 | 58 | | if (!wasOpen) await conn.OpenAsync(ct); |
| | | 59 | | try |
| | | 60 | | { |
| | | 61 | | // count |
| | 0 | 62 | | using var cntCmd = conn.CreateCommand(); |
| | 0 | 63 | | cntCmd.CommandText = "SELECT COUNT(*) FROM BitrixStockSyncApplyArticleLog" + where; |
| | 0 | 64 | | AddParam(cntCmd, "@rid", reportId); |
| | 0 | 65 | | if (!string.IsNullOrWhiteSpace(jobId)) AddParam(cntCmd, "@jid", jobId); |
| | 0 | 66 | | if (!string.IsNullOrWhiteSpace(article)) AddParam(cntCmd, "@art", article); |
| | 0 | 67 | | var total = Convert.ToInt32(await cntCmd.ExecuteScalarAsync(ct) ?? 0); |
| | 0 | 68 | | var totalPages = total == 0 ? 1 : (int)Math.Ceiling(total / (double)pageSize); |
| | 0 | 69 | | if (page > totalPages) page = totalPages; |
| | | 70 | | |
| | | 71 | | // rows |
| | 0 | 72 | | using var rowCmd = conn.CreateCommand(); |
| | 0 | 73 | | rowCmd.CommandText = @"SELECT |
| | 0 | 74 | | Id,ReportId,JobId,AppliedAtUtc,Article,OneCStore,OneCQty, |
| | 0 | 75 | | Client1Id,Client1Name,Client1QtyBefore,Client1QtyAfter,Client1WasArchived,Client1ActionCode,Client1ActionLabel, |
| | 0 | 76 | | Client2Id,Client2Name,Client2QtyBefore,Client2QtyAfter,Client2WasArchived,Client2ActionCode,Client2ActionLabel, |
| | 0 | 77 | | Summary |
| | 0 | 78 | | FROM BitrixStockSyncApplyArticleLog" + where + " ORDER BY Article, OneCStore LIMIT @take OFFSET @skip;"; |
| | 0 | 79 | | AddParam(rowCmd, "@rid", reportId); |
| | 0 | 80 | | if (!string.IsNullOrWhiteSpace(jobId)) AddParam(rowCmd, "@jid", jobId); |
| | 0 | 81 | | if (!string.IsNullOrWhiteSpace(article)) AddParam(rowCmd, "@art", article); |
| | 0 | 82 | | AddParam(rowCmd, "@take", pageSize); |
| | 0 | 83 | | AddParam(rowCmd, "@skip", (page - 1) * pageSize); |
| | | 84 | | |
| | 0 | 85 | | var items = new List<BitrixStockSyncApplyArticleLogEntry>(); |
| | 0 | 86 | | await using var r = await rowCmd.ExecuteReaderAsync(ct); |
| | 0 | 87 | | while (await r.ReadAsync(ct)) |
| | | 88 | | { |
| | 0 | 89 | | items.Add(new BitrixStockSyncApplyArticleLogEntry |
| | 0 | 90 | | { |
| | 0 | 91 | | Id = r.GetInt64(0), |
| | 0 | 92 | | ReportId = r.GetString(1), |
| | 0 | 93 | | JobId = r.GetString(2), |
| | 0 | 94 | | AppliedAtUtc = r.GetDateTime(3), |
| | 0 | 95 | | Article = r.GetString(4), |
| | 0 | 96 | | OneCStore = r.GetString(5), |
| | 0 | 97 | | OneCQty = r.GetInt32(6), |
| | 0 | 98 | | Client1 = new ApplyArticleClientEntry |
| | 0 | 99 | | { |
| | 0 | 100 | | ClientId = r.IsDBNull(7) ? null : r.GetInt32(7), |
| | 0 | 101 | | ClientName = r.IsDBNull(8) ? null : r.GetString(8), |
| | 0 | 102 | | QtyBefore = r.IsDBNull(9) ? null : r.GetInt32(9), |
| | 0 | 103 | | QtyAfter = r.IsDBNull(10) ? null : r.GetInt32(10), |
| | 0 | 104 | | WasArchived = r.IsDBNull(11) ? null : r.GetInt32(11) != 0, |
| | 0 | 105 | | ActionCode = r.IsDBNull(12) ? null : r.GetString(12), |
| | 0 | 106 | | ActionLabel = r.IsDBNull(13) ? null : r.GetString(13) |
| | 0 | 107 | | }, |
| | 0 | 108 | | Client2 = new ApplyArticleClientEntry |
| | 0 | 109 | | { |
| | 0 | 110 | | ClientId = r.IsDBNull(14) ? null : r.GetInt32(14), |
| | 0 | 111 | | ClientName = r.IsDBNull(15) ? null : r.GetString(15), |
| | 0 | 112 | | QtyBefore = r.IsDBNull(16) ? null : r.GetInt32(16), |
| | 0 | 113 | | QtyAfter = r.IsDBNull(17) ? null : r.GetInt32(17), |
| | 0 | 114 | | WasArchived = r.IsDBNull(18) ? null : r.GetInt32(18) != 0, |
| | 0 | 115 | | ActionCode = r.IsDBNull(19) ? null : r.GetString(19), |
| | 0 | 116 | | ActionLabel = r.IsDBNull(20) ? null : r.GetString(20) |
| | 0 | 117 | | }, |
| | 0 | 118 | | Summary = r.IsDBNull(21) ? null : r.GetString(21) |
| | 0 | 119 | | }); |
| | | 120 | | } |
| | | 121 | | |
| | 0 | 122 | | return new BitrixStockSyncApplyArticleLogPageDto |
| | 0 | 123 | | { |
| | 0 | 124 | | Items = items, TotalCount = total, |
| | 0 | 125 | | Page = page, PageSize = pageSize, TotalPages = totalPages |
| | 0 | 126 | | }; |
| | 0 | 127 | | } |
| | 0 | 128 | | finally { if (!wasOpen) await conn.CloseAsync(); } |
| | 0 | 129 | | } |
| | | 130 | | |
| | | 131 | | /// <summary>Получить список уникальных JobId для отчёта (для выбора прогона).</summary> |
| | | 132 | | public async Task<List<(string JobId, DateTime AppliedAt)>> GetJobIdsAsync( |
| | | 133 | | string reportId, CancellationToken ct = default) |
| | | 134 | | { |
| | 0 | 135 | | await using var db = await dbFactory.CreateDbContextAsync(ct); |
| | 0 | 136 | | var conn = db.Database.GetDbConnection(); |
| | 0 | 137 | | var wasOpen = conn.State == System.Data.ConnectionState.Open; |
| | 0 | 138 | | if (!wasOpen) await conn.OpenAsync(ct); |
| | | 139 | | try |
| | | 140 | | { |
| | 0 | 141 | | using var cmd = conn.CreateCommand(); |
| | 0 | 142 | | cmd.CommandText = "SELECT JobId, MIN(AppliedAtUtc) FROM BitrixStockSyncApplyArticleLog WHERE ReportId=@rid G |
| | 0 | 143 | | AddParam(cmd, "@rid", reportId); |
| | 0 | 144 | | var result = new List<(string, DateTime)>(); |
| | 0 | 145 | | await using var r = await cmd.ExecuteReaderAsync(ct); |
| | 0 | 146 | | while (await r.ReadAsync(ct)) |
| | 0 | 147 | | result.Add((r.GetString(0), r.GetDateTime(1))); |
| | 0 | 148 | | return result; |
| | 0 | 149 | | } |
| | 0 | 150 | | finally { if (!wasOpen) await conn.CloseAsync(); } |
| | 0 | 151 | | } |
| | | 152 | | |
| | | 153 | | public async Task<BitrixStockSyncApplyLogAnalysisDto> GetAnalysisAsync( |
| | | 154 | | string reportId, |
| | | 155 | | string? jobId = null, |
| | | 156 | | string? article = null, |
| | | 157 | | CancellationToken ct = default) |
| | | 158 | | { |
| | 0 | 159 | | await using var db = await dbFactory.CreateDbContextAsync(ct); |
| | | 160 | | |
| | 0 | 161 | | var where = " WHERE ReportId = @rid"; |
| | 0 | 162 | | if (!string.IsNullOrWhiteSpace(jobId)) where += " AND JobId = @jid"; |
| | 0 | 163 | | if (!string.IsNullOrWhiteSpace(article)) where += " AND Article LIKE CONCAT('%',@art,'%')"; |
| | | 164 | | |
| | 0 | 165 | | var conn = db.Database.GetDbConnection(); |
| | 0 | 166 | | var wasOpen = conn.State == System.Data.ConnectionState.Open; |
| | 0 | 167 | | if (!wasOpen) await conn.OpenAsync(ct); |
| | | 168 | | try |
| | | 169 | | { |
| | 0 | 170 | | using var cmd = conn.CreateCommand(); |
| | 0 | 171 | | cmd.CommandText = @"SELECT |
| | 0 | 172 | | Article, OneCStore, OneCQty, |
| | 0 | 173 | | Client1Id, Client1Name, Client1QtyBefore, Client1QtyAfter, Client1WasArchived, Client1ActionCode, Client1ActionLabel |
| | 0 | 174 | | FROM BitrixStockSyncApplyArticleLog" + where + " ORDER BY Article, OneCStore, AppliedAtUtc, Id;"; |
| | 0 | 175 | | AddParam(cmd, "@rid", reportId); |
| | 0 | 176 | | if (!string.IsNullOrWhiteSpace(jobId)) AddParam(cmd, "@jid", jobId); |
| | 0 | 177 | | if (!string.IsNullOrWhiteSpace(article)) AddParam(cmd, "@art", article); |
| | | 178 | | |
| | 0 | 179 | | var items = new List<BitrixStockSyncApplyLogAnalysisItemDto>(); |
| | 0 | 180 | | var index = new Dictionary<string, BitrixStockSyncApplyLogAnalysisItemDto>(StringComparer.OrdinalIgnoreCase) |
| | 0 | 181 | | var clientTotals = new Dictionary<string, int>(StringComparer.OrdinalIgnoreCase); |
| | | 182 | | |
| | 0 | 183 | | await using var r = await cmd.ExecuteReaderAsync(ct); |
| | 0 | 184 | | while (await r.ReadAsync(ct)) |
| | | 185 | | { |
| | 0 | 186 | | var itemKey = $"{r.GetString(0)}||{r.GetString(1)}"; |
| | 0 | 187 | | if (!index.TryGetValue(itemKey, out var item)) |
| | | 188 | | { |
| | 0 | 189 | | item = new BitrixStockSyncApplyLogAnalysisItemDto |
| | 0 | 190 | | { |
| | 0 | 191 | | Article = r.GetString(0), |
| | 0 | 192 | | OneCStore = r.GetString(1), |
| | 0 | 193 | | OneCQty = r.GetInt32(2) |
| | 0 | 194 | | }; |
| | 0 | 195 | | index[itemKey] = item; |
| | 0 | 196 | | items.Add(item); |
| | | 197 | | } |
| | | 198 | | |
| | 0 | 199 | | var clientName = r.IsDBNull(4) ? "—" : r.GetString(4); |
| | 0 | 200 | | var entry = new ApplyArticleClientEntry |
| | 0 | 201 | | { |
| | 0 | 202 | | ClientId = r.IsDBNull(3) ? null : r.GetInt32(3), |
| | 0 | 203 | | ClientName = clientName, |
| | 0 | 204 | | QtyBefore = r.IsDBNull(5) ? null : r.GetInt32(5), |
| | 0 | 205 | | QtyAfter = r.IsDBNull(6) ? null : r.GetInt32(6), |
| | 0 | 206 | | WasArchived = r.IsDBNull(7) ? null : r.GetInt32(7) != 0, |
| | 0 | 207 | | ActionCode = r.IsDBNull(8) ? null : r.GetString(8), |
| | 0 | 208 | | ActionLabel = r.IsDBNull(9) ? null : r.GetString(9) |
| | 0 | 209 | | }; |
| | 0 | 210 | | item.Clients.Add(entry); |
| | | 211 | | |
| | 0 | 212 | | if (!clientTotals.ContainsKey(clientName)) |
| | 0 | 213 | | clientTotals[clientName] = 0; |
| | 0 | 214 | | clientTotals[clientName]++; |
| | | 215 | | } |
| | | 216 | | |
| | 0 | 217 | | return new BitrixStockSyncApplyLogAnalysisDto |
| | 0 | 218 | | { |
| | 0 | 219 | | TotalRows = items.Count, |
| | 0 | 220 | | ClientTotals = clientTotals, |
| | 0 | 221 | | Items = items |
| | 0 | 222 | | }; |
| | 0 | 223 | | } |
| | | 224 | | finally |
| | | 225 | | { |
| | 0 | 226 | | if (!wasOpen) await conn.CloseAsync(); |
| | | 227 | | } |
| | 0 | 228 | | } |
| | | 229 | | |
| | | 230 | | private static void AddParam(System.Data.Common.DbCommand cmd, string name, object? value) |
| | | 231 | | { |
| | 0 | 232 | | var p = cmd.CreateParameter(); |
| | 0 | 233 | | p.ParameterName = name; |
| | 0 | 234 | | p.Value = value ?? DBNull.Value; |
| | 0 | 235 | | cmd.Parameters.Add(p); |
| | 0 | 236 | | } |
| | | 237 | | } |