Blame view

美国版/Food Labeling Management Code/Yi.Abp.Net8/module/food-labeling-us/FoodLabeling.Application/Helpers/LabelTemplateQueryHelper.cs 2.8 KB
14afbc16   李曜臣   2026-6-22
1
2
3
4
5
6
  using FoodLabeling.Application.Services.DbModels;
  using SqlSugar;
  
  namespace FoodLabeling.Application.Helpers;
  
  /// <summary>
7083cd6d   李曜臣   优化代码
7
  /// 标签模板查询列投影:仅映射库内真实列,避免 ORM SELECT 不存在的 scope / border 列。
14afbc16   李曜臣   2026-6-22
8
9
10
  /// </summary>
  public static class LabelTemplateQueryHelper
  {
7083cd6d   李曜臣   优化代码
11
12
13
14
15
16
17
18
19
20
21
22
      private static readonly HashSet<string> AllowedSortFields = new(StringComparer.OrdinalIgnoreCase)
      {
          "TemplateName",
          "TemplateCode",
          "CreationTime",
          "LastModificationTime"
      };
  
      /// <summary>只读查询入口:强制列投影。</summary>
      public static ISugarQueryable<FlLabelTemplateDbEntity> QueryProjected(ISqlSugarClient db) =>
          ProjectListColumns(db.Queryable<FlLabelTemplateDbEntity>());
  
14afbc16   李曜臣   2026-6-22
23
      /// <summary>
7083cd6d   李曜臣   优化代码
24
      /// 列表/详情/引用校验等只读场景:强制 SQL 不含 <c>AppliedPartnerType</c> / <c>AppliedRegionType</c> / <c>BorderType</c>
14afbc16   李曜臣   2026-6-22
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
      /// </summary>
      public static ISugarQueryable<FlLabelTemplateDbEntity> ProjectListColumns(
          ISugarQueryable<FlLabelTemplateDbEntity> query) =>
          query.Select(x => new FlLabelTemplateDbEntity
          {
              Id = x.Id,
              IsDeleted = x.IsDeleted,
              CreationTime = x.CreationTime,
              CreatorId = x.CreatorId,
              LastModifierId = x.LastModifierId,
              LastModificationTime = x.LastModificationTime,
              ConcurrencyStamp = x.ConcurrencyStamp,
              TemplateCode = x.TemplateCode,
              TemplateName = x.TemplateName,
              LabelType = x.LabelType,
              Unit = x.Unit,
              Width = x.Width,
              Height = x.Height,
7083cd6d   李曜臣   优化代码
43
              PrintOrientation = x.PrintOrientation,
14afbc16   李曜臣   2026-6-22
44
45
46
47
48
49
              AppliedLocationType = x.AppliedLocationType,
              ShowRuler = x.ShowRuler,
              ShowGrid = x.ShowGrid,
              VersionNo = x.VersionNo,
              State = x.State
          });
7083cd6d   李曜臣   优化代码
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
  
      /// <summary>列表安全排序:默认 IFNULL(LastModificationTime, CreationTime) DESC</summary>
      public static ISugarQueryable<FlLabelTemplateDbEntity> ApplyListSorting(
          ISugarQueryable<FlLabelTemplateDbEntity> query,
          string? sorting)
      {
          if (string.IsNullOrWhiteSpace(sorting))
          {
              return query.OrderBy("IFNULL(LastModificationTime, CreationTime) desc, TemplateCode asc");
          }
  
          var parts = sorting.Trim().Split(' ', StringSplitOptions.RemoveEmptyEntries);
          if (parts.Length >= 1 && AllowedSortFields.Contains(parts[0]))
          {
              var field = parts[0];
              var dir = parts.Length > 1 && parts[1].Equals("asc", StringComparison.OrdinalIgnoreCase)
                  ? "asc"
                  : "desc";
              return query.OrderBy($"{field} {dir}");
          }
  
          return query.OrderBy("IFNULL(LastModificationTime, CreationTime) desc, TemplateCode asc");
      }
14afbc16   李曜臣   2026-6-22
73
  }