118 lines
5.7 KiB
C#
118 lines
5.7 KiB
C#
|
|
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('/');
|
||
|
|
private int DefaultTenantId => (int?)_configuration.GetValue<long?>("JeecgIntegration:DefaultTenantId") ?? 1002;
|
||
|
|
|
||
|
|
public async Task<List<MesXslMixingProductionPlan>> GetMixingProductionPlansAsync(CancellationToken ct = default)
|
||
|
|
{
|
||
|
|
if (!_networkMonitor.IsOnline)
|
||
|
|
throw new InvalidOperationException("网络未连接,无法加载密炼生产计划");
|
||
|
|
|
||
|
|
var result = new List<MesXslMixingProductionPlan>();
|
||
|
|
int pageNo = 1;
|
||
|
|
const int pageSize = 500;
|
||
|
|
while (true)
|
||
|
|
{
|
||
|
|
var url = $"{BaseUrl}/xslmes/mesXslMixingProductionPlan/anon/list?pageNo={pageNo}&pageSize={pageSize}&tenantId={DefaultTenantId}";
|
||
|
|
using var client = _httpClientFactory.CreateClient("JeecgApi");
|
||
|
|
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);
|
||
|
|
if (!doc.RootElement.TryGetProperty("result", out var resultEl)) break;
|
||
|
|
if (resultEl.TryGetProperty("records", out var recordsEl))
|
||
|
|
{
|
||
|
|
var page = recordsEl.Deserialize<List<MesXslMixingProductionPlan>>(_jsonOpts);
|
||
|
|
if (page != null) result.AddRange(page);
|
||
|
|
}
|
||
|
|
long total = 0;
|
||
|
|
if (resultEl.TryGetProperty("total", out var totalEl)) total = totalEl.GetInt64();
|
||
|
|
if (result.Count >= total || (resultEl.TryGetProperty("records", out var r2) && r2.GetArrayLength() < pageSize)) break;
|
||
|
|
pageNo++;
|
||
|
|
}
|
||
|
|
|
||
|
|
_logger.Information($"[快检记录] 加载密炼生产计划 {result.Count} 条");
|
||
|
|
return result;
|
||
|
|
}
|
||
|
|
|
||
|
|
public async Task<MesXslRubberQuickTestStd?> 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<MesXslRubberQuickTestStd>(_jsonOpts);
|
||
|
|
}
|
||
|
|
|
||
|
|
public async Task<string?> 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() : "保存成功";
|
||
|
|
}
|
||
|
|
}
|