using Confluent.Kafka; using Crm.Core.Domain.Dto.CRM; using Microsoft.AspNetCore.SignalR; using System.Security.Cryptography; using static System.Formats.Asn1.AsnWriter; namespace Crm.Core.Domain.Hubs { public class NotificationHub : Hub { private readonly IRedisManager _redisManager; private readonly IServiceProvider _serviceProvider; public NotificationHub(IRedisManager redisManager, IServiceProvider serviceProvider) { _redisManager = redisManager; _serviceProvider = serviceProvider; } public async Task EnqueueQueue(OrderUserProtectionDto orderUserProtectionDto) { await _redisManager.EnqueueAsync(CacheKeys.MssagesKey, orderUserProtectionDto); } public async Task GetQueue() { return await _redisManager.CountAsync(CacheKeys.MssagesKey); } public async Task DequeueQueue() { return await _redisManager.DequeueAsync(CacheKeys.MssagesKey); } public async Task EnqueueStatisticsQueue(OrderUserProtectionDto orderUserProtectionDto) { await _redisManager.EnqueueAsync(CacheKeys.StatisticsKey, orderUserProtectionDto); } public async Task GetStatisticsQueue() { return await _redisManager.CountAsync(CacheKeys.StatisticsKey); } public async Task DequeueStatisticsQueue() { return await _redisManager.DequeueAsync(CacheKeys.StatisticsKey); } public async Task SendOrderUserProtectionMessage(OrderUserProtectionDto orderUserProtectionDto) { using IServiceScope scope = _serviceProvider.CreateScope(); var list = await GetList(); var cacheDomain = scope.ServiceProvider.GetRequiredService(); var enabled = await cacheDomain.GetValueParameter(ParameterEnums.CRM_CORE_NOTIFICATION_ENABLED); var excludeEids = await cacheDomain.GetParameter>(ParameterEnums.CRM_CORE_NOTIFICATION_EXCLUDE); var repeatDeptid = orderUserProtectionDto.repeat_deptid; if (repeatDeptid.HasValue) { if (repeatDeptid == 0) { Log.Error($"重复的事业部id为0,排除异常数据!"); return; } var appid = await cacheDomain.GetAppidByDeptmentId((decimal)repeatDeptid); if (string.IsNullOrEmpty(appid)) { Log.Error($"找不到重复的事业部id{repeatDeptid}"); return; } var eid = orderUserProtectionDto.repeat_eid ?? 0; // 如果排除了名单就不发送通知 if (excludeEids != null && excludeEids.Any() && excludeEids.Contains(eid)) { return; } var connectionIds = list.Where(x => x.Eid == eid && x.Appid == appid).Select(x => x.ConnectionId ?? "").ToList(); var message = $"昵称:{orderUserProtectionDto.nickname},真实姓名:{orderUserProtectionDto.name},客户ID:{orderUserProtectionDto.umid},成交保护期: {orderUserProtectionDto.deptname}{orderUserProtectionDto.EndTimeStr};权限到期时间:{orderUserProtectionDto.OrderPassTimeStr};,已经成交,您若不是他/她的成交客服,为了避免重复营销,请将他/她企微账号屏蔽和删除,请帮忙配合。"; var notificationDomain = scope.ServiceProvider.GetRequiredService(); var notification = await notificationDomain.InsetNotification(orderUserProtectionDto, message); var count = await notificationDomain.GetMessageCount(eid); if (count > 99) { return; } if (connectionIds != null && connectionIds.Any() && Clients != null && enabled == "1") { var clients = Clients.Clients(connectionIds); await clients.SendAsync("ReceiveMessage", message, notification.ID, count); } } } public async Task InsetStatistics(OrderUserProtectionDto orderUserProtectionDto) { using IServiceScope scope = _serviceProvider.CreateScope(); var notificationDomain = scope.ServiceProvider.GetRequiredService(); var cacheDomain = scope.ServiceProvider.GetRequiredService(); var excludeEids = await cacheDomain.GetParameter>(ParameterEnums.CRM_CORE_NOTIFICATION_EXCLUDE); var eid = orderUserProtectionDto.repeat_eid ?? 0; // 如果排除了名单就不纳入统计 if (excludeEids != null && excludeEids.Any() && excludeEids.Contains(eid)) { return; } await notificationDomain.InsetNotificationStatistics(orderUserProtectionDto); } public async Task Send(BAS_NOTIFICATION notification) { var list = await GetList(); using IServiceScope scope = _serviceProvider.CreateScope(); var connectionIds = list.Where(x => x.Eid == notification.EID).Select(x => x.ConnectionId ?? "").ToList(); if (connectionIds != null && connectionIds.Any()) { var notificationDomain = scope.ServiceProvider.GetRequiredService(); var count = await notificationDomain.GetMessageCount(notification.EID, notification.NOTIFICATIONTYPE); var clients = Clients.Clients(connectionIds); await clients.SendAsync(notification.NOTIFICATIONTYPE?.ToString() ?? "ReceiveMessage", notification.MESSAGE, notification.ID, count); } } public async Task SendMessage(SendMessageDto dto) { var list = await GetList(); using IServiceScope scope = _serviceProvider.CreateScope(); var cacheDomain = scope.ServiceProvider.GetRequiredService(); var appid = await cacheDomain.GetAppidByDeptmentId(dto.Deptid ?? 0); var notificationDomain = scope.ServiceProvider.GetRequiredService(); var notification = await notificationDomain.InsetNotification(dto); var connectionIds = list.Where(x => x.Eid == dto.Eid && x.Appid == appid).Select(x => x.ConnectionId ?? "").ToList(); if (connectionIds != null && connectionIds.Any()) { var clients = Clients.Clients(connectionIds); var count = await notificationDomain.GetMessageCount(dto.Eid); await clients.SendAsync(dto.Method, dto.Message, dto.Id == null ? notification.ID : dto.Id, count); } } public async Task LoginUser(string userInfo) { var user = JsonSerializer.Deserialize(userInfo); if (user == null || user.Eid == 5004) return; user.LoginTime = DateTime.Now; var list = await GetList(); if (list.Any(x => x.Eid == user.Eid && x.Ip != user.Ip) && !user.Freelogin) { var downlineUsers = list.Where(x => x.Eid == user.Eid && x.Ip != user.Ip).ToList(); foreach (var downlineUser in downlineUsers) { await ForcedDownline(downlineUser, "您的账号已在其他电脑登录,如非本人操作,请修改密码!"); list.Remove(downlineUser); } } list.Add(user); await _redisManager.SetAsync(CacheKeys.UserListKey, list, TimeSpan.FromDays(1)); await SendUserMessage(user); } /// /// 强制下线 /// /// /// /// public async Task ForcedDownline(OnlineUserInfoDto user, string? message) { using IServiceScope scope = _serviceProvider.CreateScope(); var cacheDomain = scope.ServiceProvider.GetRequiredService(); var enabled = await cacheDomain.GetValueParameter(ParameterEnums.CRM_CORE_FORCED_DOWNLINE_ENABLED); if (!string.IsNullOrEmpty(user.ConnectionId) && enabled == "1") { var client = Clients.Client(user.ConnectionId); await client.SendAsync("ForcedDownline", message); } } public async Task SendUserMessage(OnlineUserInfoDto user) { using IServiceScope scope = _serviceProvider.CreateScope(); var cacheDomain = scope.ServiceProvider.GetRequiredService(); var enabled = await cacheDomain.GetValueParameter(ParameterEnums.CRM_CORE_NOTIFICATION_ENABLED); var notificationDomain = scope.ServiceProvider.GetRequiredService(); var notifications = await notificationDomain.GetNotificationsByEid(user.Eid, user.Appid); if (notifications != null && notifications.Any() && !string.IsNullOrEmpty(user.ConnectionId) && enabled == "1") { var client = Clients.Client(user.ConnectionId); foreach (var notification in notifications) { Log.Information(notification.ToJson()); var count = await notificationDomain.GetMessageCount(user.Eid, notification.NotificationType); await client.SendAsync(notification?.NotificationType?.ToString() ?? "ReceiveMessage", notification.Message, notification.Id, count); } } } public override async Task OnDisconnectedAsync(Exception? exception) { var connectionId = Context.ConnectionId; var list = await GetList(); var user = list.Where(x => x.ConnectionId == connectionId).FirstOrDefault(); if (user != null) { list.Remove(user); await _redisManager.SetAsync(CacheKeys.UserListKey, list, TimeSpan.FromDays(1)); } } private async Task> GetList() => await _redisManager.ExistsAsync(CacheKeys.UserListKey) ? await _redisManager.GetListAsync(CacheKeys.UserListKey) : new List(); } }