feat: BizOps 美区 ZIP3 聚合与客户侧数据隔离 #33
@ -26,7 +26,7 @@ public class BizOpsOverviewOutput
|
||||
/// <summary>客户汇总。</summary>
|
||||
public BizOpsCustomerSummaryOutput CustomerSummary { get; set; } = new();
|
||||
|
||||
/// <summary>收件侧订单状态角标(当前快照)。</summary>
|
||||
/// <summary>收件侧订单状态角标(与本期 CreateTime 窗口一致,非全量库存)。</summary>
|
||||
public BizOpsOrderStatusSnapshotOutput OrderStatusSnapshot { get; set; } = new();
|
||||
|
||||
/// <summary>中国发件省热力。</summary>
|
||||
|
||||
@ -0,0 +1,37 @@
|
||||
namespace Oms.Application.Dtos.Orders;
|
||||
|
||||
/// <summary>
|
||||
/// 经营地图:美国收件 ZIP3 分布(真实订单聚合)。
|
||||
/// </summary>
|
||||
public class BizOpsUsZipDistributionOutput
|
||||
{
|
||||
/// <summary>统计天数:7 / 30 / 90。</summary>
|
||||
public int RangeDays { get; set; }
|
||||
|
||||
/// <summary>本期起(含)。</summary>
|
||||
public DateTime PeriodStart { get; set; }
|
||||
|
||||
/// <summary>本期止(不含)。</summary>
|
||||
public DateTime PeriodEndExclusive { get; set; }
|
||||
|
||||
/// <summary>目的国判定为美国的订单数。</summary>
|
||||
public int UsOrderCount { get; set; }
|
||||
|
||||
/// <summary>成功解析出 ZIP3 的订单数。</summary>
|
||||
public int MatchedZipOrderCount { get; set; }
|
||||
|
||||
/// <summary>按 ZIP3(邮编前三位)聚合的单量,按单量降序。</summary>
|
||||
public List<BizOpsUsZip3StatOutput> Zip3 { get; set; } = [];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 单个 ZIP3 桶。
|
||||
/// </summary>
|
||||
public class BizOpsUsZip3StatOutput
|
||||
{
|
||||
/// <summary>邮编前三位,如 900。</summary>
|
||||
public string Zip3 { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>订单数。</summary>
|
||||
public int OrderCount { get; set; }
|
||||
}
|
||||
@ -1,5 +1,6 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Oms.Application.Dtos.Orders;
|
||||
using Oms.Application.Shared;
|
||||
using Oms.Domain.CustomersAggregate.Enums;
|
||||
using Oms.Domain.OrdersAggregate;
|
||||
using Oms.Domain.OrdersAggregate.Consts;
|
||||
@ -78,23 +79,23 @@ public partial class OrderService
|
||||
|
||||
/// <summary>
|
||||
/// 经营地图工作台总览:发件省/目的国热力、销售产品或国家线路时效与营收毛利。
|
||||
/// 数据范围与订单列表一致:客户登录仅本客户订单;运营端按数据权限。客户响应会剥离成本/毛利与全站客户汇总。
|
||||
/// </summary>
|
||||
/// <param name="rangeDays">统计天数:7 / 30 / 90,默认 30。</param>
|
||||
/// <param name="rangeDays">统计天数:7 / 30 / 90,默认 30;传了 periodStart+periodEnd 时忽略。</param>
|
||||
/// <param name="laneDim">线路维度:product=销售产品,country=目的国。</param>
|
||||
/// <param name="failDaysThreshold">失败时效告警阈值(天),默认 18。</param>
|
||||
/// <param name="marginRateThreshold">毛利率偏低阈值(0-1),默认 0.18。</param>
|
||||
/// <param name="periodStart">自定义窗口起(含),yyyy-MM-dd;与 periodEnd 成对传入。</param>
|
||||
/// <param name="periodEnd">自定义窗口止(含),yyyy-MM-dd。</param>
|
||||
[HttpGet("/order/biz-ops-overview")]
|
||||
public async Task<BizOpsOverviewOutput> GetBizOpsOverviewAsync(
|
||||
[FromQuery] int rangeDays = 30,
|
||||
[FromQuery] string? laneDim = "product",
|
||||
[FromQuery] decimal failDaysThreshold = 18m,
|
||||
[FromQuery] decimal marginRateThreshold = 0.18m)
|
||||
[FromQuery] decimal marginRateThreshold = 0.18m,
|
||||
[FromQuery] DateTime? periodStart = null,
|
||||
[FromQuery] DateTime? periodEnd = null)
|
||||
{
|
||||
rangeDays = rangeDays switch
|
||||
{
|
||||
7 or 30 or 90 => rangeDays,
|
||||
_ => 30,
|
||||
};
|
||||
var dim = string.Equals(laneDim, "country", StringComparison.OrdinalIgnoreCase)
|
||||
? "country"
|
||||
: "product";
|
||||
@ -102,14 +103,14 @@ public partial class OrderService
|
||||
marginRateThreshold = Math.Clamp(marginRateThreshold, 0.01m, 0.9m);
|
||||
|
||||
var (filter, _) = await BuildScopedListFilterAsync();
|
||||
var today = DateTime.Today;
|
||||
var endExclusive = today.AddDays(1);
|
||||
var periodStart = today.AddDays(1 - rangeDays);
|
||||
var previousStart = periodStart.AddDays(-rangeDays);
|
||||
var previousEnd = periodStart;
|
||||
var (resolvedStart, endExclusive, resolvedDays) = ResolveBizOpsPeriod(
|
||||
rangeDays, periodStart, periodEnd);
|
||||
rangeDays = resolvedDays;
|
||||
var previousStart = resolvedStart.AddDays(-rangeDays);
|
||||
var previousEnd = resolvedStart;
|
||||
|
||||
var currentRows = await OrderRepository.ListBizOpsProjectionsByCreateTimeRangeAsync(
|
||||
filter, periodStart, endExclusive);
|
||||
filter, resolvedStart, endExclusive);
|
||||
var previousRows = await OrderRepository.ListBizOpsProjectionsByCreateTimeRangeAsync(
|
||||
filter, previousStart, previousEnd);
|
||||
|
||||
@ -120,20 +121,22 @@ public partial class OrderService
|
||||
|
||||
var productNames = await LoadProductNameMapAsync(currentRows);
|
||||
|
||||
var statusRows = await OrderRepository.GetStatusCountsByFilterAsync(filter);
|
||||
var statusCounts = statusRows
|
||||
.Where(x => !string.Equals(x.Status, OrderStatusCodes.Deleted, StringComparison.OrdinalIgnoreCase))
|
||||
.ToDictionary(x => x.Status, x => x.Count, StringComparer.OrdinalIgnoreCase);
|
||||
int StatusCount(string code) =>
|
||||
statusCounts.TryGetValue(code, out var n) ? n : 0;
|
||||
// 与地图热力同一创建时间窗,避免角标用「全量当前状态」和 7/30/90 分布对不上
|
||||
int StatusCountInPeriod(string code) =>
|
||||
currentRows.Count(x =>
|
||||
string.Equals(x.OrderStatus, code, StringComparison.OrdinalIgnoreCase));
|
||||
|
||||
var customerSummary = await BuildBizOpsCustomerSummaryAsync(periodStart, endExclusive, previousStart, previousEnd);
|
||||
// 订单已由 BuildScopedListFilterAsync 按客户/数据权限收窄;客户会话绝不能看全站客户汇总与成本毛利
|
||||
var isCustomerLogin = OmsSessionHelper.IsCustomerLogin(CurrentUser);
|
||||
var customerSummary = isCustomerLogin
|
||||
? new BizOpsCustomerSummaryOutput()
|
||||
: await BuildBizOpsCustomerSummaryAsync(resolvedStart, endExclusive, previousStart, previousEnd);
|
||||
|
||||
var provinces = BuildProvinceStats(currentRows);
|
||||
var cities = BuildCityHotspots(currentRows);
|
||||
var countries = BuildCountryStats(currentRows);
|
||||
|
||||
var currentLanes = BuildLanes(currentRows, feeMap, productNames, dim, periodStart);
|
||||
var currentLanes = BuildLanes(currentRows, feeMap, productNames, dim, resolvedStart);
|
||||
var previousLaneMap = BuildLanes(previousRows, prevFeeMap, productNames, dim, previousStart)
|
||||
.ToDictionary(x => x.Id, StringComparer.OrdinalIgnoreCase);
|
||||
|
||||
@ -155,21 +158,21 @@ public partial class OrderService
|
||||
totals.RevenueGrowthPercent = CalcGrowthPercentDecimal(totals.Revenue, previousTotals.Revenue);
|
||||
totals.GrossProfitGrowthPercent = CalcGrowthPercentDecimal(totals.GrossProfit, previousTotals.GrossProfit);
|
||||
|
||||
return new BizOpsOverviewOutput
|
||||
var output = new BizOpsOverviewOutput
|
||||
{
|
||||
RangeDays = rangeDays,
|
||||
LaneDim = dim,
|
||||
PeriodStart = periodStart,
|
||||
PeriodStart = resolvedStart,
|
||||
PeriodEndExclusive = endExclusive,
|
||||
FailDaysThreshold = failDaysThreshold,
|
||||
MarginRateThreshold = marginRateThreshold,
|
||||
CustomerSummary = customerSummary,
|
||||
OrderStatusSnapshot = new BizOpsOrderStatusSnapshotOutput
|
||||
{
|
||||
Total = statusCounts.Values.Sum(),
|
||||
Predeclared = StatusCount(OrderStatusCodes.Predeclared),
|
||||
PendingPayment = StatusCount(OrderStatusCodes.PendingPayment),
|
||||
PendingShip = StatusCount(OrderStatusCodes.PendingShip),
|
||||
Total = currentRows.Count,
|
||||
Predeclared = StatusCountInPeriod(OrderStatusCodes.Predeclared),
|
||||
PendingPayment = StatusCountInPeriod(OrderStatusCodes.PendingPayment),
|
||||
PendingShip = StatusCountInPeriod(OrderStatusCodes.PendingShip),
|
||||
},
|
||||
Provinces = provinces,
|
||||
Cities = cities,
|
||||
@ -178,6 +181,175 @@ public partial class OrderService
|
||||
Totals = totals,
|
||||
PreviousTotals = previousTotals,
|
||||
};
|
||||
|
||||
if (isCustomerLogin)
|
||||
{
|
||||
SanitizeBizOpsForCustomer(output);
|
||||
}
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 客户门户:只保留单量/目的国等自有业务可见字段;剥离营收成本毛利与全站客户汇总。
|
||||
/// 订单范围本身已按当前客户 CustomerId 过滤,不会看到其他客户订单。
|
||||
/// </summary>
|
||||
private static void SanitizeBizOpsForCustomer(BizOpsOverviewOutput output)
|
||||
{
|
||||
output.FailDaysThreshold = 0;
|
||||
output.MarginRateThreshold = 0;
|
||||
output.CustomerSummary = new BizOpsCustomerSummaryOutput();
|
||||
foreach (var lane in output.Lanes)
|
||||
{
|
||||
lane.Revenue = 0;
|
||||
lane.Cost = 0;
|
||||
lane.GrossProfit = 0;
|
||||
lane.MarginRate = 0;
|
||||
lane.RevenueGrowthPercent = null;
|
||||
lane.GrossProfitGrowthPercent = null;
|
||||
lane.DailyAvg = [];
|
||||
lane.DailyProfit = [];
|
||||
}
|
||||
|
||||
output.Totals.Revenue = 0;
|
||||
output.Totals.Cost = 0;
|
||||
output.Totals.GrossProfit = 0;
|
||||
output.Totals.RevenueGrowthPercent = null;
|
||||
output.Totals.GrossProfitGrowthPercent = null;
|
||||
output.PreviousTotals.Revenue = 0;
|
||||
output.PreviousTotals.Cost = 0;
|
||||
output.PreviousTotals.GrossProfit = 0;
|
||||
output.PreviousTotals.RevenueGrowthPercent = null;
|
||||
output.PreviousTotals.GrossProfitGrowthPercent = null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 经营地图美国下钻:按收件邮编前三位(ZIP3)聚合真实订单量。
|
||||
/// </summary>
|
||||
/// <param name="rangeDays">统计天数:7 / 30 / 90,默认 30;传了 periodStart+periodEnd 时忽略。</param>
|
||||
/// <param name="periodStart">自定义窗口起(含)。</param>
|
||||
/// <param name="periodEnd">自定义窗口止(含)。</param>
|
||||
/// <returns>美国订单 ZIP3 分布;无邮编订单计入 UsOrderCount 但不进 Zip3。</returns>
|
||||
[HttpGet("/order/biz-ops-us-zip-distribution")]
|
||||
public async Task<BizOpsUsZipDistributionOutput> GetBizOpsUsZipDistributionAsync(
|
||||
[FromQuery] int rangeDays = 30,
|
||||
[FromQuery] DateTime? periodStart = null,
|
||||
[FromQuery] DateTime? periodEnd = null)
|
||||
{
|
||||
var (filter, _) = await BuildScopedListFilterAsync();
|
||||
var (resolvedStart, endExclusive, resolvedDays) = ResolveBizOpsPeriod(
|
||||
rangeDays, periodStart, periodEnd);
|
||||
rangeDays = resolvedDays;
|
||||
|
||||
var rows = await OrderRepository.ListBizOpsUsZipOrdersByCreateTimeRangeAsync(
|
||||
filter, resolvedStart, endExclusive);
|
||||
|
||||
var zipBuckets = new Dictionary<string, int>(StringComparer.Ordinal);
|
||||
var matched = 0;
|
||||
foreach (var row in rows)
|
||||
{
|
||||
var zip3 = TryExtractUsZip3(row.ZipCode);
|
||||
if (zip3 == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
matched++;
|
||||
zipBuckets[zip3] = zipBuckets.TryGetValue(zip3, out var n) ? n + 1 : 1;
|
||||
}
|
||||
|
||||
return new BizOpsUsZipDistributionOutput
|
||||
{
|
||||
RangeDays = rangeDays,
|
||||
PeriodStart = resolvedStart,
|
||||
PeriodEndExclusive = endExclusive,
|
||||
UsOrderCount = rows.Count,
|
||||
MatchedZipOrderCount = matched,
|
||||
Zip3 = zipBuckets
|
||||
.Select(kv => new BizOpsUsZip3StatOutput { Zip3 = kv.Key, OrderCount = kv.Value })
|
||||
.OrderByDescending(x => x.OrderCount)
|
||||
.ThenBy(x => x.Zip3, StringComparer.Ordinal)
|
||||
.ToList(),
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 解析经营地图统计窗口。自定义起止优先;否则按 7/30/90 相对今天。最长 366 天。
|
||||
/// </summary>
|
||||
private static (DateTime PeriodStart, DateTime EndExclusive, int RangeDays) ResolveBizOpsPeriod(
|
||||
int rangeDays,
|
||||
DateTime? periodStart,
|
||||
DateTime? periodEnd)
|
||||
{
|
||||
var today = DateTime.Today;
|
||||
var maxEndExclusive = today.AddDays(1);
|
||||
|
||||
if (periodStart.HasValue && periodEnd.HasValue)
|
||||
{
|
||||
var start = periodStart.Value.Date;
|
||||
var endInclusive = periodEnd.Value.Date;
|
||||
if (endInclusive < start)
|
||||
{
|
||||
(start, endInclusive) = (endInclusive, start);
|
||||
}
|
||||
|
||||
var endExclusive = endInclusive.AddDays(1);
|
||||
if (endExclusive > maxEndExclusive)
|
||||
{
|
||||
endExclusive = maxEndExclusive;
|
||||
}
|
||||
|
||||
if (start >= endExclusive)
|
||||
{
|
||||
start = endExclusive.AddDays(-1);
|
||||
}
|
||||
|
||||
if ((endExclusive - start).TotalDays > 366)
|
||||
{
|
||||
start = endExclusive.AddDays(-366);
|
||||
}
|
||||
|
||||
var days = Math.Max(1, (int)(endExclusive - start).TotalDays);
|
||||
return (start, endExclusive, days);
|
||||
}
|
||||
|
||||
var preset = rangeDays switch
|
||||
{
|
||||
7 or 30 or 90 => rangeDays,
|
||||
_ => 30,
|
||||
};
|
||||
var presetStart = today.AddDays(1 - preset);
|
||||
return (presetStart, maxEndExclusive, preset);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 从收件邮编提取美国 ZIP3(取数字前三位;不足三位返回 null)。
|
||||
/// </summary>
|
||||
private static string? TryExtractUsZip3(string? zipCode)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(zipCode))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
Span<char> digits = stackalloc char[16];
|
||||
var len = 0;
|
||||
foreach (var ch in zipCode)
|
||||
{
|
||||
if (ch is < '0' or > '9')
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (len >= digits.Length)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
digits[len++] = ch;
|
||||
}
|
||||
|
||||
return len < 3 ? null : new string(digits[..3]);
|
||||
}
|
||||
|
||||
private async Task<Dictionary<string, string>> LoadProductNameMapAsync(
|
||||
|
||||
@ -30,6 +30,16 @@ public class BizOpsOrderProjectionRow
|
||||
public string? SenderCity { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 经营地图:美国收件邮编投影(仅 OrderId + 收件邮编)。
|
||||
/// </summary>
|
||||
public class BizOpsUsZipOrderRow
|
||||
{
|
||||
public int OrderId { get; set; }
|
||||
|
||||
public string? ZipCode { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 订单费用按单汇总。
|
||||
/// </summary>
|
||||
|
||||
@ -172,6 +172,15 @@ public interface IOrderRepository : IRepository<Order>
|
||||
DateTime endExclusive,
|
||||
CancellationToken cancellationToken = default);
|
||||
|
||||
/// <summary>
|
||||
/// 经营地图:美国目的国订单的收件邮编(用于 ZIP3 分布)。
|
||||
/// </summary>
|
||||
Task<List<BizOpsUsZipOrderRow>> ListBizOpsUsZipOrdersByCreateTimeRangeAsync(
|
||||
OrderListFilter filter,
|
||||
DateTime startInclusive,
|
||||
DateTime endExclusive,
|
||||
CancellationToken cancellationToken = default);
|
||||
|
||||
/// <summary>
|
||||
/// 经营地图:按订单汇总应收/应付费用。
|
||||
/// </summary>
|
||||
|
||||
@ -1877,6 +1877,59 @@ public class OrderRepository : OmsSqlSugarRepository<Order>, IOrderRepository, I
|
||||
return rows;
|
||||
}
|
||||
|
||||
public async Task<List<BizOpsUsZipOrderRow>> ListBizOpsUsZipOrdersByCreateTimeRangeAsync(
|
||||
OrderListFilter filter,
|
||||
DateTime startInclusive,
|
||||
DateTime endExclusive,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
var db = await GetDbContextAsync(cancellationToken);
|
||||
var orderIds = await ApplyListFilter(db.Queryable<Order>(), filter)
|
||||
.Where(x => x.CreateTime >= startInclusive && x.CreateTime < endExclusive)
|
||||
.Where(x => x.OrderStatus != OrderStatusCodes.Deleted)
|
||||
.Where(x =>
|
||||
(x.ConsigneeCountry != null && (
|
||||
x.ConsigneeCountry == "US" ||
|
||||
x.ConsigneeCountry == "us" ||
|
||||
x.ConsigneeCountry == "USA" ||
|
||||
x.ConsigneeCountry == "usa" ||
|
||||
x.ConsigneeCountry == "United States" ||
|
||||
x.ConsigneeCountry == "UNITED STATES"))
|
||||
|| (x.ConsigneeCountryName != null && (
|
||||
x.ConsigneeCountryName.Contains("美国") ||
|
||||
x.ConsigneeCountryName.Contains("United States") ||
|
||||
x.ConsigneeCountryName.Contains("USA"))))
|
||||
.Select(x => x.Id)
|
||||
.ToListAsync(cancellationToken);
|
||||
|
||||
if (orderIds.Count == 0)
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
const int batchSize = 800;
|
||||
var zipMap = new Dictionary<int, string?>();
|
||||
for (var i = 0; i < orderIds.Count; i += batchSize)
|
||||
{
|
||||
var batchIds = orderIds.Skip(i).Take(batchSize).ToArray();
|
||||
var recipients = await db.Queryable<OrderRecipient>()
|
||||
.Where(x => batchIds.Contains(x.OrderId) && !x.IsDeleted)
|
||||
.Select(x => new { x.OrderId, x.ZipCode, x.Id })
|
||||
.ToListAsync(cancellationToken);
|
||||
foreach (var group in recipients.GroupBy(x => x.OrderId))
|
||||
{
|
||||
var first = group.OrderBy(x => x.Id).First();
|
||||
zipMap[group.Key] = first.ZipCode;
|
||||
}
|
||||
}
|
||||
|
||||
return orderIds.Select(id => new BizOpsUsZipOrderRow
|
||||
{
|
||||
OrderId = id,
|
||||
ZipCode = zipMap.TryGetValue(id, out var zip) ? zip : null,
|
||||
}).ToList();
|
||||
}
|
||||
|
||||
public async Task<List<BizOpsOrderFeeSumRow>> SumFeesByOrderIdsAsync(
|
||||
IEnumerable<int> orderIds,
|
||||
CancellationToken cancellationToken = default)
|
||||
|
||||
Loading…
Reference in New Issue
Block a user