919c1b6a
李曜臣
1
|
1
|
using System.Text.Json;
|
143afd59
杨鑫
打印,标签
|
2
|
using FoodLabeling.Application.Helpers;
|
919c1b6a
李曜臣
1
|
3
4
5
6
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
34
35
36
|
using FoodLabeling.Application.Contracts.Dtos.Common;
using FoodLabeling.Application.Contracts.Dtos.Label;
using FoodLabeling.Application.Contracts.Dtos.LabelTemplate;
using FoodLabeling.Application.Contracts.IServices;
using FoodLabeling.Application.Services.DbModels;
using FoodLabeling.Domain.Entities;
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>
/// 标签管理(一个产品展示多个标签)
/// </summary>
public class LabelAppService : ApplicationService, ILabelAppService
{
private readonly ISqlSugarDbContext _dbContext;
private readonly IGuidGenerator _guidGenerator;
public LabelAppService(ISqlSugarDbContext dbContext, IGuidGenerator guidGenerator)
{
_dbContext = dbContext;
_guidGenerator = guidGenerator;
}
public async Task<PagedResultWithPageDto<LabelGetListOutputDto>> GetListAsync(LabelGetListInputVo input)
{
RefAsync<int> total = 0;
var productId = input.ProductId?.Trim();
|
540ac0e3
杨鑫
前端修改bug
|
37
|
var partnerId = input.PartnerId?.Trim();
|
10fd1324
李曜臣
5-17接口优化
|
38
|
var groupId = input.GroupId?.Trim();
|
919c1b6a
李曜臣
1
|
39
40
41
42
43
44
|
var locationId = input.LocationId?.Trim();
var keyword = input.Keyword?.Trim();
var labelCategoryId = input.LabelCategoryId?.Trim();
var labelTypeId = input.LabelTypeId?.Trim();
var templateCode = input.TemplateCode?.Trim();
|
28dc179d
李曜臣
产品标签关联接口实现
|
45
46
47
48
49
|
// 目标:列表每行是“标签”,同一个标签下的 products 以 “,” 拼接展示
// 因此需要按 label 维度分页(避免 label-product join 导致重复行与分页错乱)。
var labelIdsQuery = _dbContext.SqlSugarClient.Queryable<FlLabelDbEntity>()
.Where(l => !l.IsDeleted)
|
28dc179d
李曜臣
产品标签关联接口实现
|
50
51
52
53
54
55
56
57
58
59
60
61
|
.WhereIF(!string.IsNullOrWhiteSpace(labelCategoryId), l => l.LabelCategoryId == labelCategoryId)
.WhereIF(!string.IsNullOrWhiteSpace(labelTypeId), l => l.LabelTypeId == labelTypeId)
.WhereIF(input.State != null, l => l.State == input.State);
if (!string.IsNullOrWhiteSpace(templateCode))
{
labelIdsQuery = labelIdsQuery
.InnerJoin<FlLabelTemplateDbEntity>((l, tpl) => l.TemplateId == tpl.Id)
.Where((l, tpl) => !tpl.IsDeleted && tpl.TemplateCode == templateCode)
.Select((l, tpl) => l);
}
|
49755ef0
李曜臣
6-12代码优化
|
62
|
var regionSchema = await LabelRegionSchemaHelper.GetStatusAsync(_dbContext.SqlSugarClient);
|
10fd1324
李曜臣
5-17接口优化
|
63
64
|
var scopedLocationIds = await LocationScopeBindingHelper.ResolveScopedLocationIdsAsync(
_dbContext.SqlSugarClient, groupId, locationId);
|
49755ef0
李曜臣
6-12代码优化
|
65
66
67
68
69
70
71
72
73
|
var filterGroupId = groupId?.Trim();
if (!string.IsNullOrWhiteSpace(filterGroupId))
{
labelIdsQuery = scopedLocationIds is { Count: 0 }
? labelIdsQuery.Where(_ => false)
: LabelRegionScopeHelper.ApplyLabelRegionListFilter(
labelIdsQuery, filterGroupId, scopedLocationIds, regionSchema);
}
else if (scopedLocationIds is not null)
|
10fd1324
李曜臣
5-17接口优化
|
74
75
76
77
78
|
{
labelIdsQuery = scopedLocationIds.Count == 0
? labelIdsQuery.Where(_ => false)
: labelIdsQuery.Where(l => scopedLocationIds.Contains(l.LocationId));
}
|
540ac0e3
杨鑫
前端修改bug
|
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
|
else if (!string.IsNullOrWhiteSpace(groupId))
{
labelIdsQuery = LabelRegionScopeHelper.ApplyGroupIdListFilter(labelIdsQuery, groupId);
}
else if (!string.IsNullOrWhiteSpace(partnerId))
{
var partnerLocationIds = await LocationScopeBindingHelper.ResolveFilteredLocationIdsForListAsync(
_dbContext.SqlSugarClient, partnerId, null, null);
if (partnerLocationIds is not null)
{
labelIdsQuery = partnerLocationIds.Count == 0
? labelIdsQuery.Where(_ => false)
: labelIdsQuery.Where(l => partnerLocationIds.Contains(l.LocationId));
}
}
|
10fd1324
李曜臣
5-17接口优化
|
94
|
|
28dc179d
李曜臣
产品标签关联接口实现
|
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
|
// 按产品筛选:存在 label-product 关联即可
if (!string.IsNullOrWhiteSpace(productId))
{
labelIdsQuery = labelIdsQuery
.InnerJoin<FlLabelProductDbEntity>((l, lp) => lp.LabelId == l.Id)
.Where((l, lp) => lp.ProductId == productId)
.Select((l, lp) => l);
}
// 关键字:匹配 labelName/categoryName/typeName/templateName/productName
if (!string.IsNullOrWhiteSpace(keyword))
{
labelIdsQuery = labelIdsQuery
.LeftJoin<FlLabelCategoryDbEntity>((l, c) => l.LabelCategoryId == c.Id)
.LeftJoin<FlLabelTypeDbEntity>((l, c, t) => l.LabelTypeId == t.Id)
.LeftJoin<FlLabelTemplateDbEntity>((l, c, t, tpl) => l.TemplateId == tpl.Id)
.LeftJoin<FlLabelProductDbEntity>((l, c, t, tpl, lp) => lp.LabelId == l.Id)
.LeftJoin<FlProductDbEntity>((l, c, t, tpl, lp, p) => lp.ProductId == p.Id)
.Where((l, c, t, tpl, lp, p) =>
|
919c1b6a
李曜臣
1
|
114
|
l.LabelName.Contains(keyword!) ||
|
28dc179d
李曜臣
产品标签关联接口实现
|
115
116
117
118
119
120
|
(c.CategoryName != null && c.CategoryName.Contains(keyword!)) ||
(t.TypeName != null && t.TypeName.Contains(keyword!)) ||
(tpl.TemplateName != null && tpl.TemplateName.Contains(keyword!)) ||
(p.ProductName != null && p.ProductName.Contains(keyword!)))
.Select((l, c, t, tpl, lp, p) => l);
}
|
919c1b6a
李曜臣
1
|
121
|
|
28dc179d
李曜臣
产品标签关联接口实现
|
122
|
// 排序(优先外部 Sorting,否则按最后编辑倒序)
|
919c1b6a
李曜臣
1
|
123
124
|
if (!string.IsNullOrWhiteSpace(input.Sorting))
{
|
28dc179d
李曜臣
产品标签关联接口实现
|
125
126
127
128
129
|
labelIdsQuery = labelIdsQuery.OrderBy(input.Sorting);
}
else
{
labelIdsQuery = labelIdsQuery.OrderByDescending(l => l.LastModificationTime ?? l.CreationTime);
|
919c1b6a
李曜臣
1
|
130
131
|
}
|
28dc179d
李曜臣
产品标签关联接口实现
|
132
133
134
135
136
137
138
139
|
var pageLabelIds = await labelIdsQuery
.Select(l => l.Id)
.Distinct()
.ToPageListAsync(input.SkipCount, input.MaxResultCount, total);
if (pageLabelIds.Count == 0)
{
return new PagedResultWithPageDto<LabelGetListOutputDto>
|
919c1b6a
李曜臣
1
|
140
|
{
|
28dc179d
李曜臣
产品标签关联接口实现
|
141
142
143
144
145
146
|
PageIndex = 1,
PageSize = input.MaxResultCount,
TotalCount = total,
TotalPages = 0,
Items = new List<LabelGetListOutputDto>()
};
|
919c1b6a
李曜臣
1
|
147
148
|
}
|
28dc179d
李曜臣
产品标签关联接口实现
|
149
150
151
152
153
154
155
|
// 查询标签基础信息(分类/类型/模板)
var labelRows = await _dbContext.SqlSugarClient
.Queryable<FlLabelDbEntity, FlLabelCategoryDbEntity, FlLabelTypeDbEntity, FlLabelTemplateDbEntity>(
(l, c, t, tpl) => l.LabelCategoryId == c.Id && l.LabelTypeId == t.Id && l.TemplateId == tpl.Id)
.Where((l, c, t, tpl) => pageLabelIds.Contains(l.Id))
.Where((l, c, t, tpl) => !l.IsDeleted && !c.IsDeleted && !t.IsDeleted && !tpl.IsDeleted)
.Select((l, c, t, tpl) => new
|
919c1b6a
李曜臣
1
|
156
|
{
|
28dc179d
李曜臣
产品标签关联接口实现
|
157
158
159
160
|
l.Id,
l.LabelCode,
l.LabelName,
l.LocationId,
|
540ac0e3
杨鑫
前端修改bug
|
161
|
l.AppliedRegionType,
|
919c1b6a
李曜臣
1
|
162
|
LabelCategoryName = c.CategoryName,
|
919c1b6a
李曜臣
1
|
163
|
LabelTypeName = t.TypeName,
|
28dc179d
李曜臣
产品标签关联接口实现
|
164
|
TemplateName = tpl.TemplateName,
|
58d2e61c
杨鑫
最新代码
|
165
|
TemplateCode = tpl.TemplateCode,
|
28dc179d
李曜臣
产品标签关联接口实现
|
166
|
l.State,
|
919c1b6a
李曜臣
1
|
167
168
|
LastEdited = l.LastModificationTime ?? l.CreationTime
})
|
28dc179d
李曜臣
产品标签关联接口实现
|
169
170
171
172
173
|
.ToListAsync();
// 按分页顺序输出
var labelMap = labelRows.ToDictionary(x => x.Id, x => x);
var orderedLabels = pageLabelIds.Where(id => labelMap.ContainsKey(id)).Select(id => labelMap[id]).ToList();
|
49755ef0
李曜臣
6-12代码优化
|
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
|
var appliedRegionTypeMap = await LabelRegionSchemaHelper.GetAppliedRegionTypesForLabelsAsync(
_dbContext.SqlSugarClient, pageLabelIds);
var regionScopeMap = new Dictionary<string, LabelRegionScopeHelper.LabelRegionScopeDisplay>(StringComparer.Ordinal);
foreach (var lid in pageLabelIds)
{
if (!labelMap.TryGetValue(lid, out var row))
{
continue;
}
appliedRegionTypeMap.TryGetValue(lid, out var appliedType);
regionScopeMap[lid] = await LabelRegionScopeHelper.BuildScopeDisplayAsync(
_dbContext.SqlSugarClient,
lid,
appliedType,
row.LocationId);
}
|
28dc179d
李曜臣
产品标签关联接口实现
|
191
192
193
|
// 查询 products 并拼接
var productRows = await _dbContext.SqlSugarClient
|
536d25c4
李曜臣
打印预览,产品分类接口实现
|
194
195
196
197
198
199
200
|
.Queryable<FlLabelProductDbEntity>()
.InnerJoin<FlLabelDbEntity>((lp, l) => lp.LabelId == l.Id)
.InnerJoin<FlProductDbEntity>((lp, l, p) => lp.ProductId == p.Id)
.LeftJoin<FlProductCategoryDbEntity>((lp, l, p, pc) => p.CategoryId == pc.Id)
.Where((lp, l, p, pc) => pageLabelIds.Contains(lp.LabelId))
.Where((lp, l, p, pc) => !l.IsDeleted && !p.IsDeleted)
.Select((lp, l, p, pc) => new { lp.LabelId, p.ProductName, ProductCategoryName = pc.CategoryName })
|
28dc179d
李曜臣
产品标签关联接口实现
|
201
|
.ToListAsync();
|
919c1b6a
李曜臣
1
|
202
|
|
28dc179d
李曜臣
产品标签关联接口实现
|
203
204
205
206
207
208
209
|
var productsMap = productRows
.GroupBy(x => x.LabelId)
.ToDictionary(
g => g.Key,
g => new
{
Products = string.Join(",", g.Select(x => x.ProductName ?? string.Empty).Select(x => x.Trim()).Where(x => !string.IsNullOrWhiteSpace(x)).Distinct()),
|
536d25c4
李曜臣
打印预览,产品分类接口实现
|
210
|
ProductCategoryName = string.Join(",", g.Select(x => x.ProductCategoryName ?? string.Empty).Select(x => x.Trim()).Where(x => !string.IsNullOrWhiteSpace(x)).Distinct())
|
28dc179d
李曜臣
产品标签关联接口实现
|
211
|
});
|
919c1b6a
李曜臣
1
|
212
|
|
28dc179d
李曜臣
产品标签关联接口实现
|
213
|
var locationIds = orderedLabels
|
919c1b6a
李曜臣
1
|
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
|
.Select(x => x.LocationId)
.Where(x => !string.IsNullOrWhiteSpace(x))
.Select(x => x!.Trim())
.Distinct()
.ToList();
var locationMap = new Dictionary<string, LocationAggregateRoot>();
if (locationIds.Count > 0)
{
var locGuids = locationIds.Where(x => Guid.TryParse(x, out _)).Select(Guid.Parse).ToList();
if (locGuids.Count > 0)
{
var locs = await _dbContext.SqlSugarClient.Queryable<LocationAggregateRoot>()
.Where(x => !x.IsDeleted)
.Where(x => locGuids.Contains(x.Id))
.ToListAsync();
locationMap = locs.ToDictionary(x => x.Id.ToString(), x => x);
}
}
|
540ac0e3
杨鑫
前端修改bug
|
234
235
236
237
|
var regionScopeMap = await LabelRegionScopeHelper.BuildRegionScopeMapAsync(
_dbContext.SqlSugarClient,
orderedLabels.Select(x => (x.Id, x.AppliedRegionType ?? LabelRegionScopeHelper.AppliedTypeSpecified)).ToList());
|
28dc179d
李曜臣
产品标签关联接口实现
|
238
|
var items = orderedLabels.Select(x =>
|
919c1b6a
李曜臣
1
|
239
240
241
242
243
244
|
{
var locationName = string.Empty;
if (!string.IsNullOrWhiteSpace(x.LocationId) && locationMap.TryGetValue(x.LocationId!, out var loc))
{
locationName = loc.LocationName ?? loc.LocationCode;
}
|
28dc179d
李曜臣
产品标签关联接口实现
|
245
246
|
var products = productsMap.TryGetValue(x.Id, out var prod) ? prod.Products : string.Empty;
var productCategoryNameValue = productsMap.TryGetValue(x.Id, out var prod2) ? prod2.ProductCategoryName : string.Empty;
|
49755ef0
李曜臣
6-12代码优化
|
247
248
|
regionScopeMap.TryGetValue(x.Id, out var regionScope);
appliedRegionTypeMap.TryGetValue(x.Id, out var appliedRegionType);
|
919c1b6a
李曜臣
1
|
249
250
251
252
253
|
return new LabelGetListOutputDto
{
Id = x.LabelCode ?? string.Empty,
LabelName = x.LabelName ?? string.Empty,
LocationName = string.IsNullOrWhiteSpace(locationName) ? "无" : locationName,
|
49755ef0
李曜臣
6-12代码优化
|
254
255
256
257
258
259
|
Region = regionScope?.Region ?? "无",
RegionIds = regionScope?.RegionIds ?? new List<string>(),
GroupIds = regionScope?.GroupIds ?? new List<string>(),
AppliedRegionType = string.IsNullOrWhiteSpace(appliedRegionType)
? LabelRegionScopeHelper.AppliedRegionSpecified
: appliedRegionType.Trim(),
|
919c1b6a
李曜臣
1
|
260
|
LabelCategoryName = x.LabelCategoryName ?? string.Empty,
|
28dc179d
李曜臣
产品标签关联接口实现
|
261
262
|
ProductCategoryName = string.IsNullOrWhiteSpace(productCategoryNameValue) ? "无" : productCategoryNameValue,
Products = products,
|
919c1b6a
李曜臣
1
|
263
|
TemplateName = x.TemplateName ?? string.Empty,
|
58d2e61c
杨鑫
最新代码
|
264
|
TemplateCode = x.TemplateCode ?? string.Empty,
|
919c1b6a
李曜臣
1
|
265
266
267
268
269
270
271
272
|
LabelTypeName = x.LabelTypeName ?? string.Empty,
State = x.State,
LastEdited = x.LastEdited,
HasError = false
};
}).ToList();
var pageSize = input.MaxResultCount <= 0 ? items.Count : input.MaxResultCount;
|
143afd59
杨鑫
打印,标签
|
273
|
var pageIndex = pageSize <= 0 ? 1 : PagedQueryConvention.PageIndexFromSkipCount(input.SkipCount);
|
919c1b6a
李曜臣
1
|
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
|
var totalPages = pageSize <= 0 ? 0 : (int)Math.Ceiling(total / (double)pageSize);
return new PagedResultWithPageDto<LabelGetListOutputDto>
{
PageIndex = pageIndex,
PageSize = pageSize,
TotalCount = total,
TotalPages = totalPages,
Items = items
};
}
public async Task<LabelGetOutputDto> GetAsync(string id)
{
var labelCode = id?.Trim();
if (string.IsNullOrWhiteSpace(labelCode))
{
throw new UserFriendlyException("标签Code不能为空");
}
var label = await _dbContext.SqlSugarClient.Queryable<FlLabelDbEntity>()
.FirstAsync(x => !x.IsDeleted && x.LabelCode == labelCode);
if (label is null)
{
throw new UserFriendlyException("标签不存在");
}
var template = await _dbContext.SqlSugarClient.Queryable<FlLabelTemplateDbEntity>()
.FirstAsync(x => x.Id == label.TemplateId);
var category = await _dbContext.SqlSugarClient.Queryable<FlLabelCategoryDbEntity>()
.FirstAsync(x => x.Id == label.LabelCategoryId);
var type = await _dbContext.SqlSugarClient.Queryable<FlLabelTypeDbEntity>()
.FirstAsync(x => x.Id == label.LabelTypeId);
LocationAggregateRoot? location = null;
if (!string.IsNullOrWhiteSpace(label.LocationId) && Guid.TryParse(label.LocationId, out var locId))
{
location = await _dbContext.SqlSugarClient.Queryable<LocationAggregateRoot>()
.FirstAsync(x => !x.IsDeleted && x.Id == locId);
}
var productIds = await _dbContext.SqlSugarClient.Queryable<FlLabelProductDbEntity>()
.Where(x => x.LabelId == label.Id)
.Select(x => x.ProductId)
.ToListAsync();
object? labelInfo = null;
if (!string.IsNullOrWhiteSpace(label.LabelInfoJson))
{
labelInfo = JsonSerializer.Deserialize<object>(label.LabelInfoJson);
}
|
10fd1324
李曜臣
5-17接口优化
|
326
327
328
329
330
331
|
var locationId = label.LocationId ?? string.Empty;
var locationIdList = string.IsNullOrWhiteSpace(locationId)
? new List<string>()
: new List<string> { locationId.Trim() };
var partnerIds = await LocationScopeBindingHelper.ResolvePartnerIdsFromLocationIdsAsync(
_dbContext.SqlSugarClient, locationIdList);
|
49755ef0
李曜臣
6-12代码优化
|
332
333
334
|
var appliedRegionType = await LabelRegionSchemaHelper.GetAppliedRegionTypeForLabelAsync(
_dbContext.SqlSugarClient, label.Id);
var regionScope = await LabelRegionScopeHelper.BuildScopeDisplayAsync(
|
540ac0e3
杨鑫
前端修改bug
|
335
|
_dbContext.SqlSugarClient,
|
49755ef0
李曜臣
6-12代码优化
|
336
337
338
|
label.Id,
appliedRegionType,
label.LocationId);
|
10fd1324
李曜臣
5-17接口优化
|
339
|
|
919c1b6a
李曜臣
1
|
340
341
|
return new LabelGetOutputDto
{
|
8e0c49eb
李曜臣
产品标签类别优化;
|
342
|
Id = label.LabelCode ?? string.Empty,
|
919c1b6a
李曜臣
1
|
343
|
LabelName = label.LabelName,
|
10fd1324
李曜臣
5-17接口优化
|
344
|
LocationId = locationId,
|
919c1b6a
李曜臣
1
|
345
|
LocationName = location?.LocationName ?? location?.LocationCode ?? "无",
|
540ac0e3
杨鑫
前端修改bug
|
346
347
|
AppliedRegionType = scope.AppliedRegionType,
Region = scope.Region,
|
10fd1324
李曜臣
5-17接口优化
|
348
349
|
PartnerId = partnerIds.Count > 0 ? partnerIds[0] : null,
PartnerIds = partnerIds,
|
49755ef0
李曜臣
6-12代码优化
|
350
351
352
353
|
AppliedRegionType = appliedRegionType,
Region = regionScope.Region,
RegionIds = regionScope.RegionIds,
GroupIds = regionScope.GroupIds,
|
919c1b6a
李曜臣
1
|
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
|
LabelCategoryId = label.LabelCategoryId ?? string.Empty,
LabelCategoryName = category?.CategoryName ?? "无",
LabelTypeId = label.LabelTypeId ?? string.Empty,
LabelTypeName = type?.TypeName ?? "无",
TemplateCode = template?.TemplateCode ?? string.Empty,
TemplateName = template?.TemplateName ?? string.Empty,
State = label.State,
LabelInfoJson = labelInfo,
ProductIds = productIds
};
}
[UnitOfWork]
public async Task<LabelGetOutputDto> CreateAsync(LabelCreateInputVo input)
{
var labelCode = input.LabelCode?.Trim();
if (string.IsNullOrWhiteSpace(labelCode))
{
labelCode = $"LBL_{_guidGenerator.Create():N}";
}
var labelName = input.LabelName?.Trim();
if (string.IsNullOrWhiteSpace(labelName))
{
throw new UserFriendlyException("标签名称不能为空");
}
if (input.ProductIds is null || input.ProductIds.Count == 0)
{
throw new UserFriendlyException("标签至少需要绑定一个产品");
}
if (string.IsNullOrWhiteSpace(input.TemplateCode))
{
throw new UserFriendlyException("模板编码不能为空");
}
|
919c1b6a
李曜臣
1
|
390
391
392
393
394
395
396
397
398
|
if (string.IsNullOrWhiteSpace(input.LabelCategoryId))
{
throw new UserFriendlyException("标签分类Id不能为空");
}
if (string.IsNullOrWhiteSpace(input.LabelTypeId))
{
throw new UserFriendlyException("标签类型Id不能为空");
}
|
49755ef0
李曜臣
6-12代码优化
|
399
|
var scope = await LabelRegionScopeHelper.ResolveScopeForSaveAsync(
|
540ac0e3
杨鑫
前端修改bug
|
400
401
402
403
404
|
_dbContext.SqlSugarClient,
input.AppliedRegionType,
input.RegionIds,
input.GroupIds,
input.LocationId,
|
49755ef0
李曜臣
6-12代码优化
|
405
|
input.LocationIds);
|
10fd1324
李曜臣
5-17接口优化
|
406
|
|
919c1b6a
李曜臣
1
|
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
|
var template = await _dbContext.SqlSugarClient.Queryable<FlLabelTemplateDbEntity>()
.FirstAsync(x => !x.IsDeleted && x.TemplateCode == input.TemplateCode.Trim());
if (template is null)
{
throw new UserFriendlyException("模板不存在");
}
// 唯一性校验(LabelCode)
var exists = await _dbContext.SqlSugarClient.Queryable<FlLabelDbEntity>()
.AnyAsync(x => !x.IsDeleted && x.LabelCode == labelCode);
if (exists)
{
throw new UserFriendlyException("标签编码已存在");
}
// 插入 fl_label
var now = DateTime.Now;
var labelId = _guidGenerator.Create().ToString();
var labelEntity = new FlLabelDbEntity
{
Id = labelId,
IsDeleted = false,
CreationTime = now,
CreatorId = CurrentUser?.Id?.ToString(),
LastModifierId = CurrentUser?.Id?.ToString(),
LastModificationTime = now,
ConcurrencyStamp = string.Empty,
LabelCode = labelCode,
LabelName = labelName,
TemplateId = template.Id,
|
49755ef0
李曜臣
6-12代码优化
|
437
|
LocationId = scope.LocationId,
|
919c1b6a
李曜臣
1
|
438
439
440
441
442
443
444
|
LabelCategoryId = input.LabelCategoryId?.Trim(),
LabelTypeId = input.LabelTypeId?.Trim(),
State = input.State,
LabelType = null,
LabelInfoJson = input.LabelInfoJson == null ? null : JsonSerializer.Serialize(input.LabelInfoJson)
};
await _dbContext.SqlSugarClient.Insertable(labelEntity).ExecuteCommandAsync();
|
49755ef0
李曜臣
6-12代码优化
|
445
446
447
448
449
450
451
452
453
454
|
await LabelRegionSchemaHelper.SetAppliedRegionTypeAsync(
_dbContext.SqlSugarClient, labelId, scope.AppliedRegionType);
await LabelRegionScopeHelper.SaveLabelRegionsAsync(
_dbContext.SqlSugarClient,
_guidGenerator,
labelId,
scope.AppliedRegionType,
scope.RegionIds,
CurrentUser?.Id?.ToString(),
now);
|
919c1b6a
李曜臣
1
|
455
456
457
458
459
460
461
462
463
464
465
|
// 插入 fl_label_product
var productIds = input.ProductIds.Where(x => !string.IsNullOrWhiteSpace(x)).Select(x => x.Trim()).Distinct().ToList();
var rows = productIds.Select(pid => new FlLabelProductDbEntity
{
Id = _guidGenerator.Create().ToString(),
LabelId = labelId,
ProductId = pid
}).ToList();
await _dbContext.SqlSugarClient.Insertable(rows).ExecuteCommandAsync();
|
540ac0e3
杨鑫
前端修改bug
|
466
467
468
469
470
471
472
473
474
|
await LabelRegionScopeHelper.SaveRegionIdsAsync(
_dbContext.SqlSugarClient,
labelId,
resolvedScope.AppliedRegionType,
resolvedScope.RegionIds,
CurrentUser?.Id?.ToString(),
now,
() => _guidGenerator.Create().ToString());
|
10fd1324
李曜臣
5-17接口优化
|
475
476
477
|
await LabelCategoryAppService.TouchLabelCategoryLastEditedAsync(
_dbContext, labelEntity.LabelCategoryId, CurrentUser?.Id?.ToString());
|
919c1b6a
李曜臣
1
|
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
|
return await GetAsync(labelCode);
}
[UnitOfWork]
public async Task<LabelGetOutputDto> UpdateAsync(string id, LabelUpdateInputVo input)
{
var labelCode = id?.Trim();
if (string.IsNullOrWhiteSpace(labelCode))
{
throw new UserFriendlyException("标签Code不能为空");
}
var label = await _dbContext.SqlSugarClient.Queryable<FlLabelDbEntity>()
.FirstAsync(x => !x.IsDeleted && x.LabelCode == labelCode);
if (label is null)
{
throw new UserFriendlyException("标签不存在");
}
if (input.ProductIds is null || input.ProductIds.Count == 0)
{
throw new UserFriendlyException("标签至少需要绑定一个产品");
}
if (string.IsNullOrWhiteSpace(input.TemplateCode))
{
throw new UserFriendlyException("模板编码不能为空");
}
|
919c1b6a
李曜臣
1
|
506
507
508
509
510
511
512
513
514
|
if (string.IsNullOrWhiteSpace(input.LabelCategoryId))
{
throw new UserFriendlyException("标签分类Id不能为空");
}
if (string.IsNullOrWhiteSpace(input.LabelTypeId))
{
throw new UserFriendlyException("标签类型Id不能为空");
}
|
49755ef0
李曜臣
6-12代码优化
|
515
|
var scope = await LabelRegionScopeHelper.ResolveScopeForSaveAsync(
|
540ac0e3
杨鑫
前端修改bug
|
516
517
518
519
520
|
_dbContext.SqlSugarClient,
input.AppliedRegionType,
input.RegionIds,
input.GroupIds,
input.LocationId,
|
49755ef0
李曜臣
6-12代码优化
|
521
|
input.LocationIds);
|
10fd1324
李曜臣
5-17接口优化
|
522
|
|
919c1b6a
李曜臣
1
|
523
524
525
526
527
528
529
|
var template = await _dbContext.SqlSugarClient.Queryable<FlLabelTemplateDbEntity>()
.FirstAsync(x => !x.IsDeleted && x.TemplateCode == input.TemplateCode.Trim());
if (template is null)
{
throw new UserFriendlyException("模板不存在");
}
|
10fd1324
李曜臣
5-17接口优化
|
530
|
var oldCategoryId = label.LabelCategoryId;
|
919c1b6a
李曜臣
1
|
531
532
533
|
var now = DateTime.Now;
label.LabelName = input.LabelName?.Trim() ?? label.LabelName;
label.TemplateId = template.Id;
|
49755ef0
李曜臣
6-12代码优化
|
534
|
label.LocationId = scope.LocationId;
|
919c1b6a
李曜臣
1
|
535
536
537
538
539
540
541
542
|
label.LabelCategoryId = input.LabelCategoryId?.Trim();
label.LabelTypeId = input.LabelTypeId?.Trim();
label.State = input.State;
label.LastModifierId = CurrentUser?.Id?.ToString();
label.LastModificationTime = now;
label.LabelInfoJson = input.LabelInfoJson == null ? null : JsonSerializer.Serialize(input.LabelInfoJson);
await _dbContext.SqlSugarClient.Updateable(label).ExecuteCommandAsync();
|
49755ef0
李曜臣
6-12代码优化
|
543
544
545
546
547
548
549
550
551
552
|
await LabelRegionSchemaHelper.SetAppliedRegionTypeAsync(
_dbContext.SqlSugarClient, label.Id, scope.AppliedRegionType);
await LabelRegionScopeHelper.SaveLabelRegionsAsync(
_dbContext.SqlSugarClient,
_guidGenerator,
label.Id,
scope.AppliedRegionType,
scope.RegionIds,
CurrentUser?.Id?.ToString(),
now);
|
919c1b6a
李曜臣
1
|
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
|
// 重建关联
await _dbContext.SqlSugarClient.Deleteable<FlLabelProductDbEntity>()
.Where(x => x.LabelId == label.Id)
.ExecuteCommandAsync();
var productIds = input.ProductIds.Where(x => !string.IsNullOrWhiteSpace(x)).Select(x => x.Trim()).Distinct().ToList();
var rows = productIds.Select(pid => new FlLabelProductDbEntity
{
Id = _guidGenerator.Create().ToString(),
LabelId = label.Id,
ProductId = pid
}).ToList();
await _dbContext.SqlSugarClient.Insertable(rows).ExecuteCommandAsync();
|
540ac0e3
杨鑫
前端修改bug
|
568
569
570
571
572
573
574
575
576
|
await LabelRegionScopeHelper.SaveRegionIdsAsync(
_dbContext.SqlSugarClient,
label.Id,
resolvedScope.AppliedRegionType,
resolvedScope.RegionIds,
CurrentUser?.Id?.ToString(),
now,
() => _guidGenerator.Create().ToString());
|
10fd1324
李曜臣
5-17接口优化
|
577
578
579
580
581
582
583
584
585
|
var newCategoryId = label.LabelCategoryId;
await LabelCategoryAppService.TouchLabelCategoryLastEditedAsync(
_dbContext, newCategoryId, CurrentUser?.Id?.ToString());
if (!string.Equals(oldCategoryId, newCategoryId, StringComparison.Ordinal))
{
await LabelCategoryAppService.TouchLabelCategoryLastEditedAsync(
_dbContext, oldCategoryId, CurrentUser?.Id?.ToString());
}
|
919c1b6a
李曜臣
1
|
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
|
return await GetAsync(labelCode);
}
[UnitOfWork]
public async Task DeleteAsync(string id)
{
var labelCode = id?.Trim();
if (string.IsNullOrWhiteSpace(labelCode))
{
return;
}
var label = await _dbContext.SqlSugarClient.Queryable<FlLabelDbEntity>()
.FirstAsync(x => !x.IsDeleted && x.LabelCode == labelCode);
if (label is null)
{
return;
}
label.IsDeleted = true;
label.LastModifierId = CurrentUser?.Id?.ToString();
label.LastModificationTime = DateTime.Now;
await _dbContext.SqlSugarClient.Updateable(label).ExecuteCommandAsync();
|
49755ef0
李曜臣
6-12代码优化
|
610
611
612
613
614
615
616
617
|
var regionSchema = await LabelRegionSchemaHelper.GetStatusAsync(_dbContext.SqlSugarClient);
if (regionSchema.HasLabelRegionTable)
{
await _dbContext.SqlSugarClient.Deleteable<FlLabelRegionDbEntity>()
.Where(x => x.LabelId == label.Id)
.ExecuteCommandAsync();
}
|
919c1b6a
李曜臣
1
|
618
619
620
|
await _dbContext.SqlSugarClient.Deleteable<FlLabelProductDbEntity>()
.Where(x => x.LabelId == label.Id)
.ExecuteCommandAsync();
|
10fd1324
李曜臣
5-17接口优化
|
621
|
|
540ac0e3
杨鑫
前端修改bug
|
622
623
624
625
|
await _dbContext.SqlSugarClient.Deleteable<FlLabelRegionDbEntity>()
.Where(x => x.LabelId == label.Id)
.ExecuteCommandAsync();
|
10fd1324
李曜臣
5-17接口优化
|
626
627
|
await LabelCategoryAppService.TouchLabelCategoryLastEditedAsync(
_dbContext, label.LabelCategoryId, CurrentUser?.Id?.ToString());
|
919c1b6a
李曜臣
1
|
628
629
630
631
632
633
634
635
636
637
638
639
640
641
|
}
/// <summary>
/// 标签预览:不落库,只把 template elements 的 AUTO_DB/PRINT_INPUT 渲染进 config
/// </summary>
[UnitOfWork]
public async Task<LabelTemplatePreviewDto> PreviewAsync(LabelPreviewResolveInputVo input)
{
var labelCode = input?.LabelCode?.Trim();
if (string.IsNullOrWhiteSpace(labelCode))
{
throw new UserFriendlyException("labelCode不能为空");
}
|
536d25c4
李曜臣
打印预览,产品分类接口实现
|
642
643
|
var baseTime = input?.BaseTime ?? DateTime.Now;
|
919c1b6a
李曜臣
1
|
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
|
var label = await _dbContext.SqlSugarClient.Queryable<FlLabelDbEntity>()
.FirstAsync(x => !x.IsDeleted && x.LabelCode == labelCode);
if (label is null)
{
throw new UserFriendlyException("标签不存在");
}
// 选择预览产品
var productId = input?.ProductId?.Trim();
if (string.IsNullOrWhiteSpace(productId))
{
productId = await _dbContext.SqlSugarClient.Queryable<FlLabelProductDbEntity>()
.Where(x => x.LabelId == label.Id)
.Select(x => x.ProductId)
.FirstAsync();
}
if (string.IsNullOrWhiteSpace(productId))
{
throw new UserFriendlyException("该标签未绑定产品,无法预览");
}
var product = await _dbContext.SqlSugarClient.Queryable<FlProductDbEntity>()
.FirstAsync(x => !x.IsDeleted && x.Id == productId);
if (product is null)
{
throw new UserFriendlyException("预览产品不存在");
}
// 取模板头 & elements
var template = await _dbContext.SqlSugarClient.Queryable<FlLabelTemplateDbEntity>()
.FirstAsync(x => !x.IsDeleted && x.Id == label.TemplateId);
if (template is null)
{
throw new UserFriendlyException("模板不存在");
}
var elements = await _dbContext.SqlSugarClient.Queryable<FlLabelTemplateElementDbEntity>()
.Where(x => x.TemplateId == template.Id)
// SqlSugar 不提供 ThenBy,这里用组合排序键保证 OrderNum + ZIndex 的稳定顺序
.OrderBy(x => x.OrderNum * 1000000 + x.ZIndex)
.ToListAsync();
// 解析 labelInfo(可用于补充一些数值)
Dictionary<string, object?> labelInfoDict = new();
if (!string.IsNullOrWhiteSpace(label.LabelInfoJson))
{
try
{
labelInfoDict = JsonSerializer.Deserialize<Dictionary<string, object?>>(label.LabelInfoJson)
?? new Dictionary<string, object?>();
}
catch
{
// ignore
}
}
var printInputJson = input?.PrintInputJson ?? new Dictionary<string, object?>();
static int TryGetInt(object? v, int defaultValue)
{
if (v is null) return defaultValue;
if (v is int i) return i;
if (v is long l) return (int)l;
if (v is decimal d) return (int)d;
if (v is double db) return (int)db;
if (v is JsonElement je)
{
if (je.ValueKind == JsonValueKind.Number && je.TryGetInt32(out var ii)) return ii;
if (je.ValueKind == JsonValueKind.String && int.TryParse(je.GetString(), out var si)) return si;
}
if (v is string s && int.TryParse(s, out var st)) return st;
return defaultValue;
}
static string? TryToString(object? v)
{
if (v is null) return null;
if (v is string s) return s;
if (v is JsonElement je)
{
if (je.ValueKind == JsonValueKind.String) return je.GetString();
if (je.ValueKind == JsonValueKind.Number || je.ValueKind == JsonValueKind.True || je.ValueKind == JsonValueKind.False)
{
return je.ToString();
}
}
return v.ToString();
}
static void UpsertConfigValue(Dictionary<string, object?> cfg, string key, object? value)
{
if (cfg.ContainsKey(key))
{
cfg[key] = value;
return;
}
cfg.Add(key, value);
}
Dictionary<string, object?> ParseConfig(string? configJson)
{
if (string.IsNullOrWhiteSpace(configJson))
{
return new Dictionary<string, object?>();
}
try
{
return JsonSerializer.Deserialize<Dictionary<string, object?>>(configJson)
?? new Dictionary<string, object?>();
}
catch
{
return new Dictionary<string, object?>();
}
}
|
49755ef0
李曜臣
6-12代码优化
|
763
764
765
766
|
var locationId = input?.LocationId?.Trim();
var hasCompanyAutoElement = elements.Any(PartnerCompanyDisplayHelper.IsCompanyAutoElement);
FlPartnerDbEntity? partnerForCompany = null;
if (hasCompanyAutoElement)
|
919c1b6a
李曜臣
1
|
767
|
{
|
49755ef0
李曜臣
6-12代码优化
|
768
769
770
771
772
773
774
|
if (string.IsNullOrWhiteSpace(locationId))
{
throw new UserFriendlyException("预览/打印需要 locationId 以填充 Company 信息");
}
partnerForCompany = await PartnerCompanyDisplayHelper.ResolvePartnerByLocationIdAsync(
_dbContext.SqlSugarClient, locationId);
|
919c1b6a
李曜臣
1
|
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
|
}
var resolvedElements = elements.Select(el =>
{
var cfg = ParseConfig(el.ConfigJson);
// 1) 先做 AUTO_DB:按类型兜底填充
if (string.Equals(el.ValueSourceType, "AUTO_DB", StringComparison.OrdinalIgnoreCase))
{
switch (el.ElementType)
{
case "TEXT_PRODUCT":
UpsertConfigValue(cfg, "text", product.ProductName);
break;
case "TEXT_PRICE":
{
// price 可能在 labelInfo 或 printInput 里
object? priceObj = null;
if (labelInfoDict.TryGetValue("price", out var p)) priceObj = p;
if (priceObj is null && printInputJson.TryGetValue("price", out var pp)) priceObj = pp;
var priceStr = TryToString(priceObj);
if (!string.IsNullOrWhiteSpace(priceStr))
{
UpsertConfigValue(cfg, "text", priceStr);
}
}
break;
case "BARCODE":
if (!string.IsNullOrWhiteSpace(product.ProductCode))
{
UpsertConfigValue(cfg, "data", product.ProductCode);
}
break;
case "QRCODE":
if (!string.IsNullOrWhiteSpace(product.ProductCode))
{
UpsertConfigValue(cfg, "data", product.ProductCode);
}
break;
case "DATE":
|
919c1b6a
李曜臣
1
|
815
|
case "TIME":
|
540ac0e3
杨鑫
前端修改bug
|
816
|
// 日期/时间在 App/Web 端按 BaseTime 实时解析;勿把首屏时刻写入 config 以免预览/出纸过期。
|
919c1b6a
李曜臣
1
|
817
|
break;
|
49755ef0
李曜臣
6-12代码优化
|
818
819
820
821
822
823
824
825
826
827
|
case "TEXT_STATIC":
if (PartnerCompanyDisplayHelper.IsCompanyAutoElement(el.ValueSourceType, el.TypeAdd, el.ElementType)
&& partnerForCompany is not null)
{
var includes = PartnerCompanyDisplayHelper.ParseIncludeFields(cfg);
var companyText = PartnerCompanyDisplayHelper.FormatDisplayText(partnerForCompany, includes);
UpsertConfigValue(cfg, "text", companyText);
}
break;
|
919c1b6a
李曜臣
1
|
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
|
}
}
// 2) 再做 PRINT_INPUT:按 InputKey 覆盖(如果前端传了)
if (string.Equals(el.ValueSourceType, "PRINT_INPUT", StringComparison.OrdinalIgnoreCase)
&& !string.IsNullOrWhiteSpace(el.InputKey)
&& printInputJson.TryGetValue(el.InputKey!, out var inputValue))
{
var s = TryToString(inputValue);
if (!string.IsNullOrWhiteSpace(s))
{
switch (el.ElementType)
{
case "TEXT_STATIC":
case "TEXT_PRODUCT":
case "TEXT_PRICE":
UpsertConfigValue(cfg, "text", s);
cfg.Remove("inputType");
break;
case "BARCODE":
case "QRCODE":
UpsertConfigValue(cfg, "data", s);
break;
case "DATE":
case "TIME":
UpsertConfigValue(cfg, "format", s);
cfg.Remove("inputType");
break;
}
}
}
// 3) FIXED:不做任何覆盖,保持模板/标签设计时写入的固定值(由前端/设计器落库)
return new LabelTemplateElementDto
{
Id = el.ElementKey,
ElementType = el.ElementType,
|
58d2e61c
杨鑫
最新代码
|
866
|
TypeAdd = el.TypeAdd,
|
a4baaa73
李曜臣
模板与产品关联实现
|
867
|
ElementName = el.ElementName,
|
919c1b6a
李曜臣
1
|
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
|
PosX = el.PosX,
PosY = el.PosY,
Width = el.Width,
Height = el.Height,
Rotation = string.IsNullOrWhiteSpace(el.Rotation) ? "horizontal" : el.Rotation,
BorderType = string.IsNullOrWhiteSpace(el.BorderType) ? "none" : el.BorderType,
ZIndex = el.ZIndex,
OrderNum = el.OrderNum,
ValueSourceType = el.ValueSourceType,
BindingExpr = el.BindingExpr,
AutoQueryKey = el.AutoQueryKey,
InputKey = el.InputKey,
IsRequiredInput = el.IsRequiredInput,
ConfigJson = cfg
};
}).ToList();
return new LabelTemplatePreviewDto
{
Id = template.TemplateCode,
Name = template.TemplateName,
LabelType = template.LabelType ?? string.Empty,
Unit = template.Unit,
Width = template.Width,
Height = template.Height,
AppliedLocation = template.AppliedLocationType,
ShowRuler = template.ShowRuler,
ShowGrid = template.ShowGrid,
|
540ac0e3
杨鑫
前端修改bug
|
896
|
Border = NormalizeTemplateBorderType(template.BorderType),
|
919c1b6a
李曜臣
1
|
897
898
899
|
Elements = resolvedElements
};
}
|
10fd1324
李曜臣
5-17接口优化
|
900
|
|
919c1b6a
李曜臣
1
|
901
|
}
|