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}"); + } }