2026-06-17 15:41:06 +08:00
|
|
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
|
|
using Prism.Events;
|
|
|
|
|
|
using System.IO;
|
|
|
|
|
|
using System.Net.Http;
|
|
|
|
|
|
using System.Text.Json;
|
|
|
|
|
|
using System.Text.Json.Serialization;
|
|
|
|
|
|
using System.Web;
|
|
|
|
|
|
using YY.Admin.Core;
|
|
|
|
|
|
using YY.Admin.Core.Entity;
|
|
|
|
|
|
using YY.Admin.Core.Events;
|
2026-06-17 17:52:31 +08:00
|
|
|
|
using YY.Admin.Core.Helper;
|
2026-06-17 15:41:06 +08:00
|
|
|
|
using YY.Admin.Core.Services;
|
|
|
|
|
|
|
|
|
|
|
|
namespace YY.Admin.Services.Service.RubberQuickTestStd;
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>胶料快检实验标准:MES 只读拉取 + 本地缓存,断网读缓存,联网刷新</summary>
|
|
|
|
|
|
public class RubberQuickTestStdService : IRubberQuickTestStdService, ISingletonDependency
|
|
|
|
|
|
{
|
|
|
|
|
|
private readonly IHttpClientFactory _httpClientFactory;
|
|
|
|
|
|
private readonly IConfiguration _configuration;
|
|
|
|
|
|
private readonly INetworkMonitor _networkMonitor;
|
|
|
|
|
|
private readonly IEventAggregator _eventAggregator;
|
|
|
|
|
|
private readonly ILoggerService _logger;
|
|
|
|
|
|
private readonly SemaphoreSlim _syncLock = new(1, 1);
|
|
|
|
|
|
private readonly object _cacheLock = new();
|
|
|
|
|
|
private readonly string _cacheFilePath;
|
|
|
|
|
|
private List<MesXslRubberQuickTestStd> _localCache = new();
|
|
|
|
|
|
|
|
|
|
|
|
private static readonly JsonSerializerOptions _jsonOpts = new()
|
|
|
|
|
|
{
|
|
|
|
|
|
PropertyNameCaseInsensitive = true,
|
|
|
|
|
|
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
|
|
|
|
|
|
Converters = { new NullableDateTimeJsonConverter() }
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
public RubberQuickTestStdService(
|
|
|
|
|
|
IHttpClientFactory httpClientFactory,
|
|
|
|
|
|
IConfiguration configuration,
|
|
|
|
|
|
INetworkMonitor networkMonitor,
|
|
|
|
|
|
IEventAggregator eventAggregator,
|
|
|
|
|
|
ILoggerService logger)
|
|
|
|
|
|
{
|
|
|
|
|
|
_httpClientFactory = httpClientFactory;
|
|
|
|
|
|
_configuration = configuration;
|
|
|
|
|
|
_networkMonitor = networkMonitor;
|
|
|
|
|
|
_eventAggregator = eventAggregator;
|
|
|
|
|
|
_logger = logger;
|
|
|
|
|
|
|
|
|
|
|
|
var appDataDir = Path.Combine(
|
|
|
|
|
|
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
|
|
|
|
|
|
"YY.Admin", "sync-cache");
|
|
|
|
|
|
Directory.CreateDirectory(appDataDir);
|
|
|
|
|
|
_cacheFilePath = Path.Combine(appDataDir, "mes-xsl-rubber-quick-test-std-cache.json");
|
|
|
|
|
|
|
|
|
|
|
|
LoadCacheFromDisk();
|
|
|
|
|
|
_logger.Information($"[快检实验标准] 服务初始化,缓存={_localCache.Count},在线={_networkMonitor.IsOnline}");
|
|
|
|
|
|
|
|
|
|
|
|
_networkMonitor.StatusChanged += OnNetworkStatusChanged;
|
|
|
|
|
|
if (_networkMonitor.IsOnline)
|
|
|
|
|
|
_ = Task.Run(() => SyncFromRemoteAsync(CancellationToken.None));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private string BaseUrl => (_configuration.GetValue<string>("JeecgIntegration:BaseUrl") ?? "http://localhost:8080/jeecg-boot").TrimEnd('/');
|
|
|
|
|
|
private int DefaultTenantId => (int?)_configuration.GetValue<long?>("JeecgIntegration:DefaultTenantId") ?? 1002;
|
|
|
|
|
|
private HttpClient CreateClient() => _httpClientFactory.CreateClient("JeecgApi");
|
|
|
|
|
|
|
|
|
|
|
|
public async Task<RubberQuickTestStdPageResult> PageAsync(
|
|
|
|
|
|
int pageNo, int pageSize,
|
|
|
|
|
|
string? stdName = null,
|
|
|
|
|
|
string? rubberMaterialName = null,
|
|
|
|
|
|
string? enableStatus = null,
|
|
|
|
|
|
CancellationToken ct = default)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (_networkMonitor.IsOnline)
|
|
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
await SyncFromRemoteAsync(ct).ConfigureAwait(false);
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
_logger.Warning($"[快检实验标准] 列表拉取失败,使用本地缓存:{ex.Message}");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
List<MesXslRubberQuickTestStd> source;
|
|
|
|
|
|
lock (_cacheLock)
|
|
|
|
|
|
source = _localCache.Select(CloneMain).ToList();
|
|
|
|
|
|
|
|
|
|
|
|
var filtered = ApplyFilters(source, stdName, rubberMaterialName, enableStatus);
|
|
|
|
|
|
var total = filtered.Count;
|
|
|
|
|
|
var records = filtered
|
|
|
|
|
|
.Skip(Math.Max(0, (pageNo - 1) * pageSize))
|
|
|
|
|
|
.Take(pageSize)
|
|
|
|
|
|
.ToList();
|
|
|
|
|
|
return new RubberQuickTestStdPageResult(records, total, pageNo, pageSize);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async Task<MesXslRubberQuickTestStd?> GetByIdAsync(string id, CancellationToken ct = default)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (string.IsNullOrWhiteSpace(id)) return null;
|
|
|
|
|
|
|
|
|
|
|
|
if (_networkMonitor.IsOnline)
|
|
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
var url = $"{BaseUrl}/xslmes/mesXslRubberQuickTestStd/anon/queryById?id={Uri.EscapeDataString(id)}";
|
|
|
|
|
|
using var client = CreateClient();
|
|
|
|
|
|
var resp = await client.GetAsync(url, ct).ConfigureAwait(false);
|
|
|
|
|
|
if (resp.IsSuccessStatusCode)
|
|
|
|
|
|
{
|
|
|
|
|
|
var json = await resp.Content.ReadAsStringAsync(ct).ConfigureAwait(false);
|
|
|
|
|
|
using var doc = JsonDocument.Parse(json);
|
|
|
|
|
|
if (doc.RootElement.TryGetProperty("result", out var resultEl))
|
|
|
|
|
|
{
|
|
|
|
|
|
var entity = resultEl.Deserialize<MesXslRubberQuickTestStd>(_jsonOpts);
|
|
|
|
|
|
if (entity != null)
|
|
|
|
|
|
{
|
2026-06-17 17:52:31 +08:00
|
|
|
|
UpsertIfChanged(entity);
|
2026-06-17 15:41:06 +08:00
|
|
|
|
return CloneMain(entity);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
_logger.Warning($"[快检实验标准] 详情拉取失败 id={id},回退缓存:{ex.Message}");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
lock (_cacheLock)
|
|
|
|
|
|
{
|
|
|
|
|
|
var found = _localCache.FirstOrDefault(x => string.Equals(x.Id, id, StringComparison.OrdinalIgnoreCase));
|
|
|
|
|
|
return found != null ? CloneMain(found) : null;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-06-17 17:52:31 +08:00
|
|
|
|
public async Task<bool> SyncFromRemoteAsync(CancellationToken ct = default)
|
2026-06-17 15:41:06 +08:00
|
|
|
|
{
|
|
|
|
|
|
await _syncLock.WaitAsync(ct).ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!_networkMonitor.IsOnline)
|
2026-06-17 17:52:31 +08:00
|
|
|
|
return false;
|
2026-06-17 15:41:06 +08:00
|
|
|
|
|
|
|
|
|
|
var query = HttpUtility.ParseQueryString(string.Empty);
|
|
|
|
|
|
query["pageNo"] = "1";
|
|
|
|
|
|
query["pageSize"] = "10000";
|
|
|
|
|
|
query["tenantId"] = DefaultTenantId.ToString();
|
|
|
|
|
|
var url = $"{BaseUrl}/xslmes/mesXslRubberQuickTestStd/anon/list?{query}";
|
|
|
|
|
|
|
|
|
|
|
|
using var client = CreateClient();
|
|
|
|
|
|
var resp = await client.GetAsync(url, ct).ConfigureAwait(false);
|
|
|
|
|
|
resp.EnsureSuccessStatusCode();
|
|
|
|
|
|
|
|
|
|
|
|
var json = await resp.Content.ReadAsStringAsync(ct).ConfigureAwait(false);
|
|
|
|
|
|
using var doc = JsonDocument.Parse(json);
|
|
|
|
|
|
var records = doc.RootElement.GetProperty("result").GetProperty("records")
|
|
|
|
|
|
.Deserialize<List<MesXslRubberQuickTestStd>>(_jsonOpts) ?? new();
|
|
|
|
|
|
|
2026-06-17 17:52:31 +08:00
|
|
|
|
List<MesXslRubberQuickTestStd> localSnapshot;
|
|
|
|
|
|
lock (_cacheLock)
|
|
|
|
|
|
localSnapshot = _localCache.Select(CloneMain).ToList();
|
|
|
|
|
|
|
|
|
|
|
|
var (merged, stats) = MesReadOnlyCacheMergeHelper.Merge(
|
|
|
|
|
|
localSnapshot,
|
|
|
|
|
|
records,
|
|
|
|
|
|
x => x.Id,
|
|
|
|
|
|
IsStdListContentEqual,
|
|
|
|
|
|
CloneMain,
|
|
|
|
|
|
MergeStdUpdated);
|
|
|
|
|
|
|
|
|
|
|
|
if (!stats.HasChanges)
|
|
|
|
|
|
{
|
|
|
|
|
|
_logger.Information($"[快检实验标准] 与 MES 对比无差异,跳过更新 count={merged.Count}");
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-06-17 15:41:06 +08:00
|
|
|
|
lock (_cacheLock)
|
|
|
|
|
|
{
|
2026-06-17 17:52:31 +08:00
|
|
|
|
_localCache = merged;
|
2026-06-17 15:41:06 +08:00
|
|
|
|
SaveCacheToDiskUnsafe();
|
|
|
|
|
|
}
|
2026-06-17 17:52:31 +08:00
|
|
|
|
_logger.Information(
|
|
|
|
|
|
$"[快检实验标准] 差异同步完成 total={merged.Count} 新增={stats.Added} 变更={stats.Updated} 删除={stats.Removed}");
|
|
|
|
|
|
return true;
|
2026-06-17 15:41:06 +08:00
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
_logger.Warning($"[快检实验标准] 远程同步失败:{ex.Message}");
|
2026-06-17 17:52:31 +08:00
|
|
|
|
return false;
|
2026-06-17 15:41:06 +08:00
|
|
|
|
}
|
|
|
|
|
|
finally
|
|
|
|
|
|
{
|
|
|
|
|
|
_syncLock.Release();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void OnNetworkStatusChanged(bool isOnline)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!isOnline) return;
|
|
|
|
|
|
_ = Task.Run(async () =>
|
|
|
|
|
|
{
|
2026-06-17 17:52:31 +08:00
|
|
|
|
if (!await SyncFromRemoteAsync(CancellationToken.None).ConfigureAwait(false))
|
|
|
|
|
|
return;
|
2026-06-17 15:41:06 +08:00
|
|
|
|
_eventAggregator.GetEvent<RubberQuickTestStdChangedEvent>()
|
|
|
|
|
|
.Publish(new RubberQuickTestStdChangedPayload { Action = "reconnect" });
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static List<MesXslRubberQuickTestStd> ApplyFilters(
|
|
|
|
|
|
List<MesXslRubberQuickTestStd> source,
|
|
|
|
|
|
string? stdName,
|
|
|
|
|
|
string? rubberMaterialName,
|
|
|
|
|
|
string? enableStatus)
|
|
|
|
|
|
{
|
|
|
|
|
|
IEnumerable<MesXslRubberQuickTestStd> q = source;
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(stdName))
|
|
|
|
|
|
q = q.Where(x => (x.StdName ?? "").Contains(stdName.Trim(), StringComparison.OrdinalIgnoreCase));
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(rubberMaterialName))
|
|
|
|
|
|
q = q.Where(x => (x.RubberMaterialName ?? "").Contains(rubberMaterialName.Trim(), StringComparison.OrdinalIgnoreCase));
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(enableStatus))
|
|
|
|
|
|
q = q.Where(x => string.Equals(x.EnableStatus, enableStatus.Trim(), StringComparison.OrdinalIgnoreCase));
|
|
|
|
|
|
return q.OrderByDescending(x => x.CreateTime ?? DateTime.MinValue).ToList();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-06-17 17:52:31 +08:00
|
|
|
|
private bool UpsertIfChanged(MesXslRubberQuickTestStd entity)
|
2026-06-17 15:41:06 +08:00
|
|
|
|
{
|
2026-06-17 17:52:31 +08:00
|
|
|
|
if (string.IsNullOrWhiteSpace(entity.Id)) return false;
|
2026-06-17 15:41:06 +08:00
|
|
|
|
lock (_cacheLock)
|
|
|
|
|
|
{
|
|
|
|
|
|
var idx = _localCache.FindIndex(x => string.Equals(x.Id, entity.Id, StringComparison.OrdinalIgnoreCase));
|
2026-06-17 17:52:31 +08:00
|
|
|
|
if (idx >= 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (IsStdDetailContentEqual(_localCache[idx], entity))
|
|
|
|
|
|
return false;
|
|
|
|
|
|
_localCache[idx] = CloneMain(entity);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
_localCache.Insert(0, CloneMain(entity));
|
|
|
|
|
|
}
|
2026-06-17 15:41:06 +08:00
|
|
|
|
SaveCacheToDiskUnsafe();
|
2026-06-17 17:52:31 +08:00
|
|
|
|
return true;
|
2026-06-17 15:41:06 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-06-17 17:52:31 +08:00
|
|
|
|
private static bool IsStdListContentEqual(MesXslRubberQuickTestStd a, MesXslRubberQuickTestStd b) =>
|
|
|
|
|
|
string.Equals(GetStdListFingerprint(a), GetStdListFingerprint(b), StringComparison.Ordinal);
|
|
|
|
|
|
|
|
|
|
|
|
private static bool IsStdDetailContentEqual(MesXslRubberQuickTestStd a, MesXslRubberQuickTestStd b) =>
|
|
|
|
|
|
string.Equals(GetStdDetailFingerprint(a), GetStdDetailFingerprint(b), StringComparison.Ordinal);
|
|
|
|
|
|
|
|
|
|
|
|
private static string GetStdListFingerprint(MesXslRubberQuickTestStd x)
|
|
|
|
|
|
{
|
|
|
|
|
|
var snap = CloneMain(x);
|
|
|
|
|
|
snap.LineList = null;
|
|
|
|
|
|
return JsonSerializer.Serialize(snap, _jsonOpts);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static string GetStdDetailFingerprint(MesXslRubberQuickTestStd x) =>
|
|
|
|
|
|
JsonSerializer.Serialize(CloneMain(x), _jsonOpts);
|
|
|
|
|
|
|
|
|
|
|
|
private static MesXslRubberQuickTestStd MergeStdUpdated(MesXslRubberQuickTestStd local, MesXslRubberQuickTestStd remote)
|
|
|
|
|
|
{
|
|
|
|
|
|
var copy = CloneMain(remote);
|
|
|
|
|
|
if ((copy.LineList == null || copy.LineList.Count == 0) && local.LineList is { Count: > 0 })
|
|
|
|
|
|
{
|
|
|
|
|
|
copy.LineList = local.LineList.Select(l => new MesXslRubberQuickTestStdLine
|
|
|
|
|
|
{
|
|
|
|
|
|
Id = l.Id,
|
|
|
|
|
|
StdId = l.StdId,
|
|
|
|
|
|
DataPointId = l.DataPointId,
|
|
|
|
|
|
PointName = l.PointName,
|
|
|
|
|
|
LowerLimit = l.LowerLimit,
|
|
|
|
|
|
UpperLimit = l.UpperLimit,
|
|
|
|
|
|
LowerWarn = l.LowerWarn,
|
|
|
|
|
|
UpperWarn = l.UpperWarn,
|
|
|
|
|
|
TargetValue = l.TargetValue,
|
|
|
|
|
|
SortNo = l.SortNo
|
|
|
|
|
|
}).ToList();
|
|
|
|
|
|
}
|
|
|
|
|
|
return copy;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void UpsertLocalCacheMain(MesXslRubberQuickTestStd entity) => UpsertIfChanged(entity);
|
|
|
|
|
|
|
2026-06-17 15:41:06 +08:00
|
|
|
|
private void LoadCacheFromDisk()
|
|
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!File.Exists(_cacheFilePath)) return;
|
|
|
|
|
|
_localCache = JsonSerializer.Deserialize<List<MesXslRubberQuickTestStd>>(
|
|
|
|
|
|
File.ReadAllText(_cacheFilePath), _jsonOpts) ?? new();
|
|
|
|
|
|
}
|
|
|
|
|
|
catch
|
|
|
|
|
|
{
|
|
|
|
|
|
_localCache = new();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void SaveCacheToDiskUnsafe() =>
|
|
|
|
|
|
File.WriteAllText(_cacheFilePath, JsonSerializer.Serialize(_localCache, _jsonOpts));
|
|
|
|
|
|
|
|
|
|
|
|
private static MesXslRubberQuickTestStd CloneMain(MesXslRubberQuickTestStd x) => new()
|
|
|
|
|
|
{
|
|
|
|
|
|
Id = x.Id,
|
|
|
|
|
|
StdName = x.StdName,
|
|
|
|
|
|
TestMethodId = x.TestMethodId,
|
|
|
|
|
|
TestMethodName = x.TestMethodName,
|
|
|
|
|
|
MixerType = x.MixerType,
|
|
|
|
|
|
RubberMaterialId = x.RubberMaterialId,
|
|
|
|
|
|
RubberMaterialName = x.RubberMaterialName,
|
|
|
|
|
|
PsCompileId = x.PsCompileId,
|
|
|
|
|
|
IssueNumber = x.IssueNumber,
|
|
|
|
|
|
IssueDate = x.IssueDate,
|
|
|
|
|
|
IssueDeptId = x.IssueDeptId,
|
|
|
|
|
|
IssueDeptName = x.IssueDeptName,
|
|
|
|
|
|
EnableStatus = x.EnableStatus,
|
|
|
|
|
|
AuditStatus = x.AuditStatus,
|
|
|
|
|
|
TenantId = x.TenantId,
|
|
|
|
|
|
SysOrgCode = x.SysOrgCode,
|
|
|
|
|
|
CreateBy = x.CreateBy,
|
|
|
|
|
|
CreateTime = x.CreateTime,
|
|
|
|
|
|
UpdateBy = x.UpdateBy,
|
|
|
|
|
|
UpdateTime = x.UpdateTime,
|
|
|
|
|
|
LineList = x.LineList?.Select(l => new MesXslRubberQuickTestStdLine
|
|
|
|
|
|
{
|
|
|
|
|
|
Id = l.Id,
|
|
|
|
|
|
StdId = l.StdId,
|
|
|
|
|
|
DataPointId = l.DataPointId,
|
|
|
|
|
|
PointName = l.PointName,
|
|
|
|
|
|
LowerLimit = l.LowerLimit,
|
|
|
|
|
|
UpperLimit = l.UpperLimit,
|
|
|
|
|
|
LowerWarn = l.LowerWarn,
|
|
|
|
|
|
UpperWarn = l.UpperWarn,
|
|
|
|
|
|
TargetValue = l.TargetValue,
|
|
|
|
|
|
SortNo = l.SortNo
|
|
|
|
|
|
}).ToList()
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
private sealed class NullableDateTimeJsonConverter : JsonConverter<DateTime?>
|
|
|
|
|
|
{
|
|
|
|
|
|
private static readonly string[] Formats =
|
|
|
|
|
|
[
|
|
|
|
|
|
"yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm:ss.fff",
|
|
|
|
|
|
"yyyy-MM-ddTHH:mm:ss", "yyyy-MM-ddTHH:mm:ss.fff",
|
|
|
|
|
|
"yyyy-MM-ddTHH:mm:ssZ", "yyyy-MM-ddTHH:mm:ss.fffZ",
|
|
|
|
|
|
"yyyy-MM-dd"
|
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
|
|
public override DateTime? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (reader.TokenType == JsonTokenType.Null) return null;
|
|
|
|
|
|
if (reader.TokenType == JsonTokenType.String)
|
|
|
|
|
|
{
|
|
|
|
|
|
var raw = reader.GetString();
|
|
|
|
|
|
if (string.IsNullOrWhiteSpace(raw)) return null;
|
|
|
|
|
|
if (DateTime.TryParseExact(raw, Formats, System.Globalization.CultureInfo.InvariantCulture,
|
|
|
|
|
|
System.Globalization.DateTimeStyles.AssumeLocal, out var dt)) return dt;
|
|
|
|
|
|
if (DateTime.TryParse(raw, out var fb)) return fb;
|
|
|
|
|
|
}
|
|
|
|
|
|
throw new JsonException($"无法转换为 DateTime?,token={reader.TokenType}");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override void Write(Utf8JsonWriter writer, DateTime? value, JsonSerializerOptions options)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (value.HasValue) writer.WriteStringValue(value.Value.ToString("yyyy-MM-dd HH:mm:ss"));
|
|
|
|
|
|
else writer.WriteNullValue();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|