赠送套餐逻辑
All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 7m55s

This commit is contained in:
2026-04-28 17:40:06 +08:00
parent 516a7f5bdf
commit 37b4483183
34 changed files with 1172 additions and 60 deletions

View File

@@ -4,12 +4,25 @@ import (
"context"
"github.com/break/junhong_cmp_fiber/internal/model"
"github.com/break/junhong_cmp_fiber/internal/service/packageprice"
"github.com/break/junhong_cmp_fiber/internal/store/postgres"
"github.com/break/junhong_cmp_fiber/pkg/constants"
"github.com/break/junhong_cmp_fiber/pkg/errors"
"gorm.io/gorm"
)
type allowGiftPackagesKey struct{}
// WithAllowGiftPackages 标记当前校验上下文允许后台赠送订单进入验证流程。
func WithAllowGiftPackages(ctx context.Context) context.Context {
return context.WithValue(ctx, allowGiftPackagesKey{}, true)
}
func canAllowGiftPackages(ctx context.Context) bool {
allowed, _ := ctx.Value(allowGiftPackagesKey{}).(bool)
return allowed
}
type Service struct {
db *gorm.DB
iotCardStore *postgres.IotCardStore
@@ -130,6 +143,11 @@ func (s *Service) validatePackages(ctx context.Context, packageIDs []uint, serie
if pkg.SeriesID != seriesID {
return nil, 0, errors.New(errors.CodeInvalidParam, "套餐不在可购买范围内")
}
if pkg.IsGift {
if sellerShopID > 0 || !canAllowGiftPackages(ctx) {
return nil, 0, errors.New(errors.CodeInvalidParam, "赠送套餐仅支持后台发放")
}
}
// Package.status 为全局开关,任何渠道都必须检查
if pkg.Status != constants.StatusEnabled {
@@ -142,16 +160,17 @@ func (s *Service) validatePackages(ctx context.Context, packageIDs []uint, serie
if allocErr != nil {
return nil, 0, allocErr
}
// 零售价低于成本价时视为不可购买,防止亏损售卖
if allocation.RetailPrice < allocation.CostPrice {
effectiveRetailPrice := packageprice.AllocationEffectiveRetailPrice(allocation)
// 生效零售价低于成本价时视为不可购买,防止亏损售卖
if effectiveRetailPrice < allocation.CostPrice {
return nil, 0, errors.New(errors.CodeInvalidParam, "套餐价格配置异常,暂不可购买")
}
totalPrice += allocation.RetailPrice
totalPrice += effectiveRetailPrice
} else {
if pkg.ShelfStatus != constants.ShelfStatusOn {
return nil, 0, errors.New(errors.CodeInvalidParam, "套餐已下架")
}
totalPrice += pkg.SuggestedRetailPrice
totalPrice += packageprice.PackageEffectiveRetailPrice(pkg)
}
packages = append(packages, pkg)
@@ -188,9 +207,9 @@ func (s *Service) GetPurchasePrice(ctx context.Context, pkg *model.Package, sell
}
return 0, errors.Wrap(errors.CodeInternalError, err, "查询套餐分配记录失败")
}
return allocation.RetailPrice, nil
return packageprice.AllocationEffectiveRetailPrice(allocation), nil
}
return pkg.SuggestedRetailPrice, nil
return packageprice.PackageEffectiveRetailPrice(pkg), nil
}
// GetCostPrice 获取代理渠道的套餐成本价