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 IMixingProductionPlanService _mixingProductionPlanService; 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, IMixingProductionPlanService mixingProductionPlanService, ILoggerService logger) { _httpClientFactory = httpClientFactory; _configuration = configuration; _networkMonitor = networkMonitor; _mixingProductionPlanService = mixingProductionPlanService; _logger = logger; } private string BaseUrl => (_configuration.GetValue("JeecgIntegration:BaseUrl") ?? "http://localhost:8080/jeecg-boot").TrimEnd('/'); private int DefaultTenantId => (int?)_configuration.GetValue("JeecgIntegration:DefaultTenantId") ?? 1002; public async Task> GetMixingProductionPlansAsync(CancellationToken ct = default) { var result = await _mixingProductionPlanService.GetAllCachedAsync(ct).ConfigureAwait(false); _logger.Information($"[快检记录] 加载密炼生产计划 {result.Count} 条"); return result; } public async Task GetStdByRubberMaterialNameAsync(string rubberMaterialName, CancellationToken ct = default) { if (string.IsNullOrWhiteSpace(rubberMaterialName)) return null; if (!_networkMonitor.IsOnline) throw new InvalidOperationException("网络未连接,无法加载实验标准"); var encoded = Uri.EscapeDataString(rubberMaterialName.Trim()); var url = $"{BaseUrl}/xslmes/mesXslRubberQuickTestStd/anon/queryByRubberMaterialName?rubberMaterialName={encoded}"; using var client = _httpClientFactory.CreateClient("JeecgApi"); var resp = await client.GetAsync(url, 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() : "查询实验标准失败"; _logger.Warning($"[快检记录] 实验标准查询失败: {msg}"); return null; } if (!doc.RootElement.TryGetProperty("result", out var resultEl) || resultEl.ValueKind == JsonValueKind.Null) return null; return resultEl.Deserialize(_jsonOpts); } public async Task SaveRecordAsync(MesXslRubberQuickTestRecord record, CancellationToken ct = default) { if (!_networkMonitor.IsOnline) throw new InvalidOperationException("网络未连接,无法保存快检记录"); 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 ?? "保存失败"); } _logger.Information($"[快检记录] 保存成功 recordNo={record.RecordNo}"); return doc.RootElement.TryGetProperty("message", out var m) ? m.GetString() : "保存成功"; } }