20261030 #18
@ -54,4 +54,14 @@ public class KpiAreaInfoListInput
|
|||||||
/// 截止邮编(模糊)。
|
/// 截止邮编(模糊)。
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public string? RightPostcode { get; set; }
|
public string? RightPostcode { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 排序字段:leftPostcode / rightPostcode;空则按 Id 降序。
|
||||||
|
/// </summary>
|
||||||
|
public string? SortField { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 排序方向:asc / desc。
|
||||||
|
/// </summary>
|
||||||
|
public string? SortOrder { get; set; }
|
||||||
}
|
}
|
||||||
|
|||||||
@ -116,6 +116,12 @@ public class KpiAreaService : ApplicationService
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static readonly HashSet<string> InfoSortFieldWhitelist = new(StringComparer.OrdinalIgnoreCase)
|
||||||
|
{
|
||||||
|
"leftPostcode",
|
||||||
|
"rightPostcode",
|
||||||
|
};
|
||||||
|
|
||||||
private static KpiAreaInfoQueryFilter BuildInfoQueryFilter(KpiAreaInfoListInput input) =>
|
private static KpiAreaInfoQueryFilter BuildInfoQueryFilter(KpiAreaInfoListInput input) =>
|
||||||
new()
|
new()
|
||||||
{
|
{
|
||||||
@ -125,9 +131,38 @@ public class KpiAreaService : ApplicationService
|
|||||||
Province = input.Province,
|
Province = input.Province,
|
||||||
City = input.City,
|
City = input.City,
|
||||||
LeftPostcode = input.LeftPostcode,
|
LeftPostcode = input.LeftPostcode,
|
||||||
RightPostcode = input.RightPostcode
|
RightPostcode = input.RightPostcode,
|
||||||
|
SortField = NormalizeInfoSortField(input.SortField),
|
||||||
|
SortOrder = NormalizeInfoSortOrder(input.SortOrder),
|
||||||
};
|
};
|
||||||
|
|
||||||
|
private static string? NormalizeInfoSortField(string? sortField)
|
||||||
|
{
|
||||||
|
var field = sortField?.Trim();
|
||||||
|
if (string.IsNullOrWhiteSpace(field) || !InfoSortFieldWhitelist.Contains(field))
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return field;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static string? NormalizeInfoSortOrder(string? sortOrder)
|
||||||
|
{
|
||||||
|
var order = sortOrder?.Trim();
|
||||||
|
if (string.Equals(order, "asc", StringComparison.OrdinalIgnoreCase))
|
||||||
|
{
|
||||||
|
return "asc";
|
||||||
|
}
|
||||||
|
|
||||||
|
if (string.Equals(order, "desc", StringComparison.OrdinalIgnoreCase))
|
||||||
|
{
|
||||||
|
return "desc";
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 新增KPI分区明细。
|
/// 新增KPI分区明细。
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@ -15,6 +15,12 @@ public class KpiAreaInfoQueryFilter
|
|||||||
public string? City { get; set; }
|
public string? City { get; set; }
|
||||||
public string? LeftPostcode { get; set; }
|
public string? LeftPostcode { get; set; }
|
||||||
public string? RightPostcode { get; set; }
|
public string? RightPostcode { get; set; }
|
||||||
|
|
||||||
|
/// <summary>排序字段:leftPostcode / rightPostcode。</summary>
|
||||||
|
public string? SortField { get; set; }
|
||||||
|
|
||||||
|
/// <summary>排序方向:asc / desc。</summary>
|
||||||
|
public string? SortOrder { get; set; }
|
||||||
}
|
}
|
||||||
|
|
||||||
public interface IKpiAreaRepository : IRepository<KpiArea>, IKpiAreaPublishRepository
|
public interface IKpiAreaRepository : IRepository<KpiArea>, IKpiAreaPublishRepository
|
||||||
|
|||||||
@ -26,9 +26,8 @@ public class KpiAreaRepository : OmsSqlSugarRepository<KpiArea>, IKpiAreaReposit
|
|||||||
CancellationToken cancellationToken = default)
|
CancellationToken cancellationToken = default)
|
||||||
{
|
{
|
||||||
var db = await GetDbContextAsync(cancellationToken);
|
var db = await GetDbContextAsync(cancellationToken);
|
||||||
// 依赖 idx_kpi_info_area_id (AreaId, Id),避免大分区 filesort
|
// 默认按 Id 降序,可走 idx_kpi_info_area_id;邮编排序可能 filesort,分区过大时注意性能
|
||||||
return await BuildInfoQueryable(db, areaId, filter)
|
return await ApplyInfoOrder(BuildInfoQueryable(db, areaId, filter), filter)
|
||||||
.OrderBy(x => x.Id, OrderByType.Desc)
|
|
||||||
.ToPageListAsync(pageIndex, pageSize, cancellationToken);
|
.ToPageListAsync(pageIndex, pageSize, cancellationToken);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -99,6 +98,26 @@ public class KpiAreaRepository : OmsSqlSugarRepository<KpiArea>, IKpiAreaReposit
|
|||||||
return query;
|
return query;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static ISugarQueryable<KpiAreaInfo> ApplyInfoOrder(
|
||||||
|
ISugarQueryable<KpiAreaInfo> query,
|
||||||
|
KpiAreaInfoQueryFilter? filter)
|
||||||
|
{
|
||||||
|
var field = filter?.SortField?.Trim().ToLowerInvariant();
|
||||||
|
var asc = string.Equals(filter?.SortOrder?.Trim(), "asc", StringComparison.OrdinalIgnoreCase);
|
||||||
|
var order = asc ? OrderByType.Asc : OrderByType.Desc;
|
||||||
|
|
||||||
|
return field switch
|
||||||
|
{
|
||||||
|
"leftpostcode" => query
|
||||||
|
.OrderBy(x => x.LeftPostcode, order)
|
||||||
|
.OrderBy(x => x.Id, OrderByType.Desc),
|
||||||
|
"rightpostcode" => query
|
||||||
|
.OrderBy(x => x.RightPostcode, order)
|
||||||
|
.OrderBy(x => x.Id, OrderByType.Desc),
|
||||||
|
_ => query.OrderBy(x => x.Id, OrderByType.Desc),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
private static string? TrimOrNull(string? value)
|
private static string? TrimOrNull(string? value)
|
||||||
{
|
{
|
||||||
var trimmed = value?.Trim();
|
var trimmed = value?.Trim();
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user