新增业务打印绑定功能,整合打印模板与业务数据的映射配置,优化打印数据生成逻辑。新增免密接口,支持桌面端打印模板的查询与列表展示,提升用户体验和系统的实时数据同步能力。同时,重构相关控制器以增强系统的可维护性和扩展性。
This commit is contained in:
@@ -244,8 +244,10 @@ public static class NativePrintRenderService
|
||||
double fs, string fw, string color, string align, double lh,
|
||||
double designY, double pageHeightMm, int totalPages, bool bandRepeat = false)
|
||||
{
|
||||
var type = ReadAsString(el["type"], "text");
|
||||
var bindField = ReadAsString(el["bindField"]);
|
||||
var type = ReadAsString(el["type"], "text");
|
||||
// 设计器默认 bindField 为空字符串 "",须视为「未绑定」,否则误走数据分支导致标题等只剩空串
|
||||
var bindFieldRaw = ReadAsString(el["bindField"]);
|
||||
var bindField = string.IsNullOrWhiteSpace(bindFieldRaw) ? null : bindFieldRaw.Trim();
|
||||
string text;
|
||||
|
||||
if (type == "date")
|
||||
@@ -258,7 +260,8 @@ public static class NativePrintRenderService
|
||||
}
|
||||
else if (bindField != null)
|
||||
{
|
||||
text = ResolveField(data, bindField)?.ToString() ?? ReadAsString(el["text"], string.Empty);
|
||||
// 已绑定数据字段:缺键或解析不到时留空,不回退到画布上的设计占位 text(否则会误显示「采购订单」等)
|
||||
text = ResolveField(data, bindField)?.ToString() ?? string.Empty;
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -291,10 +294,11 @@ public static class NativePrintRenderService
|
||||
private static string RenderQrCode(JsonNode el, JsonObject data, string posStyle, double w, double h,
|
||||
bool bandRepeat = false, double designY = 0, double pageHeightMm = 0, int totalPages = 1)
|
||||
{
|
||||
var bindField = ReadAsString(el["bindField"]);
|
||||
var value = ReadAsString(el["value"], string.Empty);
|
||||
var bindFieldRaw = ReadAsString(el["bindField"]);
|
||||
var bindField = string.IsNullOrWhiteSpace(bindFieldRaw) ? null : bindFieldRaw.Trim();
|
||||
var value = ReadAsString(el["value"], string.Empty);
|
||||
if (bindField != null)
|
||||
value = ResolveField(data, bindField)?.ToString() ?? value;
|
||||
value = ResolveField(data, bindField)?.ToString() ?? string.Empty;
|
||||
if (string.IsNullOrWhiteSpace(value)) return string.Empty;
|
||||
|
||||
string inner;
|
||||
@@ -332,10 +336,11 @@ public static class NativePrintRenderService
|
||||
private static string RenderBarcode(JsonNode el, JsonObject data, string posStyle, double w, double h,
|
||||
bool bandRepeat = false, double designY = 0, double pageHeightMm = 0, int totalPages = 1)
|
||||
{
|
||||
var bindField = ReadAsString(el["bindField"]);
|
||||
var value = ReadAsString(el["value"], string.Empty);
|
||||
var bindFieldRaw = ReadAsString(el["bindField"]);
|
||||
var bindField = string.IsNullOrWhiteSpace(bindFieldRaw) ? null : bindFieldRaw.Trim();
|
||||
var value = ReadAsString(el["value"], string.Empty);
|
||||
if (bindField != null)
|
||||
value = ResolveField(data, bindField)?.ToString() ?? value;
|
||||
value = ResolveField(data, bindField)?.ToString() ?? string.Empty;
|
||||
if (string.IsNullOrWhiteSpace(value)) return string.Empty;
|
||||
|
||||
// 从元素配置取格式/显示文字开关;元素未设时按 Code128 + 显示文字默认
|
||||
@@ -714,10 +719,11 @@ public static class NativePrintRenderService
|
||||
private static string RenderImage(JsonNode el, JsonObject data, string posStyle,
|
||||
bool bandRepeat = false, double designY = 0, double pageHeightMm = 0, int totalPages = 1)
|
||||
{
|
||||
var bindField = ReadAsString(el["bindField"]);
|
||||
var src = ReadAsString(el["src"], string.Empty);
|
||||
var bindFieldRaw = ReadAsString(el["bindField"]);
|
||||
var bindField = string.IsNullOrWhiteSpace(bindFieldRaw) ? null : bindFieldRaw.Trim();
|
||||
var src = ReadAsString(el["src"], string.Empty);
|
||||
if (bindField != null)
|
||||
src = ResolveField(data, bindField)?.ToString() ?? src;
|
||||
src = ResolveField(data, bindField)?.ToString() ?? string.Empty;
|
||||
var fit = ReadAsString(el["fit"], "contain");
|
||||
var objFit = fit switch { "fill" => "fill", "cover" => "cover", _ => "contain" };
|
||||
var inner = $"<img src=\"{EscapeHtml(src)}\" style=\"width:100%;height:100%;object-fit:{objFit};\" />";
|
||||
@@ -1147,7 +1153,7 @@ public static class NativePrintRenderService
|
||||
// 解析原始值
|
||||
string rawValue;
|
||||
if (!string.IsNullOrWhiteSpace(cell.BindField))
|
||||
rawValue = ResolveField(data, cell.BindField!)?.ToString() ?? cell.Text;
|
||||
rawValue = ResolveField(data, cell.BindField!)?.ToString() ?? string.Empty;
|
||||
else
|
||||
rawValue = cell.Text;
|
||||
|
||||
|
||||
@@ -0,0 +1,172 @@
|
||||
using System.IO;
|
||||
using System.Net.Http;
|
||||
using System.Net.Http.Json;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.Http;
|
||||
using YY.Admin.Core.Entity;
|
||||
using YY.Admin.Core.Services;
|
||||
|
||||
namespace YY.Admin.Services.Service.Print;
|
||||
|
||||
/// <summary>业务打印绑定:免密列表 + 本地缓存(只读)</summary>
|
||||
public class PrintBizTemplateBindService : IPrintBizTemplateBindService
|
||||
{
|
||||
private readonly IHttpClientFactory _httpClientFactory;
|
||||
private readonly IConfiguration _configuration;
|
||||
private readonly object _cacheLock = new();
|
||||
private List<PrintBizTemplateBind> _localCache = new();
|
||||
private readonly string _cacheFilePath;
|
||||
|
||||
private static readonly JsonSerializerOptions JsonOpts = new()
|
||||
{
|
||||
PropertyNameCaseInsensitive = true,
|
||||
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
|
||||
Converters = { new NullableDateTimeJsonConverter() }
|
||||
};
|
||||
|
||||
private string BaseUrl =>
|
||||
(_configuration.GetValue<string>("JeecgIntegration:BaseUrl") ?? "http://localhost:8080/jeecg-boot").TrimEnd('/');
|
||||
|
||||
public PrintBizTemplateBindService(IHttpClientFactory httpClientFactory, IConfiguration configuration)
|
||||
{
|
||||
_httpClientFactory = httpClientFactory;
|
||||
_configuration = configuration;
|
||||
|
||||
var appDataDir = Path.Combine(
|
||||
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
|
||||
"YY.Admin", "sync-cache");
|
||||
Directory.CreateDirectory(appDataDir);
|
||||
_cacheFilePath = Path.Combine(appDataDir, "print-biz-bind-cache.json");
|
||||
|
||||
LoadCacheFromDisk();
|
||||
}
|
||||
|
||||
public IReadOnlyList<PrintBizTemplateBind> GetCached()
|
||||
{
|
||||
lock (_cacheLock)
|
||||
{
|
||||
return _localCache.AsReadOnly();
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<IReadOnlyList<PrintBizTemplateBind>> ListAsync(CancellationToken ct = default)
|
||||
{
|
||||
try
|
||||
{
|
||||
return await RefreshCacheAsync(ct).ConfigureAwait(false);
|
||||
}
|
||||
catch
|
||||
{
|
||||
return GetCached();
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<IReadOnlyList<PrintBizTemplateBind>> RefreshCacheAsync(CancellationToken ct = default)
|
||||
{
|
||||
var client = _httpClientFactory.CreateClient("JeecgApi");
|
||||
var url = $"{BaseUrl}/print/bizTemplateBind/anon/list?pageSize=500&pageNo=1";
|
||||
var response = await client.GetAsync(url, ct).ConfigureAwait(false);
|
||||
response.EnsureSuccessStatusCode();
|
||||
var result = await response.Content.ReadFromJsonAsync<JeecgResult<JeecgPage<PrintBizTemplateBind>>>(JsonOpts, ct).ConfigureAwait(false);
|
||||
if (result?.Success != true)
|
||||
throw new InvalidOperationException(result?.Message ?? "后端返回失败");
|
||||
|
||||
var records = result.Result?.Records ?? new List<PrintBizTemplateBind>();
|
||||
lock (_cacheLock)
|
||||
{
|
||||
_localCache = records;
|
||||
SaveCacheToDiskUnsafe();
|
||||
}
|
||||
return records.AsReadOnly();
|
||||
}
|
||||
|
||||
public async Task<PrintBizTemplateBind?> GetByIdAsync(string id, CancellationToken ct = default)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(id)) return null;
|
||||
try
|
||||
{
|
||||
var client = _httpClientFactory.CreateClient("JeecgApi");
|
||||
var url = $"{BaseUrl}/print/bizTemplateBind/anon/queryById?id={Uri.EscapeDataString(id)}";
|
||||
var response = await client.GetAsync(url, ct).ConfigureAwait(false);
|
||||
if (!response.IsSuccessStatusCode) return GetCachedById(id);
|
||||
var result = await response.Content.ReadFromJsonAsync<JeecgResult<PrintBizTemplateBind>>(JsonOpts, ct).ConfigureAwait(false);
|
||||
return result?.Success == true ? result.Result : GetCachedById(id);
|
||||
}
|
||||
catch
|
||||
{
|
||||
return GetCachedById(id);
|
||||
}
|
||||
}
|
||||
|
||||
private PrintBizTemplateBind? GetCachedById(string id)
|
||||
{
|
||||
lock (_cacheLock)
|
||||
{
|
||||
return _localCache.FirstOrDefault(x => string.Equals(x.Id, id, StringComparison.OrdinalIgnoreCase));
|
||||
}
|
||||
}
|
||||
|
||||
private void LoadCacheFromDisk()
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!File.Exists(_cacheFilePath)) return;
|
||||
var json = File.ReadAllText(_cacheFilePath);
|
||||
var data = JsonSerializer.Deserialize<List<PrintBizTemplateBind>>(json, JsonOpts);
|
||||
_localCache = data ?? new List<PrintBizTemplateBind>();
|
||||
}
|
||||
catch
|
||||
{
|
||||
_localCache = new List<PrintBizTemplateBind>();
|
||||
}
|
||||
}
|
||||
|
||||
private void SaveCacheToDiskUnsafe()
|
||||
{
|
||||
try
|
||||
{
|
||||
var json = JsonSerializer.Serialize(_localCache, JsonOpts);
|
||||
File.WriteAllText(_cacheFilePath, json);
|
||||
}
|
||||
catch { /* 离线或磁盘异常时忽略 */ }
|
||||
}
|
||||
|
||||
private record JeecgResult<T>(bool Success, T? Result, string? Message);
|
||||
private record JeecgPage<T>(List<T> Records, long Total);
|
||||
|
||||
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"
|
||||
];
|
||||
|
||||
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 exact))
|
||||
return exact;
|
||||
if (DateTime.TryParse(raw, out var fallback)) return fallback;
|
||||
}
|
||||
throw new JsonException($"无法将 JSON 值转换为 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
using Prism.Events;
|
||||
using System.Text.Json;
|
||||
using YY.Admin.Core;
|
||||
using YY.Admin.Core.Events;
|
||||
namespace YY.Admin.Services.Service.Print;
|
||||
|
||||
/// <summary>订阅 STOMP:PRINT_BIZ_TEMPLATE_BIND_CHANGED,驱动列表静默刷新;网络恢复补偿。</summary>
|
||||
public class PrintBizTemplateBindSyncCoordinator : ISingletonDependency
|
||||
{
|
||||
private readonly IEventAggregator _eventAggregator;
|
||||
private readonly ILoggerService _logger;
|
||||
private SubscriptionToken? _remoteCommandToken;
|
||||
private SubscriptionToken? _networkStatusToken;
|
||||
|
||||
public PrintBizTemplateBindSyncCoordinator(
|
||||
IEventAggregator eventAggregator,
|
||||
SyncPollManager pollManager,
|
||||
ILoggerService logger)
|
||||
{
|
||||
_eventAggregator = eventAggregator;
|
||||
_logger = logger;
|
||||
|
||||
_remoteCommandToken = _eventAggregator
|
||||
.GetEvent<RemoteCommandReceivedEvent>()
|
||||
.Subscribe(OnRemoteCommand, ThreadOption.BackgroundThread);
|
||||
_networkStatusToken = _eventAggregator
|
||||
.GetEvent<NetworkStatusChangedEvent>()
|
||||
.Subscribe(OnNetworkStatusChanged, ThreadOption.BackgroundThread);
|
||||
|
||||
pollManager.Register("业务打印绑定", () =>
|
||||
{
|
||||
_eventAggregator.GetEvent<PrintBizTemplateBindChangedEvent>()
|
||||
.Publish(new PrintBizTemplateBindChangedPayload { Action = "poll" });
|
||||
return Task.CompletedTask;
|
||||
});
|
||||
|
||||
_logger.Information("[业务打印绑定推送] PrintBizTemplateBindSyncCoordinator 已启动");
|
||||
}
|
||||
|
||||
private void OnNetworkStatusChanged(NetworkStatusChangedPayload payload)
|
||||
{
|
||||
if (!payload.IsOnline) return;
|
||||
_logger.Information("[业务打印绑定推送] 网络恢复,触发补偿刷新");
|
||||
_eventAggregator.GetEvent<PrintBizTemplateBindChangedEvent>()
|
||||
.Publish(new PrintBizTemplateBindChangedPayload { Action = "reconnect" });
|
||||
}
|
||||
|
||||
private void OnRemoteCommand(RemoteCommandPayload payload)
|
||||
{
|
||||
try
|
||||
{
|
||||
var json = payload.CommandJson ?? string.Empty;
|
||||
if (string.IsNullOrWhiteSpace(json)) return;
|
||||
|
||||
using var doc = JsonDocument.Parse(json);
|
||||
if (!doc.RootElement.TryGetProperty("cmd", out var cmdEl)) return;
|
||||
var cmd = cmdEl.GetString() ?? string.Empty;
|
||||
if (!cmd.Equals("PRINT_BIZ_TEMPLATE_BIND_CHANGED", StringComparison.OrdinalIgnoreCase)) return;
|
||||
|
||||
doc.RootElement.TryGetProperty("action", out var actionEl);
|
||||
doc.RootElement.TryGetProperty("bindId", out var idEl);
|
||||
|
||||
var changedPayload = new PrintBizTemplateBindChangedPayload
|
||||
{
|
||||
Action = actionEl.GetString() ?? string.Empty,
|
||||
BindId = idEl.ValueKind == JsonValueKind.String ? idEl.GetString() : null
|
||||
};
|
||||
|
||||
_logger.Information($"[业务打印绑定推送] 收到变更 action={changedPayload.Action}, bindId={changedPayload.BindId}");
|
||||
_eventAggregator.GetEvent<PrintBizTemplateBindChangedEvent>().Publish(changedPayload);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.Warning($"[业务打印绑定推送] 处理 STOMP 信号失败: {ex.Message}");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -237,6 +237,39 @@ public class RawMaterialEntryService : IRawMaterialEntryService, ISingletonDepen
|
||||
return null;
|
||||
}
|
||||
|
||||
public async Task<(string templateJson, string printDataJson, string? errorMessage)> PrepareNativePrintAsync(string id, CancellationToken ct = default)
|
||||
{
|
||||
if (!_networkMonitor.IsOnline)
|
||||
return (string.Empty, "{}", "当前离线,无法获取打印数据");
|
||||
try
|
||||
{
|
||||
var url = $"{BaseUrl}/xslmes/mesXslRawMaterialEntry/anon/prepareNativePrint?id={Uri.EscapeDataString(id)}&tenantId={DefaultTenantId}";
|
||||
using var client = CreateClient();
|
||||
var resp = await client.GetAsync(url, ct).ConfigureAwait(false);
|
||||
var json = await resp.Content.ReadAsStringAsync(ct).ConfigureAwait(false);
|
||||
using var doc = JsonDocument.Parse(json);
|
||||
var root = doc.RootElement;
|
||||
if (!root.TryGetProperty("code", out var codeEl) || codeEl.GetInt32() != 200)
|
||||
{
|
||||
var msg = root.TryGetProperty("message", out var msgEl) ? msgEl.GetString() : "未知错误";
|
||||
return (string.Empty, "{}", msg ?? "服务端返回错误");
|
||||
}
|
||||
var result = root.GetProperty("result");
|
||||
var templateJson = result.TryGetProperty("templateJson", out var tjEl) ? tjEl.GetString() : null;
|
||||
var printDataJson = result.TryGetProperty("printData", out var pdEl)
|
||||
? pdEl.GetRawText()
|
||||
: "{}";
|
||||
if (string.IsNullOrWhiteSpace(templateJson))
|
||||
return (string.Empty, "{}", "服务端未返回模板 JSON,请先在「业务打印绑定」中配置原料入场记录");
|
||||
return (templateJson!, printDataJson, null);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.Warning($"[原料入场] 准备打印数据失败 id={id}: {ex.Message}");
|
||||
return (string.Empty, "{}", $"获取打印数据失败:{ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
public IReadOnlyList<MesXslRawMaterialEntry> GetCachedSnapshot()
|
||||
{
|
||||
// 注意:不允许直接返回 _localCache 引用,避免外部修改污染缓存;用 Clone 做深拷贝。
|
||||
|
||||
Reference in New Issue
Block a user