From aae9add71c9bf6ce004568285df71a2724225d5e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=92=9F=E5=96=9C=E6=97=BA?= Date: Thu, 25 Jul 2024 15:03:47 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E8=BD=A8=E8=BF=B9=E6=98=A0=E5=B0=84?= =?UTF-8?q?=E7=95=8C=E9=9D=A2=E7=9A=84=E6=8E=A8=E9=80=81=E9=85=8D=E7=BD=AE?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=8E=A8=E9=80=81=E6=97=B6=E9=97=B4=E6=A0=BC?= =?UTF-8?q?=E5=BC=8F=20=E5=BC=80=E5=8F=91=E6=B5=8B=E8=AF=95=E5=AE=8C?= =?UTF-8?q?=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Dtos/TrackPush/TrackPushInput.cs | 2 +- .../EventHandlers/TrackPushEventHandler.cs | 50 +++++++++++++------ .../Helpers/DateTimeHelper.cs | 11 ++-- .../ProvideServices/TrackPushService.cs | 24 ++++++++- 4 files changed, 65 insertions(+), 22 deletions(-) diff --git a/src/SJZY.Track.Application.Contract/Dtos/TrackPush/TrackPushInput.cs b/src/SJZY.Track.Application.Contract/Dtos/TrackPush/TrackPushInput.cs index bc1d10bc..28a18876 100644 --- a/src/SJZY.Track.Application.Contract/Dtos/TrackPush/TrackPushInput.cs +++ b/src/SJZY.Track.Application.Contract/Dtos/TrackPush/TrackPushInput.cs @@ -21,7 +21,7 @@ public class TrackUpdateInput public class TrackItem { public string Location { get; set; } = null!; - public DateTime Date { get; set; } + public string Date { get; set; } public double TimeZone { get; set; } public string? Description { get; set; } public string? DescriptionCn { get; set; } diff --git a/src/SJZY.Track.Application/CAP/EventHandlers/TrackPushEventHandler.cs b/src/SJZY.Track.Application/CAP/EventHandlers/TrackPushEventHandler.cs index cd7ec8f7..30ef3bdc 100644 --- a/src/SJZY.Track.Application/CAP/EventHandlers/TrackPushEventHandler.cs +++ b/src/SJZY.Track.Application/CAP/EventHandlers/TrackPushEventHandler.cs @@ -1,4 +1,5 @@ -using DotNetCore.CAP; +using System.Globalization; +using DotNetCore.CAP; using Newtonsoft.Json; using SJZY.Expand.ABP.Core.PublicMethords; using SJZY.InterfaceRelay.Application.Contracts; @@ -87,8 +88,6 @@ public class TrackPushEventHandler : ITransientDependency, ICapSubscribe if (!config.IsAutoPush) continue; if (config.OrderType != order.OrderType) continue; - // todo 确认是不是这里转换 - DateTimeFormat( @event.SystemPushInfo.TrackTime,"yyyy-MM-dd HH:mm:ss"); var log = new TrackPushRecordIndex { OptionType = @event.OptionType.ToString(), @@ -114,7 +113,7 @@ public class TrackPushEventHandler : ITransientDependency, ICapSubscribe var tracks = await _trackDetailsRepository.GetListAsync(@event.OrderIds, @event.TrackCode); tracks = tracks.DistinctBy(x => x.OrderId).ToList(); // 防止重复推送 - + var temp=this.DateTimeFormat(DateTime.Now, -8.0, config); if (tracks.Count == 0) return; // 一个能推的轨迹都不存在 // tips:人工推送只要能推就推了,不会校验已推和顺序问题,因为可能是更新 @@ -161,12 +160,6 @@ public class TrackPushEventHandler : ITransientDependency, ICapSubscribe throw new BusinessException(message: $"requestId: {r.RequestId}; {r.Msg}"); } } - - - private string DateTimeFormat(DateTime dateTime,string format) - { - return dateTime.ToString("yyyy-MM-dd HH:mm:ss"); - } private async Task DoUpdateAsync(TrackPushEvent @event) { @@ -210,12 +203,14 @@ public class TrackPushEventHandler : ITransientDependency, ICapSubscribe private TrackUpdateInput BuiltUpdateItem(TrackPushContentConfigDto config, TrackPushRecordIndex log) { + var trackDatetime = log.TrackTime.AddHours(config.TrackTimeZone ?? log.TrackTimeZone - log.TrackTimeZone); + var trackDateStr = DateTimeFormat(trackDatetime,config.TrackTimeZone ?? log.TrackTimeZone,config); + var track = new TrackItem { // 推真实的发生地和发生时间(时间是计算后的) Location = log.TrackPlace, - Date = log.TrackTime.AddHours( - config.TrackTimeZone ?? log.TrackTimeZone - log.TrackTimeZone), + Date = trackDateStr, TimeZone = config.TrackTimeZone ?? log.TrackTimeZone, Description = config.TrackEnContentValue, DescriptionCn = config.TrackContentValue, @@ -236,12 +231,14 @@ public class TrackPushEventHandler : ITransientDependency, ICapSubscribe private async Task BuiltPushItem(TrackPushContentConfigDto config, TrackPushRecordIndex log) { + var trackDatetime = log.TrackTime.AddHours(config.TrackTimeZone ?? log.TrackTimeZone - log.TrackTimeZone); + var trackDateStr = DateTimeFormat(trackDatetime,config.TrackTimeZone ?? log.TrackTimeZone,config); + var track = new TrackItem { // 推真实的发生地和发生时间(时间是计算后的) Location = log.TrackPlace, - Date = log.TrackTime.AddHours( - config.TrackTimeZone ?? log.TrackTimeZone - log.TrackTimeZone), + Date = trackDateStr, TimeZone = config.TrackTimeZone ?? log.TrackTimeZone, Description = config.TrackEnContentValue, DescriptionCn = config.TrackContentValue, @@ -258,7 +255,6 @@ public class TrackPushEventHandler : ITransientDependency, ICapSubscribe if (config.IsAllPush) { - //todo 这个是指每次都带上之前的轨迹嘛 var history = await _trackPushRecordsRepository.GetLatestTrackPushRecordAsync(log.OrderId); if (history != default) { @@ -296,4 +292,28 @@ public class TrackPushEventHandler : ITransientDependency, ICapSubscribe return matched != null && matched.TrackCode == trackCode ? matched : default; } + + /// + /// 转换推送时间格式 + /// + /// + /// + /// + /// + private string DateTimeFormat(DateTime dateTime,double timezone, TrackPushContentConfigDto config) + { + if (config.DateTimeFormatType == DateTimeFormatEnum.Timestamp) + return DateTimeHelper.ConvertToUnixTimestamp(dateTime).ToString(); + + if (config.DateTimeFormatType == DateTimeFormatEnum.DateTimeAndTimezone + && !string.IsNullOrEmpty(config.DateTimeFormat) + && !string.IsNullOrEmpty(config.TimezoneFormat)) + { + var zoneStr = DateTimeHelper.TimezoneFormat(timezone, config.TimezoneFormat); + var datetimeStr = DateTimeHelper.FormatDateTime(dateTime, config.DateTimeFormat); + return $"{datetimeStr} {zoneStr}"; + } + + return dateTime.ToString("yyyy-MM-ddTHH:mm:ss.fffK"); + } } \ No newline at end of file diff --git a/src/SJZY.Track.Application/Helpers/DateTimeHelper.cs b/src/SJZY.Track.Application/Helpers/DateTimeHelper.cs index b0f3e0bd..907cab70 100644 --- a/src/SJZY.Track.Application/Helpers/DateTimeHelper.cs +++ b/src/SJZY.Track.Application/Helpers/DateTimeHelper.cs @@ -1,13 +1,15 @@ +using SJZY.OTel; + namespace SJZY.Track.Application.Helpers; public static class DateTimeHelper { /// - /// Generates a Unix timestamp from a DateTime object. + /// convert to a Unix timestamp from a DateTime object. /// /// The DateTime object to convert to a Unix timestamp. /// The Unix timestamp as a long. - public static long GenerateUnixTimestamp(DateTime dateTime) + public static long ConvertToUnixTimestamp(DateTime dateTime) { return new DateTimeOffset(dateTime).ToUnixTimeSeconds(); } @@ -29,8 +31,9 @@ public static class DateTimeHelper /// /// /// - public static string TimezoneFormat(float timezone, string format) + public static string TimezoneFormat(double timezone, string format) { - return string.Format(format, timezone); + //时区是整数丢掉浮点数部分 + return format.Replace("hh", ((int)timezone).ToString()); } } \ No newline at end of file diff --git a/src/SJZY.Track.Application/Services/ProvideServices/TrackPushService.cs b/src/SJZY.Track.Application/Services/ProvideServices/TrackPushService.cs index 41a91a72..c7663f86 100644 --- a/src/SJZY.Track.Application/Services/ProvideServices/TrackPushService.cs +++ b/src/SJZY.Track.Application/Services/ProvideServices/TrackPushService.cs @@ -1,5 +1,6 @@ using DotNetCore.CAP; using Microsoft.AspNetCore.Authorization; +using SJZY.Track.Application.CAP.EventHandlers; using SJZY.Track.Application.Contract.Dtos.ProvideServices; using SJZY.Track.Application.Contract.Events; using SJZY.Track.Domain.Aggregates.OrderAggregate; @@ -21,16 +22,17 @@ public class TrackPushService : ApplicationService private readonly ICapPublisher _capPublisher; private readonly ITrackPushRecordsRepository _trackPushRecordsRepository; private readonly IHttpClientProxy _requestCurrentUserService; - + TrackPushEventHandler _trackPushEventHandler; public TrackPushService(IOrderRepository orderRepository , ICapPublisher capPublisher , ITrackPushRecordsRepository trackPushRecordsRepository, - IHttpClientProxy requestCurrentUserService) + IHttpClientProxy requestCurrentUserService, TrackPushEventHandler trackPushEventHandler) { _orderRepository = orderRepository; _capPublisher = capPublisher; _trackPushRecordsRepository = trackPushRecordsRepository; _requestCurrentUserService = requestCurrentUserService; + _trackPushEventHandler = trackPushEventHandler; } /// @@ -41,6 +43,24 @@ public class TrackPushService : ApplicationService [HttpPost("Track/Push")] public async Task PushTrackAsync(PushTrackInput input) { + var trackPushEvenst = new TrackPushEvent +{ + OptionType = OptionType.新增, // Assuming OptionType.新增 corresponds to 1 + SubSystemId = 9534, + CustomId = 228618, + OrderIds = new int[] { 1 }, + TrackCode = "CP", + OperationMan = "郑志红", + SystemPushInfo = new SystemPushStruct + { + TrackStatus = null, + TrackContent = null, + TrackPlace = null, + TrackTime = DateTime.Parse("0001-01-01T00:00:00"), + TrackTimeZone = 0.0f + } +}; + await _trackPushEventHandler.ConsumerAsync(trackPushEvenst); input.OrderNos = input.OrderNos.Where(x => !x.IsNullOrWhiteSpace()).Distinct().ToList(); // 单号去重、去空 var orders = await _orderRepository.GetArrayAsync(x =>