| | | 1 | | using System; |
| | | 2 | | using Microsoft.EntityFrameworkCore; |
| | | 3 | | using Microsoft.Extensions.Logging; |
| | | 4 | | using System.Collections.Generic; |
| | | 5 | | using System.Linq; |
| | | 6 | | using System.Threading.Tasks; |
| | | 7 | | using WinSolutions.Sveta.Server.Data.DataModel.Contexts; |
| | | 8 | | using WinSolutions.Sveta.Server.Data.DataModel.Entities; |
| | | 9 | | using WinSolutions.Sveta.Server.Domain; |
| | | 10 | | using WinSolutions.Sveta.Server.Services.Interfaces; |
| | | 11 | | using WinSolutions.Sveta.Server.Data.DataModel.Kinds; |
| | | 12 | | using WinSolutions.Sveta.Common; |
| | | 13 | | using SVETA.Api.Helpers; |
| | | 14 | | |
| | | 15 | | namespace WinSolutions.Sveta.Server.Services.Implements |
| | | 16 | | { |
| | | 17 | | public class NotificationUsersService : SvetaServiceBase, INotificationUsersService |
| | | 18 | | { |
| | | 19 | | private readonly SvetaDbContext _db; |
| | | 20 | | private readonly ILogger<NotificationUsersService> _logger; |
| | | 21 | | |
| | | 22 | | public NotificationUsersService(SvetaDbContext db, ILogger<NotificationUsersService> logger, IAuthenticationServ |
| | 186 | 23 | | : base(authenticationService) |
| | 186 | 24 | | { |
| | 186 | 25 | | _db = db; |
| | 186 | 26 | | _logger = logger; |
| | 186 | 27 | | } |
| | | 28 | | |
| | | 29 | | /// <summary> |
| | | 30 | | /// создает запись |
| | | 31 | | /// </summary> |
| | | 32 | | /// <param name="data">объект NotificationUsers</param> |
| | | 33 | | /// <returns></returns> |
| | | 34 | | public async Task Create(NotificationUsers data) |
| | 745 | 35 | | { |
| | 745 | 36 | | _db.Entry(data.NotificationsStatus).State = EntityState.Unchanged; |
| | 745 | 37 | | _db.Entry(data.RecState).State = EntityState.Unchanged; |
| | 745 | 38 | | _db.Entry(data.User).State = EntityState.Unchanged; |
| | 745 | 39 | | _db.Entry(data.Notification).State = EntityState.Unchanged; |
| | 745 | 40 | | await _db.NotificationUsers.AddAsync(data); |
| | 745 | 41 | | await _db.SaveChangesAsync(CurrentUserId); |
| | 745 | 42 | | } |
| | | 43 | | |
| | | 44 | | /// <summary> |
| | | 45 | | /// создает массив записей |
| | | 46 | | /// </summary> |
| | | 47 | | /// <param name="data">массив объектов NotificationUsers</param> |
| | | 48 | | /// <returns></returns> |
| | | 49 | | public async Task Create(NotificationUsers[] data) |
| | 0 | 50 | | { |
| | 0 | 51 | | foreach (NotificationUsers nuser in data) |
| | 0 | 52 | | { |
| | 0 | 53 | | _db.Entry(nuser.NotificationsStatus).State = EntityState.Unchanged; |
| | 0 | 54 | | _db.Entry(nuser.RecState).State = EntityState.Unchanged; |
| | 0 | 55 | | _db.Entry(nuser.User).State = EntityState.Unchanged; |
| | 0 | 56 | | _db.Entry(nuser.Notification).State = EntityState.Unchanged; |
| | 0 | 57 | | } |
| | 0 | 58 | | await _db.NotificationUsers.AddRangeAsync(data); |
| | 0 | 59 | | await _db.SaveChangesAsync(CurrentUserId); |
| | 0 | 60 | | } |
| | | 61 | | |
| | | 62 | | /// <summary> |
| | | 63 | | /// Удаляет запись |
| | | 64 | | /// </summary> |
| | | 65 | | /// <param name="id">id записи</param> |
| | | 66 | | /// <returns></returns> |
| | | 67 | | public async Task Delete(long id) |
| | 0 | 68 | | { |
| | 0 | 69 | | var data = await _db.NotificationUsers.Where(d => !d.IsDeleted).FirstAsync(n => n.Id == id); |
| | 0 | 70 | | if (data == null) |
| | 0 | 71 | | { |
| | 0 | 72 | | throw new KeyNotFoundException($"Уведомление с id={id} не найдено"); |
| | | 73 | | } |
| | 0 | 74 | | data.IsDeleted = true; |
| | 0 | 75 | | await _db.SaveChangesAsync(CurrentUserId); |
| | 0 | 76 | | } |
| | | 77 | | |
| | | 78 | | /// <summary> |
| | | 79 | | /// Получает список записей с пагинацией, сортировкой и фльтрацией |
| | | 80 | | /// </summary> |
| | | 81 | | /// <param name="id">id юзера</param> |
| | | 82 | | /// <param name="page">номер страницы</param> |
| | | 83 | | /// <param name="limit">размер страницы</param> |
| | | 84 | | /// <param name="filter">фильтр по теме или телу</param> |
| | | 85 | | /// <param name="notificationsType">тип нотификации</param> |
| | | 86 | | /// <param name="notificationsStatus">статус нотификации</param> |
| | | 87 | | /// <param name="dateFrom">дата с</param> |
| | | 88 | | /// <param name="dateTo">дата по</param> |
| | | 89 | | /// <param name="sort">тип сортировки</param> |
| | | 90 | | /// <returns></returns> |
| | | 91 | | public async Task<PaginatedData<List<NotificationUsers>>> GetAllNotificationsUser(long id, int page, int? limit, |
| | 0 | 92 | | { |
| | 0 | 93 | | var data = _db.NotificationUsers |
| | 0 | 94 | | .Include(u => u.User) |
| | 0 | 95 | | .Include(d => d.RecState) |
| | 0 | 96 | | .Include(n => n.Notification) |
| | 0 | 97 | | .ThenInclude(notifications => notifications.NotificationsType) |
| | 0 | 98 | | .Include(n => n.Notification) |
| | 0 | 99 | | .ThenInclude(notifications => notifications.User) |
| | 0 | 100 | | .Include(s => s.NotificationsStatus) |
| | 0 | 101 | | .Where(x => x.User.Id == id) |
| | 0 | 102 | | .Where(d => !d.IsDeleted) |
| | 0 | 103 | | .Where(t => notificationsType == null || notificationsType == t.Notification.NotificationsType.Id) |
| | 0 | 104 | | .Where(t => notificationsStatus == null || notificationsStatus == t.NotificationsStatus.Id) |
| | 0 | 105 | | .Where(f => string.IsNullOrWhiteSpace(filter) || f.Notification.Subject.ToUpper().Contains(filter.ToUpper( |
| | | 106 | | |
| | 0 | 107 | | if (!dateFrom.IsNullOrMinValue()) |
| | 0 | 108 | | data = data.Where(x => x.CreationDateTime >= dateFrom); |
| | | 109 | | |
| | 0 | 110 | | if (!dateTo.IsNullOrMinValue()) |
| | 0 | 111 | | data = data.Where(x => x.CreationDateTime < dateTo); |
| | 0 | 112 | | data = SortNotification(data, sort?.ToLower()); |
| | 0 | 113 | | var filtered = data?.Count() ?? 0; |
| | 0 | 114 | | if (limit != null) |
| | 0 | 115 | | data = data.Skip(page * (int)limit).Take((int)limit); |
| | 0 | 116 | | var total = await GetNotificationsCount(id, 0, null, null, null, null); |
| | | 117 | | |
| | | 118 | | |
| | 0 | 119 | | return new PaginatedData<List<NotificationUsers>> |
| | 0 | 120 | | { |
| | 0 | 121 | | Result = await data.ToListAsync(), |
| | 0 | 122 | | TotalCount = total, |
| | 0 | 123 | | TotalFilteredCount = filtered |
| | 0 | 124 | | }; |
| | 0 | 125 | | } |
| | | 126 | | |
| | | 127 | | /// <summary> |
| | | 128 | | /// получает записи |
| | | 129 | | /// </summary> |
| | | 130 | | /// <param name="notificationId">id родительской записи</param> |
| | | 131 | | /// <param name="userId">id юзера</param> |
| | | 132 | | /// <returns></returns> |
| | | 133 | | public async Task<List<NotificationUsers>> GetNotifications(long notificationId, long? userId = null) |
| | 0 | 134 | | { |
| | 0 | 135 | | var items = _db.NotificationUsers |
| | 0 | 136 | | .Include(s => s.NotificationsStatus) |
| | 0 | 137 | | .Include(u => u.User) |
| | 0 | 138 | | .Include(n => n.Notification) |
| | 0 | 139 | | .Where(x => x.Notification.Id == notificationId) |
| | 0 | 140 | | .Where(u => userId == null || u.User.Id == userId) |
| | 0 | 141 | | .Where(d => !d.IsDeleted); |
| | | 142 | | |
| | 0 | 143 | | return await items.ToListAsync(); |
| | 0 | 144 | | } |
| | | 145 | | |
| | | 146 | | /// <summary> |
| | | 147 | | /// Получить общее количество сообщений с фильтрами |
| | | 148 | | /// </summary> |
| | | 149 | | /// <param name="userId">id юзера</param> |
| | | 150 | | /// <param name="page">номер страницы</param> |
| | | 151 | | /// <param name="limit">размер страницы</param> |
| | | 152 | | /// <param name="filter">фильтр по теме и телу</param> |
| | | 153 | | /// <param name="notificationType">тип нотификации</param> |
| | | 154 | | /// <param name="notificationStatus">статус нотификации</param> |
| | | 155 | | /// <returns></returns> |
| | | 156 | | public async Task<int> GetNotificationsCount(long userId, int page, int? limit, string filter, long? notificatio |
| | 0 | 157 | | { |
| | 0 | 158 | | var data = _db.NotificationUsers |
| | 0 | 159 | | .Include(u => u.User) |
| | 0 | 160 | | .Include(d => d.RecState) |
| | 0 | 161 | | .Include(n => n.Notification) |
| | 0 | 162 | | .ThenInclude(u => u.User) |
| | 0 | 163 | | .Include(s => s.NotificationsStatus) |
| | 0 | 164 | | .Include(s => s.Notification.NotificationsType) |
| | 0 | 165 | | .Where(x => x.User.Id == userId) |
| | 0 | 166 | | .Where(d => !d.IsDeleted) |
| | 0 | 167 | | .Where(t => (notificationsType != null ? notificationsType == t.Notification.NotificationsType.Id : true)) |
| | 0 | 168 | | .Where(t => (notificationsStatus != null ? notificationsStatus == t.NotificationsStatus.Id : true)) |
| | 0 | 169 | | .Where(f => !string.IsNullOrWhiteSpace(filter) ? f.Notification.Subject.ToUpper().Contains(filter.ToUpper( |
| | 0 | 170 | | if (dateFrom.HasValue) |
| | 0 | 171 | | data = data.Where(x => x.CreationDateTime >= dateFrom.Value.Date); |
| | | 172 | | |
| | 0 | 173 | | if (dateTo.HasValue) |
| | 0 | 174 | | data = data.Where(x => x.CreationDateTime <= dateTo.Value.Date.AddDays(1).AddMilliseconds(-1)); |
| | | 175 | | |
| | 0 | 176 | | return await data.CountAsync(); |
| | 0 | 177 | | } |
| | | 178 | | |
| | | 179 | | /// <summary> |
| | | 180 | | /// Получить общее количество сообщений для одного пользователя |
| | | 181 | | /// </summary> |
| | | 182 | | /// <param name="id">id пользователя</param> |
| | | 183 | | /// <param name="notificationStatus">статус нотификации</param> |
| | | 184 | | /// <param name="notificationType">Тип нотификации</param> |
| | | 185 | | /// <param name="dateFrom">дата с</param> |
| | | 186 | | /// <param name="dateTo">дата по</param> |
| | | 187 | | /// <returns></returns> |
| | | 188 | | public async Task<int> GetNotificationsUserCount(long id, long? notificationStatus, long? notificationType, Date |
| | 0 | 189 | | { |
| | 0 | 190 | | var data = _db.NotificationUsers |
| | 0 | 191 | | .Include(n => n.Notification) |
| | 0 | 192 | | .Include(s => s.NotificationsStatus) |
| | 0 | 193 | | .Include(t => t.Notification.NotificationsType) |
| | 0 | 194 | | .Where(x => x.User.Id == id) |
| | 0 | 195 | | .Where(d => !d.IsDeleted) |
| | 0 | 196 | | .Where(s => notificationStatus == null || s.NotificationsStatus.Id == notificationStatus) |
| | 0 | 197 | | .Where(s => notificationType == null || s.Notification.NotificationsType.Id == notificationType); |
| | 0 | 198 | | if (!dateFrom.IsNullOrMinValue()) |
| | 0 | 199 | | data = data.Where(x => x.CreationDateTime >= dateFrom); |
| | | 200 | | |
| | 0 | 201 | | if (!dateTo.IsNullOrMinValue()) |
| | 0 | 202 | | data = data.Where(x => x.CreationDateTime < dateTo); |
| | 0 | 203 | | return await data.CountAsync(); |
| | 0 | 204 | | } |
| | | 205 | | |
| | | 206 | | /// <summary> |
| | | 207 | | /// проверяет существует ли запись |
| | | 208 | | /// </summary> |
| | | 209 | | /// <param name="id">id записи</param> |
| | | 210 | | /// <returns></returns> |
| | 0 | 211 | | public async Task<bool> NotificationUsersExists(long id) => await _db.NotificationUsers |
| | 0 | 212 | | .Where(e => e.Id == id) |
| | 0 | 213 | | .Where(d => !d.IsDeleted) |
| | 0 | 214 | | .AnyAsync(); |
| | | 215 | | |
| | | 216 | | |
| | | 217 | | /// <summary> |
| | | 218 | | /// Помечает все отправленные сообщения прочитанными |
| | | 219 | | /// </summary> |
| | | 220 | | /// <param name="userId">id юзера</param> |
| | | 221 | | /// <returns></returns> |
| | | 222 | | public async Task MarkAllNotificationsUser(long userId) |
| | 0 | 223 | | { |
| | 0 | 224 | | var items = await _db.NotificationUsers |
| | 0 | 225 | | .Include(u => u.User) |
| | 0 | 226 | | .Include(s => s.NotificationsStatus) |
| | 0 | 227 | | .Where(x => x.User.Id == userId) |
| | 0 | 228 | | .Where(m => !m.IsDeleted) |
| | 0 | 229 | | .Where(n => n.NotificationsStatus.Id == (int)NotificationStatus.Sent) |
| | 0 | 230 | | .ToListAsync(); |
| | 0 | 231 | | foreach (var item in items) |
| | 0 | 232 | | { |
| | 0 | 233 | | item.NotificationsStatus = await _db.refNotificationsStatus.Where(e => !e.IsDeleted).FirstOrDefaultAsync |
| | 0 | 234 | | _db.NotificationUsers.Update(item); |
| | 0 | 235 | | } |
| | | 236 | | |
| | 0 | 237 | | await _db.SaveChangesAsync(CurrentUserId); |
| | 0 | 238 | | } |
| | | 239 | | |
| | | 240 | | /// <summary> |
| | | 241 | | /// обновляет запись |
| | | 242 | | /// </summary> |
| | | 243 | | /// <param name="data">объект NotificationUsers</param> |
| | | 244 | | /// <returns></returns> |
| | | 245 | | public async Task Update(NotificationUsers data) |
| | 0 | 246 | | { |
| | 0 | 247 | | if (!(await NotificationUsersExists(data.Id))) |
| | 0 | 248 | | { |
| | 0 | 249 | | throw new ArgumentException($"Пользовательское уведомление с id={data.Id} не найдено"); |
| | | 250 | | } |
| | 0 | 251 | | _db.NotificationUsers.Update(data); |
| | 0 | 252 | | await _db.SaveChangesAsync(CurrentUserId); |
| | 0 | 253 | | } |
| | | 254 | | |
| | | 255 | | /// <summary> |
| | | 256 | | /// Сортирует коммуникации по полям |
| | | 257 | | /// </summary> |
| | | 258 | | /// <param name="movements"></param> |
| | | 259 | | /// <param name="sort">сортировка по умолчанию по id - id|desc,created_on,created_on|desc,state,state|desc,statu |
| | | 260 | | /// <returns></returns> |
| | 0 | 261 | | private IQueryable<NotificationUsers> SortNotification(IQueryable<NotificationUsers> notifies, string sort = def |
| | 0 | 262 | | { |
| | 0 | 263 | | "created_on" => notifies.OrderBy(d => d.CreationDateTime), |
| | 0 | 264 | | "created_on|desc" => notifies.OrderByDescending(d => d.CreationDateTime), |
| | 0 | 265 | | "state" => notifies.OrderBy(d => d.Notification.RecState.Id), |
| | 0 | 266 | | "state|desc" => notifies.OrderByDescending(d => d.Notification.RecState.Id), |
| | 0 | 267 | | "subject" => notifies.OrderBy(d => d.Notification.Subject), |
| | 0 | 268 | | "subject|desc" => notifies.OrderByDescending(d => d.Notification.Subject), |
| | 0 | 269 | | "body" => notifies.OrderBy(d => d.Notification.Body), |
| | 0 | 270 | | "body|desc" => notifies.OrderByDescending(d => d.Notification.Body), |
| | 0 | 271 | | "id|desc" => notifies.OrderByDescending(d => d.Notification.Id), |
| | 0 | 272 | | "id" => notifies.OrderBy(d => d.Notification.Id), |
| | 0 | 273 | | _ => notifies.OrderBy(d => d.NotificationsStatus.Id).ThenByDescending(x=>x.CreationDateTime), |
| | 0 | 274 | | }; |
| | | 275 | | } |
| | | 276 | | } |