< Summary

Information
Class: Services.DataServices.OzonChatResponseStatisticDataServices
Assembly: Services
File(s): /home/runner/work/Studio2Prod/Studio2Prod/AppRepository/DataServices/OzonChatResponseStatisticDataServices.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 98
Coverable lines: 98
Total lines: 249
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 14
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

File(s)

/home/runner/work/Studio2Prod/Studio2Prod/AppRepository/DataServices/OzonChatResponseStatisticDataServices.cs

#LineLine coverage
 1using OzonDomains.Models;
 2using OzonRepositories.Data;
 3
 4namespace Services.DataServices
 5{
 6    public class OzonChatResponseStatisticDataServices : IDataServices
 7    {
 8        private readonly OzonChatResponseStatisticRepository _repository;
 9
 010        public OzonChatResponseStatisticDataServices(OzonChatResponseStatisticRepository repository)
 11        {
 012            _repository = repository;
 013        }
 14
 15        public async Task<int> Add(OzonChatResponseStatistic value)
 16        {
 017            return await _repository.Add(value);
 018        }
 19
 20        public async Task<int> Delete(OzonChatResponseStatistic value)
 21        {
 022            return await _repository.Delete(value);
 023        }
 24
 25        public async Task<List<OzonChatResponseStatistic>> Get()
 26        {
 027            return await _repository.Get();
 028        }
 29
 30        public async Task<OzonChatResponseStatistic?> GetAsync(int id)
 31        {
 032            return await _repository.GetAsync(id);
 033        }
 34
 35        public async Task<OzonChatResponseStatistic?> GetAsync(OzonChatResponseStatistic value)
 36        {
 037            return await _repository.GetAsync(value);
 038        }
 39
 40        public async Task<int> Update(OzonChatResponseStatistic value)
 41        {
 042            return await _repository.Update(value);
 043        }
 44
 45        public async Task<int> SaveChanges()
 46        {
 047            return await _repository.SaveChanges();
 048        }
 49
 50        /// <summary>
 51        /// Получить статистику по чату
 52        /// </summary>
 53        public async Task<List<OzonChatResponseStatistic>> GetByChatIdAsync(string chatId)
 54        {
 055            return await _repository.GetByChatIdAsync(chatId);
 056        }
 57
 58        /// <summary>
 59        /// Получить статистику по клиенту
 60        /// </summary>
 61        public async Task<List<OzonChatResponseStatistic>> GetByClientIdAsync(int clientId, DateTime? from = null, DateT
 62        {
 063            return await _repository.GetByClientIdAsync(clientId, from, to);
 064        }
 65
 66        /// <summary>
 67        /// Получить неотвеченные сообщения по клиенту
 68        /// </summary>
 69        public async Task<List<OzonChatResponseStatistic>> GetUnansweredByClientIdAsync(int clientId)
 70        {
 071            return await _repository.GetUnansweredByClientIdAsync(clientId);
 072        }
 73
 74        /// <summary>
 75        /// Получить статистику по заказу
 76        /// </summary>
 77        public async Task<List<OzonChatResponseStatistic>> GetByOrderIdAsync(int orderId)
 78        {
 079            return await _repository.GetByOrderIdAsync(orderId);
 080        }
 81
 82        /// <summary>
 83        /// Получить запись по ID сообщения клиента
 84        /// </summary>
 85        public async Task<OzonChatResponseStatistic?> GetByClientMessageIdAsync(string chatId, long clientMessageId)
 86        {
 087            return await _repository.GetByClientMessageIdAsync(chatId, clientMessageId);
 088        }
 89
 90        /// <summary>
 91        /// Создать или обновить запись статистики сообщения клиента
 92        /// </summary>
 93        public async Task<OzonChatResponseStatistic> CreateOrUpdateClientMessageAsync(
 94            string chatId,
 95            long clientMessageId,
 96            string clientMessageText,
 97            DateTime clientMessageDate,
 98            int clientId,
 99            int? orderId = null,
 100            string? postingNumber = null,
 101            string? chatType = null,
 102            string? chatStatus = null)
 103        {
 0104            var existing = await _repository.GetByClientMessageIdAsync(chatId, clientMessageId);
 105
 0106            if (existing != null)
 107            {
 108                // Обновляем только если есть изменения
 0109                existing.ClientMessageText = clientMessageText;
 0110                existing.ClientMessageDate = clientMessageDate;
 0111                existing.OrderId = orderId ?? existing.OrderId;
 0112                existing.PostingNumber = postingNumber ?? existing.PostingNumber;
 0113                existing.ChatType = chatType ?? existing.ChatType;
 0114                existing.ChatStatus = chatStatus ?? existing.ChatStatus;
 0115                await _repository.Update(existing);
 0116                return existing;
 117            }
 118
 119            // Создаем новую запись
 0120            var newStatistic = new OzonChatResponseStatistic
 0121            {
 0122                ChatId = chatId,
 0123                ClientId = clientId,
 0124                OrderId = orderId,
 0125                PostingNumber = postingNumber,
 0126                ClientMessageId = clientMessageId,
 0127                ClientMessageText = clientMessageText,
 0128                ClientMessageDate = clientMessageDate,
 0129                ChatType = chatType,
 0130                ChatStatus = chatStatus,
 0131                IsAnswered = false,
 0132                CreatedAt = DateTime.UtcNow,
 0133                UpdatedAt = DateTime.UtcNow
 0134            };
 135
 0136            await _repository.Add(newStatistic);
 0137            return newStatistic;
 0138        }
 139
 140        /// <summary>
 141        /// Обновить информацию об ответе продавца
 142        /// </summary>
 143        public async Task<int> UpdateResponseAsync(
 144            string chatId,
 145            long clientMessageId,
 146            long sellerResponseMessageId,
 147            string sellerResponseText,
 148            DateTime sellerResponseDate,
 149            string? responderName = null)
 150        {
 0151            return await _repository.UpdateResponseAsync(
 0152                chatId,
 0153                clientMessageId,
 0154                sellerResponseMessageId,
 0155                sellerResponseText,
 0156                sellerResponseDate,
 0157                responderName);
 0158        }
 159
 160        /// <summary>
 161        /// Получить среднее время ответа по клиенту за период
 162        /// </summary>
 163        public async Task<double?> GetAverageResponseTimeAsync(int clientId, DateTime? from = null, DateTime? to = null)
 164        {
 0165            return await _repository.GetAverageResponseTimeAsync(clientId, from, to);
 0166        }
 167
 168        /// <summary>
 169        /// Получить статистику ответов (всего/отвечено) по клиенту за период
 170        /// </summary>
 171        public async Task<(int Total, int Answered, double AnswerRate)> GetAnswerStatsAsync(
 172            int clientId, DateTime? from = null, DateTime? to = null)
 173        {
 0174            return await _repository.GetAnswerStatsAsync(clientId, from, to);
 0175        }
 176
 177        /// <summary>
 178        /// Получить топ-N самых медленных ответов по клиенту
 179        /// </summary>
 180        public async Task<List<OzonChatResponseStatistic>> GetSlowestResponsesAsync(int clientId, int topN = 10)
 181        {
 0182            return await _repository.GetSlowestResponsesAsync(clientId, topN);
 0183        }
 184
 185        /// <summary>
 186        /// Получить статистику по типам чатов
 187        /// </summary>
 188        public async Task<Dictionary<string, int>> GetStatsByChatTypeAsync(int clientId, DateTime? from = null, DateTime
 189        {
 0190            return await _repository.GetStatsByChatTypeAsync(clientId, from, to);
 0191        }
 192
 193        /// <summary>
 194        /// Добавить запись об ответе менеджера (упрощённый метод)
 195        /// </summary>
 196        public async Task<OzonChatResponseStatistic> AddManagerMessageAsync(
 197            string chatId,
 198            int clientId,
 199            string messageText,
 200            string responderName,
 201            long? sellerMessageId = null,
 202            int? orderId = null,
 203            string? postingNumber = null,
 204            string? chatType = null,
 205            string? chatStatus = null)
 206        {
 0207            var statistic = new OzonChatResponseStatistic
 0208            {
 0209                ChatId = chatId,
 0210                ClientId = clientId,
 0211                OrderId = orderId,
 0212                PostingNumber = postingNumber,
 0213                ClientMessageId = 0, // Не известно на какое сообщение отвечаем
 0214                ClientMessageText = null,
 0215                ClientMessageDate = DateTime.UtcNow,
 0216                SellerResponseMessageId = sellerMessageId,
 0217                SellerResponseText = messageText,
 0218                SellerResponseDate = DateTime.UtcNow,
 0219                ResponseTimeSeconds = 0,
 0220                IsAnswered = true,
 0221                ChatType = chatType ?? "BUYER_SELLER",
 0222                ChatStatus = chatStatus ?? "Opened",
 0223                ResponderName = responderName,
 0224                CreatedAt = DateTime.UtcNow,
 0225                UpdatedAt = DateTime.UtcNow
 0226            };
 227
 0228            await _repository.Add(statistic);
 0229            return statistic;
 0230        }
 231
 232        /// <summary>
 233        /// Получить ChatId чатов, где сообщение покупателя содержит поисковый запрос
 234        /// </summary>
 235        public async Task<List<string>> GetChatIdsByMessageQueryAsync(string query, int clientId = 0, int limit = 100)
 236        {
 0237            return await _repository.GetChatIdsByMessageQueryAsync(query, clientId, limit);
 0238        }
 239
 240        /// <summary>
 241        /// Получить образцы сообщений покупателей для построения списка подсказок
 242        /// </summary>
 243        public async Task<List<(string ChatId, string MessageText, string? PostingNumber, int ClientId)>> GetSampleBuyer
 244            string query, int clientId = 0, int limit = 200)
 245        {
 0246            return await _repository.GetSampleBuyerMessagesAsync(query, clientId, limit);
 0247        }
 248    }
 249}