2893c050
李曜臣
2026-07-13
|
1
|
using System.Text.Json;
|
59e51671
“wangming”
1
|
2
3
4
5
6
|
using FoodLabeling.Application.Helpers;
using FoodLabeling.Application.Contracts.Dtos.Common;
using FoodLabeling.Application.Contracts.Dtos.LabelTemplate;
using FoodLabeling.Application.Contracts.IServices;
using FoodLabeling.Application.Services.DbModels;
|
59e51671
“wangming”
1
|
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
using SqlSugar;
using Volo.Abp;
using Volo.Abp.Application.Services;
using Volo.Abp.Guids;
using Volo.Abp.Uow;
using Yi.Framework.SqlSugarCore.Abstractions;
namespace FoodLabeling.Application.Services;
/// <summary>
/// 标签模板管理(Label Templates)
/// </summary>
public class LabelTemplateAppService : ApplicationService, ILabelTemplateAppService
{
private readonly ISqlSugarDbContext _dbContext;
private readonly IGuidGenerator _guidGenerator;
public LabelTemplateAppService(ISqlSugarDbContext dbContext, IGuidGenerator guidGenerator)
{
_dbContext = dbContext;
_guidGenerator = guidGenerator;
}
public async Task<PagedResultWithPageDto<LabelTemplateGetListOutputDto>> GetListAsync(LabelTemplateGetListInputVo input)
{
RefAsync<int> total = 0;
var keyword = input.Keyword?.Trim();
|
2893c050
李曜臣
2026-07-13
|
34
35
36
37
38
39
|
var scopedLocationIds = await TeamMemberListScopeHelper.ResolveListLocationIdsAsync(
CurrentUser,
_dbContext,
input.PartnerId,
input.GroupId,
input.LocationId);
|
59e51671
“wangming”
1
|
40
41
42
43
44
45
46
47
|
var query = _dbContext.SqlSugarClient.Queryable<FlLabelTemplateDbEntity>()
.Where(x => !x.IsDeleted)
.WhereIF(!string.IsNullOrWhiteSpace(keyword), x =>
x.TemplateName.Contains(keyword!) || x.TemplateCode.Contains(keyword!))
.WhereIF(!string.IsNullOrWhiteSpace(input.LabelType), x => x.LabelType == input.LabelType)
.WhereIF(input.State != null, x => x.State == input.State);
|
2893c050
李曜臣
2026-07-13
|
48
|
query = await LabelTemplateScopeHelper.ApplyTemplateScopeFilterAsync(
|
30543ac5
李曜臣
泰鄂版同步代码
|
49
50
51
52
53
54
|
_dbContext.SqlSugarClient,
query,
scopedLocationIds,
input.PartnerId,
input.GroupId,
input.LocationId);
|
59e51671
“wangming”
1
|
55
|
|
2893c050
李曜臣
2026-07-13
|
56
57
|
query = LabelTemplateQueryHelper.ApplyListSorting(query, input.Sorting);
query = LabelTemplateQueryHelper.ProjectListColumns(query);
|
59e51671
“wangming”
1
|
58
59
60
61
62
63
64
65
66
67
68
69
|
var pageEntities = await query.ToPageListAsync(input.SkipCount, input.MaxResultCount, total);
var templateIds = pageEntities.Select(x => x.Id).ToList();
// element count (Contents)
var elementCounts = await _dbContext.SqlSugarClient.Queryable<FlLabelTemplateElementDbEntity>()
.Where(x => templateIds.Contains(x.TemplateId))
.GroupBy(x => x.TemplateId)
.Select(x => new { TemplateId = x.TemplateId, Count = SqlFunc.AggregateCount(x.Id) })
.ToListAsync();
var elementCountMap = elementCounts.ToDictionary(x => x.TemplateId, x => (int)x.Count);
|
2893c050
李曜臣
2026-07-13
|
70
71
72
73
74
75
|
var scopeMap = await LabelTemplateScopeHelper.BuildScopeDisplayMapAsync(
_dbContext.SqlSugarClient, pageEntities);
var itemsMap = await LabelTemplateListItemsHelper.ResolveTemplateItemsMapAsync(
_dbContext.SqlSugarClient, templateIds);
var contentsFromElementsMap = await LabelTemplateContentsHelper.ResolveContentsMapFromElementsAsync(
_dbContext.SqlSugarClient, templateIds);
|
59e51671
“wangming”
1
|
76
77
78
|
var items = pageEntities.Select(x =>
{
|
2893c050
李曜臣
2026-07-13
|
79
80
|
scopeMap.TryGetValue(x.Id, out var scope);
itemsMap.TryGetValue(x.Id, out var itemsDisplay);
|
59e51671
“wangming”
1
|
81
82
|
var lastEdited = x.LastModificationTime ?? x.CreationTime;
var contentsCount = elementCountMap.TryGetValue(x.Id, out var c) ? c : 0;
|
2893c050
李曜臣
2026-07-13
|
83
|
var locationDisplay = scope?.Location ?? EmptyDisplay;
|
59e51671
“wangming”
1
|
84
|
var sizeText = $"{x.Width}x{x.Height}{x.Unit}";
|
2893c050
李曜臣
2026-07-13
|
85
86
87
88
|
contentsFromElementsMap.TryGetValue(x.Id, out var fromElements);
var contentsText = !string.IsNullOrWhiteSpace(fromElements)
? fromElements!
: (itemsDisplay?.Items ?? FoodLabelingDisplayConsts.NotAvailable);
|
59e51671
“wangming”
1
|
89
90
91
92
93
94
95
|
return new LabelTemplateGetListOutputDto
{
Id = x.TemplateCode, // front-end uses templateCode as identifier
TemplateCode = x.TemplateCode,
TemplateName = x.TemplateName,
LabelType = x.LabelType,
|
2893c050
李曜臣
2026-07-13
|
96
97
98
99
100
101
102
103
104
|
LocationText = locationDisplay,
Company = scope?.Company ?? string.Empty,
Region = scope?.Region ?? string.Empty,
Location = locationDisplay,
PartnerIds = scope?.PartnerIds ?? new List<string>(),
CompanyIds = scope?.PartnerIds ?? new List<string>(),
RegionIds = scope?.RegionIds ?? new List<string>(),
GroupIds = scope?.RegionIds ?? new List<string>(),
LocationIds = scope?.LocationIds ?? new List<string>(),
|
59e51671
“wangming”
1
|
105
|
ContentsCount = contentsCount,
|
2893c050
李曜臣
2026-07-13
|
106
107
108
|
Contents = contentsText,
Items = contentsText,
ItemNames = itemsDisplay?.ItemNames ?? new List<string>(),
|
59e51671
“wangming”
1
|
109
|
SizeText = sizeText,
|
2893c050
李曜臣
2026-07-13
|
110
|
PrintOrientation = LabelTemplatePrintOrientationHelper.Normalize(x.PrintOrientation),
|
59e51671
“wangming”
1
|
111
112
113
114
115
116
117
118
119
120
|
VersionNo = x.VersionNo,
LastEdited = lastEdited
};
}).ToList();
return BuildPagedResult(input.SkipCount, input.MaxResultCount, total, items);
}
public async Task<LabelTemplateGetOutputDto> GetAsync(string id)
{
|
2893c050
李曜臣
2026-07-13
|
121
|
var template = await LabelTemplateQueryHelper.QueryProjected(_dbContext.SqlSugarClient)
|
59e51671
“wangming”
1
|
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
|
.FirstAsync(x => !x.IsDeleted && x.TemplateCode == id);
if (template is null)
{
throw new UserFriendlyException("模板不存在");
}
var elements = await _dbContext.SqlSugarClient.Queryable<FlLabelTemplateElementDbEntity>()
.Where(x => x.TemplateId == template.Id)
.OrderBy(x => x.OrderNum)
.ToListAsync();
List<LabelTemplateElementDto> MapElements()
{
return elements.Select(e =>
{
object? cfg = null;
if (!string.IsNullOrWhiteSpace(e.ConfigJson))
{
cfg = JsonSerializer.Deserialize<object>(e.ConfigJson);
}
return new LabelTemplateElementDto
{
Id = e.ElementKey,
ElementType = e.ElementType,
TypeAdd = e.TypeAdd,
ElementName = e.ElementName,
PosX = e.PosX,
PosY = e.PosY,
Width = e.Width,
Height = e.Height,
Rotation = e.Rotation,
BorderType = e.BorderType,
ZIndex = e.ZIndex,
OrderNum = e.OrderNum,
ValueSourceType = e.ValueSourceType,
BindingExpr = e.BindingExpr,
AutoQueryKey = e.AutoQueryKey,
InputKey = e.InputKey,
IsRequiredInput = e.IsRequiredInput,
ConfigJson = cfg
};
}).ToList();
}
|
59e51671
“wangming”
1
|
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
|
var defaultRows = await _dbContext.SqlSugarClient.Queryable<FlLabelTemplateProductDefaultDbEntity>()
.Where(x => x.TemplateId == template.Id)
.OrderBy(x => x.OrderNum)
.ToListAsync();
var productDefaults = defaultRows.Select(x =>
{
object? defaults = null;
if (!string.IsNullOrWhiteSpace(x.DefaultValuesJson))
{
defaults = JsonSerializer.Deserialize<object>(x.DefaultValuesJson);
}
return new LabelTemplateProductDefaultDto
{
ProductId = x.ProductId,
LabelTypeId = x.LabelTypeId,
DefaultValues = defaults,
OrderNum = x.OrderNum
};
}).ToList();
|
2893c050
李曜臣
2026-07-13
|
189
190
191
192
193
194
195
196
197
198
199
200
201
|
var borderType = await LabelTemplateScopeSchemaHelper.GetBorderTypeForTemplateAsync(
_dbContext.SqlSugarClient, template.Id);
var mappedElements = MapElements();
var contents = LabelTemplateContentsHelper.BuildFromElements(mappedElements);
if (string.IsNullOrWhiteSpace(contents))
{
var storedContents = await LabelTemplateScopeSchemaHelper.GetContentsForTemplateAsync(
_dbContext.SqlSugarClient, template.Id);
contents = storedContents?.Trim() ?? string.Empty;
}
var dto = new LabelTemplateGetOutputDto
|
59e51671
“wangming”
1
|
202
203
204
205
206
207
208
209
|
{
Id = template.TemplateCode,
TemplateCode = template.TemplateCode,
TemplateName = template.TemplateName,
LabelType = template.LabelType,
Unit = template.Unit,
Width = template.Width,
Height = template.Height,
|
2893c050
李曜臣
2026-07-13
|
210
|
PrintOrientation = LabelTemplatePrintOrientationHelper.Normalize(template.PrintOrientation),
|
59e51671
“wangming”
1
|
211
212
213
|
AppliedLocationType = template.AppliedLocationType,
ShowRuler = template.ShowRuler,
ShowGrid = template.ShowGrid,
|
2893c050
李曜臣
2026-07-13
|
214
|
BorderType = NormalizeTemplateBorderType(borderType),
|
59e51671
“wangming”
1
|
215
216
|
VersionNo = template.VersionNo,
State = template.State,
|
2893c050
李曜臣
2026-07-13
|
217
218
|
Contents = contents,
Elements = mappedElements,
|
59e51671
“wangming”
1
|
219
220
|
TemplateProductDefaults = productDefaults
};
|
2893c050
李曜臣
2026-07-13
|
221
222
|
await FillTemplateScopeOnDtoAsync(dto, template);
|
a60a45f4
李曜臣
2026-07-21
|
223
|
|
30543ac5
李曜臣
泰鄂版同步代码
|
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
|
var collapsed = await ScopeAllEchoHelper.CollapseScopeIdsToAllSentinelAsync(
_dbContext.SqlSugarClient,
dto.PartnerIds,
dto.RegionIds,
dto.LocationIds,
ScopeAllEchoHelper.ForLabelTemplateDimensions(
dto.AppliedPartnerType,
dto.AppliedRegionType,
dto.AppliedLocationType));
dto.PartnerIds = collapsed.PartnerIds;
dto.CompanyIds = collapsed.PartnerIds;
dto.RegionIds = collapsed.RegionIds;
dto.GroupIds = collapsed.RegionIds;
dto.LocationIds = collapsed.LocationIds;
dto.AppliedLocationIds = collapsed.LocationIds;
|
a60a45f4
李曜臣
2026-07-21
|
239
|
|
2893c050
李曜臣
2026-07-13
|
240
|
return dto;
|
59e51671
“wangming”
1
|
241
242
|
}
|
30543ac5
李曜臣
泰鄂版同步代码
|
243
244
245
246
|
/// <summary>
/// 新增标签模板。Company / Region / Location 三维范围;
/// <c>regionIds</c>、<c>groupIds</c>、<c>locationIds</c>、<c>appliedLocationIds</c> 可传 <c>ALL</c> 哨兵归档为对应维度 ALL。
/// </summary>
|
59e51671
“wangming”
1
|
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
|
[UnitOfWork]
public async Task<LabelTemplateGetOutputDto> CreateAsync(LabelTemplateCreateInputVo input)
{
var code = input.TemplateCode?.Trim();
var name = input.TemplateName?.Trim();
if (string.IsNullOrWhiteSpace(code))
{
throw new UserFriendlyException("模板编码不能为空");
}
if (string.IsNullOrWhiteSpace(name))
{
throw new UserFriendlyException("模板名称不能为空");
}
var duplicated = await _dbContext.SqlSugarClient.Queryable<FlLabelTemplateDbEntity>()
.AnyAsync(x => !x.IsDeleted && x.TemplateCode == code);
if (duplicated)
{
throw new UserFriendlyException("模板编码已存在");
}
|
a60a45f4
李曜臣
2026-07-21
|
268
269
|
await PurgeSoftDeletedTemplatesByCodeAsync(code);
|
2893c050
李曜臣
2026-07-13
|
270
271
272
273
274
|
var scope = await LabelTemplateScopeHelper.ResolveScopeForSaveAsync(_dbContext.SqlSugarClient, input);
LabelTemplatePrintOrientationHelper.EnsureValidOrThrow(input.PrintOrientation);
var printOrientation = LabelTemplatePrintOrientationHelper.Normalize(input.PrintOrientation);
var contents = LabelTemplateContentsHelper.ResolveForSave(input.Contents, input.Elements);
|
59e51671
“wangming”
1
|
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
|
var now = DateTime.Now;
var templateId = _guidGenerator.Create().ToString();
var entity = new FlLabelTemplateDbEntity
{
Id = templateId,
IsDeleted = false,
CreationTime = now,
CreatorId = CurrentUser?.Id?.ToString(),
LastModifierId = CurrentUser?.Id?.ToString(),
LastModificationTime = now,
ConcurrencyStamp = string.Empty,
TemplateCode = code,
TemplateName = name,
LabelType = input.LabelType,
Unit = string.IsNullOrWhiteSpace(input.Unit) ? "inch" : input.Unit.Trim(),
Width = input.Width,
Height = input.Height,
|
2893c050
李曜臣
2026-07-13
|
292
293
|
PrintOrientation = printOrientation,
AppliedLocationType = scope.AppliedLocationType,
|
59e51671
“wangming”
1
|
294
295
296
297
298
299
300
|
ShowRuler = input.ShowRuler,
ShowGrid = input.ShowGrid,
VersionNo = 1,
State = input.State
};
await _dbContext.SqlSugarClient.Insertable(entity).ExecuteCommandAsync();
|
2893c050
李曜臣
2026-07-13
|
301
302
303
304
|
await LabelTemplateScopeSchemaHelper.SetBorderTypeAsync(
_dbContext.SqlSugarClient,
entity.Id,
NormalizeTemplateBorderType(input.BorderType));
|
59e51671
“wangming”
1
|
305
|
|
2893c050
李曜臣
2026-07-13
|
306
|
await RebuildTemplateElementsAndDefaultsAsync(
|
59e51671
“wangming”
1
|
307
308
|
entity.Id,
input.Elements,
|
59e51671
“wangming”
1
|
309
310
|
new List<LabelTemplateProductDefaultDto>());
|
2893c050
李曜臣
2026-07-13
|
311
312
313
314
315
316
317
318
319
320
|
await PersistTemplateContentsAsync(entity.Id, contents);
await LabelTemplateScopeHelper.SaveTemplateScopeAsync(
_dbContext.SqlSugarClient,
_guidGenerator,
entity.Id,
scope,
CurrentUser?.Id?.ToString(),
now);
|
59e51671
“wangming”
1
|
321
322
323
|
return await GetAsync(code);
}
|
30543ac5
李曜臣
泰鄂版同步代码
|
324
325
326
327
328
329
|
/// <summary>
/// 编辑标签模板(版本号 +1)。适用范围经 <see cref="LabelTemplateScopeHelper.ResolveScopeForSaveAsync"/> 与
/// <see cref="LabelTemplateScopeHelper.SaveTemplateScopeAsync"/> 落库(含主表 <c>AppliedLocationType</c> 及
/// <c>AppliedPartnerType</c> / <c>AppliedRegionType</c>);
/// <c>regionIds</c>、<c>groupIds</c>、<c>locationIds</c>、<c>appliedLocationIds</c> 可传 <c>ALL</c>,与 GET 回显对称。
/// </summary>
|
59e51671
“wangming”
1
|
330
331
332
|
[UnitOfWork]
public async Task<LabelTemplateGetOutputDto> UpdateAsync(string id, LabelTemplateUpdateInputVo input)
{
|
2893c050
李曜臣
2026-07-13
|
333
|
var template = await LabelTemplateQueryHelper.QueryProjected(_dbContext.SqlSugarClient)
|
59e51671
“wangming”
1
|
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
|
.FirstAsync(x => !x.IsDeleted && x.TemplateCode == id);
if (template is null)
{
throw new UserFriendlyException("模板不存在");
}
var code = input.TemplateCode?.Trim();
var name = input.TemplateName?.Trim();
if (!string.IsNullOrWhiteSpace(code) && !string.Equals(code, template.TemplateCode, StringComparison.OrdinalIgnoreCase))
{
var duplicated = await _dbContext.SqlSugarClient.Queryable<FlLabelTemplateDbEntity>()
.AnyAsync(x => !x.IsDeleted && x.TemplateCode == code);
if (duplicated)
{
throw new UserFriendlyException("模板编码已存在");
}
}
|
2893c050
李曜臣
2026-07-13
|
352
353
354
355
356
357
358
|
var scope = await LabelTemplateScopeHelper.ResolveScopeForSaveAsync(_dbContext.SqlSugarClient, input);
LabelTemplatePrintOrientationHelper.EnsureValidOrThrow(input.PrintOrientation);
var printOrientation = string.IsNullOrWhiteSpace(input.PrintOrientation)
? LabelTemplatePrintOrientationHelper.Normalize(template.PrintOrientation)
: LabelTemplatePrintOrientationHelper.Normalize(input.PrintOrientation);
var contents = LabelTemplateContentsHelper.ResolveForSave(input.Contents, input.Elements);
|
59e51671
“wangming”
1
|
359
360
361
362
363
|
template.TemplateName = name ?? template.TemplateName;
template.LabelType = input.LabelType;
template.Unit = string.IsNullOrWhiteSpace(input.Unit) ? template.Unit : input.Unit.Trim();
template.Width = input.Width;
template.Height = input.Height;
|
2893c050
李曜臣
2026-07-13
|
364
365
|
template.PrintOrientation = printOrientation;
template.AppliedLocationType = scope.AppliedLocationType;
|
59e51671
“wangming”
1
|
366
367
368
369
370
371
372
373
374
375
376
377
|
template.ShowRuler = input.ShowRuler;
template.ShowGrid = input.ShowGrid;
template.State = input.State;
template.VersionNo = template.VersionNo + 1;
template.LastModifierId = CurrentUser?.Id?.ToString();
template.LastModificationTime = DateTime.Now;
if (!string.IsNullOrWhiteSpace(code))
{
template.TemplateCode = code;
}
await _dbContext.SqlSugarClient.Updateable(template).ExecuteCommandAsync();
|
2893c050
李曜臣
2026-07-13
|
378
379
380
381
382
|
// 强制落库 PrintOrientation(避免 ORM 元数据未刷新时 UPDATE 漏写该列)
await _dbContext.SqlSugarClient.Updateable<FlLabelTemplateDbEntity>()
.SetColumns(it => it.PrintOrientation == printOrientation)
.Where(it => it.Id == template.Id)
.ExecuteCommandAsync();
|
59e51671
“wangming”
1
|
383
|
|
2893c050
李曜臣
2026-07-13
|
384
|
await RebuildTemplateElementsAndDefaultsAsync(
|
59e51671
“wangming”
1
|
385
386
|
template.Id,
input.Elements,
|
59e51671
“wangming”
1
|
387
388
|
input.TemplateProductDefaults);
|
2893c050
李曜臣
2026-07-13
|
389
390
391
392
393
394
395
396
397
398
399
400
401
402
|
await PersistTemplateContentsAsync(template.Id, contents);
await LabelTemplateScopeHelper.SaveTemplateScopeAsync(
_dbContext.SqlSugarClient,
_guidGenerator,
template.Id,
scope,
CurrentUser?.Id?.ToString(),
template.LastModificationTime ?? DateTime.Now);
await LabelTemplateScopeSchemaHelper.SetBorderTypeAsync(
_dbContext.SqlSugarClient,
template.Id,
NormalizeTemplateBorderType(input.BorderType));
|
59e51671
“wangming”
1
|
403
404
405
406
407
408
|
return await GetAsync(template.TemplateCode);
}
[UnitOfWork]
public async Task DeleteAsync(string id)
{
|
2893c050
李曜臣
2026-07-13
|
409
|
var template = await LabelTemplateQueryHelper.QueryProjected(_dbContext.SqlSugarClient)
|
59e51671
“wangming”
1
|
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
|
.FirstAsync(x => !x.IsDeleted && x.TemplateCode == id);
if (template is null)
{
return;
}
var used = await _dbContext.SqlSugarClient.Queryable<FlLabelDbEntity>()
.AnyAsync(x => !x.IsDeleted && x.TemplateId == template.Id);
if (used)
{
throw new UserFriendlyException("该模板已被标签引用,无法删除");
}
template.IsDeleted = true;
template.LastModifierId = CurrentUser?.Id?.ToString();
template.LastModificationTime = DateTime.Now;
await _dbContext.SqlSugarClient.Updateable(template).ExecuteCommandAsync();
// 删除子表数据(子表无 IsDeleted 字段)
await _dbContext.SqlSugarClient.Deleteable<FlLabelTemplateElementDbEntity>()
.Where(x => x.TemplateId == template.Id)
.ExecuteCommandAsync();
|
2893c050
李曜臣
2026-07-13
|
433
434
|
await LabelTemplateScopeHelper.DeleteTemplateScopeChildRowsAsync(
_dbContext.SqlSugarClient, template.Id);
|
59e51671
“wangming”
1
|
435
436
437
438
439
|
await _dbContext.SqlSugarClient.Deleteable<FlLabelTemplateProductDefaultDbEntity>()
.Where(x => x.TemplateId == template.Id)
.ExecuteCommandAsync();
}
|
2893c050
李曜臣
2026-07-13
|
440
|
private async Task RebuildTemplateElementsAndDefaultsAsync(
|
59e51671
“wangming”
1
|
441
442
|
string templateDbId,
List<LabelTemplateElementDto> elements,
|
59e51671
“wangming”
1
|
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
|
List<LabelTemplateProductDefaultDto>? templateProductDefaults)
{
// elements 重建
await _dbContext.SqlSugarClient.Deleteable<FlLabelTemplateElementDbEntity>()
.Where(x => x.TemplateId == templateDbId)
.ExecuteCommandAsync();
if (elements is not null && elements.Count > 0)
{
var rows = elements.Select(e =>
{
var elementName = EnsureElementName(e.ElementName);
object? cfg = e.ConfigJson;
var configJson = cfg == null ? null : JsonSerializer.Serialize(cfg);
return new FlLabelTemplateElementDbEntity
{
Id = _guidGenerator.Create().ToString(),
TemplateId = templateDbId,
ElementKey = e.Id,
ElementType = e.ElementType,
TypeAdd = string.IsNullOrWhiteSpace(e.TypeAdd) ? null : e.TypeAdd.Trim(),
ElementName = elementName,
PosX = e.PosX,
PosY = e.PosY,
Width = e.Width,
Height = e.Height,
Rotation = string.IsNullOrWhiteSpace(e.Rotation) ? "horizontal" : e.Rotation,
BorderType = string.IsNullOrWhiteSpace(e.BorderType) ? "none" : e.BorderType,
ZIndex = e.ZIndex,
OrderNum = e.OrderNum,
ValueSourceType = string.IsNullOrWhiteSpace(e.ValueSourceType) ? "FIXED" : e.ValueSourceType,
BindingExpr = e.BindingExpr,
AutoQueryKey = e.AutoQueryKey,
InputKey = e.InputKey,
IsRequiredInput = e.IsRequiredInput,
ConfigJson = configJson
};
}).ToList();
await _dbContext.SqlSugarClient.Insertable(rows).ExecuteCommandAsync();
}
|
59e51671
“wangming”
1
|
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
|
// 模板-产品-标签类型默认值:仅在显式传入时重建,避免普通编辑误清空
if (templateProductDefaults is not null)
{
var duplicateCheckSet = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
foreach (var row in templateProductDefaults)
{
var productId = row.ProductId?.Trim();
var labelTypeId = row.LabelTypeId?.Trim();
if (string.IsNullOrWhiteSpace(productId) || string.IsNullOrWhiteSpace(labelTypeId))
{
continue;
}
var key = $"{productId}::{labelTypeId}";
if (!duplicateCheckSet.Add(key))
{
throw new UserFriendlyException($"模板默认值绑定重复:产品[{productId}]与标签类型[{labelTypeId}]只能存在一条");
}
}
await _dbContext.SqlSugarClient.Deleteable<FlLabelTemplateProductDefaultDbEntity>()
.Where(x => x.TemplateId == templateDbId)
.ExecuteCommandAsync();
if (templateProductDefaults.Count > 0)
{
var rows = templateProductDefaults.Select((x, idx) =>
{
var productId = x.ProductId?.Trim();
var labelTypeId = x.LabelTypeId?.Trim();
if (string.IsNullOrWhiteSpace(productId))
{
throw new UserFriendlyException("模板默认值绑定中,产品Id不能为空");
}
if (string.IsNullOrWhiteSpace(labelTypeId))
{
throw new UserFriendlyException("模板默认值绑定中,标签类型Id不能为空");
}
var json = x.DefaultValues is null ? null : JsonSerializer.Serialize(x.DefaultValues);
return new FlLabelTemplateProductDefaultDbEntity
{
Id = _guidGenerator.Create().ToString(),
TemplateId = templateDbId,
ProductId = productId,
LabelTypeId = labelTypeId,
DefaultValuesJson = json,
OrderNum = x.OrderNum <= 0 ? idx + 1 : x.OrderNum
};
}).ToList();
await _dbContext.SqlSugarClient.Insertable(rows).ExecuteCommandAsync();
}
}
}
|
2893c050
李曜臣
2026-07-13
|
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
|
private const string EmptyDisplay = FoodLabelingDisplayConsts.NotAvailable;
private async Task FillTemplateScopeOnDtoAsync(LabelTemplateGetOutputDto dto, FlLabelTemplateDbEntity template)
{
var scopeMap = await LabelTemplateScopeHelper.BuildScopeDisplayMapAsync(
_dbContext.SqlSugarClient, new List<FlLabelTemplateDbEntity> { template });
scopeMap.TryGetValue(template.Id, out var scope);
dto.Company = scope?.Company ?? string.Empty;
dto.Region = scope?.Region ?? string.Empty;
dto.Location = scope?.Location ?? string.Empty;
dto.AppliedPartnerType = scope?.AppliedPartnerType ?? LabelTemplateScopeHelper.ScopeAll;
dto.AppliedRegionType = scope?.AppliedRegionType ?? LabelTemplateScopeHelper.ScopeAll;
dto.AppliedLocationType = scope?.AppliedLocationType ?? template.AppliedLocationType;
dto.PartnerIds = scope?.PartnerIds ?? new List<string>();
dto.CompanyIds = scope?.PartnerIds ?? new List<string>();
dto.RegionIds = scope?.RegionIds ?? new List<string>();
dto.GroupIds = scope?.RegionIds ?? new List<string>();
dto.LocationIds = scope?.LocationIds ?? new List<string>();
dto.AppliedLocationIds = dto.LocationIds;
}
|
a60a45f4
李曜臣
2026-07-21
|
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
|
/// <summary>模板编码唯一键不区分软删:同编码软删行会挡住新建,创建前物理清理关联与主表。</summary>
private async Task PurgeSoftDeletedTemplatesByCodeAsync(string templateCode)
{
var softDeleted = await _dbContext.SqlSugarClient.Queryable<FlLabelTemplateDbEntity>()
.Where(x => x.IsDeleted && x.TemplateCode == templateCode)
.ToListAsync();
if (softDeleted.Count == 0)
{
return;
}
foreach (var row in softDeleted)
{
await _dbContext.SqlSugarClient.Deleteable<FlLabelTemplatePartnerDbEntity>()
.Where(x => x.TemplateId == row.Id)
.ExecuteCommandAsync();
await _dbContext.SqlSugarClient.Deleteable<FlLabelTemplateRegionDbEntity>()
.Where(x => x.TemplateId == row.Id)
.ExecuteCommandAsync();
await _dbContext.SqlSugarClient.Deleteable<FlLabelTemplateLocationDbEntity>()
.Where(x => x.TemplateId == row.Id)
.ExecuteCommandAsync();
await _dbContext.SqlSugarClient.Deleteable<FlLabelTemplateElementDbEntity>()
.Where(x => x.TemplateId == row.Id)
.ExecuteCommandAsync();
await _dbContext.SqlSugarClient.Deleteable<FlLabelTemplateProductDefaultDbEntity>()
.Where(x => x.TemplateId == row.Id)
.ExecuteCommandAsync();
await _dbContext.SqlSugarClient.Deleteable<FlLabelTemplateDbEntity>()
.Where(x => x.Id == row.Id)
.ExecuteCommandAsync();
}
}
|
2893c050
李曜臣
2026-07-13
|
598
599
600
601
|
/// <summary>落库 Contents(列未迁移时 no-op)。</summary>
private Task PersistTemplateContentsAsync(string templateDbId, string contents) =>
LabelTemplateScopeSchemaHelper.SetContentsAsync(_dbContext.SqlSugarClient, templateDbId, contents);
|
59e51671
“wangming”
1
|
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
|
private static string EnsureElementName(string? elementName)
{
var normalizedName = elementName?.Trim();
if (string.IsNullOrWhiteSpace(normalizedName))
{
throw new UserFriendlyException("组件名字不能为空");
}
return normalizedName;
}
private static PagedResultWithPageDto<T> BuildPagedResult<T>(int skipCount, int maxResultCount, int total, List<T> items)
{
var pageSize = maxResultCount <= 0 ? items.Count : maxResultCount;
var pageIndex = pageSize <= 0 ? 1 : PagedQueryConvention.PageIndexFromSkipCount(skipCount);
var totalPages = pageSize <= 0 ? 0 : (int)Math.Ceiling(total / (double)pageSize);
return new PagedResultWithPageDto<T>
{
PageIndex = pageIndex,
PageSize = pageSize,
TotalCount = total,
TotalPages = totalPages,
Items = items
};
}
private static PagedResultWithPageDto<T> BuildPagedResult<T>(int skipCount, int maxResultCount, RefAsync<int> total, List<T> items)
{
var pageSize = maxResultCount <= 0 ? items.Count : maxResultCount;
var pageIndex = pageSize <= 0 ? 1 : PagedQueryConvention.PageIndexFromSkipCount(skipCount);
var totalPages = pageSize <= 0 ? 0 : (int)Math.Ceiling(total.Value / (double)pageSize);
return new PagedResultWithPageDto<T>
{
PageIndex = pageIndex,
PageSize = pageSize,
TotalCount = total.Value,
TotalPages = totalPages,
Items = items
};
}
|
2893c050
李曜臣
2026-07-13
|
642
643
644
645
646
647
|
private static string NormalizeTemplateBorderType(string? raw)
{
var v = (raw ?? string.Empty).Trim().ToLowerInvariant();
return v is "line" or "dotted" ? v : "none";
}
|
59e51671
“wangming”
1
|
648
|
}
|