| | | 1 | | using Microsoft.EntityFrameworkCore; |
| | | 2 | | using OzonDomains.Models; |
| | | 3 | | using OzonRepositories.Context; |
| | | 4 | | |
| | | 5 | | namespace OzonRepositories.Data |
| | | 6 | | { |
| | | 7 | | public class OzonChatResponseStatisticRepository : MainRepository, IRepository<OzonChatResponseStatistic> |
| | | 8 | | { |
| | 1 | 9 | | public OzonChatResponseStatisticRepository(ApplicationDbContext orderContext) : base(orderContext) |
| | | 10 | | { |
| | 1 | 11 | | } |
| | | 12 | | |
| | | 13 | | public async Task<int> Add(OzonChatResponseStatistic value) |
| | | 14 | | { |
| | 0 | 15 | | await _context.OzonChatResponseStatistics.AddAsync(value); |
| | 0 | 16 | | return await _context.SaveChangesAsync(); |
| | 0 | 17 | | } |
| | | 18 | | |
| | | 19 | | public async Task<int> Delete(OzonChatResponseStatistic value) |
| | | 20 | | { |
| | 0 | 21 | | _context.OzonChatResponseStatistics.Remove(value); |
| | 0 | 22 | | return await _context.SaveChangesAsync(); |
| | 0 | 23 | | } |
| | | 24 | | |
| | | 25 | | public async Task<List<OzonChatResponseStatistic>> Get() |
| | | 26 | | { |
| | 0 | 27 | | return await _context.OzonChatResponseStatistics |
| | 0 | 28 | | .Include(x => x.Client) |
| | 0 | 29 | | .Include(x => x.Order) |
| | 0 | 30 | | .OrderByDescending(x => x.ClientMessageDate) |
| | 0 | 31 | | .ToListAsync(); |
| | 0 | 32 | | } |
| | | 33 | | |
| | | 34 | | public async Task<OzonChatResponseStatistic?> GetAsync(int id) |
| | | 35 | | { |
| | 0 | 36 | | return await _context.OzonChatResponseStatistics |
| | 0 | 37 | | .Include(x => x.Client) |
| | 0 | 38 | | .Include(x => x.Order) |
| | 0 | 39 | | .FirstOrDefaultAsync(x => x.Id == id) ?? new OzonChatResponseStatistic(); |
| | 0 | 40 | | } |
| | | 41 | | |
| | | 42 | | public async Task<OzonChatResponseStatistic?> GetAsync(OzonChatResponseStatistic value) |
| | | 43 | | { |
| | 0 | 44 | | return await _context.OzonChatResponseStatistics |
| | 0 | 45 | | .Include(x => x.Client) |
| | 0 | 46 | | .Include(x => x.Order) |
| | 0 | 47 | | .FirstOrDefaultAsync(x => x.ChatId == value.ChatId && |
| | 0 | 48 | | x.ClientMessageId == value.ClientMessageId) ?? new OzonChatResponseStatistic() |
| | 0 | 49 | | } |
| | | 50 | | |
| | | 51 | | public async Task<int> Update(OzonChatResponseStatistic value) |
| | | 52 | | { |
| | 0 | 53 | | value.UpdatedAt = DateTime.UtcNow; |
| | 0 | 54 | | _context.OzonChatResponseStatistics.Update(value); |
| | 0 | 55 | | return await _context.SaveChangesAsync(); |
| | 0 | 56 | | } |
| | | 57 | | |
| | | 58 | | /// <summary> |
| | | 59 | | /// Получить статистику по чату |
| | | 60 | | /// </summary> |
| | | 61 | | public async Task<List<OzonChatResponseStatistic>> GetByChatIdAsync(string chatId) |
| | | 62 | | { |
| | 0 | 63 | | return await _context.OzonChatResponseStatistics |
| | 0 | 64 | | .Include(x => x.Client) |
| | 0 | 65 | | .Include(x => x.Order) |
| | 0 | 66 | | .Where(x => x.ChatId == chatId) |
| | 0 | 67 | | .OrderByDescending(x => x.ClientMessageDate) |
| | 0 | 68 | | .ToListAsync(); |
| | 0 | 69 | | } |
| | | 70 | | |
| | | 71 | | /// <summary> |
| | | 72 | | /// Получить статистику по клиенту (фильтруем по дате ответа или создания записи) |
| | | 73 | | /// </summary> |
| | | 74 | | public async Task<List<OzonChatResponseStatistic>> GetByClientIdAsync(int clientId, DateTime? from = null, DateT |
| | | 75 | | { |
| | 0 | 76 | | var query = _context.OzonChatResponseStatistics |
| | 0 | 77 | | .Include(x => x.Client) |
| | 0 | 78 | | .Include(x => x.Order) |
| | 0 | 79 | | .Where(x => x.ClientId == clientId); |
| | | 80 | | |
| | 0 | 81 | | if (from.HasValue) |
| | 0 | 82 | | query = query.Where(x => (x.SellerResponseDate ?? x.CreatedAt) >= from.Value); |
| | | 83 | | |
| | 0 | 84 | | if (to.HasValue) |
| | 0 | 85 | | query = query.Where(x => (x.SellerResponseDate ?? x.CreatedAt) <= to.Value); |
| | | 86 | | |
| | 0 | 87 | | return await query |
| | 0 | 88 | | .OrderByDescending(x => x.SellerResponseDate ?? x.CreatedAt) |
| | 0 | 89 | | .ToListAsync(); |
| | 0 | 90 | | } |
| | | 91 | | |
| | | 92 | | /// <summary> |
| | | 93 | | /// Получить неотвеченные сообщения по клиенту |
| | | 94 | | /// </summary> |
| | | 95 | | public async Task<List<OzonChatResponseStatistic>> GetUnansweredByClientIdAsync(int clientId) |
| | | 96 | | { |
| | 0 | 97 | | return await _context.OzonChatResponseStatistics |
| | 0 | 98 | | .Include(x => x.Client) |
| | 0 | 99 | | .Include(x => x.Order) |
| | 0 | 100 | | .Where(x => x.ClientId == clientId && !x.IsAnswered) |
| | 0 | 101 | | .OrderBy(x => x.ClientMessageDate) |
| | 0 | 102 | | .ToListAsync(); |
| | 0 | 103 | | } |
| | | 104 | | |
| | | 105 | | /// <summary> |
| | | 106 | | /// Получить статистику по заказу |
| | | 107 | | /// </summary> |
| | | 108 | | public async Task<List<OzonChatResponseStatistic>> GetByOrderIdAsync(int orderId) |
| | | 109 | | { |
| | 0 | 110 | | return await _context.OzonChatResponseStatistics |
| | 0 | 111 | | .Include(x => x.Client) |
| | 0 | 112 | | .Include(x => x.Order) |
| | 0 | 113 | | .Where(x => x.OrderId == orderId) |
| | 0 | 114 | | .OrderByDescending(x => x.ClientMessageDate) |
| | 0 | 115 | | .ToListAsync(); |
| | 0 | 116 | | } |
| | | 117 | | |
| | | 118 | | /// <summary> |
| | | 119 | | /// Получить запись по ID сообщения клиента |
| | | 120 | | /// </summary> |
| | | 121 | | public async Task<OzonChatResponseStatistic?> GetByClientMessageIdAsync(string chatId, long clientMessageId) |
| | | 122 | | { |
| | 0 | 123 | | return await _context.OzonChatResponseStatistics |
| | 0 | 124 | | .Include(x => x.Client) |
| | 0 | 125 | | .Include(x => x.Order) |
| | 0 | 126 | | .FirstOrDefaultAsync(x => x.ChatId == chatId && x.ClientMessageId == clientMessageId); |
| | 0 | 127 | | } |
| | | 128 | | |
| | | 129 | | /// <summary> |
| | | 130 | | /// Обновить информацию об ответе продавца |
| | | 131 | | /// </summary> |
| | | 132 | | public async Task<int> UpdateResponseAsync(string chatId, long clientMessageId, |
| | | 133 | | long sellerResponseMessageId, string sellerResponseText, DateTime sellerResponseDate, string? responderName |
| | | 134 | | { |
| | 0 | 135 | | var statistic = await GetByClientMessageIdAsync(chatId, clientMessageId); |
| | | 136 | | |
| | 0 | 137 | | if (statistic == null) |
| | 0 | 138 | | return 0; |
| | | 139 | | |
| | 0 | 140 | | statistic.SellerResponseMessageId = sellerResponseMessageId; |
| | 0 | 141 | | statistic.SellerResponseText = sellerResponseText; |
| | 0 | 142 | | statistic.SellerResponseDate = sellerResponseDate; |
| | 0 | 143 | | statistic.ResponseTimeSeconds = (int)(sellerResponseDate - statistic.ClientMessageDate).TotalSeconds; |
| | 0 | 144 | | statistic.IsAnswered = true; |
| | 0 | 145 | | statistic.ResponderName = responderName; |
| | 0 | 146 | | statistic.UpdatedAt = DateTime.UtcNow; |
| | | 147 | | |
| | 0 | 148 | | return await Update(statistic); |
| | 0 | 149 | | } |
| | | 150 | | |
| | | 151 | | /// <summary> |
| | | 152 | | /// Получить среднее время ответа по клиенту за период |
| | | 153 | | /// </summary> |
| | | 154 | | public async Task<double?> GetAverageResponseTimeAsync(int clientId, DateTime? from = null, DateTime? to = null) |
| | | 155 | | { |
| | 0 | 156 | | var query = _context.OzonChatResponseStatistics |
| | 0 | 157 | | .Where(x => x.ClientId == clientId && x.IsAnswered && x.ResponseTimeSeconds.HasValue); |
| | | 158 | | |
| | 0 | 159 | | if (from.HasValue) |
| | 0 | 160 | | query = query.Where(x => x.ClientMessageDate >= from.Value); |
| | | 161 | | |
| | 0 | 162 | | if (to.HasValue) |
| | 0 | 163 | | query = query.Where(x => x.ClientMessageDate <= to.Value); |
| | | 164 | | |
| | 0 | 165 | | if (!await query.AnyAsync()) |
| | 0 | 166 | | return null; |
| | | 167 | | |
| | 0 | 168 | | return await query.AverageAsync(x => x.ResponseTimeSeconds!.Value); |
| | 0 | 169 | | } |
| | | 170 | | |
| | | 171 | | /// <summary> |
| | | 172 | | /// Получить статистику ответов (всего/отвечено) по клиенту за период |
| | | 173 | | /// </summary> |
| | | 174 | | public async Task<(int Total, int Answered, double AnswerRate)> GetAnswerStatsAsync( |
| | | 175 | | int clientId, DateTime? from = null, DateTime? to = null) |
| | | 176 | | { |
| | 0 | 177 | | var query = _context.OzonChatResponseStatistics |
| | 0 | 178 | | .Where(x => x.ClientId == clientId); |
| | | 179 | | |
| | 0 | 180 | | if (from.HasValue) |
| | 0 | 181 | | query = query.Where(x => x.ClientMessageDate >= from.Value); |
| | | 182 | | |
| | 0 | 183 | | if (to.HasValue) |
| | 0 | 184 | | query = query.Where(x => x.ClientMessageDate <= to.Value); |
| | | 185 | | |
| | 0 | 186 | | var total = await query.CountAsync(); |
| | 0 | 187 | | var answered = await query.CountAsync(x => x.IsAnswered); |
| | 0 | 188 | | var answerRate = total > 0 ? (double)answered / total * 100 : 0; |
| | | 189 | | |
| | 0 | 190 | | return (total, answered, answerRate); |
| | 0 | 191 | | } |
| | | 192 | | |
| | | 193 | | /// <summary> |
| | | 194 | | /// Получить топ-N самых медленных ответов по клиенту |
| | | 195 | | /// </summary> |
| | | 196 | | public async Task<List<OzonChatResponseStatistic>> GetSlowestResponsesAsync(int clientId, int topN = 10) |
| | | 197 | | { |
| | 0 | 198 | | return await _context.OzonChatResponseStatistics |
| | 0 | 199 | | .Include(x => x.Client) |
| | 0 | 200 | | .Include(x => x.Order) |
| | 0 | 201 | | .Where(x => x.ClientId == clientId && x.IsAnswered && x.ResponseTimeSeconds.HasValue) |
| | 0 | 202 | | .OrderByDescending(x => x.ResponseTimeSeconds) |
| | 0 | 203 | | .Take(topN) |
| | 0 | 204 | | .ToListAsync(); |
| | 0 | 205 | | } |
| | | 206 | | |
| | | 207 | | /// <summary> |
| | | 208 | | /// Получить статистику по типам чатов |
| | | 209 | | /// </summary> |
| | | 210 | | public async Task<Dictionary<string, int>> GetStatsByChatTypeAsync(int clientId, DateTime? from = null, DateTime |
| | | 211 | | { |
| | 0 | 212 | | var query = _context.OzonChatResponseStatistics |
| | 0 | 213 | | .Where(x => x.ClientId == clientId && !string.IsNullOrEmpty(x.ChatType)); |
| | | 214 | | |
| | 0 | 215 | | if (from.HasValue) |
| | 0 | 216 | | query = query.Where(x => x.ClientMessageDate >= from.Value); |
| | | 217 | | |
| | 0 | 218 | | if (to.HasValue) |
| | 0 | 219 | | query = query.Where(x => x.ClientMessageDate <= to.Value); |
| | | 220 | | |
| | 0 | 221 | | return await query |
| | 0 | 222 | | .GroupBy(x => x.ChatType) |
| | 0 | 223 | | .Select(g => new { ChatType = g.Key!, Count = g.Count() }) |
| | 0 | 224 | | .ToDictionaryAsync(x => x.ChatType, x => x.Count); |
| | 0 | 225 | | } |
| | | 226 | | |
| | | 227 | | public new async Task<int> SaveChanges() |
| | | 228 | | { |
| | 0 | 229 | | return await _context.SaveChangesAsync(); |
| | 0 | 230 | | } |
| | | 231 | | |
| | | 232 | | /// <summary> |
| | | 233 | | /// Получить ChatId чатов, где сообщение покупателя содержит поисковый запрос |
| | | 234 | | /// </summary> |
| | | 235 | | public async Task<List<string>> GetChatIdsByMessageQueryAsync(string query, int clientId = 0, int limit = 100) |
| | | 236 | | { |
| | 1 | 237 | | var q = _context.OzonChatResponseStatistics |
| | 1 | 238 | | .AsNoTracking() |
| | 1 | 239 | | .Where(x => x.ClientMessageText != null && |
| | 1 | 240 | | EF.Functions.Like(x.ClientMessageText, $"%{query}%")); |
| | | 241 | | |
| | 1 | 242 | | if (clientId > 0) |
| | 1 | 243 | | q = q.Where(x => x.ClientId == clientId); |
| | | 244 | | |
| | 1 | 245 | | return await q |
| | 1 | 246 | | .Select(x => x.ChatId) |
| | 1 | 247 | | .Distinct() |
| | 1 | 248 | | .OrderBy(x => x) |
| | 1 | 249 | | .Take(limit) |
| | 1 | 250 | | .ToListAsync(); |
| | 1 | 251 | | } |
| | | 252 | | |
| | | 253 | | /// <summary> |
| | | 254 | | /// Получить образцы сообщений покупателей для построения списка подсказок |
| | | 255 | | /// </summary> |
| | | 256 | | public async Task<List<(string ChatId, string MessageText, string? PostingNumber, int ClientId)>> GetSampleBuyer |
| | | 257 | | string query, int clientId = 0, int limit = 200) |
| | | 258 | | { |
| | 0 | 259 | | var q = _context.OzonChatResponseStatistics |
| | 0 | 260 | | .AsNoTracking() |
| | 0 | 261 | | .Where(x => x.ClientMessageText != null && |
| | 0 | 262 | | EF.Functions.Like(x.ClientMessageText, $"%{query}%")); |
| | | 263 | | |
| | 0 | 264 | | if (clientId > 0) |
| | 0 | 265 | | q = q.Where(x => x.ClientId == clientId); |
| | | 266 | | |
| | 0 | 267 | | var rows = await q |
| | 0 | 268 | | .OrderByDescending(x => x.ClientMessageDate) |
| | 0 | 269 | | .Take(limit) |
| | 0 | 270 | | .Select(x => new { x.ChatId, x.ClientMessageText, x.PostingNumber, x.ClientId }) |
| | 0 | 271 | | .ToListAsync(); |
| | | 272 | | |
| | 0 | 273 | | return rows.Select(r => (r.ChatId, r.ClientMessageText!, r.PostingNumber, r.ClientId)).ToList(); |
| | 0 | 274 | | } |
| | | 275 | | } |
| | | 276 | | } |