2026-06-17 15:41:06 +08:00
|
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
|
using System.Net.Http;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Text.Json;
|
|
|
|
|
using System.Text.Json.Serialization;
|
|
|
|
|
using YY.Admin.Core;
|
|
|
|
|
using YY.Admin.Core.Entity;
|
|
|
|
|
using YY.Admin.Core.Services;
|
|
|
|
|
|
|
|
|
|
namespace YY.Admin.Services.Service.RubberQuickTest;
|
|
|
|
|
|
|
|
|
|
public class RubberQuickTestOperationService : IRubberQuickTestOperationService, ISingletonDependency
|
|
|
|
|
{
|
|
|
|
|
private readonly IHttpClientFactory _httpClientFactory;
|
|
|
|
|
private readonly IConfiguration _configuration;
|
|
|
|
|
private readonly INetworkMonitor _networkMonitor;
|
|
|
|
|
private readonly ILoggerService _logger;
|
|
|
|
|
|
|
|
|
|
private static readonly JsonSerializerOptions _jsonOpts = new()
|
|
|
|
|
{
|
|
|
|
|
PropertyNameCaseInsensitive = true,
|
|
|
|
|
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
|
|
|
|
|
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
public RubberQuickTestOperationService(
|
|
|
|
|
IHttpClientFactory httpClientFactory,
|
|
|
|
|
IConfiguration configuration,
|
|
|
|
|
INetworkMonitor networkMonitor,
|
|
|
|
|
ILoggerService logger)
|
|
|
|
|
{
|
|
|
|
|
_httpClientFactory = httpClientFactory;
|
|
|
|
|
_configuration = configuration;
|
|
|
|
|
_networkMonitor = networkMonitor;
|
|
|
|
|
_logger = logger;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string BaseUrl => (_configuration.GetValue<string>("JeecgIntegration:BaseUrl") ?? "http://localhost:8080/jeecg-boot").TrimEnd('/');
|
|
|
|
|
|
|
|
|
|
public async Task<string?> SaveRecordAsync(MesXslRubberQuickTestRecord record, CancellationToken ct = default)
|
|
|
|
|
{
|
|
|
|
|
if (!_networkMonitor.IsOnline)
|
2026-06-18 15:18:11 +08:00
|
|
|
throw new InvalidOperationException("网络未连接,无法保存胶料快检记录");
|
2026-06-17 15:41:06 +08:00
|
|
|
|
|
|
|
|
var url = $"{BaseUrl}/xslmes/mesXslRubberQuickTestRecord/anon/add";
|
|
|
|
|
using var client = _httpClientFactory.CreateClient("JeecgApi");
|
|
|
|
|
var body = JsonSerializer.Serialize(record, _jsonOpts);
|
|
|
|
|
using var content = new StringContent(body, Encoding.UTF8, "application/json");
|
|
|
|
|
var resp = await client.PostAsync(url, content, ct).ConfigureAwait(false);
|
|
|
|
|
var json = await resp.Content.ReadAsStringAsync(ct).ConfigureAwait(false);
|
|
|
|
|
using var doc = JsonDocument.Parse(json);
|
|
|
|
|
if (!doc.RootElement.TryGetProperty("success", out var successEl) || !successEl.GetBoolean())
|
|
|
|
|
{
|
|
|
|
|
var msg = doc.RootElement.TryGetProperty("message", out var msgEl) ? msgEl.GetString() : "保存失败";
|
|
|
|
|
throw new InvalidOperationException(msg ?? "保存失败");
|
|
|
|
|
}
|
2026-06-18 15:18:11 +08:00
|
|
|
|
|
|
|
|
string? recordNo = null;
|
|
|
|
|
if (doc.RootElement.TryGetProperty("result", out var resultEl))
|
|
|
|
|
{
|
|
|
|
|
if (resultEl.ValueKind == JsonValueKind.String)
|
|
|
|
|
recordNo = resultEl.GetString();
|
|
|
|
|
else if (resultEl.ValueKind == JsonValueKind.Object
|
|
|
|
|
&& resultEl.TryGetProperty("recordNo", out var noEl))
|
|
|
|
|
recordNo = noEl.GetString();
|
|
|
|
|
}
|
|
|
|
|
recordNo ??= record.RecordNo;
|
|
|
|
|
record.RecordNo = recordNo;
|
|
|
|
|
_logger.Information($"[胶料快检记录] 保存成功 recordNo={recordNo}");
|
|
|
|
|
return recordNo;
|
2026-06-17 15:41:06 +08:00
|
|
|
}
|
|
|
|
|
}
|