修复订单金额落库不对的问题
All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 8m17s

This commit is contained in:
Break
2026-06-01 11:21:29 +08:00
parent 6c8352491c
commit 944526d9ef
16 changed files with 454 additions and 37 deletions

View File

@@ -696,8 +696,9 @@ func parseGatewayTime(raw gateway.FlexString) *time.Time {
}
// GetPackages 获取资产的所有套餐列表(支持分页和状态筛选)
// callerAccountType: 调用方账号类型,"platform" 时返回成本价paid_amount其他类型不返回
// page 默认 1pageSize 默认 50pageSize 最大 100
func (s *Service) GetPackages(ctx context.Context, assetType string, id uint, page, pageSize int, status *int) (*dto.AssetPackagesResult, error) {
func (s *Service) GetPackages(ctx context.Context, assetType string, id uint, page, pageSize int, status *int, callerAccountType string) (*dto.AssetPackagesResult, error) {
// 分页参数边界处理
if page < 1 {
page = 1
@@ -757,6 +758,10 @@ func (s *Service) GetPackages(ctx context.Context, assetType string, id uint, pa
pkgType = pkg.PackageType
expiryBase = pkg.ExpiryBase
}
var paidAmount *int64
if callerAccountType == constants.OwnerTypePlatform {
paidAmount = u.PaidAmount
}
item := &dto.AssetPackageResponse{
PackageUsageID: u.ID,
PackageID: u.PackageID,
@@ -780,7 +785,8 @@ func (s *Service) GetPackages(ctx context.Context, assetType string, id uint, pa
ExpiresAt: u.ExpiresAt,
MasterUsageID: u.MasterUsageID,
Priority: u.Priority,
PaidAmount: u.PaidAmount,
PaidAmount: paidAmount,
RetailAmount: u.RetailAmount,
CreatedAt: u.CreatedAt,
}
all = append(all, item)
@@ -811,7 +817,8 @@ func (s *Service) GetPackages(ctx context.Context, assetType string, id uint, pa
}
// GetCurrentPackage 获取资产当前生效的主套餐
func (s *Service) GetCurrentPackage(ctx context.Context, assetType string, id uint) (*dto.AssetPackageResponse, error) {
// callerAccountType: 调用方账号类型,"platform" 时返回成本价paid_amount其他类型不返回
func (s *Service) GetCurrentPackage(ctx context.Context, assetType string, id uint, callerAccountType string) (*dto.AssetPackageResponse, error) {
carrierType := assetType
if assetType == "card" {
carrierType = "iot_card"
@@ -839,6 +846,10 @@ func (s *Service) GetCurrentPackage(ctx context.Context, assetType string, id ui
pkgType = pkg.PackageType
expiryBase = pkg.ExpiryBase
}
var paidAmount *int64
if callerAccountType == constants.OwnerTypePlatform {
paidAmount = usage.PaidAmount
}
return &dto.AssetPackageResponse{
PackageUsageID: usage.ID,
PackageID: usage.PackageID,
@@ -862,7 +873,8 @@ func (s *Service) GetCurrentPackage(ctx context.Context, assetType string, id ui
ExpiresAt: usage.ExpiresAt,
MasterUsageID: usage.MasterUsageID,
Priority: usage.Priority,
PaidAmount: usage.PaidAmount,
PaidAmount: paidAmount,
RetailAmount: usage.RetailAmount,
CreatedAt: usage.CreatedAt,
}, nil
}

View File

@@ -242,6 +242,7 @@ func (s *Service) CreateAdminOrder(ctx context.Context, req *dto.CreateAdminOrde
purchaseRole := ""
var sellerShopID *uint = resourceShopID
var sellerCostPrice int64
var itemCostPriceMap map[uint]int64 // 各套餐成本单价映射,用于 OrderItem.CostPrice 字段
// 根据支付方式分别处理
if req.PaymentMethod == model.PaymentMethodOffline {
@@ -249,7 +250,6 @@ func (s *Service) CreateAdminOrder(ctx context.Context, req *dto.CreateAdminOrde
if containsGift {
orderBuyerType = model.BuyerTypePersonal
orderBuyerID = 0
totalAmount = 0
paymentMethod = model.PaymentMethodOffline
paymentStatus = model.PaymentStatusPaid
paidAt = &now
@@ -286,8 +286,7 @@ func (s *Service) CreateAdminOrder(ctx context.Context, req *dto.CreateAdminOrde
}
orderBuyerType = model.BuyerTypeAgent
orderBuyerID = purchaseBuyerID
itemUnitPriceMap = buyerCostPriceMap
totalAmount = buyerTotalCost
itemCostPriceMap = buyerCostPriceMap
paymentMethod = model.PaymentMethodOffline
paymentStatus = model.PaymentStatusPaid
paidAt = purchasePaidAt
@@ -298,7 +297,7 @@ func (s *Service) CreateAdminOrder(ctx context.Context, req *dto.CreateAdminOrde
operatorID = nil
operatorType = constants.OwnerTypePlatform
purchaseRole = model.PurchaseRolePurchasedByPlatform
actualPaidAmount = nil
actualPaidAmount = &buyerTotalCost
}
}
@@ -320,8 +319,7 @@ func (s *Service) CreateAdminOrder(ctx context.Context, req *dto.CreateAdminOrde
orderBuyerType = model.BuyerTypeAgent
orderBuyerID = operatorShopID
itemUnitPriceMap = operatorCostPriceMap
totalAmount = operatorTotalCost
itemCostPriceMap = operatorCostPriceMap
paymentMethod = model.PaymentMethodWallet
paymentStatus = model.PaymentStatusPaid
paidAt = &now
@@ -336,13 +334,13 @@ func (s *Service) CreateAdminOrder(ctx context.Context, req *dto.CreateAdminOrde
} else {
// ==== 子场景 2.2:代理代购(给下级购买)====
// 获取买家成本价
buyerCostPriceMap, buyerTotalCost, err := s.buildCostPriceMap(ctx, validationResult.Packages, *resourceShopID)
// 获取买家成本价(买家的价格映射用于 CostPrice 快照,总价不再使用)
buyerCostPriceMap, _, err := s.buildCostPriceMap(ctx, validationResult.Packages, *resourceShopID)
if err != nil {
return nil, err
}
// 获取操作者成本价
// 获取操作者成本价(操作方实际扣款金额和佣金基准)
_, operatorTotalCost, err := s.buildCostPriceMap(ctx, validationResult.Packages, operatorShopID)
if err != nil {
return nil, err
@@ -350,8 +348,7 @@ func (s *Service) CreateAdminOrder(ctx context.Context, req *dto.CreateAdminOrde
orderBuyerType = model.BuyerTypeAgent
orderBuyerID = *resourceShopID
itemUnitPriceMap = buyerCostPriceMap
totalAmount = buyerTotalCost
itemCostPriceMap = buyerCostPriceMap
paymentMethod = model.PaymentMethodWallet
paymentStatus = model.PaymentStatusPaid
paidAt = &now
@@ -375,8 +372,7 @@ func (s *Service) CreateAdminOrder(ctx context.Context, req *dto.CreateAdminOrde
orderBuyerType = model.BuyerTypeAgent
orderBuyerID = targetShopID
itemUnitPriceMap = targetCostPriceMap
totalAmount = targetTotalCost
itemCostPriceMap = targetCostPriceMap
paymentMethod = model.PaymentMethodWallet
paymentStatus = model.PaymentStatusPaid
paidAt = &now
@@ -465,7 +461,7 @@ func (s *Service) CreateAdminOrder(ctx context.Context, req *dto.CreateAdminOrde
order.BuyerPhone, order.BuyerNickname = s.fetchBuyerSnapshot(ctx, orderBuyerID)
}
items := s.buildOrderItems(userID, validationResult.Packages, itemUnitPriceMap)
items := s.buildOrderItems(userID, validationResult.Packages, itemUnitPriceMap, itemCostPriceMap)
idempotencyKey := buildOrderIdempotencyKey(buyerType, buyerID, orderType, carrierType, carrierID, req.PackageIDs)
@@ -606,7 +602,8 @@ func (s *Service) CreateH5Order(ctx context.Context, req *dto.CreateOrderRequest
purchaseRole := ""
var sellerShopID *uint = resourceShopID
var sellerCostPrice int64
var expiresAt *time.Time // 待支付订单设置过期时间,立即支付的订单为 nil
var expiresAt *time.Time // 待支付订单设置过期时间,立即支付的订单为 nil
var itemCostPriceMap map[uint]int64 // 各套餐成本单价映射,用于 OrderItem.CostPrice 字段
// 场景判断offline平台代购、wallet代理钱包支付、其他待支付
if req.PaymentMethod == model.PaymentMethodOffline {
@@ -617,8 +614,7 @@ func (s *Service) CreateH5Order(ctx context.Context, req *dto.CreateOrderRequest
}
orderBuyerType = model.BuyerTypeAgent
orderBuyerID = purchaseBuyerID
itemUnitPriceMap = buyerCostPriceMap
totalAmount = buyerTotalCost
itemCostPriceMap = buyerCostPriceMap
paymentMethod = model.PaymentMethodOffline
paymentStatus = model.PaymentStatusPaid
paidAt = purchasePaidAt
@@ -629,7 +625,7 @@ func (s *Service) CreateH5Order(ctx context.Context, req *dto.CreateOrderRequest
operatorID = nil
operatorType = constants.OwnerTypePlatform
purchaseRole = model.PurchaseRolePurchasedByPlatform
actualPaidAmount = nil
actualPaidAmount = &buyerTotalCost
} else if req.PaymentMethod == model.PaymentMethodWallet {
// ==== 场景 2代理钱包支付wallet====
@@ -653,8 +649,7 @@ func (s *Service) CreateH5Order(ctx context.Context, req *dto.CreateOrderRequest
orderBuyerType = model.BuyerTypeAgent
orderBuyerID = operatorShopID
itemUnitPriceMap = operatorCostPriceMap
totalAmount = operatorTotalCost
itemCostPriceMap = operatorCostPriceMap
paymentMethod = model.PaymentMethodWallet
paymentStatus = model.PaymentStatusPaid
paidAt = &now
@@ -669,13 +664,13 @@ func (s *Service) CreateH5Order(ctx context.Context, req *dto.CreateOrderRequest
} else {
// ==== 子场景 2.2:代理代购(给下级购买)====
// 获取买家成本价
buyerCostPriceMap, buyerTotalCost, err := s.buildCostPriceMap(ctx, validationResult.Packages, *resourceShopID)
// 获取买家成本价(价格映射用于 CostPrice 快照,总价不再使用)
buyerCostPriceMap, _, err := s.buildCostPriceMap(ctx, validationResult.Packages, *resourceShopID)
if err != nil {
return nil, err
}
// 获取操作者成本价
// 获取操作者成本价(操作方实际扣款金额和佣金基准)
_, operatorTotalCost, err := s.buildCostPriceMap(ctx, validationResult.Packages, operatorShopID)
if err != nil {
return nil, err
@@ -683,8 +678,7 @@ func (s *Service) CreateH5Order(ctx context.Context, req *dto.CreateOrderRequest
orderBuyerType = model.BuyerTypeAgent
orderBuyerID = *resourceShopID
itemUnitPriceMap = buyerCostPriceMap
totalAmount = buyerTotalCost
itemCostPriceMap = buyerCostPriceMap
paymentMethod = model.PaymentMethodWallet
paymentStatus = model.PaymentStatusPaid
paidAt = &now
@@ -750,7 +744,7 @@ func (s *Service) CreateH5Order(ctx context.Context, req *dto.CreateOrderRequest
PaymentConfigID: h5PaymentConfigID,
}
items := s.buildOrderItems(userID, validationResult.Packages, itemUnitPriceMap)
items := s.buildOrderItems(userID, validationResult.Packages, itemUnitPriceMap, itemCostPriceMap)
idempotencyKey := buildOrderIdempotencyKey(buyerType, buyerID, req.OrderType, carrierType, carrierID, req.PackageIDs)
@@ -824,7 +818,9 @@ func (s *Service) resolvePurchaseOnBehalfInfo(ctx context.Context, result *purch
return *resourceShopID, buyerCostPriceMap, buyerTotalCost, &now, nil
}
func (s *Service) buildOrderItems(operatorID uint, packages []*model.Package, unitPriceMap map[uint]int64) []*model.OrderItem {
// buildOrderItems 构建订单明细列表
// retailPriceMap: 零售单价映射UnitPricecostPriceMap: 成本单价映射CostPrice无成本价时传 nil
func (s *Service) buildOrderItems(operatorID uint, packages []*model.Package, retailPriceMap map[uint]int64, costPriceMap map[uint]int64) []*model.OrderItem {
items := make([]*model.OrderItem, 0, len(packages))
for _, pkg := range packages {
if pkg == nil {
@@ -832,10 +828,15 @@ func (s *Service) buildOrderItems(operatorID uint, packages []*model.Package, un
}
unitPrice := packageprice.PackageEffectiveRetailPrice(pkg)
if price, ok := unitPriceMap[pkg.ID]; ok {
if price, ok := retailPriceMap[pkg.ID]; ok {
unitPrice = price
}
var costPrice int64
if costPriceMap != nil {
costPrice = costPriceMap[pkg.ID]
}
item := &model.OrderItem{
BaseModel: model.BaseModel{
Creator: operatorID,
@@ -846,6 +847,7 @@ func (s *Service) buildOrderItems(operatorID uint, packages []*model.Package, un
PackageType: &pkg.PackageType,
Quantity: 1,
UnitPrice: unitPrice,
CostPrice: costPrice,
Amount: unitPrice,
PackagePriceConfigStatus: pkg.PriceConfigStatus,
PackageIsGift: pkg.IsGift,
@@ -2295,6 +2297,7 @@ func (s *Service) activateMainPackage(ctx context.Context, tx *gorm.DB, order *m
// 创建套餐使用记录
virtualTotalMBSnapshot, displayGainRatioSnapshot, enableVirtualDataSnapshot := model.BuildPackageUsageSnapshotValues(pkg)
retailAmount := order.TotalAmount
usage := &model.PackageUsage{
BaseModel: model.BaseModel{
Creator: order.Creator,
@@ -2314,6 +2317,7 @@ func (s *Service) activateMainPackage(ctx context.Context, tx *gorm.DB, order *m
DataResetCycle: pkg.DataResetCycle,
PendingRealnameActivation: pendingRealnameActivation,
PaidAmount: order.ActualPaidAmount,
RetailAmount: &retailAmount,
PackagePriceConfigStatus: pkg.PriceConfigStatus,
PackageIsGift: pkg.IsGift,
}
@@ -2380,6 +2384,7 @@ func (s *Service) activateAddonPackage(ctx context.Context, tx *gorm.DB, order *
}
virtualTotalMBSnapshot, displayGainRatioSnapshot, enableVirtualDataSnapshot := model.BuildPackageUsageSnapshotValues(pkg)
addonRetailAmount := order.TotalAmount
usage := &model.PackageUsage{
BaseModel: model.BaseModel{
Creator: order.Creator,
@@ -2401,6 +2406,7 @@ func (s *Service) activateAddonPackage(ctx context.Context, tx *gorm.DB, order *
ExpiresAt: &expiresAt,
DataResetCycle: pkg.DataResetCycle,
PaidAmount: order.ActualPaidAmount,
RetailAmount: &addonRetailAmount,
PackagePriceConfigStatus: pkg.PriceConfigStatus,
PackageIsGift: pkg.IsGift,
}