diff --git a/src/Oms.Application/Services/Agents/AgentPortalService.cs b/src/Oms.Application/Services/Agents/AgentPortalService.cs
index 20c43ba..f863acf 100644
--- a/src/Oms.Application/Services/Agents/AgentPortalService.cs
+++ b/src/Oms.Application/Services/Agents/AgentPortalService.cs
@@ -111,31 +111,22 @@ public class AgentPortalService : ApplicationService
};
}
- /// 名下订单分页(强制 Order.AgentId;不含应付)。
+ /// 名下订单分页(按当前绑定客户可见;不含应付)。
[HttpPost("/agent/portal/orders/list")]
public async Task> GetOrdersAsync(AgentPortalPageInput input)
{
var agent = await GetCurrentAgentOrThrowAsync();
var keyword = input.Keyword?.Trim();
- Expression> predicate = string.IsNullOrWhiteSpace(keyword)
- ? x => x.AgentId == agent.Id
- : x => x.AgentId == agent.Id
- && (x.ServiceNo.Contains(keyword!)
- || (x.TrackNo != null && x.TrackNo.Contains(keyword!))
- || (x.ProductCode != null && x.ProductCode.Contains(keyword!)));
+ var filter = new OrderListFilter
+ {
+ OwnedByAgentId = agent.Id,
+ Keyword = string.IsNullOrWhiteSpace(keyword) ? null : keyword,
+ Status = "all"
+ };
var page = OmsDocHelper.NormalizePage(input.Page);
var pageSize = OmsDocHelper.NormalizePageSize(input.PageSize);
- var entities = await OrderRepository.GetPagedListAsync(
- predicate,
- page,
- pageSize,
- new Dictionary
- {
- [nameof(Order.CreateTime)] = false,
- [nameof(Order.Id)] = false
- });
- var total = await OrderRepository.CountAsync(predicate);
+ var (entities, total) = await OrderRepository.GetFilteredPagedListAsync(page, pageSize, filter);
var customerIds = entities.Where(x => x.CustomerId.HasValue).Select(x => x.CustomerId!.Value).Distinct().ToList();
var customers = customerIds.Count == 0
@@ -175,7 +166,7 @@ public class AgentPortalService : ApplicationService
public async Task GetWorkplaceStatsAsync()
{
var agent = await GetCurrentAgentOrThrowAsync();
- var filter = new OrderListFilter { AgentId = agent.Id };
+ var filter = new OrderListFilter { OwnedByAgentId = agent.Id };
var rows = await OrderRepository.GetStatusCountsByFilterAsync(filter);
var counts = rows
.Where(x => !string.Equals(x.Status, OrderStatusCodes.Deleted, StringComparison.OrdinalIgnoreCase))
@@ -244,7 +235,7 @@ public class AgentPortalService : ApplicationService
{
var agent = await GetCurrentAgentOrThrowAsync();
days = Math.Clamp(days, 1, 31);
- var filter = new OrderListFilter { AgentId = agent.Id };
+ var filter = new OrderListFilter { OwnedByAgentId = agent.Id };
var today = DateTime.Today;
var rangeStart = today.AddDays(-(days - 1));
var rangeEnd = today.AddDays(1);
diff --git a/src/Oms.Application/Services/Orders/OrderService.Helpers.cs b/src/Oms.Application/Services/Orders/OrderService.Helpers.cs
index b1ed712..a3d5ca0 100644
--- a/src/Oms.Application/Services/Orders/OrderService.Helpers.cs
+++ b/src/Oms.Application/Services/Orders/OrderService.Helpers.cs
@@ -1225,7 +1225,7 @@ public partial class OrderService
}
///
- /// 渠道代理门户:列表仅可见下单快照 AgentId=本代理的运单。
+ /// 渠道代理门户:列表按名下客户过滤(客户/主客户当前 AgentId);返佣仍认订单快照 AgentId。
///
private async Task ApplyAgentOrderScopeAsync(OrderListFilter filter)
{
@@ -1243,7 +1243,9 @@ public partial class OrderService
throw new BusinessException("代理不存在");
}
- filter.AgentId = agent.Id;
+ filter.OwnedByAgentId = agent.Id;
+ // 避免与快照字段叠滤,导致换绑前历史单被挡掉
+ filter.AgentId = null;
if (filter.CustomerId is > 0)
{
diff --git a/src/Oms.Application/Services/Orders/OrderService.cs b/src/Oms.Application/Services/Orders/OrderService.cs
index 154f1b7..b42546b 100644
--- a/src/Oms.Application/Services/Orders/OrderService.cs
+++ b/src/Oms.Application/Services/Orders/OrderService.cs
@@ -1358,9 +1358,26 @@ public partial class OrderService : ApplicationService
{
var agentRepository =
LazyServiceProvider.GetRequiredService();
- var userId = OmsSessionHelper.ResolveMapUserId(CurrentUser);
- var agent = userId > 0 ? await agentRepository.GetByUserIdAsync(userId) : null;
- if (agent == null || order.AgentId != agent.Id)
+ if (order.CustomerId is not > 0)
+ {
+ throw new BusinessException("订单不存在");
+ }
+
+ var customer = await CustomerRepository.FirstOrDefaultAsync(x => x.Id == order.CustomerId.Value);
+ if (customer == null)
+ {
+ throw new BusinessException("订单不存在");
+ }
+
+ try
+ {
+ await OmsSessionHelper.EnsureAgentOwnsCustomerAsync(
+ CustomerRepository,
+ agentRepository,
+ CurrentUser,
+ customer);
+ }
+ catch (BusinessException)
{
throw new BusinessException("订单不存在");
}
diff --git a/src/Oms.Domain/OrdersAggregate/OrderListFilter.cs b/src/Oms.Domain/OrdersAggregate/OrderListFilter.cs
index 419d536..429d54c 100644
--- a/src/Oms.Domain/OrdersAggregate/OrderListFilter.cs
+++ b/src/Oms.Domain/OrdersAggregate/OrderListFilter.cs
@@ -35,10 +35,15 @@ public class OrderListFilter
public int? CustomerId { get; set; }
///
- /// 渠道代理 Id(门户列表强制按下单快照 AgentId 过滤)。
+ /// 按下单快照 Order.AgentId 过滤(返佣归属;历史单换绑后不变)。
///
public int? AgentId { get; set; }
+ ///
+ /// 按名下客户可见:订单客户(或主客户)当前 AgentId = 此值。代理门户运单列表用此字段。
+ ///
+ public int? OwnedByAgentId { get; set; }
+
///
/// 发货仓 ID(订单或绑定行所属仓)。
///
diff --git a/src/Oms.SqlSugarCore/Orders/OrderRepository.cs b/src/Oms.SqlSugarCore/Orders/OrderRepository.cs
index 9ef3900..8a46cbf 100644
--- a/src/Oms.SqlSugarCore/Orders/OrderRepository.cs
+++ b/src/Oms.SqlSugarCore/Orders/OrderRepository.cs
@@ -53,6 +53,23 @@ public class OrderRepository : OmsSqlSugarRepository, IOrderRepository, I
query = query.Where(x => x.AgentId == agentId);
}
+ // 名下客户可见:本客户 AgentId,或子客户的主客户 AgentId
+ if (filter.OwnedByAgentId.HasValue)
+ {
+ var ownedByAgentId = filter.OwnedByAgentId.Value;
+ query = query.Where(o =>
+ SqlFunc.Subqueryable()
+ .Where(c => c.Id == o.CustomerId && c.AgentId == ownedByAgentId)
+ .Any()
+ || SqlFunc.Subqueryable()
+ .Where(c => c.Id == o.CustomerId
+ && c.ParentCustomerId != null
+ && SqlFunc.Subqueryable()
+ .Where(m => m.Id == c.ParentCustomerId && m.AgentId == ownedByAgentId)
+ .Any())
+ .Any());
+ }
+
var status = filter.Status?.Trim();
var isDeletedTab = string.Equals(status, OrderStatusCodes.Deleted, StringComparison.OrdinalIgnoreCase);
if (isDeletedTab)
@@ -1510,6 +1527,7 @@ public class OrderRepository : OmsSqlSugarRepository, IOrderRepository, I
CreateTimeEnd = filter.CreateTimeEnd,
CustomerId = filter.CustomerId,
AgentId = filter.AgentId,
+ OwnedByAgentId = filter.OwnedByAgentId,
FieldFilters = filter.FieldFilters,
SortField = filter.SortField,
SortOrder = filter.SortOrder,
@@ -1557,6 +1575,7 @@ public class OrderRepository : OmsSqlSugarRepository, IOrderRepository, I
CreateTimeEnd = filter.CreateTimeEnd,
CustomerId = filter.CustomerId,
AgentId = filter.AgentId,
+ OwnedByAgentId = filter.OwnedByAgentId,
WarehouseId = filter.WarehouseId,
FulfillmentType = filter.FulfillmentType,
StockBindStatus = filter.StockBindStatus,