| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.Diagnostics; |
| | | 4 | | using System.Linq; |
| | | 5 | | using System.Net.Http; |
| | | 6 | | using System.Threading; |
| | | 7 | | using System.Threading.Tasks; |
| | | 8 | | using Microsoft.AspNetCore.SignalR; |
| | | 9 | | using Microsoft.Extensions.Logging; |
| | | 10 | | using Microsoft.Extensions.Options; |
| | | 11 | | using Newtonsoft.Json; |
| | | 12 | | using Newtonsoft.Json.Serialization; |
| | | 13 | | using Serilog; |
| | | 14 | | using SVETA.Api.Data.Domain; |
| | | 15 | | using SVETA.Api.Data.DTO.Notification; |
| | | 16 | | using SVETA.Api.Helpers; |
| | | 17 | | using SVETA.Api.Hubs; |
| | | 18 | | using SVETA.API.IntegrationTest.Common; |
| | | 19 | | using SVETA.Api.Services.Interfaces; |
| | | 20 | | using WinSolutions.Sveta.Server.Data.DataModel.Entities; |
| | | 21 | | using WinSolutions.Sveta.Server.Data.DataModel.Kinds; |
| | | 22 | | using WinSolutions.Sveta.Server.Services.Interfaces; |
| | | 23 | | using WinSolutions.Sveta.Server.Services.Implements; |
| | | 24 | | |
| | | 25 | | namespace SVETA.Api.Services.Implements |
| | | 26 | | { |
| | | 27 | | public class NotificationWorker: INotificationWorker |
| | | 28 | | { |
| | | 29 | | private readonly ILogger<INotificationWorker> _logger; |
| | | 30 | | private readonly INotificationService _notificationService; |
| | | 31 | | private readonly IDepartmentService _departmentService; |
| | | 32 | | private readonly IUserService _userService; |
| | | 33 | | private readonly IContragentService _contragentService; |
| | | 34 | | private readonly IEmailService _emailService; |
| | | 35 | | private readonly CommonSettings _commonSettings; |
| | | 36 | | private readonly IHubContext<NotificationHub> _notificationHub; |
| | | 37 | | private readonly TelegramSettings _telegramSettings; |
| | | 38 | | private readonly ITelegramNotificationService _telegramService; |
| | | 39 | | private readonly IAddressService _addressService; |
| | | 40 | | |
| | 0 | 41 | | public NotificationWorker(ILogger<INotificationWorker> logger, |
| | 0 | 42 | | INotificationService notificationService, |
| | 0 | 43 | | IHubContext<NotificationHub> notificationHub, |
| | 0 | 44 | | IDepartmentService departmentService, |
| | 0 | 45 | | IContragentService contragentService, |
| | 0 | 46 | | IEmailService emailService, |
| | 0 | 47 | | IUserService userService, |
| | 0 | 48 | | IOptions<CommonSettings> commonSettingsOption, |
| | 0 | 49 | | ITelegramNotificationService telegramService, |
| | 0 | 50 | | IAddressService addressService, |
| | 0 | 51 | | IOptions<TelegramSettings> telegramOptions) |
| | 0 | 52 | | { |
| | 0 | 53 | | _telegramService = telegramService; |
| | 0 | 54 | | _logger = logger; |
| | 0 | 55 | | _notificationService = notificationService; |
| | 0 | 56 | | _notificationHub = notificationHub; |
| | 0 | 57 | | _telegramSettings = telegramOptions.Value; |
| | 0 | 58 | | _departmentService = departmentService; |
| | 0 | 59 | | _userService = userService; |
| | 0 | 60 | | _contragentService = contragentService; |
| | 0 | 61 | | _emailService = emailService; |
| | 0 | 62 | | _commonSettings = commonSettingsOption.Value; |
| | 0 | 63 | | _addressService = addressService; |
| | 0 | 64 | | } |
| | | 65 | | |
| | | 66 | | |
| | | 67 | | /// <summary> |
| | | 68 | | /// Создает уведомление |
| | | 69 | | /// </summary> |
| | | 70 | | /// <param name="movement">Документ</param> |
| | | 71 | | /// <param name="subject">Тема уведомления</param> |
| | | 72 | | /// <param name="body">Тело уведомления</param> |
| | | 73 | | /// <param name="sendEmailToDepartment">Отправлять письмо на рассылку департамента-владельца статуса</param> |
| | | 74 | | /// <returns></returns> |
| | | 75 | | public async Task CreateNotification(Movement movement, string subject, string body = default, bool sendEmailToD |
| | 0 | 76 | | { |
| | 0 | 77 | | IEnumerable<User> userList = null; |
| | 0 | 78 | | List<string> departmentsEmail = new List<string>() { }; |
| | | 79 | | |
| | 0 | 80 | | switch (movement.MovementStatus.Id) |
| | | 81 | | { |
| | | 82 | | case (long)MovementsStatus.OrderDraft: |
| | | 83 | | case (long)MovementsStatus.Reject: |
| | | 84 | | case (long)MovementsStatus.InProgress: |
| | | 85 | | case (long)MovementsStatus.Finished: |
| | | 86 | | case (long)MovementsStatus.PaymentAwaiting: |
| | | 87 | | case (long)MovementsStatus.ReadyForShipment: |
| | | 88 | | case (long)MovementsStatus.Correction: |
| | | 89 | | case (long)MovementsStatus.SupplierReject: |
| | | 90 | | case (long)MovementsStatus.Shipped: |
| | 0 | 91 | | userList = await _userService.GetDepartmentUsers(movement.Receiver.Id); |
| | 0 | 92 | | departmentsEmail.Add(movement.Receiver.Email); |
| | 0 | 93 | | break; |
| | | 94 | | case (long)MovementsStatus.ShipmentDraft: |
| | | 95 | | case (long)MovementsStatus.Received: |
| | | 96 | | case (long)MovementsStatus.CustomerReject: |
| | 0 | 97 | | userList = await _userService.GetDepartmentUsers(movement.Sender.Id); |
| | 0 | 98 | | departmentsEmail.Add(movement.Sender.Email); |
| | 0 | 99 | | break; |
| | | 100 | | case (long)MovementsStatus.InQueue: |
| | 0 | 101 | | userList = await _contragentService.GetContragentUsers(ContragentKind.Platform); |
| | 0 | 102 | | departmentsEmail.Add((await _departmentService.GetPlatformDepartment())?.Email); |
| | 0 | 103 | | break; |
| | | 104 | | case (long)MovementsStatus.Picking: |
| | | 105 | | case (long)MovementsStatus.ClaimInProgress: |
| | 0 | 106 | | userList = (await _contragentService.GetContragentUsers(ContragentKind.Platform)).Union(await _userS |
| | 0 | 107 | | await _userService.GetDepartmentUsers(movement.Sender.Id)); |
| | 0 | 108 | | departmentsEmail.Add((await _departmentService.GetPlatformDepartment()).Email); |
| | 0 | 109 | | departmentsEmail.Add(movement.Receiver.Email); |
| | 0 | 110 | | departmentsEmail.Add(movement.Sender.Email); |
| | 0 | 111 | | break; |
| | | 112 | | case (long)MovementsStatus.ClaimAccepted: |
| | | 113 | | case (long)MovementsStatus.ClaimDeclined: |
| | 0 | 114 | | userList = (await _userService.GetDepartmentUsers(movement.Receiver.Id)).Union( |
| | 0 | 115 | | await _userService.GetDepartmentUsers(movement.Sender.Id)); |
| | 0 | 116 | | departmentsEmail.Add(movement.Receiver.Email); |
| | 0 | 117 | | departmentsEmail.Add(movement.Sender.Email); |
| | 0 | 118 | | break; |
| | 0 | 119 | | }; |
| | 0 | 120 | | var tasks = new List<Task>(); |
| | 0 | 121 | | if (userList != null && userList.Count() > 0) |
| | 0 | 122 | | { |
| | 0 | 123 | | var list = await _notificationService.CreateNotificationForUsers(subject, body, userList.ToList(), false |
| | 0 | 124 | | tasks.Add(Task.Factory.StartNew(async (list) => |
| | 0 | 125 | | { |
| | 0 | 126 | | await SendNotification((List<NotificationUsers>)list); |
| | 0 | 127 | | }, list)); |
| | 0 | 128 | | } |
| | | 129 | | |
| | | 130 | | |
| | 0 | 131 | | if (sendEmailToDepartment && departmentsEmail.Count > 0) |
| | 0 | 132 | | { |
| | 0 | 133 | | if (movement.Notes?.Count > 0) |
| | 0 | 134 | | { |
| | 0 | 135 | | var listComments = await FormatComment(movement.Notes); |
| | 0 | 136 | | body += $"\n Комментарии к документу" + |
| | 0 | 137 | | $": \n{string.Join("\n", listComments)}"; |
| | 0 | 138 | | } |
| | | 139 | | |
| | 0 | 140 | | await _emailService.Create(subject, body, departmentsEmail); |
| | 0 | 141 | | } |
| | | 142 | | |
| | 0 | 143 | | if (movement.MovementStatus.Id != (long) MovementsStatus.OrderDraft) |
| | 0 | 144 | | { |
| | 0 | 145 | | if ((await _telegramService.CanSendToTelegram("#order"))) |
| | 0 | 146 | | { |
| | 0 | 147 | | string textTelegram = |
| | 0 | 148 | | $"#order " + |
| | 0 | 149 | | $"\nЗаявка № {movement.DocumentNumber} {_commonSettings.BaseFrontUrl}/{MovementHelpers.CreatePos |
| | 0 | 150 | | $"переведена в статус \"{movement.MovementStatus.Name}\". " + |
| | 0 | 151 | | $"\n Заказчик: {movement.Customer.ShortName} " + |
| | 0 | 152 | | $"\n Фактический адрес: {(await _addressService.GetPhysicAddressByContragent(movement.Customer.I |
| | 0 | 153 | | $"\n e-mail: {movement.Customer.Email} " + |
| | 0 | 154 | | $"\n Номер телефона: {movement.Customer.PhoneNumber} " + |
| | 0 | 155 | | $"\n Сумма заказа: {movement.PrepaimentSum} " + |
| | 0 | 156 | | $"\n Время заказа: {movement.CreationDateTime} " + |
| | 0 | 157 | | $"\n Статус заказа: {movement.MovementStatus.Name}"; |
| | 0 | 158 | | if (movement.Notes?.Count > 0) |
| | 0 | 159 | | { |
| | 0 | 160 | | var listComments = await FormatComment(movement.Notes); |
| | | 161 | | |
| | 0 | 162 | | textTelegram += $"\n Комментарии к документу" + |
| | 0 | 163 | | $": \n{string.Join("\n", listComments)}"; |
| | 0 | 164 | | } |
| | | 165 | | |
| | 0 | 166 | | tasks.Add(Task.Factory.StartNew(async () => |
| | 0 | 167 | | { |
| | 0 | 168 | | await Telegram.SendNotification(textTelegram); |
| | 0 | 169 | | })); |
| | 0 | 170 | | } |
| | 0 | 171 | | } |
| | 0 | 172 | | Task.WhenAll(tasks.ToArray()); |
| | 0 | 173 | | } |
| | | 174 | | |
| | | 175 | | public async Task SendNotification(List<NotificationUsers> list) |
| | 0 | 176 | | { |
| | 0 | 177 | | if (list != null && list.Count > 0) |
| | 0 | 178 | | { |
| | 0 | 179 | | Parallel.ForEach(list,notification => |
| | 0 | 180 | | { |
| | 0 | 181 | | SendBySocket(notification); |
| | 0 | 182 | | }); |
| | 0 | 183 | | } |
| | 0 | 184 | | } |
| | | 185 | | |
| | | 186 | | private async Task<List<string>> FormatComment(List<MovementNote> notes) |
| | 0 | 187 | | { |
| | 0 | 188 | | var listComments = new List<string>(); |
| | 0 | 189 | | for (int i = 0; i < notes.Count; i++) |
| | 0 | 190 | | { |
| | 0 | 191 | | string res = notes[i].CreationDateTime.ToString("dd.MM.yyyy hh:mm") + " "; |
| | 0 | 192 | | if (notes[i].CreatedByUserId.HasValue) |
| | 0 | 193 | | { |
| | 0 | 194 | | res += (await _userService.GetUser(notes[i].CreatedByUserId.Value)).GetName(); |
| | 0 | 195 | | } |
| | 0 | 196 | | res += " --- " + notes[i].Body; |
| | 0 | 197 | | listComments.Add(res); |
| | 0 | 198 | | } |
| | | 199 | | |
| | 0 | 200 | | return listComments; |
| | 0 | 201 | | } |
| | | 202 | | |
| | | 203 | | void SendBySocket(NotificationUsers notification) |
| | 0 | 204 | | { |
| | 0 | 205 | | string connectionId = default; |
| | 0 | 206 | | UserHandler.ConnectedIds.TryGetValue(notification.User.ExternalKey.ToString(), out connectionId); |
| | 0 | 207 | | if (connectionId != default) |
| | 0 | 208 | | { |
| | 0 | 209 | | _notificationHub |
| | 0 | 210 | | .Clients |
| | 0 | 211 | | .Client(connectionId) |
| | 0 | 212 | | .SendAsync("notificationReceived", JsonConvert.SerializeObject(new NotificationResponseDTO |
| | 0 | 213 | | { |
| | 0 | 214 | | NotificationId = notification.Notification.Id, |
| | 0 | 215 | | Subject = notification.Notification.Subject, |
| | 0 | 216 | | Body = notification.Notification.Body, |
| | 0 | 217 | | SenderId = notification.Notification.User.Id, |
| | 0 | 218 | | NotificationsType = notification.Notification.NotificationsType.Id, |
| | 0 | 219 | | NotificationsStatus = notification.NotificationsStatus.Id, |
| | 0 | 220 | | TimeToTurnOff = notification.Notification.TimeToTurnOff, |
| | 0 | 221 | | CreationDateTime = notification.CreationDateTime |
| | 0 | 222 | | }, new JsonSerializerSettings |
| | 0 | 223 | | { |
| | 0 | 224 | | ContractResolver = new CamelCasePropertyNamesContractResolver() |
| | 0 | 225 | | }) ).Wait(); |
| | 0 | 226 | | } |
| | 0 | 227 | | } |
| | | 228 | | } |
| | | 229 | | } |