using Fake.Application; using Microsoft.AspNetCore.Mvc; using Oms.Application.Dtos.Channels; using Oms.Application.Dtos.Orders; using Oms.Application.Services.Orders; using Oms.Application.Services.Orders.Issues; using Oms.Application.Shared; using Oms.Domain.ChannelsAggregate.Entities; using Oms.Domain.OrdersAggregate.Consts; using Oms.Domain.OrdersAggregate.Entities; using Oms.Domain.SystemAggregate.Repositories; namespace Oms.Application.Services.Channels; public partial class SalesProductService { private IWarehouseAddressRepository WarehouseAddressRepository => LazyServiceProvider.GetRequiredService(); /// /// 基于当前库内卖价(非必须已发布)模拟计费,用于运营核对附加服务/偏远/申报比例等。 /// [HttpPost("/channel/sales-product/billing/simulate")] public async Task SimulateBillingAsync( [FromBody] SalesProductBillingSimulateInput input) { if (input.Length <= 0 || input.Width <= 0 || input.Height <= 0 || input.ActualWeight <= 0) { throw new BusinessException("长宽高与实重必须大于0"); } var product = await GetProductOrThrowAsync(input.SalesProductId); var countryCode = input.CountryCode?.Trim().ToUpperInvariant() ?? throw new BusinessException("目的国家不能为空"); if (string.IsNullOrWhiteSpace(countryCode)) { throw new BusinessException("目的国家不能为空"); } var warnings = new List(); // 模拟用当前草稿:LIVE Infos,不强制走已发布包 var priceSnapshot = new SalesProductPricePublishSnapshotDto { SalesProductId = product.Id, Prices = (await GetPriceOutputsAsync(product.Id)).Select(PublishSnapshotSlimmer.SlimPrice).ToList() }; await PriceAreaSnapshotHelper.ApplyLivePriceAreaInfosAsync( priceSnapshot, product, KpiAreaRepository, KpiAreaPackService); var rulesSnapshot = new SalesProductRulesPublishSnapshotDto { Product = MapProductToRulesSaveInput(product) }; await RulesSnapshotHelper.ApplyLiveRulesToSnapshotAsync( rulesSnapshot, product, KpiAreaRepository, WeightRuleRepository, WeightRoundingRepository, KpiAreaPackService); var weight = new OrderWeight { Type = OrderWeightTypeCodes.Forecast, Length = input.Length, Width = input.Width, Height = input.Height, ActualWeight = input.ActualWeight, Cubic = ParcelBillingCalculator.CalculateCubic(input.Length, input.Width, input.Height) }; decimal chargeableWeight; try { chargeableWeight = await ResolveSimulateChargeableWeightAsync(product, rulesSnapshot, weight); } catch (BusinessException) { throw; } catch (InvalidOperationException ex) { throw new BusinessException($"计费重计算失败:{ex.Message}"); } var zoneKeys = ParcelBillingCalculator.TryResolveZoneKeysFromPriceSnapshot( priceSnapshot, countryCode, input.Province, input.City, input.ZipCode); if (zoneKeys == null || zoneKeys.Count == 0) { throw new BusinessException("价格分区未匹配"); } var isRemote = input.IsRemote ?? await IsSimulateRemoteHitAsync(product, rulesSnapshot, countryCode, input.Province, input.City, input.ZipCode); var additionalServices = (input.AdditionalServices ?? []) .Where(x => !string.IsNullOrWhiteSpace(x)) .Select(x => x.Trim()) .Distinct(StringComparer.OrdinalIgnoreCase) .ToList(); var allowed = OmsDocHelper.DeserializeJson>(product.AdditionalServices) ?? []; var allowedSet = allowed .Where(x => !string.IsNullOrWhiteSpace(x)) .Select(x => x.Trim()) .ToHashSet(StringComparer.OrdinalIgnoreCase); var invalidServices = additionalServices.Where(x => !allowedSet.Contains(x)).ToList(); if (invalidServices.Count > 0) { warnings.Add($"附加服务不在产品可选范围:{string.Join(", ", invalidServices)}"); additionalServices = additionalServices.Where(allowedSet.Contains).ToList(); } var cargoAttribute = string.IsNullOrWhiteSpace(input.CargoAttribute) ? CargoAttributeCodes.General : input.CargoAttribute.Trim(); var warehouseName = await ResolveSimulateWarehouseNameAsync(input.WarehouseId); var priceTierKey = string.IsNullOrWhiteSpace(input.PriceTierKey) ? null : input.PriceTierKey.Trim(); var priceVersion = DateTime.Now.ToString("yyyyMMddHHmmss"); var priceEffectiveTime = DateTime.Now; var conditionContext = OrderRuleEvaluationContext.FromOrderOnly(new Order { ProductCode = product.ProductCode, ConsigneeCountry = countryCode, CargoAttribute = cargoAttribute, WarehouseId = input.WarehouseId, DeclaredSum = input.DeclaredSum, DeclaredCurrency = input.DeclaredCurrency, IsRemote = isRemote ? (byte)1 : (byte)0, AdditionalServices = additionalServices.Count == 0 ? null : OmsDocHelper.SerializeJson(additionalServices) }); var remoteAreaId = product.RemoteAreaId is > 0 ? product.RemoteAreaId : rulesSnapshot.RemoteAreaId; var remoteZoneKeys = isRemote ? ParcelBillingCalculator.BuildRemoteZoneKeys(remoteAreaId, rulesSnapshot.RemoteAreaName) : Array.Empty(); var receivableLines = ParcelBillingCalculator.CalculateQuoteReceivableFeeLines( priceSnapshot, priceVersion, priceEffectiveTime, chargeableWeight, zoneKeys, cargoAttribute, priceTierKey, input.WarehouseId, warehouseName, conditionContext, input.DeclaredSum, input.DeclaredCurrency, remoteZoneKeys); if (receivableLines.Count == 0 && ParcelBillingCalculator.HasEnabledReceivablePriceItems(priceSnapshot)) { var detail = ParcelBillingCalculator.ExplainQuotePriceMiss( priceSnapshot, chargeableWeight, zoneKeys, cargoAttribute, priceTierKey, input.WarehouseId, warehouseName); warnings.Add($"卖价未匹配到有效价格:{detail}"); } var receivableFees = receivableLines.Select(x => new SalesProductBillingSimulateFeeOutput { CostItem = x.Fee.CostItem, Amount = x.Fee.Amount, Currency = x.Fee.Currency ?? string.Empty, Formula = x.FormulaPart }).ToList(); var payableFees = new List(); decimal? payableChargeableWeight = null; try { (payableFees, payableChargeableWeight) = await TrySimulatePayableFeesAsync( product, rulesSnapshot, weight, cargoAttribute, input.WarehouseId, warehouseName, isRemote, remoteZoneKeys, countryCode, input.Province, input.City, input.ZipCode, input.DeclaredSum, input.DeclaredCurrency, priceVersion, priceEffectiveTime, warnings); } catch (Exception ex) { warnings.Add($"应付估算失败:{ex.Message}"); } var receivableTotal = SumByCurrency(receivableFees); var payableTotal = SumByCurrency(payableFees); decimal? margin = null; if (receivableTotal.Count == 1 && payableTotal.Count == 1 && string.Equals(receivableTotal.Keys.First(), payableTotal.Keys.First(), StringComparison.OrdinalIgnoreCase)) { margin = receivableTotal.Values.First() - payableTotal.Values.First(); } else if (receivableTotal.Count > 1 || payableTotal.Count > 1) { warnings.Add("多币种费用无法直接计算毛利"); } else if (receivableTotal.Count == 1 && payableTotal.Count == 0) { warnings.Add("仅返回应收估算,无应付费用"); } return new SalesProductBillingSimulateOutput { ChargeableWeight = chargeableWeight, PayableChargeableWeight = payableChargeableWeight, ZoneKeys = zoneKeys.ToList(), IsRemote = isRemote, ReceivableFees = receivableFees, PayableFees = payableFees, ReceivableTotal = receivableTotal, PayableTotal = payableTotal, Margin = margin, Warnings = warnings }; } private async Task ResolveSimulateChargeableWeightAsync( SalesProduct product, SalesProductRulesPublishSnapshotDto rulesSnapshot, OrderWeight weight) { var weightRuleId = product.WeightRuleId is > 0 ? product.WeightRuleId : rulesSnapshot.WeightRuleId; if (weightRuleId is not > 0) { throw new BusinessException("未配置计重规则"); } IReadOnlyList ruleInfos; if (rulesSnapshot.WeightRuleInfos is { Count: > 0 }) { ruleInfos = RulesSnapshotHelper.ToWeightRuleEntities(rulesSnapshot.WeightRuleInfos); } else { ruleInfos = await WeightRuleRepository.GetAllInfosByRuleIdAsync(weightRuleId.Value); } var (_, rawWeight) = ParcelBillingCalculator.CalculateRawBillableWeight(weight, ruleInfos); var weightRoundingId = product.WeightRoundingId is > 0 ? product.WeightRoundingId : rulesSnapshot.WeightRoundingId; if (weightRoundingId is not > 0) { throw new BusinessException("未配置计重进位"); } IReadOnlyList ranges; if (rulesSnapshot.WeightRoundingInfos is { Count: > 0 }) { ranges = RulesSnapshotHelper.ToWeightRoundingEntities(rulesSnapshot.WeightRoundingInfos); } else { ranges = await WeightRoundingRepository.GetAllInfosByRoundingIdAsync(weightRoundingId.Value); } return ParcelBillingCalculator.ApplyRounding(rawWeight, ranges); } private Task IsSimulateRemoteHitAsync( SalesProduct product, SalesProductRulesPublishSnapshotDto rulesSnapshot, string countryCode, string? province, string? city, string? zipCode) { if (rulesSnapshot.RemoteAreaInfos is { Count: > 0 }) { return Task.FromResult(GeoPartitionMatcher.IsHit( rulesSnapshot.RemoteAreaInfos, countryCode, province, city, zipCode)); } return Task.FromResult(false); } private async Task ResolveSimulateWarehouseNameAsync(int? warehouseId) { if (warehouseId is not > 0) { return null; } var warehouse = await WarehouseAddressRepository.FirstOrDefaultAsync(x => x.Id == warehouseId.Value); return warehouse?.WarehouseName; } private async Task<(List Fees, decimal? PayableChargeableWeight)> TrySimulatePayableFeesAsync( SalesProduct product, SalesProductRulesPublishSnapshotDto rulesSnapshot, OrderWeight weight, string cargoAttribute, int? warehouseId, string? warehouseName, bool productIsRemote, IReadOnlyList productRemoteZoneKeys, string countryCode, string? province, string? city, string? zipCode, decimal? declaredSum, string? declaredCurrency, string priceVersion, DateTime priceEffectiveTime, List warnings) { var providers = rulesSnapshot.Providers.Count > 0 ? rulesSnapshot.Providers : (await GetAllocationCoreAsync(product.Id)).Providers .Select(PublishSnapshotSlimmer.SlimAllocationProvider) .ToList(); var filtered = AllocationResolver.FilterProvidersDetailed( providers, customerId: null, OrderRuleEvaluationContext.FromOrderOnly(new Order { ProductCode = product.ProductCode, CargoAttribute = cargoAttribute, WarehouseId = warehouseId, DeclaredSum = declaredSum, DeclaredCurrency = declaredCurrency, IsRemote = productIsRemote ? (byte)1 : (byte)0 })).Passed; var provider = filtered.OrderBy(x => x.SerialNumber).FirstOrDefault(x => x.ServiceChannelId is > 0); if (provider?.ServiceChannelId is not > 0) { warnings.Add("未配置可用排货服务渠道,跳过应付估算"); return ([], null); } var channelId = provider.ServiceChannelId.Value; var channel = await ServiceChannelRepository.FirstOrDefaultAsync(x => x.Id == channelId); if (channel == null) { warnings.Add($"服务渠道 {channelId} 不存在,跳过应付估算"); return ([], null); } decimal payableChargeableWeight; try { payableChargeableWeight = await ResolveSimulateChannelPayableWeightAsync(channel, weight); } catch (Exception ex) { warnings.Add($"服务渠道 {channelId} 计重失败,跳过应付估算:{ex.Message}"); return ([], null); } var channelPriceSnapshot = new ServiceChannelPricePublishSnapshotDto { ServiceChannelId = channelId, Prices = (await BuildChannelPriceOutputsAsync(channelId)) .Select(PublishSnapshotSlimmer.SlimChannelPrice) .ToList() }; if (channelPriceSnapshot.Prices.Count == 0) { warnings.Add($"服务渠道 {channelId} 无买价,跳过应付估算"); return ([], payableChargeableWeight); } await PriceAreaSnapshotHelper.ApplyLiveChannelPriceAreaInfosAsync( channelPriceSnapshot, KpiAreaRepository, KpiAreaPackService); var channelPrices = channelPriceSnapshot.Prices .Select(PublishSnapshotSlimmer.ToChannelPriceListOutput) .ToList(); IReadOnlyList payableZoneKeys = Array.Empty(); if (channelPriceSnapshot.PriceAreaInfos is { Count: > 0 }) { var resolved = ParcelBillingCalculator.TryResolveZoneKeysFromChannelPriceSnapshot( channelPriceSnapshot, countryCode, province, city, zipCode); if (resolved == null || resolved.Count == 0) { warnings.Add($"服务渠道 {channelId} 买价分区未匹配,跳过应付估算"); return ([], payableChargeableWeight); } payableZoneKeys = resolved; } var channelRemoteAreaId = channel.RemoteAreaId; var channelIsRemote = productIsRemote; if (channelRemoteAreaId is > 0) { var channelRules = new ServiceChannelRulesPublishSnapshotDto(); await KpiAreaPackService.LoadLiveRemoteIntoChannelRulesSnapshotAsync( channelRules, channelRemoteAreaId); channelIsRemote = GeoPartitionMatcher.IsHit( channelRules.RemoteAreaInfos, countryCode, province, city, zipCode); } var payableRemoteZoneKeys = channelIsRemote ? ParcelBillingCalculator.BuildRemoteZoneKeys( channelRemoteAreaId is > 0 ? channelRemoteAreaId : null) : Array.Empty(); if (channelIsRemote && payableRemoteZoneKeys.Count == 0) { payableRemoteZoneKeys = productRemoteZoneKeys; } var payableContext = OrderRuleEvaluationContext.FromOrderOnly(new Order { ProductCode = product.ProductCode, ConsigneeCountry = countryCode, CargoAttribute = cargoAttribute, WarehouseId = warehouseId, DeclaredSum = declaredSum, DeclaredCurrency = declaredCurrency, IsRemote = channelIsRemote ? (byte)1 : (byte)0 }); var payable = ParcelBillingCalculator.CalculatePayableFees( channelPrices, priceVersion, priceEffectiveTime, payableChargeableWeight, payableZoneKeys, cargoAttribute, OrderFeeChargingStageCodes.Forecast, warehouseId, warehouseName, payableContext, declaredSum, declaredCurrency, payableRemoteZoneKeys); if (payable.Count == 0) { warnings.Add($"服务渠道 {channelId} 买价未匹配,跳过应付估算"); return ([], payableChargeableWeight); } warnings.Add($"应付按排货服务渠道 {channelId} 估算(首个可用渠道,渠道计重+价区)"); return ( payable.Select(x => new SalesProductBillingSimulateFeeOutput { CostItem = x.CostItem, Amount = x.Amount, Currency = x.Currency ?? string.Empty }).ToList(), payableChargeableWeight); } /// /// 试算应付:按服务渠道计重规则/进位(不改应收计重)。 /// private async Task ResolveSimulateChannelPayableWeightAsync( ServiceChannel channel, OrderWeight weight) { if (channel.WeightRuleId is not > 0) { throw new BusinessException("渠道未配置计重规则"); } if (channel.WeightRoundingId is not > 0) { throw new BusinessException("渠道未配置计重进位"); } var ruleInfos = await WeightRuleRepository.GetAllInfosByRuleIdAsync(channel.WeightRuleId.Value); var (_, rawWeight) = ParcelBillingCalculator.CalculateRawBillableWeight(weight, ruleInfos); var ranges = await WeightRoundingRepository.GetAllInfosByRoundingIdAsync(channel.WeightRoundingId.Value); return ParcelBillingCalculator.ApplyRounding(rawWeight, ranges); } private async Task> BuildChannelPriceOutputsAsync(int serviceChannelId) { var prices = await ServiceChannelRepository.GetPricesAsync(serviceChannelId); var priceIds = prices.Select(x => x.Id).ToArray(); var factorMap = (await ServiceChannelRepository.GetPriceFactorsAsync(priceIds)) .GroupBy(x => x.PriceId) .ToDictionary(x => x.Key, x => x.OrderBy(item => item.SerialNumber).ToList()); var detailMap = (await ServiceChannelRepository.GetPriceDetailsAsync(priceIds)) .GroupBy(x => x.PriceId) .ToDictionary(x => x.Key, x => x.OrderBy(item => item.Id).ToList()); return prices.Select(price => new ServiceChannelPriceListOutput { Id = price.Id, ServiceChannelId = price.ServiceChannelId, SerialNumber = price.SerialNumber, Status = (byte)price.Status, PriceName = price.PriceName, CostItem = price.CostItem, CostUnit = price.CostUnit, Currency = price.Currency, ChargingCondition = price.ChargingCondition, Factors = factorMap.TryGetValue(price.Id, out var factors) ? factors.Select(f => new ChannelPriceFactorOutput { Id = f.Id, PriceId = f.PriceId, SerialNumber = f.SerialNumber, Factor = f.Factor, FactorValues = OmsDocHelper.DeserializeJson>(f.FactorValues) ?? [] }).ToList() : [], Details = detailMap.TryGetValue(price.Id, out var details) ? details.Select(d => new ChannelPriceDetailOutput { Id = d.Id, PriceId = d.PriceId, FactorValuesJson = OmsDocHelper.DeserializeJsonObject(d.FactorValuesJson), CalculationFormula = d.CalculationFormula, CalculatedValue = d.CalculatedValue, CustomerGroupValues = OmsDocHelper.DeserializeJsonObject(d.CustomerGroupValues) }).ToList() : [] }).ToList(); } private static Dictionary SumByCurrency(IEnumerable fees) { return fees .GroupBy(x => string.IsNullOrWhiteSpace(x.Currency) ? "UNK" : x.Currency.Trim(), StringComparer.OrdinalIgnoreCase) .ToDictionary(g => g.Key, g => g.Sum(x => x.Amount), StringComparer.OrdinalIgnoreCase); } }