From 24d08cbc8460f0cf3f9c1f73de662fad46bb795b Mon Sep 17 00:00:00 2001
From: hardstylewyl <827778732@qq.com>
Date: Fri, 23 May 2025 15:21:20 +0800
Subject: [PATCH] =?UTF-8?q?pref:=E8=BD=A8=E8=BF=B9=E6=8E=A8=E9=80=81?=
=?UTF-8?q?=E9=85=8D=E7=BD=AE=E8=A1=A5=E5=85=85=E6=94=AF=E6=8C=81=E5=8F=AA?=
=?UTF-8?q?=E4=BD=BF=E7=94=A8=E6=97=B6=E9=97=B4=E6=A0=BC=E5=BC=8F=E5=8C=96?=
=?UTF-8?q?=20add-validation:=E5=A2=9E=E5=8A=A0=E6=8E=A8=E9=80=81=E9=85=8D?=
=?UTF-8?q?=E7=BD=AE=E6=96=B0=E5=A2=9E/=E6=9B=B4=E6=96=B0=E7=9A=84?=
=?UTF-8?q?=E6=A0=BC=E6=97=B6=E9=97=B4=E6=A0=BC=E5=BC=8F=E5=8C=96=E6=A8=A1?=
=?UTF-8?q?=E6=9D=BF=E9=AA=8C=E8=AF=81=20=20-=20=E5=9C=A8=E6=97=B6?=
=?UTF-8?q?=E9=97=B4=E5=92=8C=E6=97=B6=E5=8C=BA=E6=A8=A1=E5=BC=8F=E4=B8=8B?=
=?UTF-8?q?=EF=BC=8C=E4=B8=8D=E5=85=81=E8=AE=B8=E5=8F=AA=E8=BE=93=E5=85=A5?=
=?UTF-8?q?=E6=97=B6=E5=8C=BA=E6=A8=A1=E6=9D=BF?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../EventHandlers/TrackPushEventHandler.cs | 21 ++++---
.../Services/TrackPush/TrackPushService.cs | 62 ++++++++++++++++++-
2 files changed, 74 insertions(+), 9 deletions(-)
diff --git a/src/SJZY.Track.Application/CAP/EventHandlers/TrackPushEventHandler.cs b/src/SJZY.Track.Application/CAP/EventHandlers/TrackPushEventHandler.cs
index 628c4d17..c6de6489 100644
--- a/src/SJZY.Track.Application/CAP/EventHandlers/TrackPushEventHandler.cs
+++ b/src/SJZY.Track.Application/CAP/EventHandlers/TrackPushEventHandler.cs
@@ -463,7 +463,7 @@ public class TrackPushEventHandler : ITransientDependency, ICapSubscribe
if (string.IsNullOrWhiteSpace(compareFields))
return trackIdentifies.Contains($"{code}{timeStr}");
- var extra = x.GetValueOrDefault("extra");
+ var extra = x.GetValueOrDefault("extra");
if (extra == null) return false;
if (((JsonElement) extra).TryGetProperty(compareFields, out var comparerValueJsonElement))
@@ -538,15 +538,22 @@ public class TrackPushEventHandler : ITransientDependency, ICapSubscribe
if (config.DateTimeFormatType == (int) DateTimeFormatEnum.Timestamp)
return DateTimeHelper.ConvertToUnixTimestamp(dateTime).ToString();
- if (config.DateTimeFormatType == (int) DateTimeFormatEnum.DateTimeAndTimezone
- && !string.IsNullOrEmpty(config.DateTimeFormat)
- && !string.IsNullOrEmpty(config.TimezoneFormat))
+ if (config.DateTimeFormatType == (int) DateTimeFormatEnum.DateTimeAndTimezone)
{
- var zoneStr = DateTimeHelper.TimezoneFormat(timezone, config.TimezoneFormat);
- var datetimeStr = DateTimeHelper.FormatDateTime(dateTime, config.DateTimeFormat);
- return $"{datetimeStr} {zoneStr}";
+ // 只有时间
+ if (!string.IsNullOrWhiteSpace(config.DateTimeFormat) && string.IsNullOrWhiteSpace(config.TimezoneFormat))
+ return DateTimeHelper.FormatDateTime(dateTime, config.DateTimeFormat);
+
+ // 时间和时区
+ if (!string.IsNullOrWhiteSpace(config.DateTimeFormat) && !string.IsNullOrWhiteSpace(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");
}
diff --git a/src/SJZY.Track.Application/Services/TrackPush/TrackPushService.cs b/src/SJZY.Track.Application/Services/TrackPush/TrackPushService.cs
index 646da5e2..8cd08d74 100644
--- a/src/SJZY.Track.Application/Services/TrackPush/TrackPushService.cs
+++ b/src/SJZY.Track.Application/Services/TrackPush/TrackPushService.cs
@@ -5,12 +5,14 @@ using SJZY.Expand.ABP.Core.Logger;
using SJZY.Expand.ABP.Core.PublicMethords;
using SJZY.Track.Application.Contract.Dtos.TrackPush;
using SJZY.Track.Application.Contract.Services;
+using SJZY.Track.Application.Helpers;
using SJZY.Track.Application.Localization;
using SJZY.Track.Application.Services.Warning.Handlers;
using SJZY.Track.Domain.Aggregates.OrderAggregate;
using SJZY.Track.Domain.Aggregates.TrackPushAggregate;
using SJZY.Track.Domain.Dtos.Common;
using SJZY.Track.Domain.Dtos.TrackPush;
+using SJZY.Track.Domain.Enums;
using Volo.Abp.DependencyInjection;
namespace SJZY.Track.Application.Services.TrackPush;
@@ -112,7 +114,7 @@ public class TrackPushService : ApplicationService, ISingletonDependency
///
///
[HttpPost("Edit")]
- public async Task EditAsync([FromBody] EditTrackPushConfigInput input)
+ public async Task EditAsync([FromBody] EditTrackPushConfigInput input)
{
if (input.Options != OptionTypeNoNull.删除)
{
@@ -124,6 +126,14 @@ public class TrackPushService : ApplicationService, ISingletonDependency
throw new BusinessException(message: _stringLocalizer["IsTrackPushConfigAny"]);
}
+ var msg = string.Empty;
+ if (input.Options is OptionTypeNoNull.新增 or OptionTypeNoNull.修改)
+ {
+ var (success, message) = CheckDateTimeFormat(input.DateTimeFormat, input.DateTimeFormatType, input.TimezoneFormat);
+ if (!success) throw new BusinessException(message: message);
+ msg = message;
+ }
+
using var ts = TransactionScopeHelper.Create();
switch (input.Options)
@@ -161,7 +171,7 @@ public class TrackPushService : ApplicationService, ISingletonDependency
}
ts.Complete();
- return true;
+ return msg;
}
@@ -346,4 +356,52 @@ public class TrackPushService : ApplicationService, ISingletonDependency
}
});
}
+
+ private static (bool Success, string Message) CheckDateTimeFormat(string? dateTimeFormat, int? dateTimeFormatType, string? timezoneFormat)
+ {
+ const int defaultTimeZone = 8;
+ var dateTime = DateTime.Now;
+ string dateStr;
+
+ try
+ {
+ if (dateTimeFormatType == (int) DateTimeFormatEnum.Timestamp)
+ {
+ dateStr = DateTimeHelper.ConvertToUnixTimestamp(dateTime).ToString();
+ return (true, $"格式化样例:{dateStr}");
+ }
+
+ if (dateTimeFormatType == (int) DateTimeFormatEnum.DateTimeAndTimezone)
+ {
+ // 没时间有时区,出错
+ if (string.IsNullOrWhiteSpace(dateTimeFormat) && !string.IsNullOrWhiteSpace(timezoneFormat))
+ return (false, "如果填写了时区格式,那么时间格式是必须的");
+
+ // 只有时间
+ if (!string.IsNullOrWhiteSpace(dateTimeFormat) && string.IsNullOrWhiteSpace(timezoneFormat))
+ {
+ var datetimeStr = DateTimeHelper.FormatDateTime(dateTime, dateTimeFormat);
+ return (true, $"格式化样例:{datetimeStr}");
+ }
+
+ // 时间和时区同时存在
+ if (!string.IsNullOrWhiteSpace(dateTimeFormat) && !string.IsNullOrWhiteSpace(timezoneFormat))
+ {
+ var zoneStr = DateTimeHelper.TimezoneFormat(defaultTimeZone, timezoneFormat);
+ var datetimeStr = DateTimeHelper.FormatDateTime(dateTime, dateTimeFormat);
+ dateStr = $"{datetimeStr} {zoneStr}";
+ return (true, $"格式化样例:{dateStr}");
+ }
+ }
+ }
+ catch (Exception e)
+ {
+ return (false, $"时间格式错误请检查:{e.Message}");
+ }
+
+ // 默认情况
+ dateTimeFormat ??= "yyyy-MM-ddTHH:mm:ss.fffK";
+ dateStr = dateTime.ToString(dateTimeFormat);
+ return (true, $"格式化样例:{dateStr}");
+ }
}