All checks were successful
Build and Deploy / deploy (push) Successful in 1m23s
补齐访问审计与订单/财务侧数据权限接入,并增加公告批量确认已读接口。 Co-authored-by: Cursor <cursoragent@cursor.com>
343 lines
13 KiB
C#
343 lines
13 KiB
C#
using Fake.Application;
|
|
using Fake.Users;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using MiniExcelLibs;
|
|
using Oms.Application.DataPerm;
|
|
using Oms.Application.Dtos.Finance;
|
|
using Oms.Application.Dtos.Shared;
|
|
using Oms.Application.Services.Orders;
|
|
using Oms.Application.Shared;
|
|
using Oms.Domain.AgentsAggregate.Repositories;
|
|
using Oms.Domain.CustomersAggregate.Repositories;
|
|
using Oms.Domain.OrdersAggregate;
|
|
using Oms.Domain.OrdersAggregate.Consts;
|
|
using Oms.Domain.OrdersAggregate.Repositories;
|
|
using Oms.Domain.SystemAggregate.Repositories;
|
|
|
|
namespace Oms.Application.Services.Finance;
|
|
|
|
/// <summary>
|
|
/// 财务相关 API。
|
|
/// </summary>
|
|
[ApiExplorerSettings(GroupName = "OMS-Finance")]
|
|
public class FinanceService : ApplicationService
|
|
{
|
|
private const string FeeItemDictType = "fee_item";
|
|
private const int ExportMaxRows = 50000;
|
|
|
|
private IOrderRepository OrderRepository =>
|
|
LazyServiceProvider.GetRequiredService<IOrderRepository>();
|
|
|
|
private ICustomerRepository CustomerRepository =>
|
|
LazyServiceProvider.GetRequiredService<ICustomerRepository>();
|
|
|
|
private IAgentRepository AgentRepository =>
|
|
LazyServiceProvider.GetRequiredService<IAgentRepository>();
|
|
|
|
private IDictRepository DictRepository =>
|
|
LazyServiceProvider.GetRequiredService<IDictRepository>();
|
|
|
|
private EffectiveScopeBuilder DataScopeBuilder =>
|
|
LazyServiceProvider.GetRequiredService<EffectiveScopeBuilder>();
|
|
|
|
private DataPermAccessAuditor DataPermAccessAuditor =>
|
|
LazyServiceProvider.GetRequiredService<DataPermAccessAuditor>();
|
|
|
|
/// <summary>
|
|
/// 分页查询应收应付(订单费用联查订单)。
|
|
/// </summary>
|
|
[HttpGet("/finance/receivable-payable/list")]
|
|
public async Task<OmsPageResult<ReceivablePayablePaginatedOutput>> GetReceivablePayableListAsync(
|
|
[FromQuery] ReceivablePayablePaginatedInput input)
|
|
{
|
|
var timer = DataPermAccessAuditor.StartTimer();
|
|
var (filter, scope) = await BuildScopedFilterAsync(input);
|
|
var pageIndex = OmsDocHelper.NormalizePage(input.Page);
|
|
var pageSize = OmsDocHelper.NormalizePageSize(input.PageSize);
|
|
var (rows, total) = await OrderRepository.GetReceivablePayablePagedListAsync(
|
|
pageIndex,
|
|
pageSize,
|
|
filter);
|
|
timer.Stop();
|
|
await DataPermAccessAuditor.TryWriteAsync(
|
|
scope,
|
|
"/finance/receivable-payable/list",
|
|
"GET",
|
|
rows.Count,
|
|
(int)timer.ElapsedMilliseconds,
|
|
DataPermConstants.AccessLogSamplePercentList);
|
|
|
|
var items = await MapReceivablePayableRowsAsync(rows);
|
|
return new OmsPageResult<ReceivablePayablePaginatedOutput>
|
|
{
|
|
Items = items,
|
|
Total = total
|
|
};
|
|
}
|
|
|
|
/// <summary>
|
|
/// 应收应付汇总(当前筛选条件下全量费用明细)。
|
|
/// </summary>
|
|
[HttpGet("/finance/receivable-payable/summary")]
|
|
public async Task<ReceivablePayableSummaryOutput> GetReceivablePayableSummaryAsync(
|
|
[FromQuery] ReceivablePayablePaginatedInput input)
|
|
{
|
|
var (filter, _) = await BuildScopedFilterAsync(input);
|
|
var rows = await OrderRepository.GetReceivablePayableSummaryAsync(filter);
|
|
var items = rows
|
|
.Select(row => new ReceivablePayableSummaryItemOutput
|
|
{
|
|
FeeType = row.FeeType,
|
|
FeeTypeName = GetFeeTypeName(row.FeeType),
|
|
Currency = row.Currency,
|
|
TotalAmount = row.TotalAmount,
|
|
LineCount = row.LineCount
|
|
})
|
|
.OrderBy(x => x.FeeType)
|
|
.ThenBy(x => x.Currency)
|
|
.ToList();
|
|
|
|
return new ReceivablePayableSummaryOutput
|
|
{
|
|
TotalLines = items.Sum(x => x.LineCount),
|
|
Items = items
|
|
};
|
|
}
|
|
|
|
/// <summary>
|
|
/// 导出应收应付 Excel。
|
|
/// </summary>
|
|
[HttpGet("/finance/receivable-payable/export")]
|
|
public async Task<FileContentResult> ExportReceivablePayableAsync(
|
|
[FromQuery] ReceivablePayablePaginatedInput input)
|
|
{
|
|
var timer = DataPermAccessAuditor.StartTimer();
|
|
var (filter, scope) = await BuildScopedFilterAsync(input);
|
|
var rows = await OrderRepository.GetReceivablePayableExportListAsync(filter, ExportMaxRows);
|
|
timer.Stop();
|
|
await DataPermAccessAuditor.TryWriteAsync(
|
|
scope,
|
|
"/finance/receivable-payable/export",
|
|
"GET",
|
|
rows.Count,
|
|
(int)timer.ElapsedMilliseconds,
|
|
DataPermConstants.AccessLogSamplePercentExport);
|
|
var items = await MapReceivablePayableRowsAsync(rows);
|
|
var exportRows = items.Select(item => new
|
|
{
|
|
运单号 = item.ServiceNo,
|
|
客户单号 = item.ClientNo,
|
|
跟踪号 = item.TrackNo,
|
|
客户编码 = item.CustomerCode,
|
|
代理 = item.AgentName ?? (item.AgentId.HasValue ? $"代理#{item.AgentId}" : ""),
|
|
费用类型 = item.FeeTypeName,
|
|
费用项 = item.CostItemName ?? item.CostItem,
|
|
金额 = item.Amount,
|
|
币种 = item.Currency,
|
|
计费阶段 = item.ChargingStageName,
|
|
订单状态 = item.OrderStatusName,
|
|
支付状态 = item.IsPaid == 1 ? "已支付" : "未支付",
|
|
产品编码 = item.ProductCode,
|
|
渠道编码 = item.ChannelCode,
|
|
目的国 = item.ConsigneeCountryName,
|
|
下游服务商 = item.DownstreamProvider,
|
|
价格编号 = item.PriceNo,
|
|
价格生效时间 = item.PriceEffectiveTime,
|
|
支付时间 = item.PayTime,
|
|
收货时间 = item.ReceiveTime,
|
|
发货时间 = item.ShipTime
|
|
}).ToList();
|
|
|
|
await using var stream = new MemoryStream();
|
|
await MiniExcel.SaveAsAsync(stream, exportRows);
|
|
stream.Position = 0;
|
|
return new FileContentResult(stream.ToArray(),
|
|
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
|
|
{
|
|
FileDownloadName = $"receivable-payable-{DateTime.Now:yyyyMMddHHmmss}.xlsx"
|
|
};
|
|
}
|
|
|
|
private async Task<(OrderReceivablePayableFilter Filter, DataScopeContext? Scope)> BuildScopedFilterAsync(
|
|
ReceivablePayablePaginatedInput input)
|
|
{
|
|
var customerId = await OmsSessionHelper.ResolveListCustomerIdAsync(
|
|
CustomerRepository,
|
|
CurrentUser,
|
|
input.CustomerId);
|
|
var filter = BuildFilter(input, customerId);
|
|
var scope = await ApplyInternalOrderDataScopeAsync(filter);
|
|
return (filter, scope);
|
|
}
|
|
|
|
private async Task<List<ReceivablePayablePaginatedOutput>> MapReceivablePayableRowsAsync(
|
|
List<OrderReceivablePayableRow> rows)
|
|
{
|
|
var customerIds = rows
|
|
.Select(x => x.Order.CustomerId)
|
|
.Where(x => x.HasValue)
|
|
.Select(x => x!.Value)
|
|
.Distinct()
|
|
.ToArray();
|
|
var customers = customerIds.Length == 0
|
|
? []
|
|
: await CustomerRepository.GetListAsync(x => customerIds.Contains(x.Id));
|
|
var customerMap = customers.ToDictionary(x => x.Id);
|
|
|
|
var agentIds = rows
|
|
.Select(x => x.Order.AgentId)
|
|
.Where(x => x.HasValue)
|
|
.Select(x => x!.Value)
|
|
.Distinct()
|
|
.ToArray();
|
|
var agents = agentIds.Length == 0
|
|
? []
|
|
: await AgentRepository.GetListAsync(x => agentIds.Contains(x.Id));
|
|
var agentNameMap = agents.ToDictionary(x => x.Id, x => x.AgentName);
|
|
|
|
var feeItemNameMap = await BuildFeeItemNameMapAsync();
|
|
|
|
return rows.Select(row =>
|
|
{
|
|
string? customerCode = null;
|
|
if (row.Order.CustomerId.HasValue
|
|
&& customerMap.TryGetValue(row.Order.CustomerId.Value, out var customer))
|
|
{
|
|
customerCode = customer.CustomerCode;
|
|
}
|
|
|
|
string? agentName = null;
|
|
if (row.Order.AgentId.HasValue
|
|
&& agentNameMap.TryGetValue(row.Order.AgentId.Value, out var name))
|
|
{
|
|
agentName = name;
|
|
}
|
|
|
|
var costItem = row.Fee.CostItem;
|
|
feeItemNameMap.TryGetValue(costItem, out var costItemName);
|
|
|
|
return new ReceivablePayablePaginatedOutput
|
|
{
|
|
OrderId = row.Order.Id,
|
|
ServiceNo = row.Order.ServiceNo,
|
|
ClientNo = row.Order.ClientNo,
|
|
TrackNo = row.Order.TrackNo,
|
|
CustomerCode = customerCode,
|
|
AgentId = row.Order.AgentId,
|
|
AgentName = agentName,
|
|
OrderStatus = row.Order.OrderStatus,
|
|
OrderStatusName = OmsDocHelper.GetOrderStatusName(row.Order.OrderStatus),
|
|
IsPaid = row.Order.IsPaid,
|
|
PayTime = row.Order.PayTime,
|
|
ReceiveTime = row.Order.ReceiveTime,
|
|
ShipTime = row.Order.ShipTime,
|
|
CreateTime = row.Order.CreateTime,
|
|
ProductCode = row.Order.ProductCode,
|
|
ChannelCode = row.Order.ChannelCode,
|
|
ConsigneeCountryName = row.Order.ConsigneeCountryName,
|
|
FeeType = row.Fee.Type,
|
|
FeeTypeName = GetFeeTypeName(row.Fee.Type),
|
|
CostItem = costItem,
|
|
CostItemName = costItemName ?? ParcelBillingCalculator.ResolveFeeDisplayName(costItem, null),
|
|
Amount = row.Fee.Amount,
|
|
Currency = row.Fee.Currency,
|
|
DownstreamProvider = row.Fee.DownstreamProvider,
|
|
PriceNo = row.Fee.PriceNo,
|
|
PriceEffectiveTime = row.Fee.PriceEffectiveTime,
|
|
ChargingStage = row.Fee.ChargingStage,
|
|
ChargingStageName = GetChargingStageName(row.Fee.ChargingStage)
|
|
};
|
|
}).ToList();
|
|
}
|
|
|
|
private async Task<Dictionary<string, string>> BuildFeeItemNameMapAsync()
|
|
{
|
|
var dictRows = await DictRepository.GetListAsync(x => x.DictType == FeeItemDictType);
|
|
return dictRows
|
|
.Where(x => !string.IsNullOrWhiteSpace(x.DictCode))
|
|
.GroupBy(x => x.DictCode.Trim(), StringComparer.OrdinalIgnoreCase)
|
|
.ToDictionary(
|
|
g => g.Key,
|
|
g => g.First().DictName?.Trim() ?? g.Key,
|
|
StringComparer.OrdinalIgnoreCase);
|
|
}
|
|
|
|
private static OrderReceivablePayableFilter BuildFilter(
|
|
ReceivablePayablePaginatedInput input,
|
|
int? customerId)
|
|
{
|
|
var timeField = NormalizeTimeField(input.TimeField);
|
|
var useCreateTime = timeField == "createTime";
|
|
return new()
|
|
{
|
|
Keyword = input.Keyword,
|
|
CustomerId = customerId,
|
|
CustomerCode = input.CustomerCode,
|
|
ProductCode = string.IsNullOrWhiteSpace(input.ProductCode) ? null : input.ProductCode.Trim(),
|
|
ChannelCode = string.IsNullOrWhiteSpace(input.ChannelCode) ? null : input.ChannelCode.Trim(),
|
|
AgentId = input.AgentId is > 0 ? input.AgentId : null,
|
|
CreateTimeStart = useCreateTime ? input.StartTime : null,
|
|
CreateTimeEnd = useCreateTime ? input.EndTime : null,
|
|
TimeField = timeField,
|
|
TimeStart = input.StartTime,
|
|
TimeEnd = input.EndTime,
|
|
FeeType = input.FeeType,
|
|
CostItem = input.CostItem,
|
|
ChargingStage = input.ChargingStage,
|
|
EffectiveStageOnly = input.EffectiveStageOnly,
|
|
IsPaid = NormalizeIsPaid(input.IsPaid)
|
|
};
|
|
}
|
|
|
|
private static int? NormalizeIsPaid(int? isPaid)
|
|
{
|
|
if (!isPaid.HasValue) return null;
|
|
return isPaid.Value == 1 ? 1 : isPaid.Value == 0 ? 0 : null;
|
|
}
|
|
|
|
private static string NormalizeTimeField(string? timeField)
|
|
{
|
|
var raw = timeField?.Trim();
|
|
return raw switch
|
|
{
|
|
"receiveTime" or "shipTime" or "payTime" => raw,
|
|
_ => "createTime"
|
|
};
|
|
}
|
|
|
|
private async Task<DataScopeContext?> ApplyInternalOrderDataScopeAsync(OrderReceivablePayableFilter filter)
|
|
{
|
|
if (OmsSessionHelper.IsCustomerLogin(CurrentUser))
|
|
{
|
|
return null;
|
|
}
|
|
|
|
var userGuidRaw = CurrentUser.FindClaimValueOrNull(global::System.Security.Claims.ClaimTypes.NameIdentifier);
|
|
if (string.IsNullOrWhiteSpace(userGuidRaw) || !Guid.TryParse(userGuidRaw, out var userGuid))
|
|
{
|
|
return null;
|
|
}
|
|
|
|
var context = await DataScopeBuilder.BuildAsync(userGuid, DataScopeApplier.OrderResourceCode);
|
|
DataScopeApplier.ApplyToOrderListFilter(filter, context);
|
|
return context;
|
|
}
|
|
|
|
private static string GetFeeTypeName(string feeType) =>
|
|
feeType switch
|
|
{
|
|
OrderFeeTypeCodes.Receivable => "应收",
|
|
OrderFeeTypeCodes.Payable => "应付",
|
|
_ => feeType
|
|
};
|
|
|
|
private static string? GetChargingStageName(string? chargingStage) =>
|
|
chargingStage switch
|
|
{
|
|
OrderFeeChargingStageCodes.Forecast => "预报",
|
|
OrderFeeChargingStageCodes.Inbound => "入库",
|
|
OrderFeeChargingStageCodes.Outbound => "出库",
|
|
_ => chargingStage
|
|
};
|
|
}
|