feat: KPI分区明细列表支持起始/截止邮编排序
All checks were successful
Build and Deploy / deploy (push) Successful in 1m13s
All checks were successful
Build and Deploy / deploy (push) Successful in 1m13s
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
parent
8a4848644f
commit
221a2c8a66
@ -54,4 +54,14 @@ public class KpiAreaInfoListInput
|
||||
/// 截止邮编(模糊)。
|
||||
/// </summary>
|
||||
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) =>
|
||||
new()
|
||||
{
|
||||
@ -125,9 +131,38 @@ public class KpiAreaService : ApplicationService
|
||||
Province = input.Province,
|
||||
City = input.City,
|
||||
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>
|
||||
/// 新增KPI分区明细。
|
||||
/// </summary>
|
||||
|
||||
@ -15,6 +15,12 @@ public class KpiAreaInfoQueryFilter
|
||||
public string? City { get; set; }
|
||||
public string? LeftPostcode { 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
|
||||
|
||||
@ -26,9 +26,8 @@ public class KpiAreaRepository : OmsSqlSugarRepository<KpiArea>, IKpiAreaReposit
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
var db = await GetDbContextAsync(cancellationToken);
|
||||
// 依赖 idx_kpi_info_area_id (AreaId, Id),避免大分区 filesort
|
||||
return await BuildInfoQueryable(db, areaId, filter)
|
||||
.OrderBy(x => x.Id, OrderByType.Desc)
|
||||
// 默认按 Id 降序,可走 idx_kpi_info_area_id;邮编排序可能 filesort,分区过大时注意性能
|
||||
return await ApplyInfoOrder(BuildInfoQueryable(db, areaId, filter), filter)
|
||||
.ToPageListAsync(pageIndex, pageSize, cancellationToken);
|
||||
}
|
||||
|
||||
@ -99,6 +98,26 @@ public class KpiAreaRepository : OmsSqlSugarRepository<KpiArea>, IKpiAreaReposit
|
||||
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)
|
||||
{
|
||||
var trimmed = value?.Trim();
|
||||
|
||||
Loading…
Reference in New Issue
Block a user