Kaynağa Gözat

blacklist delay msg

xf 2 yıl önce
ebeveyn
işleme
240d894098

+ 5 - 8
src/Hotline.Api/Controllers/CallController.cs

@@ -53,7 +53,7 @@ namespace Hotline.Api.Controllers
         public async Task<PagedDto<CallDto>> GetCallList([FromQuery] GetCallListRequest request)
         {
             var (total, items) = await _callRepository.Queryable()
-                .Includes(d=>d.CallDetails)
+                .Includes(d => d.CallDetails)
                 .WhereIF(!string.IsNullOrEmpty(request.PhoneNum), d => d.FromNo.Contains(request.PhoneNum!))
                 .WhereIF(!string.IsNullOrEmpty(request.ToNum), d => d.ToNo.Contains(request.ToNum!))
                 .WhereIF(request.Direction is not null, d => d.CallDirection == request.Direction)
@@ -73,14 +73,14 @@ namespace Hotline.Api.Controllers
         [HttpGet("messed-paged")]
         public async Task<PagedDto<CallDto>> GetCallListMissed([FromQuery] GetCallListRequest request)
         {
-            var(total, items) = await _callRepository.Queryable()
+            var (total, items) = await _callRepository.Queryable()
                 .Where(x => !x.CallDetails.Any(d => d.EventName == "ANSWERED"))
                 .WhereIF(!string.IsNullOrEmpty(request.PhoneNum), d => d.FromNo.Contains(request.PhoneNum!))
                 .WhereIF(!string.IsNullOrEmpty(request.ToNum), d => d.ToNo.Contains(request.ToNum!))
                 .WhereIF(request.Direction is not null, d => d.CallDirection == request.Direction)
                 .Includes(d => d.CallDetails)
                 .OrderByDescending(d => d.CreationTime)
-                .ToPagedListAsync(request.PageIndex,request.PageSize);
+                .ToPagedListAsync(request.PageIndex, request.PageSize);
             return new PagedDto<CallDto>(total, _mapper.Map<IReadOnlyList<CallDto>>(items));
         }
 
@@ -134,10 +134,7 @@ namespace Hotline.Api.Controllers
         [HttpPost("blacklist")]
         public async Task AddBlacklist([FromBody] AddBlacklistDto dto)
         {
-            var exists = await _blacklistRepository.AnyAsync(d => d.PhoneNo == dto.PhoneNo);
-            if (exists) return;
-            var blacklist = _mapper.Map<Blacklist>(dto);
-            await _blacklistDomainService.AddAsync(blacklist, HttpContext.RequestAborted);
+            await _blacklistDomainService.AddAsync(dto, HttpContext.RequestAborted);
         }
 
         /// <summary>
@@ -148,7 +145,7 @@ namespace Hotline.Api.Controllers
         [HttpDelete("blacklist/{phone}")]
         public void RemoveBlacklist(string phone)
         {
-            _blacklistDomainService.Remove(phone);
+            _blacklistDomainService.RemoveAsync(phone);
         }
 
         /// <summary>

+ 10 - 2
src/Hotline.Application/Handlers/CallCenter/ListManage/BlacklistExpiredHandler.cs

@@ -4,16 +4,24 @@ using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
 using DotNetCore.CAP;
+using Hotline.CallCenter.BlackLists;
 using Hotline.Share.Mq;
 
 namespace Hotline.Application.Handlers.CallCenter.ListManage
 {
     public class BlacklistExpiredHandler : ICapSubscribe
     {
+        private readonly IBlacklistDomainService _blacklistDomainService;
+
+        public BlacklistExpiredHandler(IBlacklistDomainService blacklistDomainService)
+        {
+            _blacklistDomainService = blacklistDomainService;
+        }
+
         [CapSubscribe(EventNames.CallCenterBlackListExpired)]
-        public async Task BlacklistExpiredAsync(CancellationToken cancellationToken)
+        public async Task BlacklistExpiredAsync(string phoneNo, CancellationToken cancellationToken)
         {
-            throw new NotImplementedException();
+            await _blacklistDomainService.RemoveAsync(phoneNo, cancellationToken);
         }
     }
 }

+ 68 - 0
src/Hotline/CallCenter/BlackLists/BlacklistDomainService.cs

@@ -0,0 +1,68 @@
+using DotNetCore.CAP;
+using Hotline.Share.Dtos.CallCenter;
+using Hotline.Share.Mq;
+using MapsterMapper;
+using XF.Domain.Cache;
+using XF.Domain.Dependency;
+
+namespace Hotline.CallCenter.BlackLists;
+
+public class BlacklistDomainService : IBlacklistDomainService, IScopeDependency
+{
+    private readonly ITypedCache<Blacklist> _blackCache;
+    private readonly IBlacklistRepository _blacklistRepository;
+    private readonly IMapper _mapper;
+    private readonly ICapPublisher _capPublisher;
+
+    public BlacklistDomainService(
+        ITypedCache<Blacklist> blackCache,
+        IBlacklistRepository blacklistRepository,
+        IMapper mapper,
+        ICapPublisher capPublisher)
+    {
+        _blackCache = blackCache;
+        _blacklistRepository = blacklistRepository;
+        _mapper = mapper;
+        _capPublisher = capPublisher;
+    }
+
+    /// <summary>
+    /// 将电话加入黑名单
+    /// </summary>
+    public async Task AddAsync(AddBlacklistDto dto, CancellationToken cancellationToken = default)
+    {
+        var cache = await _blackCache.GetOrSetAsync(
+            Blacklist.GetKey(dto.PhoneNo),
+            k => _blacklistRepository.Queryable().FirstAsync(d => d.PhoneNo == dto.PhoneNo).GetAwaiter().GetResult(),
+            TimeSpan.FromSeconds(dto.Duration),
+            cancellationToken);
+        if (cache != null) return;
+
+        var blacklist = _mapper.Map<Blacklist>(dto);
+        await _blacklistRepository.AddAsync(blacklist, cancellationToken);
+        await _blackCache.SetAsync(Blacklist.GetKey(blacklist.PhoneNo), blacklist, TimeSpan.FromSeconds(blacklist.Duration), cancellationToken);
+        await _capPublisher.PublishDelayAsync(TimeSpan.FromSeconds(blacklist.Duration),
+            EventNames.CallCenterBlackListExpired, blacklist.PhoneNo, cancellationToken: cancellationToken);
+    }
+
+    /// <summary>
+    /// 删除黑名单信息
+    /// </summary>
+    /// <param name="phoneNo"></param>
+    public async Task RemoveAsync(string phoneNo, CancellationToken cancellationToken = default)
+    {
+        await _blackCache.RemoveAsync(Blacklist.GetKey(phoneNo), cancellationToken);
+        await _blacklistRepository.RemoveAsync(d => d.PhoneNo == phoneNo, true, cancellationToken);
+    }
+
+    /// <summary>
+    /// 是否在黑名单中
+    /// </summary>
+    /// <param name="phoneNo"></param>
+    /// <returns></returns>
+    public bool IsInBlacklist(string phoneNo)
+    {
+        var blacklist = _blackCache.Get(Blacklist.GetKey(phoneNo));
+        return blacklist != null;
+    }
+}

+ 3 - 46
src/Hotline/CallCenter/BlackLists/IBlacklistDomainService.cs

@@ -1,5 +1,4 @@
-using XF.Domain.Cache;
-using XF.Domain.Dependency;
+using Hotline.Share.Dtos.CallCenter;
 
 namespace Hotline.CallCenter.BlackLists
 {
@@ -8,13 +7,13 @@ namespace Hotline.CallCenter.BlackLists
         /// <summary>
         /// 将电话加入黑名单
         /// </summary>
-        Task AddAsync(Blacklist blacklist, CancellationToken cancellationToken = default);
+        Task AddAsync(AddBlacklistDto dto, CancellationToken cancellationToken = default);
 
         /// <summary>
         /// 删除黑名单信息
         /// </summary>
         /// <param name="phoneNo"></param>
-        Task Remove(string phoneNo);
+        Task RemoveAsync(string phoneNo, CancellationToken cancellationToken = default);
 
         /// <summary>
         /// 是否在黑名单中
@@ -23,46 +22,4 @@ namespace Hotline.CallCenter.BlackLists
         /// <returns></returns>
         bool IsInBlacklist(string phoneNo);
     }
-
-    public class BlacklistDomainService : IBlacklistDomainService, IScopeDependency
-    {
-        private readonly ITypedCache<Blacklist> _blackCache;
-        private readonly IBlacklistRepository _blacklistRepository;
-
-        public BlacklistDomainService(ITypedCache<Blacklist> blackCache, IBlacklistRepository blacklistRepository)
-        {
-            _blackCache = blackCache;
-            _blacklistRepository = blacklistRepository;
-        }
-
-        /// <summary>
-        /// 将电话加入黑名单
-        /// </summary>
-        public async Task AddAsync(Blacklist blacklist, CancellationToken cancellationToken = default)
-        {
-            blacklist.Id = await _blacklistRepository.AddAsync(blacklist, cancellationToken);
-            _blackCache.GetOrSet(Blacklist.GetKey(blacklist.PhoneNo), blacklist, TimeSpan.FromSeconds(blacklist.Duration));
-        }
-
-        /// <summary>
-        /// 删除黑名单信息
-        /// </summary>
-        /// <param name="phoneNo"></param>
-        public async Task Remove(string phoneNo)
-        {
-            _blackCache.Remove(Blacklist.GetKey(phoneNo));
-            await _blacklistRepository.RemoveAsync(d => d.PhoneNo == phoneNo, true);
-        }
-
-        /// <summary>
-        /// 是否在黑名单中
-        /// </summary>
-        /// <param name="phoneNo"></param>
-        /// <returns></returns>
-        public bool IsInBlacklist(string phoneNo)
-        {
-            var blacklist = _blackCache.Get(Blacklist.GetKey(phoneNo));
-            return blacklist != null;
-        }
-    }
 }

+ 6 - 4
src/XF.Domain/Dependency/DependencyInjectionExtensions.cs

@@ -43,11 +43,10 @@ public static class DependencyInjectionExtensions
     /// <returns></returns>
     private static List<Assembly> GetAllAssembly()
     {
-
         var allAssemblies = AppDomain.CurrentDomain.GetAssemblies().ToList();
 
         return allAssemblies;
-
+        //
         HashSet<string> loadedAssemblies = new();
 
         foreach (var item in allAssemblies)
@@ -84,21 +83,24 @@ internal class ServiceRegister
 {
     public static void Register(IServiceCollection services, Type type)
     {
-        if (type.Name == "IUserCacheManager")
-            throw new Exception();
+        //if (type.Name == "IUserCacheManager")
+        //    throw new Exception();
         if (type == null) return;
         var serviceType = GetServiceType(type);
         if (HasImplInterface(type, typeof(ISingletonDependency)))
         {
             services.AddSingleton(serviceType, type);
+            Console.WriteLine($"lifeTime: singleton, {serviceType.FullName} => {type.FullName}");
         }
         else if (HasImplInterface(type, typeof(IScopeDependency)))
         {
             services.AddScoped(serviceType, type);
+            Console.WriteLine($"lifeTime: scope, {serviceType.FullName} => {type.FullName}");
         }
         else if (HasImplInterface(type, typeof(ITransientDependency)))
         {
             services.AddTransient(serviceType, type);
+            Console.WriteLine($"lifeTime: transient, {serviceType.FullName} => {type.FullName}");
         }
         else
         {

+ 1 - 1
src/XF.Domain/XF.Domain.csproj

@@ -5,7 +5,7 @@
     <ImplicitUsings>enable</ImplicitUsings>
     <Nullable>enable</Nullable>
     <GeneratePackageOnBuild>True</GeneratePackageOnBuild>
-    <Version>1.0.5</Version>
+    <Version>1.0.6</Version>
     <Authors>xf</Authors>
   </PropertyGroup>