From f42e2db302937892e512bb630463a8787a0e1213 Mon Sep 17 00:00:00 2001 From: xiaolipro <2357729423@qq.com> Date: Wed, 18 Sep 2024 10:38:32 +0800 Subject: [PATCH] =?UTF-8?q?=E6=A2=B3=E7=90=86=E9=A2=84=E8=AD=A6=E9=80=9A?= =?UTF-8?q?=E7=9F=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Dtos/Forewarning/AreaCacheEntry.cs | 2 +- .../Forewarning/ForewarningRuleCacheEntry.cs | 2 +- .../Dtos/Forewarning/RuleAreaBaseInfo.cs | 2 +- .../Events/ClearRulCacheEvent.cs | 8 ++ .../Services}/IForewarningCacheService.cs | 5 +- .../TrackButtonUpScanner.cs | 5 +- .../HostServices/RedisHostService.cs | 4 +- .../SJZY.Track.Application.csproj | 3 +- .../ForewarningCacheService.cs | 12 +- .../ForewarningNotifyService.cs | 119 +++++++----------- .../ForewarningServices/ForewarningService.cs | 2 +- .../ForewarningServices/KpiAreaService.cs | 2 +- .../SjzyTrackApplicationModule.cs | 9 +- .../Dtos/Forewarning/ClearRulCacheEvent.cs | 6 - .../Dtos/Forewarning/RuleAreaBaseInfoVo.cs | 17 +++ .../Queries/IWarningQueries.cs | 2 +- .../Queries/WarningQueries.cs | 4 +- .../SJZY.Track.Repository.csproj | 2 +- 18 files changed, 103 insertions(+), 103 deletions(-) rename src/{SJZY.Track.Domain => SJZY.Track.Application.Contract}/Dtos/Forewarning/AreaCacheEntry.cs (90%) rename src/{SJZY.Track.Domain => SJZY.Track.Application.Contract}/Dtos/Forewarning/ForewarningRuleCacheEntry.cs (95%) rename src/{SJZY.Track.Domain => SJZY.Track.Application.Contract}/Dtos/Forewarning/RuleAreaBaseInfo.cs (82%) create mode 100644 src/SJZY.Track.Application.Contract/Events/ClearRulCacheEvent.cs rename src/{SJZY.Track.Domain/Aggregates/ForewarningAggregate => SJZY.Track.Application.Contract/Services}/IForewarningCacheService.cs (85%) delete mode 100644 src/SJZY.Track.Domain/Dtos/Forewarning/ClearRulCacheEvent.cs create mode 100644 src/SJZY.Track.Domain/Dtos/Forewarning/RuleAreaBaseInfoVo.cs diff --git a/src/SJZY.Track.Domain/Dtos/Forewarning/AreaCacheEntry.cs b/src/SJZY.Track.Application.Contract/Dtos/Forewarning/AreaCacheEntry.cs similarity index 90% rename from src/SJZY.Track.Domain/Dtos/Forewarning/AreaCacheEntry.cs rename to src/SJZY.Track.Application.Contract/Dtos/Forewarning/AreaCacheEntry.cs index e29b82b8..c4de4ba6 100644 --- a/src/SJZY.Track.Domain/Dtos/Forewarning/AreaCacheEntry.cs +++ b/src/SJZY.Track.Application.Contract/Dtos/Forewarning/AreaCacheEntry.cs @@ -1,4 +1,4 @@ -namespace SJZY.Track.Domain.Dtos.Forewarning; +namespace SJZY.Track.Application.Contract.Dtos.Forewarning; public class AreaCacheEntry { diff --git a/src/SJZY.Track.Domain/Dtos/Forewarning/ForewarningRuleCacheEntry.cs b/src/SJZY.Track.Application.Contract/Dtos/Forewarning/ForewarningRuleCacheEntry.cs similarity index 95% rename from src/SJZY.Track.Domain/Dtos/Forewarning/ForewarningRuleCacheEntry.cs rename to src/SJZY.Track.Application.Contract/Dtos/Forewarning/ForewarningRuleCacheEntry.cs index 75c6b552..76a31a74 100644 --- a/src/SJZY.Track.Domain/Dtos/Forewarning/ForewarningRuleCacheEntry.cs +++ b/src/SJZY.Track.Application.Contract/Dtos/Forewarning/ForewarningRuleCacheEntry.cs @@ -1,4 +1,4 @@ -namespace SJZY.Track.Domain.Dtos.Forewarning; +namespace SJZY.Track.Application.Contract.Dtos.Forewarning; public class ForewarningRuleCacheEntry { diff --git a/src/SJZY.Track.Domain/Dtos/Forewarning/RuleAreaBaseInfo.cs b/src/SJZY.Track.Application.Contract/Dtos/Forewarning/RuleAreaBaseInfo.cs similarity index 82% rename from src/SJZY.Track.Domain/Dtos/Forewarning/RuleAreaBaseInfo.cs rename to src/SJZY.Track.Application.Contract/Dtos/Forewarning/RuleAreaBaseInfo.cs index cc6959eb..ee217da3 100644 --- a/src/SJZY.Track.Domain/Dtos/Forewarning/RuleAreaBaseInfo.cs +++ b/src/SJZY.Track.Application.Contract/Dtos/Forewarning/RuleAreaBaseInfo.cs @@ -1,4 +1,4 @@ -namespace SJZY.Track.Domain.Dtos.Forewarning; +namespace SJZY.Track.Application.Contract.Dtos.Forewarning; public class RuleAreaBaseInfo { diff --git a/src/SJZY.Track.Application.Contract/Events/ClearRulCacheEvent.cs b/src/SJZY.Track.Application.Contract/Events/ClearRulCacheEvent.cs new file mode 100644 index 00000000..54ec00e9 --- /dev/null +++ b/src/SJZY.Track.Application.Contract/Events/ClearRulCacheEvent.cs @@ -0,0 +1,8 @@ +using SJZY.Track.Application.Contract.Dtos.Forewarning; + +namespace SJZY.Track.Application.Contract.Events; + +public class ClearRulCacheEvent +{ + public List Items { get; set; } +} diff --git a/src/SJZY.Track.Domain/Aggregates/ForewarningAggregate/IForewarningCacheService.cs b/src/SJZY.Track.Application.Contract/Services/IForewarningCacheService.cs similarity index 85% rename from src/SJZY.Track.Domain/Aggregates/ForewarningAggregate/IForewarningCacheService.cs rename to src/SJZY.Track.Application.Contract/Services/IForewarningCacheService.cs index 2c82e8d0..4ed849d3 100644 --- a/src/SJZY.Track.Domain/Aggregates/ForewarningAggregate/IForewarningCacheService.cs +++ b/src/SJZY.Track.Application.Contract/Services/IForewarningCacheService.cs @@ -1,6 +1,7 @@ -using SJZY.Track.Domain.Dtos.Forewarning; +using SJZY.Track.Application.Contract.Dtos.Forewarning; +using SJZY.Track.Application.Contract.Events; -namespace SJZY.Track.Domain.Aggregates.ForewarningAggregate; +namespace SJZY.Track.Application.Contract.Services; public interface IForewarningCacheService { diff --git a/src/SJZY.Track.Application/BackgroundServices/TrackButtonUpScanner.cs b/src/SJZY.Track.Application/BackgroundServices/TrackButtonUpScanner.cs index 0f26fcc8..2fc53b30 100644 --- a/src/SJZY.Track.Application/BackgroundServices/TrackButtonUpScanner.cs +++ b/src/SJZY.Track.Application/BackgroundServices/TrackButtonUpScanner.cs @@ -23,10 +23,11 @@ public class TrackButtonUpScanner : BackgroundService _orderRepository = orderRepository; } - protected override Task ExecuteAsync(CancellationToken stoppingToken) { - throw new NotImplementedException(); + // 查看哪些要兜底的轨迹(服务商拉取的,且时区发生地是空的),根据子系统、服务商分组尝试兜底 + // 维护轨迹明细、订单主表 + return Task.CompletedTask; } /// diff --git a/src/SJZY.Track.Application/HostServices/RedisHostService.cs b/src/SJZY.Track.Application/HostServices/RedisHostService.cs index c7544719..a165c0ed 100644 --- a/src/SJZY.Track.Application/HostServices/RedisHostService.cs +++ b/src/SJZY.Track.Application/HostServices/RedisHostService.cs @@ -1,7 +1,7 @@ using Microsoft.Extensions.Hosting; using SJZY.Expand.ABP.Core; -using SJZY.Track.Domain.Aggregates.ForewarningAggregate; -using SJZY.Track.Domain.Dtos.Forewarning; +using SJZY.Track.Application.Contract.Events; +using SJZY.Track.Application.Contract.Services; namespace SJZY.Track.Application.HostServices; diff --git a/src/SJZY.Track.Application/SJZY.Track.Application.csproj b/src/SJZY.Track.Application/SJZY.Track.Application.csproj index 9c1fe137..35e87215 100644 --- a/src/SJZY.Track.Application/SJZY.Track.Application.csproj +++ b/src/SJZY.Track.Application/SJZY.Track.Application.csproj @@ -9,11 +9,10 @@ - - + diff --git a/src/SJZY.Track.Application/Services/ForewarningServices/ForewarningCacheService.cs b/src/SJZY.Track.Application/Services/ForewarningServices/ForewarningCacheService.cs index c89475f9..b23a2d4d 100644 --- a/src/SJZY.Track.Application/Services/ForewarningServices/ForewarningCacheService.cs +++ b/src/SJZY.Track.Application/Services/ForewarningServices/ForewarningCacheService.cs @@ -1,7 +1,9 @@ using System.Collections.Concurrent; using SJZY.Expand.ABP.Core; using SJZY.Expand.ABP.Core.PublicMethords; -using SJZY.Track.Domain.Aggregates.ForewarningAggregate; +using SJZY.Track.Application.Contract.Dtos.Forewarning; +using SJZY.Track.Application.Contract.Events; +using SJZY.Track.Application.Contract.Services; using SJZY.Track.Domain.Aggregates.KPIAreaAggregate; using SJZY.Track.Domain.Dtos.Forewarning; using SJZY.Track.Domain.Queries; @@ -64,7 +66,7 @@ public class ForewarningCacheService : IForewarningCacheService, ISingletonDepen public async Task ClearRedisByAreaId(int areaId) { var baseInfos = await _warningQueries.GetWarningRuleByAreaId(areaId); - if (baseInfos == null || baseInfos.Count == 0) return; + if (!baseInfos.Any()) return; baseInfos = baseInfos.DistinctBy(val => WarningKey(val.SystemId, val.CustomerId)).ToList(); var willDelKeys = baseInfos.Select(val => WarningKey(val.SystemId, val.CustomerId)).ToArray(); @@ -72,7 +74,11 @@ public class ForewarningCacheService : IForewarningCacheService, ISingletonDepen await RedisHelper.Client.DelAsync(AreaKey(areaId)); var eventModel = new ClearRulCacheEvent { - Items = baseInfos + Items = baseInfos.Select(x => new RuleAreaBaseInfo + { + SystemId = x.SystemId, + CustomerId = x.CustomerId + }).ToList() }; await PublishEvent(eventModel); diff --git a/src/SJZY.Track.Application/Services/ForewarningServices/ForewarningNotifyService.cs b/src/SJZY.Track.Application/Services/ForewarningServices/ForewarningNotifyService.cs index 20764451..a71f5711 100644 --- a/src/SJZY.Track.Application/Services/ForewarningServices/ForewarningNotifyService.cs +++ b/src/SJZY.Track.Application/Services/ForewarningServices/ForewarningNotifyService.cs @@ -2,9 +2,6 @@ using System.Text; using FreeRedis; using SJZY.Expand.ABP.Core; using SJZY.Expand.ABP.Core.Logger; -using SJZY.FSCenter.Application.Dtos.Message; -using SJZY.FSCenter.Client.Client.Message; -using SJZY.FSCenter.Repository.Enum; using SJZY.Track.Application.Contract.Dtos.Forewarning; using SJZY.Track.Application.Contract.Services; using SJZY.Track.Domain.Aggregates.ForewarningAggregate; @@ -24,23 +21,23 @@ namespace SJZY.Track.Application.Services.ForewarningServices; /// public class ForewarningNotifyService : IForewarningNotifyService, ITransientDependency { - private readonly IForewarningCacheService _cacheService; - private readonly IMessageClient _messageClient; + private readonly IForewarningCacheService _forewarningCacheService; private readonly IHttpClientProxy _requestSendMessageService; private readonly IWarningQueries _warningQueries; private readonly IWarningRecordEsRepository _warningRecordEsRepository; - public ForewarningNotifyService(IForewarningCacheService cacheService, IWarningQueries warningQueries, + public ForewarningNotifyService(IWarningQueries warningQueries, IWarningRecordEsRepository warningRecordEsRepository, - IHttpClientProxy requestSendMessageService, IMessageClient messageClient) + IHttpClientProxy requestSendMessageService, + IForewarningCacheService forewarningCacheService) { - _cacheService = cacheService; _warningQueries = warningQueries; _warningRecordEsRepository = warningRecordEsRepository; _requestSendMessageService = requestSendMessageService; - _messageClient = messageClient; + _forewarningCacheService = forewarningCacheService; } + /// /// 创建预警通知 /// @@ -49,7 +46,7 @@ public class ForewarningNotifyService : IForewarningNotifyService, ITransientDep { if (dto.Orders.Count == 0) return; - var rules = await _cacheService.GetRulesAsync(dto.SubSystemId, dto.CustomerId); + var rules = await _forewarningCacheService.GetRulesAsync(dto.SubSystemId, dto.CustomerId); if (rules == null || rules.Count == 0) return; var adds = new List<(int ruleId, int orderId, int counter, long ts)>(); @@ -134,7 +131,7 @@ public class ForewarningNotifyService : IForewarningNotifyService, ITransientDep var warningRecords = await WriteWarningRecordAsync(entries, notifyRules); // 发通知 - await SendCoreAsync(entries, notifyRules); + await SendCoreAsync(subSystemId, entries, notifyRules); // 移除已处理的预警 await RedisHelper.Client.ZRemRangeByScoreAsync(redisKey, 0, timestamp); @@ -169,7 +166,8 @@ public class ForewarningNotifyService : IForewarningNotifyService, ITransientDep return warningRecords; } - private Task SendCoreAsync(List entries, List notifyRules) + private Task SendCoreAsync(int subSystemId, List entries, + List notifyRules) { var userRules = new Dictionary>(); foreach (var rule in notifyRules) @@ -187,64 +185,10 @@ public class ForewarningNotifyService : IForewarningNotifyService, ITransientDep foreach (var userRule in userRules) { - var siteInternalContent = BuildNotifyContent(userRule.Value, entries, NotifyMode.SiteInternal); - var feiShuContent = BuildNotifyContent(userRule.Value, entries, NotifyMode.FeiShu); - - if (!string.IsNullOrEmpty(siteInternalContent)) - { - _ = Task.Run(async () => - { - // todo:发送站内通知 - var res = await _requestSendMessageService.Service.SendMessageByEmptyTemplateAsync( - new SendMessageByEmptyTemplateInput - { - IsNeedToHandle = null, - BizId = null, - MsgSource = null, - SystemId = 0, - TemplateCode = null, - Channels = null, - MsgTitle = null, - MsgContent = null, - PcURl = null, - MobileURl = null, - FileUrl = null, - fileName = null, - receivers = null, - SiteLetterComponentStyle = (ComponentStyle) 0 - }); - - if (res.Code != GlobalConst.SuccessCode) - { - LogHelper.Error($"发送站内通知失败,用户:{userRule.Key},内容:{siteInternalContent}"); - } - }); - } - - if (!string.IsNullOrEmpty(feiShuContent)) - { - _ = Task.Run(async () => - { - // 发送飞书通知 - var res = await _messageClient.SendMessageAsync(new SendMessageInput - { - AppId = null, - AppSecret = null, - ReceiveIdType = IdTypeEnums.user_id, - MsgType = MsgTypeEnums.文本, - ReceiveIds = new List - { - userRule.Key.ToString() - }, - Content = feiShuContent - }); - - if (res.Code != GlobalConst.SuccessCode) - { - LogHelper.Error($"发送飞书通知失败,用户:{userRule.Key},内容:{feiShuContent}"); - } - }); - } + // 以人的维度发送通知,异步 + _ = DoNotifyAsync(subSystemId, userRule.Key, userRule.Value, entries, + NotifyMode.SiteInternal); + _ = DoNotifyAsync(subSystemId, userRule.Key, userRule.Value, entries, NotifyMode.FeiShu); } return Task.CompletedTask; @@ -253,7 +197,8 @@ public class ForewarningNotifyService : IForewarningNotifyService, ITransientDep private string GetForewarningNotifySetKeyRedisKey(int subSystemId) => $"{GlobalConst.ForewarningNotifySetKey}:{subSystemId}"; - private string BuildNotifyContent(List rules, List entries, + private async Task DoNotifyAsync(int subSystemId, int userId, List rules, + List entries, NotifyMode mode) { /* 以人的维度发送通知 @@ -283,7 +228,37 @@ public class ForewarningNotifyService : IForewarningNotifyService, ITransientDep } } - return notifyContent.ToString(); + var msgContent = notifyContent.ToString(); + + if (string.IsNullOrEmpty(msgContent)) return; + + var res = await _requestSendMessageService.Service.SendMessageByEmptyTemplateAsync( + new SendMessageByEmptyTemplateInput + { + IsNeedToHandle = true, + MsgSource = MsgSourceEnum.系统提醒, + SystemId = subSystemId, + TemplateCode = "trackWarning", + Channels = new List() + { + mode == NotifyMode.FeiShu ? ChannelEnum.飞书 : ChannelEnum.站内信, + }, + MsgTitle = "轨迹运输时效预警", + MsgContent = msgContent, + receivers = new List + { + new() + { + RecipientType = 1, + ReceiverId = userId, + } + }, + }); + + if (res.Code != GlobalConst.SuccessCode) + { + LogHelper.Error($"发送{mode.ToString()}通知失败,用户:{userId},内容:{msgContent}"); + } } private static List MatchRule(List rules, diff --git a/src/SJZY.Track.Application/Services/ForewarningServices/ForewarningService.cs b/src/SJZY.Track.Application/Services/ForewarningServices/ForewarningService.cs index 1ef4fa2a..d0698568 100644 --- a/src/SJZY.Track.Application/Services/ForewarningServices/ForewarningService.cs +++ b/src/SJZY.Track.Application/Services/ForewarningServices/ForewarningService.cs @@ -1,5 +1,6 @@ using System.ComponentModel.DataAnnotations; using SJZY.Track.Application.Contract.Dtos.Forewarning; +using SJZY.Track.Application.Contract.Services; using SJZY.Track.Application.Helpers; using SJZY.Track.Domain.Aggregates.ForewarningAggregate; using SJZY.Track.Domain.Aggregates.TrackStatusAggregate; @@ -22,7 +23,6 @@ public class ForewarningService : ApplicationService _forewarningCacheService = forewarningCacheService; } - /// /// 获取预警分页列表 /// diff --git a/src/SJZY.Track.Application/Services/ForewarningServices/KpiAreaService.cs b/src/SJZY.Track.Application/Services/ForewarningServices/KpiAreaService.cs index 7f6bd213..f54c3504 100644 --- a/src/SJZY.Track.Application/Services/ForewarningServices/KpiAreaService.cs +++ b/src/SJZY.Track.Application/Services/ForewarningServices/KpiAreaService.cs @@ -1,4 +1,4 @@ -using SJZY.Track.Domain.Aggregates.ForewarningAggregate; +using SJZY.Track.Application.Contract.Services; using SJZY.Track.Domain.Aggregates.KPIAreaAggregate; using SJZY.Track.Domain.Dtos.Forewarning; diff --git a/src/SJZY.Track.Application/SjzyTrackApplicationModule.cs b/src/SJZY.Track.Application/SjzyTrackApplicationModule.cs index 993f6137..1ea550bc 100644 --- a/src/SJZY.Track.Application/SjzyTrackApplicationModule.cs +++ b/src/SJZY.Track.Application/SjzyTrackApplicationModule.cs @@ -7,8 +7,8 @@ using Savorboard.CAP.InMemoryMessageQueue; using SJZY.Expand.ABP.Core.Localization; using SJZY.Expand.ABP.Core.Logger; using SJZY.Expand.ABP.Core.PublicMethords; -using SJZY.FSCenter.Client; using SJZY.InterfaceRelay.Application.Contracts; +using SJZY.Track.Application.BackgroundServices; using SJZY.Track.Application.CAP; using SJZY.Track.Application.Contract; using SJZY.Track.Application.Localization; @@ -35,8 +35,7 @@ namespace SJZY.Track.Application; typeof(SjzyUserCenterHttpApiModule), typeof(SJZYInterfaceRelayApplicationContractsModule), typeof(SjzyTrackDomainModule), - typeof(SjzyTrackApplicationContractModule), - typeof(FSCenterModule) + typeof(SjzyTrackApplicationContractModule) )] public class SjzyTrackApplicationModule : AbpModule { @@ -152,8 +151,8 @@ public class SjzyTrackApplicationModule : AbpModule }; }, typeof(SjzyTrackApplicationModule).Assembly); - // 飞书 - context.Services.Configure(option => { option.SystemKey = "aaa"; }); + context.Services.AddHostedService(); + context.Services.AddHostedService(); } public override void PreConfigureServices(ServiceConfigurationContext context) diff --git a/src/SJZY.Track.Domain/Dtos/Forewarning/ClearRulCacheEvent.cs b/src/SJZY.Track.Domain/Dtos/Forewarning/ClearRulCacheEvent.cs deleted file mode 100644 index 30606ba3..00000000 --- a/src/SJZY.Track.Domain/Dtos/Forewarning/ClearRulCacheEvent.cs +++ /dev/null @@ -1,6 +0,0 @@ -namespace SJZY.Track.Domain.Dtos.Forewarning; - -public class ClearRulCacheEvent -{ - public List Items { get; set; } -} diff --git a/src/SJZY.Track.Domain/Dtos/Forewarning/RuleAreaBaseInfoVo.cs b/src/SJZY.Track.Domain/Dtos/Forewarning/RuleAreaBaseInfoVo.cs new file mode 100644 index 00000000..3590a56d --- /dev/null +++ b/src/SJZY.Track.Domain/Dtos/Forewarning/RuleAreaBaseInfoVo.cs @@ -0,0 +1,17 @@ +namespace SJZY.Track.Domain.Dtos.Forewarning; + +public class RuleAreaBaseInfoVo +{ + public RuleAreaBaseInfoVo() + { + } + + public RuleAreaBaseInfoVo(int systemId, int customerId) + { + SystemId = systemId; + CustomerId = customerId; + } + + public int SystemId { get; set; } + public int CustomerId { get; set; } +} diff --git a/src/SJZY.Track.Domain/Queries/IWarningQueries.cs b/src/SJZY.Track.Domain/Queries/IWarningQueries.cs index 3a6320e5..e6a7ce00 100644 --- a/src/SJZY.Track.Domain/Queries/IWarningQueries.cs +++ b/src/SJZY.Track.Domain/Queries/IWarningQueries.cs @@ -23,5 +23,5 @@ public interface IWarningQueries /// /// /// - Task?> GetWarningRuleByAreaId(int areaId); + Task> GetWarningRuleByAreaId(int areaId); } diff --git a/src/SJZY.Track.Repository/Queries/WarningQueries.cs b/src/SJZY.Track.Repository/Queries/WarningQueries.cs index 4d970615..c140c531 100644 --- a/src/SJZY.Track.Repository/Queries/WarningQueries.cs +++ b/src/SJZY.Track.Repository/Queries/WarningQueries.cs @@ -79,12 +79,12 @@ public class WarningQueries : ISugarRepository, IWarningQueries, .ToListAsync(); } - public Task?> GetWarningRuleByAreaId(int areaId) + public Task> GetWarningRuleByAreaId(int areaId) { return Context.Queryable() .InnerJoin((a, b) => a.WarningId == b.Id) .Where((a, b) => b.IsEnable && a.AreaId == areaId) - .Select((a, b) => new RuleAreaBaseInfo + .Select((a, b) => new RuleAreaBaseInfoVo { CustomerId = b.CustomerId, SystemId = b.SubSystemId diff --git a/src/SJZY.Track.Repository/SJZY.Track.Repository.csproj b/src/SJZY.Track.Repository/SJZY.Track.Repository.csproj index 79eeb89e..7948e437 100644 --- a/src/SJZY.Track.Repository/SJZY.Track.Repository.csproj +++ b/src/SJZY.Track.Repository/SJZY.Track.Repository.csproj @@ -10,7 +10,7 @@ - +