LabelTemplateQueryHelper.cs 2.8 KB
using FoodLabeling.Application.Services.DbModels;
using SqlSugar;

namespace FoodLabeling.Application.Helpers;

/// <summary>
/// 标签模板查询列投影:仅映射库内真实列,避免 ORM SELECT 不存在的 scope / border 列。
/// </summary>
public static class LabelTemplateQueryHelper
{
    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>());

    /// <summary>
    /// 列表/详情/引用校验等只读场景:强制 SQL 不含 <c>AppliedPartnerType</c> / <c>AppliedRegionType</c> / <c>BorderType</c>。
    /// </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,
            PrintOrientation = x.PrintOrientation,
            AppliedLocationType = x.AppliedLocationType,
            ShowRuler = x.ShowRuler,
            ShowGrid = x.ShowGrid,
            VersionNo = x.VersionNo,
            State = x.State
        });

    /// <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");
    }
}