diff --git a/美国版/Food Labeling Management Code/Yi.Abp.Net8/module/food-labeling-us/FoodLabeling.Application.Contracts/Dtos/UsAppLabeling/PrintLogGetListInputVo.cs b/美国版/Food Labeling Management Code/Yi.Abp.Net8/module/food-labeling-us/FoodLabeling.Application.Contracts/Dtos/UsAppLabeling/PrintLogGetListInputVo.cs index b73fce9..f819a65 100644 --- a/美国版/Food Labeling Management Code/Yi.Abp.Net8/module/food-labeling-us/FoodLabeling.Application.Contracts/Dtos/UsAppLabeling/PrintLogGetListInputVo.cs +++ b/美国版/Food Labeling Management Code/Yi.Abp.Net8/module/food-labeling-us/FoodLabeling.Application.Contracts/Dtos/UsAppLabeling/PrintLogGetListInputVo.cs @@ -1,16 +1,15 @@ +using Volo.Abp.Application.Dtos; + namespace FoodLabeling.Application.Contracts.Dtos.UsAppLabeling; /// -/// App 打印日志分页(接口 10) +/// App 打印日志分页查询入参(仅当前登录账号 + 当前门店) /// -public class PrintLogGetListInputVo +public class PrintLogGetListInputVo : PagedAndSortedResultRequestDto { - /// 当前门店 Id + /// + /// 当前门店 Id(location.Id,Guid 字符串) + /// public string LocationId { get; set; } = string.Empty; - - /// 页码,从 1 开始 - public int SkipCount { get; set; } = 1; - - /// 每页条数 - public int MaxResultCount { get; set; } = 20; } + diff --git a/美国版/Food Labeling Management Code/Yi.Abp.Net8/module/food-labeling-us/FoodLabeling.Application.Contracts/Dtos/UsAppLabeling/PrintLogItemDto.cs b/美国版/Food Labeling Management Code/Yi.Abp.Net8/module/food-labeling-us/FoodLabeling.Application.Contracts/Dtos/UsAppLabeling/PrintLogItemDto.cs index 66fa701..85a1cf3 100644 --- a/美国版/Food Labeling Management Code/Yi.Abp.Net8/module/food-labeling-us/FoodLabeling.Application.Contracts/Dtos/UsAppLabeling/PrintLogItemDto.cs +++ b/美国版/Food Labeling Management Code/Yi.Abp.Net8/module/food-labeling-us/FoodLabeling.Application.Contracts/Dtos/UsAppLabeling/PrintLogItemDto.cs @@ -1,44 +1,64 @@ -using System.Collections.Generic; - namespace FoodLabeling.Application.Contracts.Dtos.UsAppLabeling; /// -/// 单条打印日志(接口 10) +/// 打印日志列表项 /// public class PrintLogItemDto { + /// 任务Id(fl_label_print_task.Id) public string TaskId { get; set; } = string.Empty; - public string BatchId { get; set; } = string.Empty; + /// 批次Id(同一次点击 Print 共享) + public string? BatchId { get; set; } + /// 第几份(从 1 开始) public int CopyIndex { get; set; } + /// 标签Id public string LabelId { get; set; } = string.Empty; + /// 标签编码 public string LabelCode { get; set; } = string.Empty; + /// 产品Id public string? ProductId { get; set; } + /// 产品名称 public string ProductName { get; set; } = "无"; - public string PrintedAt { get; set; } = string.Empty; + /// 标签类型名称(来自 fl_label_type.TypeName) + public string TypeName { get; set; } = string.Empty; - public string OperatorName { get; set; } = string.Empty; + /// 模板尺寸(来自 fl_label_template.Width/Height/Unit) + public string? LabelSizeText { get; set; } - public string LocationName { get; set; } = string.Empty; + /// 本次任务落库的渲染模板 JSON(fl_label_print_task.RenderTemplateJson) + public string? RenderTemplateJson { get; set; } - /// 标签分类名(展示用,可为空) - public string? LabelCategoryName { get; set; } + /// + /// 本次打印的内容快照(来自 fl_label_print_data,按 PrintTaskId 关联) + /// + public List PrintDataList { get; set; } = new(); - /// 标签幅面/模板摘要(如 2"x2" Basic) - public string? LabelTemplateSummary { get; set; } + /// 打印时间(PrintedAt ?? CreationTime) + public DateTime PrintedAt { get; set; } - /// 标签幅面文案(与列表/预览 labelSizeText 一致,如 2.00x2.00inch) - public string? LabelSizeText { get; set; } + /// 操作人姓名(当前登录账号 Name) + public string OperatorName { get; set; } = string.Empty; + + /// 门店名称 + public string LocationName { get; set; } = "无"; +} - /// 标签种类名称(fl_label_type.TypeName) - public string? TypeName { get; set; } +/// +/// 打印内容快照项(fl_label_print_data) +/// +public class PrintLogDataItemDto +{ + public string ElementId { get; set; } = string.Empty; - /// 本次份打印内容元素快照(由 RenderDataJson 解析) - public List PrintDataList { get; set; } = new(); + public string? RenderValue { get; set; } + + public object? RenderConfigJson { get; set; } } + diff --git a/美国版/Food Labeling Management Code/Yi.Abp.Net8/module/food-labeling-us/FoodLabeling.Application.Contracts/Dtos/UsAppLabeling/UsAppLabelPrintInputVo.cs b/美国版/Food Labeling Management Code/Yi.Abp.Net8/module/food-labeling-us/FoodLabeling.Application.Contracts/Dtos/UsAppLabeling/UsAppLabelPrintInputVo.cs index 905debe..0977cdf 100644 --- a/美国版/Food Labeling Management Code/Yi.Abp.Net8/module/food-labeling-us/FoodLabeling.Application.Contracts/Dtos/UsAppLabeling/UsAppLabelPrintInputVo.cs +++ b/美国版/Food Labeling Management Code/Yi.Abp.Net8/module/food-labeling-us/FoodLabeling.Application.Contracts/Dtos/UsAppLabeling/UsAppLabelPrintInputVo.cs @@ -31,6 +31,12 @@ public class UsAppLabelPrintInputVo public int PrintQuantity { get; set; } = 1; /// + /// 客户端幂等请求Id(可选)。 + /// 同一个 clientRequestId 重复调用 print 接口时,后端会直接返回首次创建的 batchId/taskIds,不会重复写库。 + /// + public string? ClientRequestId { get; set; } + + /// /// 业务基准时间(用于 DATE/TIME 等元素的计算) /// public DateTime? BaseTime { get; set; } diff --git a/美国版/Food Labeling Management Code/Yi.Abp.Net8/module/food-labeling-us/FoodLabeling.Application.Contracts/Dtos/UsAppLabeling/UsAppLabelPrintOutputDto.cs b/美国版/Food Labeling Management Code/Yi.Abp.Net8/module/food-labeling-us/FoodLabeling.Application.Contracts/Dtos/UsAppLabeling/UsAppLabelPrintOutputDto.cs index 3cde5a7..1566425 100644 --- a/美国版/Food Labeling Management Code/Yi.Abp.Net8/module/food-labeling-us/FoodLabeling.Application.Contracts/Dtos/UsAppLabeling/UsAppLabelPrintOutputDto.cs +++ b/美国版/Food Labeling Management Code/Yi.Abp.Net8/module/food-labeling-us/FoodLabeling.Application.Contracts/Dtos/UsAppLabeling/UsAppLabelPrintOutputDto.cs @@ -1,3 +1,5 @@ +using System.Collections.Generic; + namespace FoodLabeling.Application.Contracts.Dtos.UsAppLabeling; /// @@ -12,10 +14,5 @@ public class UsAppLabelPrintOutputDto public string? BatchId { get; set; } public List TaskIds { get; set; } = new(); - - /// - /// 供 App 本地 BLE 重打:合并模板 JSON 字符串(与接口 9 落库的 printInputJson 同构,含 elements[])。 - /// - public string? MergedTemplateJson { get; set; } } diff --git a/美国版/Food Labeling Management Code/Yi.Abp.Net8/module/food-labeling-us/FoodLabeling.Application.Contracts/Dtos/UsAppLabeling/UsAppLabelReprintInputVo.cs b/美国版/Food Labeling Management Code/Yi.Abp.Net8/module/food-labeling-us/FoodLabeling.Application.Contracts/Dtos/UsAppLabeling/UsAppLabelReprintInputVo.cs index e8f5f57..0c18998 100644 --- a/美国版/Food Labeling Management Code/Yi.Abp.Net8/module/food-labeling-us/FoodLabeling.Application.Contracts/Dtos/UsAppLabeling/UsAppLabelReprintInputVo.cs +++ b/美国版/Food Labeling Management Code/Yi.Abp.Net8/module/food-labeling-us/FoodLabeling.Application.Contracts/Dtos/UsAppLabeling/UsAppLabelReprintInputVo.cs @@ -1,21 +1,47 @@ +using System; +using System.Collections.Generic; + namespace FoodLabeling.Application.Contracts.Dtos.UsAppLabeling; /// -/// App 重新打印入参(接口 11) +/// App 重新打印入参(根据历史任务Id重打) /// public class UsAppLabelReprintInputVo { + /// + /// 当前门店Id(用于权限校验,必须与历史任务一致) + /// public string LocationId { get; set; } = string.Empty; + /// + /// 历史打印任务Id(fl_label_print_task.Id) + /// public string TaskId { get; set; } = string.Empty; + /// + /// 重新打印份数(<=0 则按 1 处理;默认 1) + /// public int PrintQuantity { get; set; } = 1; + /// + /// 客户端幂等请求Id(可选)。 + /// 同一个 clientRequestId 重复调用 reprint 接口时,后端会直接返回首次创建的 batchId/taskIds,不会重复写库。 + /// public string? ClientRequestId { get; set; } + /// + /// 重新打印时可覆盖打印机Id(可选) + /// public string? PrinterId { get; set; } + /// + /// 重新打印时可覆盖打印机蓝牙 MAC(可选) + /// public string? PrinterMac { get; set; } + /// + /// 重新打印时可覆盖打印机地址(可选) + /// public string? PrinterAddress { get; set; } } + diff --git a/美国版/Food Labeling Management Code/Yi.Abp.Net8/module/food-labeling-us/FoodLabeling.Application.Contracts/IServices/IUsAppLabelingAppService.cs b/美国版/Food Labeling Management Code/Yi.Abp.Net8/module/food-labeling-us/FoodLabeling.Application.Contracts/IServices/IUsAppLabelingAppService.cs index 0f59435..ccde16d 100644 --- a/美国版/Food Labeling Management Code/Yi.Abp.Net8/module/food-labeling-us/FoodLabeling.Application.Contracts/IServices/IUsAppLabelingAppService.cs +++ b/美国版/Food Labeling Management Code/Yi.Abp.Net8/module/food-labeling-us/FoodLabeling.Application.Contracts/IServices/IUsAppLabelingAppService.cs @@ -1,5 +1,6 @@ using FoodLabeling.Application.Contracts.Dtos.Common; using FoodLabeling.Application.Contracts.Dtos.UsAppLabeling; +using FoodLabeling.Application.Contracts.Dtos.Common; using Volo.Abp.Application.Services; namespace FoodLabeling.Application.Contracts.IServices; @@ -25,12 +26,12 @@ public interface IUsAppLabelingAppService : IApplicationService Task PrintAsync(UsAppLabelPrintInputVo input); /// - /// 接口 10:当前账号在当前门店的打印日志分页 + /// App 重新打印:根据历史任务Id重打(创建新任务与明细) /// - Task> GetPrintLogListAsync(PrintLogGetListInputVo input); + Task ReprintAsync(UsAppLabelReprintInputVo input); /// - /// 接口 11:按历史任务重打并落库,返回新任务信息及可本地打印的模板 JSON + /// App 打印日志:获取当前登录账号在当前门店打印的记录(分页,时间倒序) /// - Task ReprintAsync(UsAppLabelReprintInputVo input); + Task> GetPrintLogListAsync(PrintLogGetListInputVo input); } diff --git a/美国版/Food Labeling Management Code/Yi.Abp.Net8/module/food-labeling-us/FoodLabeling.Application/Services/DbModels/FlLabelPrintDataDbEntity.cs b/美国版/Food Labeling Management Code/Yi.Abp.Net8/module/food-labeling-us/FoodLabeling.Application/Services/DbModels/FlLabelPrintDataDbEntity.cs index 415b402..2bfa4bf 100644 --- a/美国版/Food Labeling Management Code/Yi.Abp.Net8/module/food-labeling-us/FoodLabeling.Application/Services/DbModels/FlLabelPrintDataDbEntity.cs +++ b/美国版/Food Labeling Management Code/Yi.Abp.Net8/module/food-labeling-us/FoodLabeling.Application/Services/DbModels/FlLabelPrintDataDbEntity.cs @@ -11,30 +11,14 @@ public class FlLabelPrintDataDbEntity [SugarColumn(IsPrimaryKey = true)] public string Id { get; set; } = string.Empty; - public bool IsDeleted { get; set; } + public string PrintTaskId { get; set; } = string.Empty; - public DateTime CreationTime { get; set; } + public string ElementId { get; set; } = string.Empty; - public string? CreatorId { get; set; } + public string? ElementName { get; set; } - public string? LastModifierId { get; set; } + public string? RenderValue { get; set; } - public DateTime? LastModificationTime { get; set; } - - public string ConcurrencyStamp { get; set; } = string.Empty; - - public string TaskId { get; set; } = string.Empty; - - public int? CopyIndex { get; set; } - - /// - /// 原始打印输入(json 字段,直接保存为字符串) - /// - public string? PrintInputJson { get; set; } - - /// - /// 解析后的可打印数据(建议保存为 json 字符串) - /// - public string? RenderDataJson { get; set; } + public string? RenderConfigJson { get; set; } } diff --git a/美国版/Food Labeling Management Code/Yi.Abp.Net8/module/food-labeling-us/FoodLabeling.Application/Services/DbModels/FlLabelPrintTaskDbEntity.cs b/美国版/Food Labeling Management Code/Yi.Abp.Net8/module/food-labeling-us/FoodLabeling.Application/Services/DbModels/FlLabelPrintTaskDbEntity.cs index 8b73e7e..dd56f1d 100644 --- a/美国版/Food Labeling Management Code/Yi.Abp.Net8/module/food-labeling-us/FoodLabeling.Application/Services/DbModels/FlLabelPrintTaskDbEntity.cs +++ b/美国版/Food Labeling Management Code/Yi.Abp.Net8/module/food-labeling-us/FoodLabeling.Application/Services/DbModels/FlLabelPrintTaskDbEntity.cs @@ -11,36 +11,44 @@ public class FlLabelPrintTaskDbEntity [SugarColumn(IsPrimaryKey = true)] public string Id { get; set; } = string.Empty; - public bool IsDeleted { get; set; } + public string? BatchId { get; set; } - public DateTime CreationTime { get; set; } - - public string? CreatorId { get; set; } + public int CopyIndex { get; set; } = 1; - public string? LastModifierId { get; set; } + public string? ClientRequestId { get; set; } - public DateTime? LastModificationTime { get; set; } + public string LabelId { get; set; } = string.Empty; - public string ConcurrencyStamp { get; set; } = string.Empty; - - public string? LocationId { get; set; } + public string TemplateId { get; set; } = string.Empty; - public string? LabelCode { get; set; } + public string? LabelTypeId { get; set; } public string? ProductId { get; set; } - public string? LabelTypeId { get; set; } + public string? LocationId { get; set; } + + public DateTime? BaseTime { get; set; } - public string? TemplateCode { get; set; } + public string? PrintInputJson { get; set; } - public int PrintQuantity { get; set; } + public string? TemplateProductDefaultValuesJson { get; set; } - public DateTime? BaseTime { get; set; } + public string RenderTemplateJson { get; set; } = string.Empty; public string? PrinterId { get; set; } public string? PrinterMac { get; set; } public string? PrinterAddress { get; set; } + + public string Status { get; set; } = "CREATED"; + + public DateTime? PrintedAt { get; set; } + + public string? ErrorMessage { get; set; } + + public string? CreatedBy { get; set; } + + public DateTime CreationTime { get; set; } } diff --git a/美国版/Food Labeling Management Code/Yi.Abp.Net8/module/food-labeling-us/FoodLabeling.Application/Services/UsAppLabelingAppService.cs b/美国版/Food Labeling Management Code/Yi.Abp.Net8/module/food-labeling-us/FoodLabeling.Application/Services/UsAppLabelingAppService.cs index e329d24..bfbadbd 100644 --- a/美国版/Food Labeling Management Code/Yi.Abp.Net8/module/food-labeling-us/FoodLabeling.Application/Services/UsAppLabelingAppService.cs +++ b/美国版/Food Labeling Management Code/Yi.Abp.Net8/module/food-labeling-us/FoodLabeling.Application/Services/UsAppLabelingAppService.cs @@ -19,6 +19,7 @@ using Volo.Abp; using Volo.Abp.Application.Services; using Volo.Abp.Guids; using Volo.Abp.Uow; +using Yi.Framework.Rbac.Domain.Entities; using Yi.Framework.SqlSugarCore.Abstractions; namespace FoodLabeling.Application.Services; @@ -31,12 +32,18 @@ public class UsAppLabelingAppService : ApplicationService, IUsAppLabelingAppServ private readonly ISqlSugarDbContext _dbContext; private readonly ILabelAppService _labelAppService; private readonly IGuidGenerator _guidGenerator; + private readonly ISqlSugarRepository _userRepository; - public UsAppLabelingAppService(ISqlSugarDbContext dbContext, ILabelAppService labelAppService, IGuidGenerator guidGenerator) + public UsAppLabelingAppService( + ISqlSugarDbContext dbContext, + ILabelAppService labelAppService, + IGuidGenerator guidGenerator, + ISqlSugarRepository userRepository) { _dbContext = dbContext; _labelAppService = labelAppService; _guidGenerator = guidGenerator; + _userRepository = userRepository; } /// @@ -366,6 +373,28 @@ public class UsAppLabelingAppService : ApplicationService, IUsAppLabelingAppServ } var quantity = input.PrintQuantity <= 0 ? 1 : input.PrintQuantity; + var clientRequestId = input.ClientRequestId?.Trim(); + if (!string.IsNullOrWhiteSpace(clientRequestId)) + { + // 幂等:同一个 clientRequestId 重复调用,直接返回首次创建的任务集合 + var existed = await _dbContext.SqlSugarClient.Queryable() + .Where(x => x.ClientRequestId == clientRequestId) + .OrderBy(x => x.CopyIndex) + .ToListAsync(); + + if (existed is not null && existed.Count > 0) + { + var existedBatchId = existed.First().BatchId; + var existedTaskIds = existed.Select(x => x.Id).ToList(); + return new UsAppLabelPrintOutputDto + { + TaskId = existedTaskIds.FirstOrDefault() ?? string.Empty, + PrintQuantity = existedTaskIds.Count, + BatchId = existedBatchId, + TaskIds = existedTaskIds + }; + } + } // 校验 label + location,并补齐一些顶部字段用于任务表落库 var labelRow = await _dbContext.SqlSugarClient @@ -377,9 +406,10 @@ public class UsAppLabelingAppService : ApplicationService, IUsAppLabelingAppServ .Where((l, t, tpl) => l.LabelCode == labelCode) .Select((l, t, tpl) => new { + l.Id, l.LocationId, l.LabelTypeId, - TemplateCode = tpl.TemplateCode + l.TemplateId }) .FirstAsync(); @@ -393,114 +423,112 @@ public class UsAppLabelingAppService : ApplicationService, IUsAppLabelingAppServ throw new UserFriendlyException("该标签不属于当前门店"); } - string? printInputJsonStr = null; - string renderDataJsonStr; - var templateSnapshotOk = false; + var previewProductId = await ResolvePreviewProductIdAsync(labelRow.Id, input.ProductId); + var normalizedPrintInput = input.PrintInputJson?.ToDictionary(x => x.Key, x => (object?)x.Value); - if (input.PrintInputJson.HasValue) + // 解析模板 elements(与预览一致的渲染数据) + var resolvedTemplate = await _labelAppService.PreviewAsync(new LabelPreviewResolveInputVo { - var piRoot = input.PrintInputJson.Value; - if (piRoot.ValueKind == JsonValueKind.Object - && piRoot.TryGetProperty("elements", out var elArr) - && elArr.ValueKind == JsonValueKind.Array) - { - // App 传入整份合并模板(与 label-template JSON 同构):落库 printInputJson / renderDataJson 均存同一份,供重打 - printInputJsonStr = piRoot.GetRawText(); - renderDataJsonStr = printInputJsonStr; - templateSnapshotOk = true; - } - } + LabelCode = labelCode, + ProductId = previewProductId, + BaseTime = input.BaseTime, + PrintInputJson = normalizedPrintInput + }); - Dictionary? flatPrintInput = null; - if (!templateSnapshotOk && input.PrintInputJson.HasValue) - { - var piFlat = input.PrintInputJson.Value; - if (piFlat.ValueKind == JsonValueKind.Object) - { - try - { - flatPrintInput = JsonSerializer.Deserialize>(piFlat.GetRawText()); - } - catch - { - flatPrintInput = null; - } - } - } + var templateProductDefaultValuesJson = await ResolveTemplateProductDefaultValuesJsonAsync( + labelRow.TemplateId, + previewProductId, + labelRow.LabelTypeId); - if (!templateSnapshotOk) - { - var resolvedTemplate = await _labelAppService.PreviewAsync(new LabelPreviewResolveInputVo - { - LabelCode = labelCode, - ProductId = input.ProductId?.Trim(), - BaseTime = input.BaseTime, - PrintInputJson = flatPrintInput - }); - renderDataJsonStr = JsonSerializer.Serialize(resolvedTemplate); - printInputJsonStr = input.PrintInputJson.HasValue - ? input.PrintInputJson.Value.GetRawText() - : null; - } + var printInputJsonStr = input.PrintInputJson is null + ? null + : JsonSerializer.Serialize(input.PrintInputJson); + var renderTemplateJsonStr = JsonSerializer.Serialize(resolvedTemplate); var now = DateTime.Now; var currentUserId = CurrentUser?.Id?.ToString(); - var taskId = _guidGenerator.Create().ToString(); + var batchId = _guidGenerator.Create().ToString(); + var taskIds = new List(); - var task = new FlLabelPrintTaskDbEntity + for (var i = 1; i <= quantity; i++) { - Id = taskId, - IsDeleted = false, - CreationTime = now, - CreatorId = currentUserId, - ConcurrencyStamp = _guidGenerator.Create().ToString("N"), - LocationId = locationId, - LabelCode = labelCode, - ProductId = input.ProductId?.Trim(), - LabelTypeId = labelRow.LabelTypeId, - TemplateCode = labelRow.TemplateCode, - PrintQuantity = quantity, - BaseTime = input.BaseTime, - PrinterId = input.PrinterId?.Trim(), - PrinterMac = input.PrinterMac?.Trim(), - PrinterAddress = input.PrinterAddress?.Trim() - }; + var taskId = _guidGenerator.Create().ToString(); + taskIds.Add(taskId); - await _dbContext.SqlSugarClient.Insertable(task).ExecuteCommandAsync(); + var task = new FlLabelPrintTaskDbEntity + { + Id = taskId, + BatchId = batchId, + CopyIndex = i, + ClientRequestId = string.IsNullOrWhiteSpace(clientRequestId) ? null : clientRequestId, + LabelId = labelRow.Id, + TemplateId = labelRow.TemplateId, + LabelTypeId = labelRow.LabelTypeId, + ProductId = previewProductId, + LocationId = locationId, + BaseTime = input.BaseTime, + PrintInputJson = printInputJsonStr, + TemplateProductDefaultValuesJson = templateProductDefaultValuesJson, + RenderTemplateJson = renderTemplateJsonStr, + PrinterId = input.PrinterId?.Trim(), + PrinterMac = input.PrinterMac?.Trim(), + PrinterAddress = input.PrinterAddress?.Trim(), + Status = "CREATED", + PrintedAt = null, + ErrorMessage = null, + CreatedBy = currentUserId, + CreationTime = now + }; - var dataRows = Enumerable.Range(1, quantity).Select(i => new FlLabelPrintDataDbEntity - { - Id = _guidGenerator.Create().ToString(), - IsDeleted = false, - CreationTime = now, - CreatorId = currentUserId, - ConcurrencyStamp = _guidGenerator.Create().ToString("N"), - TaskId = taskId, - CopyIndex = i, - PrintInputJson = printInputJsonStr, - RenderDataJson = renderDataJsonStr - }).ToList(); + await _dbContext.SqlSugarClient.Insertable(task).ExecuteCommandAsync(); + + var rows = resolvedTemplate.Elements.Select(e => + { + var cfgJson = e.ConfigJson is null ? null : JsonSerializer.Serialize(e.ConfigJson); + string? renderValue = null; + if (e.ConfigJson is JsonElement je && je.ValueKind == JsonValueKind.Object && je.TryGetProperty("text", out var tv)) + { + renderValue = tv.ValueKind == JsonValueKind.String ? tv.GetString() : tv.ToString(); + } + else if (e.ConfigJson is Dictionary dict && dict.TryGetValue("text", out var v)) + { + renderValue = v?.ToString(); + } - await _dbContext.SqlSugarClient.Insertable(dataRows).ExecuteCommandAsync(); + return new FlLabelPrintDataDbEntity + { + Id = _guidGenerator.Create().ToString(), + PrintTaskId = taskId, + ElementId = e.Id?.Trim() ?? string.Empty, + ElementName = e.ElementName?.Trim(), + RenderValue = renderValue, + RenderConfigJson = cfgJson + }; + }).Where(x => !string.IsNullOrWhiteSpace(x.ElementId)).ToList(); + + if (rows.Count > 0) + { + await _dbContext.SqlSugarClient.Insertable(rows).ExecuteCommandAsync(); + } + } return new UsAppLabelPrintOutputDto { - TaskId = taskId, + TaskId = taskIds.FirstOrDefault() ?? string.Empty, PrintQuantity = quantity, - BatchId = taskId, - TaskIds = new List { taskId }, - MergedTemplateJson = null + BatchId = batchId, + TaskIds = taskIds }; } /// - /// 接口 10:分页打印日志(当前用户 + 当前门店) + /// App 重新打印:根据历史任务Id重打(创建新任务与明细) /// [Authorize] - [HttpPost] - public virtual async Task> GetPrintLogListAsync(PrintLogGetListInputVo input) + [UnitOfWork] + public virtual async Task ReprintAsync(UsAppLabelReprintInputVo input) { - if (input == null) + if (input is null) { throw new UserFriendlyException("入参不能为空"); } @@ -511,399 +539,337 @@ public class UsAppLabelingAppService : ApplicationService, IUsAppLabelingAppServ throw new UserFriendlyException("门店Id不能为空"); } - var userId = CurrentUser.Id?.ToString(); - if (string.IsNullOrWhiteSpace(userId)) + var taskId = input.TaskId?.Trim(); + if (string.IsNullOrWhiteSpace(taskId)) { - throw new UserFriendlyException("未登录"); - } - - var pageIndex = input.SkipCount <= 0 ? 1 : input.SkipCount; - var pageSize = input.MaxResultCount <= 0 ? 20 : Math.Min(input.MaxResultCount, 200); - - RefAsync total = 0; - var dataRows = await _dbContext.SqlSugarClient - .Queryable((d, t) => d.TaskId == t.Id) - .Where((d, t) => !d.IsDeleted && !t.IsDeleted) - .Where((d, t) => t.CreatorId == userId && t.LocationId == locationId) - .OrderBy((d, t) => d.CreationTime, OrderByType.Desc) - .Select((d, t) => d) - .ToPageListAsync(pageIndex, pageSize, total); - - string? locationDisplayName = null; - if (Guid.TryParse(locationId, out var locGuid)) - { - var locRows = await _dbContext.SqlSugarClient.Queryable() - .Where(x => x.Id == locGuid && !x.IsDeleted) - .Select(x => x.LocationName) - .Take(1) - .ToListAsync(); - locationDisplayName = locRows.FirstOrDefault(); + throw new UserFriendlyException("taskId不能为空"); } - var operatorName = CurrentUser.Name?.Trim(); - if (string.IsNullOrWhiteSpace(operatorName)) - { - operatorName = CurrentUser.UserName?.Trim(); - } - if (string.IsNullOrWhiteSpace(operatorName)) + var quantity = input.PrintQuantity <= 0 ? 1 : input.PrintQuantity; + var clientRequestId = input.ClientRequestId?.Trim(); + if (!string.IsNullOrWhiteSpace(clientRequestId)) { - operatorName = "无"; - } - - var taskIds = dataRows.Select(x => x.TaskId).Distinct().ToList(); - var tasks = taskIds.Count == 0 - ? new List() - : await _dbContext.SqlSugarClient.Queryable() - .Where(t => taskIds.Contains(t.Id)) - .ToListAsync(); - var taskMap = tasks.ToDictionary(x => x.Id, x => x); - - var labelCodes = tasks.Select(t => t.LabelCode).Where(x => !string.IsNullOrWhiteSpace(x)).Distinct().ToList(); - var labels = labelCodes.Count == 0 - ? new List() - : await _dbContext.SqlSugarClient.Queryable() - .Where(l => !l.IsDeleted && labelCodes.Contains(l.LabelCode)) - .ToListAsync(); - var labelByCode = labels.GroupBy(x => x.LabelCode).ToDictionary(g => g.Key, g => g.First()); - - var categoryIds = labels - .Select(x => x.LabelCategoryId) - .Where(x => !string.IsNullOrWhiteSpace(x)) - .Select(x => x!.Trim()) - .Distinct() - .ToList(); - var categories = categoryIds.Count == 0 - ? new List() - : await _dbContext.SqlSugarClient.Queryable() - .Where(c => !c.IsDeleted && categoryIds.Contains(c.Id)) + var existed = await _dbContext.SqlSugarClient.Queryable() + .Where(x => x.ClientRequestId == clientRequestId) + .OrderBy(x => x.CopyIndex) .ToListAsync(); - var catMap = categories.ToDictionary(x => x.Id, x => x); - - var templateIds = labels - .Select(x => x.TemplateId) - .Where(x => !string.IsNullOrWhiteSpace(x)) - .Select(x => x!.Trim()) - .Distinct() - .ToList(); - var templates = templateIds.Count == 0 - ? new List() - : await _dbContext.SqlSugarClient.Queryable() - .Where(tpl => !tpl.IsDeleted && templateIds.Contains(tpl.Id)) - .ToListAsync(); - var tplMap = templates.ToDictionary(x => x.Id, x => x); - - var labelTypeIds = labels - .Select(x => x.LabelTypeId) - .Where(x => !string.IsNullOrWhiteSpace(x)) - .Select(x => x!.Trim()) - .Distinct() - .ToList(); - var labelTypes = labelTypeIds.Count == 0 - ? new List() - : await _dbContext.SqlSugarClient.Queryable() - .Where(lt => !lt.IsDeleted && labelTypeIds.Contains(lt.Id)) - .ToListAsync(); - var typeMap = labelTypes.ToDictionary(x => x.Id, x => x); - - var productIds = tasks - .Select(t => t.ProductId) - .Where(x => !string.IsNullOrWhiteSpace(x)) - .Select(x => x!.Trim()) - .Distinct() - .ToList(); - var products = productIds.Count == 0 - ? new List() - : await _dbContext.SqlSugarClient.Queryable() - .Where(p => !p.IsDeleted && productIds.Contains(p.Id)) - .ToListAsync(); - var prodMap = products.ToDictionary(x => x.Id, x => x); - var items = dataRows.Select(d => - { - taskMap.TryGetValue(d.TaskId, out var t); - var lblId = ""; - string? catName = null; - string? tplSummary = null; - string? labelSizeText = null; - string? typeName = null; - if (t != null && !string.IsNullOrWhiteSpace(t.LabelCode) && labelByCode.TryGetValue(t.LabelCode.Trim(), out var lbl)) + if (existed is not null && existed.Count > 0) { - lblId = lbl.Id; - if (!string.IsNullOrWhiteSpace(lbl.LabelCategoryId) && catMap.TryGetValue(lbl.LabelCategoryId.Trim(), out var c)) - { - catName = string.IsNullOrWhiteSpace(c.CategoryName) ? "无" : c.CategoryName.Trim(); - } - if (!string.IsNullOrWhiteSpace(lbl.LabelTypeId) && typeMap.TryGetValue(lbl.LabelTypeId.Trim(), out var lt)) - { - typeName = string.IsNullOrWhiteSpace(lt.TypeName) ? null : lt.TypeName.Trim(); - } - if (!string.IsNullOrWhiteSpace(lbl.TemplateId) && tplMap.TryGetValue(lbl.TemplateId.Trim(), out var tpl)) + var existedBatchId = existed.First().BatchId; + var existedTaskIds = existed.Select(x => x.Id).ToList(); + return new UsAppLabelPrintOutputDto { - tplSummary = $"{tpl.Width}x{tpl.Height}{tpl.Unit} {tpl.TemplateName}".Trim(); - labelSizeText = string.Format( - CultureInfo.InvariantCulture, - "{0:0.00}x{1:0.00}{2}", - tpl.Width, - tpl.Height, - tpl.Unit); - } - } - - var productName = "无"; - if (t != null && !string.IsNullOrWhiteSpace(t.ProductId) && prodMap.TryGetValue(t.ProductId.Trim(), out var p)) - { - productName = string.IsNullOrWhiteSpace(p.ProductName) ? "无" : p.ProductName.Trim(); + TaskId = existedTaskIds.FirstOrDefault() ?? string.Empty, + PrintQuantity = existedTaskIds.Count, + BatchId = existedBatchId, + TaskIds = existedTaskIds + }; } + } - return new PrintLogItemDto - { - TaskId = d.TaskId, - BatchId = d.TaskId, - CopyIndex = d.CopyIndex ?? 1, - LabelId = string.IsNullOrWhiteSpace(lblId) ? (t?.LabelCode ?? "") : lblId, - LabelCode = t?.LabelCode ?? "", - ProductId = t?.ProductId, - ProductName = productName, - PrintedAt = d.CreationTime.ToString("yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture), - OperatorName = operatorName, - LocationName = string.IsNullOrWhiteSpace(locationDisplayName) ? "无" : locationDisplayName!, - LabelCategoryName = catName, - LabelTemplateSummary = tplSummary, - LabelSizeText = labelSizeText, - TypeName = typeName, - PrintDataList = BuildPrintDataListFromRenderJson(d.RenderDataJson) - }; - }).ToList(); - - var totalCount = total.Value; - var totalPages = pageSize <= 0 ? 0 : (int)Math.Ceiling(totalCount / (double)pageSize); - - return new PagedResultWithPageDto + var currentUserId = CurrentUser?.Id?.ToString(); + if (string.IsNullOrWhiteSpace(currentUserId)) { - PageIndex = pageIndex, - PageSize = pageSize, - TotalCount = totalCount, - TotalPages = totalPages, - Items = items - }; - } + throw new UserFriendlyException("未登录"); + } - private static List BuildPrintDataListFromRenderJson(string? renderDataJson) - { - var list = new List(); - if (string.IsNullOrWhiteSpace(renderDataJson)) + var old = await _dbContext.SqlSugarClient.Queryable() + .FirstAsync(x => x.Id == taskId); + if (old is null) { - return list; + throw new UserFriendlyException("打印任务不存在"); } - try + // 仅允许重打自己在当前门店的任务 + if (!string.Equals(old.CreatedBy?.Trim(), currentUserId, StringComparison.OrdinalIgnoreCase)) { - using var doc = JsonDocument.Parse(renderDataJson); - var root = doc.RootElement; - if (root.ValueKind == JsonValueKind.Object - && root.TryGetProperty("elements", out var elArr) - && elArr.ValueKind == JsonValueKind.Array) - { - foreach (var el in elArr.EnumerateArray()) - { - if (el.ValueKind != JsonValueKind.Object) - { - continue; - } - - var id = el.TryGetProperty("id", out var idEl) ? idEl.GetString() ?? string.Empty : string.Empty; - list.Add(new PrintLogDataItemDto - { - ElementId = id, - RenderValue = string.Empty, - RenderConfigJson = el.Clone() - }); - } - - if (list.Count > 0) - { - return list; - } - } + throw new UserFriendlyException("无权限重打该任务"); } - catch + if (!string.Equals(old.LocationId?.Trim(), locationId, StringComparison.OrdinalIgnoreCase)) { - // 继续尝试强类型解析 + throw new UserFriendlyException("该任务不属于当前门店"); } + LabelTemplatePreviewDto? resolvedTemplate = null; try { - var preview = JsonSerializer.Deserialize(renderDataJson); - if (preview?.Elements == null || preview.Elements.Count == 0) - { - return list; - } - - foreach (var el in preview.Elements) - { - var elJson = JsonSerializer.SerializeToElement(el); - list.Add(new PrintLogDataItemDto - { - ElementId = el.Id ?? string.Empty, - RenderValue = GetElementRenderValue(el), - RenderConfigJson = elJson - }); - } + resolvedTemplate = JsonSerializer.Deserialize(old.RenderTemplateJson); } catch { - // 历史数据或非预览结构时忽略 + resolvedTemplate = null; } - return list; - } + if (resolvedTemplate is null) + { + throw new UserFriendlyException("历史任务渲染快照解析失败,无法重打"); + } - private static string GetElementRenderValue(LabelTemplateElementDto el) - { - try + var now = DateTime.Now; + var batchId = _guidGenerator.Create().ToString(); + var taskIds = new List(); + + for (var i = 1; i <= quantity; i++) { - if (el.ConfigJson is JsonElement je) + var newTaskId = _guidGenerator.Create().ToString(); + taskIds.Add(newTaskId); + + var newTask = new FlLabelPrintTaskDbEntity + { + Id = newTaskId, + BatchId = batchId, + CopyIndex = i, + ClientRequestId = string.IsNullOrWhiteSpace(clientRequestId) ? null : clientRequestId, + LabelId = old.LabelId, + TemplateId = old.TemplateId, + LabelTypeId = old.LabelTypeId, + ProductId = old.ProductId, + LocationId = old.LocationId, + BaseTime = old.BaseTime, + PrintInputJson = old.PrintInputJson, + TemplateProductDefaultValuesJson = old.TemplateProductDefaultValuesJson, + RenderTemplateJson = old.RenderTemplateJson, + PrinterId = string.IsNullOrWhiteSpace(input.PrinterId) ? old.PrinterId : input.PrinterId.Trim(), + PrinterMac = string.IsNullOrWhiteSpace(input.PrinterMac) ? old.PrinterMac : input.PrinterMac.Trim(), + PrinterAddress = string.IsNullOrWhiteSpace(input.PrinterAddress) ? old.PrinterAddress : input.PrinterAddress.Trim(), + Status = "CREATED", + PrintedAt = null, + ErrorMessage = null, + CreatedBy = currentUserId, + CreationTime = now + }; + + await _dbContext.SqlSugarClient.Insertable(newTask).ExecuteCommandAsync(); + + var rows = resolvedTemplate.Elements.Select(e => { - if (je.ValueKind == JsonValueKind.Object) + var cfgJson = e.ConfigJson is null ? null : JsonSerializer.Serialize(e.ConfigJson); + string? renderValue = null; + if (e.ConfigJson is JsonElement je && je.ValueKind == JsonValueKind.Object && je.TryGetProperty("text", out var tv)) { - if (je.TryGetProperty("text", out var t)) - { - return t.GetString() ?? string.Empty; - } - if (je.TryGetProperty("Text", out var t2)) - { - return t2.GetString() ?? string.Empty; - } + renderValue = tv.ValueKind == JsonValueKind.String ? tv.GetString() : tv.ToString(); + } + else if (e.ConfigJson is Dictionary dict && dict.TryGetValue("text", out var v)) + { + renderValue = v?.ToString(); } + + return new FlLabelPrintDataDbEntity + { + Id = _guidGenerator.Create().ToString(), + PrintTaskId = newTaskId, + ElementId = e.Id?.Trim() ?? string.Empty, + ElementName = e.ElementName?.Trim(), + RenderValue = renderValue, + RenderConfigJson = cfgJson + }; + }).Where(x => !string.IsNullOrWhiteSpace(x.ElementId)).ToList(); + + if (rows.Count > 0) + { + await _dbContext.SqlSugarClient.Insertable(rows).ExecuteCommandAsync(); } } - catch - { - // ignore - } - return string.Empty; + return new UsAppLabelPrintOutputDto + { + TaskId = taskIds.FirstOrDefault() ?? string.Empty, + PrintQuantity = quantity, + BatchId = batchId, + TaskIds = taskIds + }; } /// - /// 接口 11:按历史任务重打并落库,返回可本地打印的合并模板 JSON + /// App 打印日志:获取当前登录账号在当前门店打印的记录(分页,时间倒序) /// + /// + /// 仅返回满足: + /// - CreatedBy == CurrentUser.Id + /// - LocationId == input.LocationId + /// 的打印任务记录(fl_label_print_task)。 + /// + /// 示例请求: + /// ```json + /// { + /// "locationId": "11111111-1111-1111-1111-111111111111", + /// "skipCount": 1, + /// "maxResultCount": 20 + /// } + /// ``` + /// + /// 参数说明: + /// - locationId: 当前门店 Id(必填) + /// - skipCount: 页码(从 1 开始,遵循本项目约定) + /// - maxResultCount: 每页条数 + /// + /// 分页查询入参 + /// 分页打印日志 + /// 成功 + /// 参数错误/未登录 + /// 服务器错误 [Authorize] - [UnitOfWork] [HttpPost] - public virtual async Task ReprintAsync(UsAppLabelReprintInputVo input) + public virtual async Task> GetPrintLogListAsync(PrintLogGetListInputVo input) { - if (input == null) + if (input is null) { throw new UserFriendlyException("入参不能为空"); } + if (!CurrentUser.Id.HasValue) + { + throw new UserFriendlyException("用户未登录"); + } + var locationId = input.LocationId?.Trim(); if (string.IsNullOrWhiteSpace(locationId)) { throw new UserFriendlyException("门店Id不能为空"); } - var histTaskId = input.TaskId?.Trim(); - if (string.IsNullOrWhiteSpace(histTaskId)) - { - throw new UserFriendlyException("taskId不能为空"); - } + var currentUserIdStr = CurrentUser.Id.Value.ToString(); - var userId = CurrentUser.Id?.ToString(); - if (string.IsNullOrWhiteSpace(userId)) - { - throw new UserFriendlyException("未登录"); - } + var currentUser = await _userRepository.GetByIdAsync(CurrentUser.Id.Value); + var operatorName = currentUser?.Name?.Trim() ?? string.Empty; - var taskRows = await _dbContext.SqlSugarClient.Queryable() - .Where(t => t.Id == histTaskId && !t.IsDeleted) - .Take(1) - .ToListAsync(); - var task = taskRows.FirstOrDefault(); - if (task == null) + var locationName = "无"; + if (Guid.TryParse(locationId, out var locationGuid)) { - throw new UserFriendlyException("打印任务不存在"); + var loc = await _dbContext.SqlSugarClient.Queryable() + .Where(x => !x.IsDeleted && x.Id == locationGuid) + .Select(x => new { x.LocationCode, x.LocationName }) + .FirstAsync(); + if (loc is not null) + { + var name = loc.LocationName?.Trim(); + if (!string.IsNullOrWhiteSpace(name)) + { + locationName = name; + } + } } - if (!string.Equals(task.CreatorId?.Trim(), userId, StringComparison.Ordinal)) - { - throw new UserFriendlyException("无权操作该打印任务"); - } + RefAsync total = 0; - if (!string.Equals(task.LocationId?.Trim(), locationId, StringComparison.OrdinalIgnoreCase)) - { - throw new UserFriendlyException("该任务不属于当前门店"); - } + var query = _dbContext.SqlSugarClient + .Queryable() + .LeftJoin((t, l) => t.LabelId == l.Id) + .LeftJoin((t, l, p) => t.ProductId == p.Id) + .LeftJoin((t, l, p, lt) => t.LabelTypeId == lt.Id) + .LeftJoin((t, l, p, lt, tpl) => t.TemplateId == tpl.Id) + .Where((t, l, p, lt, tpl) => t.CreatedBy == currentUserIdStr && t.LocationId == locationId) + .OrderBy((t, l, p, lt, tpl) => SqlFunc.IsNull(t.PrintedAt, t.CreationTime), OrderByType.Desc) + .OrderBy((t, l, p, lt, tpl) => t.CreationTime, OrderByType.Desc) + .Select((t, l, p, lt, tpl) => new + { + t.Id, + t.BatchId, + t.CopyIndex, + t.LabelId, + LabelCode = l.LabelCode, + t.ProductId, + ProductName = p.ProductName, + TypeName = lt.TypeName, + TemplateWidth = tpl.Width, + TemplateHeight = tpl.Height, + TemplateUnit = tpl.Unit, + t.RenderTemplateJson, + t.PrintedAt, + t.CreationTime + }); - var histDataRows = await _dbContext.SqlSugarClient.Queryable() - .Where(d => d.TaskId == histTaskId && !d.IsDeleted) - .OrderBy(d => d.CopyIndex) - .Take(1) - .ToListAsync(); - var histData = histDataRows.FirstOrDefault(); - if (histData == null) - { - throw new UserFriendlyException("打印明细不存在"); - } + var pageRows = await query.ToPageListAsync(input.SkipCount, input.MaxResultCount, total); - var mergedJson = !string.IsNullOrWhiteSpace(histData.PrintInputJson) - ? histData.PrintInputJson! - : histData.RenderDataJson; - if (string.IsNullOrWhiteSpace(mergedJson)) - { - throw new UserFriendlyException("无法重打:历史打印数据为空"); - } + var taskIds = pageRows.Select(x => x.Id).Where(x => !string.IsNullOrWhiteSpace(x)).Distinct().ToList(); - var qty = input.PrintQuantity <= 0 ? 1 : input.PrintQuantity; + var dataRows = taskIds.Count == 0 + ? new List() + : await _dbContext.SqlSugarClient.Queryable() + .Where(x => taskIds.Contains(x.PrintTaskId)) + .ToListAsync(); - var now = DateTime.Now; - var newTaskId = _guidGenerator.Create().ToString(); - var newTask = new FlLabelPrintTaskDbEntity - { - Id = newTaskId, - IsDeleted = false, - CreationTime = now, - CreatorId = userId, - ConcurrencyStamp = _guidGenerator.Create().ToString("N"), - LocationId = locationId, - LabelCode = task.LabelCode, - ProductId = task.ProductId, - LabelTypeId = task.LabelTypeId, - TemplateCode = task.TemplateCode, - PrintQuantity = qty, - BaseTime = task.BaseTime, - PrinterId = !string.IsNullOrWhiteSpace(input.PrinterId) ? input.PrinterId.Trim() : task.PrinterId, - PrinterMac = !string.IsNullOrWhiteSpace(input.PrinterMac) ? input.PrinterMac.Trim() : task.PrinterMac, - PrinterAddress = !string.IsNullOrWhiteSpace(input.PrinterAddress) ? input.PrinterAddress.Trim() : task.PrinterAddress - }; - await _dbContext.SqlSugarClient.Insertable(newTask).ExecuteCommandAsync(); + var dataMap = dataRows + .GroupBy(x => x.PrintTaskId) + .ToDictionary( + g => g.Key, + g => g.Select(d => + { + object? cfg = null; + if (!string.IsNullOrWhiteSpace(d.RenderConfigJson)) + { + try + { + cfg = JsonSerializer.Deserialize(d.RenderConfigJson); + } + catch + { + cfg = null; + } + } - var dataRows = Enumerable.Range(1, qty).Select(i => new FlLabelPrintDataDbEntity - { - Id = _guidGenerator.Create().ToString(), - IsDeleted = false, - CreationTime = now, - CreatorId = userId, - ConcurrencyStamp = _guidGenerator.Create().ToString("N"), - TaskId = newTaskId, - CopyIndex = i, - PrintInputJson = histData.PrintInputJson, - RenderDataJson = histData.RenderDataJson + return new PrintLogDataItemDto + { + ElementId = d.ElementId, + RenderValue = d.RenderValue, + RenderConfigJson = cfg + }; + }).ToList()); + + var items = pageRows.Select(x => new PrintLogItemDto + { + TaskId = x.Id, + BatchId = x.BatchId, + CopyIndex = x.CopyIndex, + LabelId = x.LabelId, + LabelCode = x.LabelCode ?? string.Empty, + ProductId = x.ProductId, + ProductName = string.IsNullOrWhiteSpace(x.ProductName) ? "无" : x.ProductName.Trim(), + TypeName = x.TypeName ?? string.Empty, + LabelSizeText = FormatLabelSizeWithUnit(x.TemplateWidth, x.TemplateHeight, x.TemplateUnit), + RenderTemplateJson = x.RenderTemplateJson, + PrintDataList = dataMap.TryGetValue(x.Id, out var list) ? list : new List(), + PrintedAt = x.PrintedAt ?? x.CreationTime, + OperatorName = operatorName, + LocationName = locationName }).ToList(); - await _dbContext.SqlSugarClient.Insertable(dataRows).ExecuteCommandAsync(); + var pageSize = input.MaxResultCount <= 0 ? items.Count : input.MaxResultCount; + var pageIndex = pageSize <= 0 ? 1 : PagedQueryConvention.PageIndexFromSkipCount(input.SkipCount); + var totalCount = (long)total; + var totalPages = pageSize <= 0 ? 0 : (int)Math.Ceiling(totalCount / (double)pageSize); - return new UsAppLabelPrintOutputDto + return new PagedResultWithPageDto { - TaskId = newTaskId, - PrintQuantity = qty, - BatchId = newTaskId, - TaskIds = new List { newTaskId }, - MergedTemplateJson = mergedJson + PageIndex = pageIndex, + PageSize = pageSize, + TotalCount = totalCount, + TotalPages = totalPages, + Items = items }; } + private async Task ResolveTemplateProductDefaultValuesJsonAsync( + string templateId, + string? productId, + string labelTypeId) + { + if (string.IsNullOrWhiteSpace(templateId) || string.IsNullOrWhiteSpace(productId) || string.IsNullOrWhiteSpace(labelTypeId)) + { + return null; + } + + var productDefault = await _dbContext.SqlSugarClient.Queryable() + .Where(x => x.TemplateId == templateId) + .Where(x => x.ProductId == productId) + .Where(x => x.LabelTypeId == labelTypeId) + .OrderBy(x => x.OrderNum) + .FirstAsync(); + + return string.IsNullOrWhiteSpace(productDefault?.DefaultValuesJson) ? null : productDefault!.DefaultValuesJson; + } + private ISugarQueryable BuildLabelingJoinQuery( string locationId, List productIds, @@ -1032,4 +998,13 @@ public class UsAppLabelingAppService : ApplicationService, IUsAppLabelingAppServ ? $"{ws}\"x{hs}\"" : $"{ws}x{hs}{u}"; } + + private static string? FormatLabelSizeWithUnit(decimal w, decimal h, string unit) + { + var u = (unit ?? "inch").Trim().ToLowerInvariant(); + var ws = w.ToString(CultureInfo.InvariantCulture); + var hs = h.ToString(CultureInfo.InvariantCulture); + var normalizedUnit = u is "in" ? "inch" : u; + return $"{ws}x{hs}{normalizedUnit}"; + } } diff --git a/项目相关文档/标签模块接口对接说明.md b/项目相关文档/标签模块接口对接说明.md index 12501f2..cc13fe6 100644 --- a/项目相关文档/标签模块接口对接说明.md +++ b/项目相关文档/标签模块接口对接说明.md @@ -907,6 +907,7 @@ curl -X POST "http://localhost:19001/api/app/us-app-labeling/preview" \ | `labelCode` | string | 是 | 标签编码(`fl_label.LabelCode`) | | `productId` | string | 否 | 打印用产品Id;不传则默认取该标签绑定的第一个产品(用于模板解析) | | `printQuantity` | number | 否 | 打印份数;`<=0` 按 1 处理 | +| `clientRequestId` | string | 否 | 客户端幂等请求Id;同一个值重复调用会直接返回首次创建的 `batchId/taskIds`,避免重复写库 用法:前端/客户端每次点击 Print 生成一个稳定的 clientRequestId(比如 uuid) | | `baseTime` | string | 否 | 业务基准时间(用于 DATE/TIME 元素计算) | | `printInputJson` | object | 否 | 打印输入(用于模板 PRINT_INPUT 元素),key 建议与模板元素 `inputKey` 对齐 | | `printerId` | string | 否 | 打印机Id(可选,用于追踪) | @@ -916,11 +917,11 @@ curl -X POST "http://localhost:19001/api/app/us-app-labeling/preview" \ #### 数据落库说明 - **任务表**:`fl_label_print_task` - - 插入 1 条任务记录(`locationId / labelCode / productId / labelTypeId / templateCode / printQuantity / baseTime / printer...` 等)。 + - **一份打印 = 一条任务**:当 `printQuantity = N` 时,后端会插入 **N 条任务记录**(同一次点击 Print 共享一个 `BatchId`,并记录 `CopyIndex=1..N`)。 + - 任务表会保存:本次打印的输入、命中的模板默认值、以及整份 resolved 后的模板快照 JSON,便于追溯/重打。 - **明细表**:`fl_label_print_data` - - 按 `printQuantity` 插入 N 条明细记录(`copyIndex = 1..N`)。 - - `printInputJson`:保存本次打印的原始输入(JSON 字符串)。 - - `renderDataJson`:保存本次解析后的模板预览结构(`LabelTemplatePreviewDto`,包含 resolved 后的 `elements[].config`),供追溯/重打使用。 + - **按组件写快照**:每个任务会按模板 `elements[]` 逐个插入明细记录(`ElementId/ElementName/RenderValue/RenderConfigJson`)。 + - 适用于按组件维度审计/统计/追溯。 > 模板解析的数据源来自 `fl_label_template` + `fl_label_template_element`,与预览接口一致。 @@ -928,8 +929,11 @@ curl -X POST "http://localhost:19001/api/app/us-app-labeling/preview" \ | 字段 | 类型 | 说明 | |---|---|---| -| `taskId` | string | 打印任务Id(用于后续查询/重打/统计) | +| `taskId` | string | 第 1 份打印任务Id(兼容旧逻辑) | | `printQuantity` | number | 实际写入的份数 | +| `batchId` | string | 本次点击 Print 的批次Id | +| `taskIds` | string[] | 本次生成的所有任务Id(长度=printQuantity) | + #### 错误与边界 @@ -964,3 +968,123 @@ curl -X POST "http://localhost:19001/api/app/us-app-labeling/print" \ -d '{"locationId":"11111111-1111-1111-1111-111111111111","labelCode":"LBL_CHICKEN_DEFROST","productId":"22222222-2222-2222-2222-222222222222","printQuantity":2,"baseTime":"2026-03-26T10:30:00","printInputJson":{"price":"12.99"}}' ``` +--- + +## 接口 10:App 打印日志(当前登录账号 + 当前门店) + +**场景**:移动端“打印记录/历史”页面。只展示**当前登录账号**在**当前门店**打印的记录,便于追溯/重打。 + +### 10.1 分页获取打印日志 + +#### HTTP + +- **方法**:`POST`(与本模块其它复杂入参接口一致;若与 Swagger 不一致,**以 Swagger 为准**) +- **路径**:`/api/app/us-app-labeling/get-print-log-list` +- **鉴权**:需要登录(`Authorization: Bearer ...`) + +#### 入参(Body:`PrintLogGetListInputVo`) + +> 本项目分页约定:`skipCount` 表示 **页码(从 1 开始)**,不是 0 基 offset。 + +| 参数名(JSON) | 类型 | 必填 | 说明 | +|---|---|---|---| +| `locationId` | string | 是 | 当前门店Id(仅返回该门店记录) | +| `skipCount` | number | 否 | 页码,从 1 开始;默认 1 | +| `maxResultCount` | number | 否 | 每页条数;默认按后端/ABP 默认 | + +#### 过滤条件(后端固定逻辑) + +- `fl_label_print_task.CreatedBy == CurrentUser.Id` +- `fl_label_print_task.LocationId == locationId` +- 按时间倒序:`PrintedAt ?? CreationTime`(越新的越靠前) + +#### 出参(`PagedResultWithPageDto`) + +| 字段 | 类型 | 说明 | +|---|---|---| +| `pageIndex` | number | 当前页码(从 1 开始) | +| `pageSize` | number | 每页条数 | +| `totalCount` | number | 总条数 | +| `totalPages` | number | 总页数 | +| `items` | PrintLogItemDto[] | 列表 | + +`PrintLogItemDto`: + +| 字段 | 类型 | 说明 | +|---|---|---| +| `taskId` | string | 任务Id(fl_label_print_task.Id) | +| `batchId` | string | 批次Id(同一次点击 Print 共享) | +| `copyIndex` | number | 第几份(从 1 开始) | +| `labelId` | string | 标签Id | +| `labelCode` | string | 标签编码(来自 fl_label.LabelCode) | +| `productId` | string | 产品Id | +| `productName` | string | 产品名(来自 fl_product.ProductName;无则 “无”) | +| `typeName` | string | 标签类型名称(来自 fl_label_type.TypeName) | +| `labelSizeText` | string | 模板尺寸(宽高+单位,如 `2.00x2.00inch` / `6.00x4.00cm`) | +| `renderTemplateJson` | string | 本次任务落库的渲染模板 JSON(`fl_label_print_task.RenderTemplateJson`) | +| `printDataList` | PrintLogDataItemDto[] | 本次打印内容快照(来自 fl_label_print_data,按 taskId 关联) | +| `printedAt` | string | 打印时间(PrintedAt ?? CreationTime) | +| `operatorName` | string | 操作人姓名(当前登录账号 Name) | +| `locationName` | string | 门店名称 | + +`PrintLogDataItemDto`: + +| 字段 | 类型 | 说明 | +|---|---|---| +| `elementId` | string | 模板组件Id(fl_label_print_data.ElementId) | +| `renderValue` | string | 最终渲染值(fl_label_print_data.RenderValue) | +| `renderConfigJson` | object | 最终渲染配置(fl_label_print_data.RenderConfigJson 反序列化) | + +#### curl + +```bash +curl -X POST "http://localhost:19001/api/app/us-app-labeling/get-print-log-list" \ + -H "Authorization: " \ + -H "Content-Type: application/json" \ + -d '{"locationId":"11111111-1111-1111-1111-111111111111","skipCount":1,"maxResultCount":20}' +``` + +--- + +## 接口 11:App 重新打印(根据任务Id重打) + +**场景**:移动端“打印记录/历史”页面点击 **Reprint**。后端根据历史任务 `taskId` 创建一批新的打印任务与明细。 + +### 11.1 重打 + +#### HTTP + +- **方法**:`POST` +- **路径**:`/api/app/us-app-labeling/reprint`(若与 Swagger 不一致,**以 Swagger 为准**) +- **鉴权**:需要登录(`Authorization: Bearer ...`) + +#### 入参(Body:`UsAppLabelReprintInputVo`) + +| 参数名(JSON) | 类型 | 必填 | 说明 | +|---|---|---|---| +| `locationId` | string | 是 | 当前门店Id(后端校验历史任务必须属于该门店) | +| `taskId` | string | 是 | 历史打印任务Id(`fl_label_print_task.Id`) | +| `printQuantity` | number | 否 | 重新打印份数;`<=0` 按 1 处理;默认 1 | +| `clientRequestId` | string | 否 | 客户端幂等请求Id;同一个值重复调用会直接返回首次创建的 `batchId/taskIds`,避免重复写库 | +| `printerId` | string | 否 | 可选,覆盖历史任务的打印机Id | +| `printerMac` | string | 否 | 可选,覆盖历史任务的打印机MAC | +| `printerAddress` | string | 否 | 可选,覆盖历史任务的打印机地址 | + +#### 权限校验(后端固定逻辑) + +- 历史任务必须满足:`CreatedBy == CurrentUser.Id` +- 且 `LocationId == locationId` + +#### 出参(`UsAppLabelPrintOutputDto`) + +字段与接口 9 一致:`taskId / printQuantity / batchId / taskIds` + +#### curl + +```bash +curl -X POST "http://localhost:19001/api/app/us-app-labeling/reprint" \ + -H "Authorization: " \ + -H "Content-Type: application/json" \ + -d '{"locationId":"11111111-1111-1111-1111-111111111111","taskId":"3a205389-78dd-4750-51ab-720344c9f607","printQuantity":1}' +``` +