fix(*): 支付与Agent运行 - 修复商品策略并保证审批恢复稳定
This commit is contained in:
1 parent
37bb4d7242
commit
3dd84ff25b
17 files changed
+1836
-226
No files matched your search
@@ -57,12 +57,18 @@ type UpdatePaymentProviderConfigRequest struct {
|
||||
}
|
||||
|
||||
type TopupProductRequest struct {
|
||||
Name string `json:"name"`
|
||||
Description string `json:"description"`
|
||||
AmountFen int64 `json:"amountFen"`
|
||||
CreditsMicrocredits int64 `json:"creditsMicrocredits"`
|
||||
Enabled bool `json:"enabled"`
|
||||
SortOrder int `json:"sortOrder"`
|
||||
Name string `json:"name"`
|
||||
Description string `json:"description"`
|
||||
AmountFen int64 `json:"amountFen"`
|
||||
CreditsMicrocredits int64 `json:"creditsMicrocredits"`
|
||||
Enabled bool `json:"enabled"`
|
||||
SortOrder int `json:"sortOrder"`
|
||||
SaleStrategy model.TopupSaleStrategy `json:"saleStrategy"`
|
||||
PeriodDays int `json:"periodDays"`
|
||||
PeriodPurchaseLimit int `json:"periodPurchaseLimit"`
|
||||
StockTotal int64 `json:"stockTotal"`
|
||||
SaleStartAt *time.Time `json:"saleStartAt"`
|
||||
SaleEndAt *time.Time `json:"saleEndAt"`
|
||||
}
|
||||
|
||||
type CreatePaymentOrderRequest struct {
|
||||
@@ -430,28 +436,63 @@ func (s *Service) TopupProducts(actor *model.User) ([]model.TopupProduct, error)
|
||||
if err := s.RequireFeature(FeatureCredits); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return s.repo.TopupProducts(false)
|
||||
products, err := s.repo.TopupProducts(false)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return decorateTopupProducts(products, time.Now()), nil
|
||||
}
|
||||
|
||||
func (s *Service) AdminTopupProducts(actor *model.User) ([]model.TopupProduct, error) {
|
||||
if err := s.RequireAdmin(actor); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return s.repo.TopupProducts(true)
|
||||
products, err := s.repo.TopupProducts(true)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return decorateTopupProducts(products, time.Now()), nil
|
||||
}
|
||||
|
||||
func decorateTopupProducts(products []model.TopupProduct, now time.Time) []model.TopupProduct {
|
||||
for index := range products {
|
||||
product := &products[index]
|
||||
if product.SaleStrategy == "" {
|
||||
product.SaleStrategy = model.TopupSaleStrategyUnlimited
|
||||
}
|
||||
product.CanPurchase = product.Enabled
|
||||
product.SaleStatus = "on_sale"
|
||||
if !product.Enabled {
|
||||
product.CanPurchase = false
|
||||
product.SaleStatus = "disabled"
|
||||
} else if product.SaleStrategy == model.TopupSaleStrategyInventory && product.StockRemaining <= 0 {
|
||||
product.CanPurchase = false
|
||||
product.SaleStatus = "sold_out"
|
||||
} else if product.SaleStrategy == model.TopupSaleStrategyTimed {
|
||||
if product.SaleStartAt != nil && now.Before(*product.SaleStartAt) {
|
||||
product.CanPurchase = false
|
||||
product.SaleStatus = "upcoming"
|
||||
} else if product.SaleEndAt != nil && !now.Before(*product.SaleEndAt) {
|
||||
product.CanPurchase = false
|
||||
product.SaleStatus = "ended"
|
||||
}
|
||||
}
|
||||
}
|
||||
return products
|
||||
}
|
||||
|
||||
func (s *Service) CreateTopupProduct(actor *model.User, request TopupProductRequest) (*model.TopupProduct, error) {
|
||||
if err := s.RequireAdmin(actor); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
product, err := topupProductFromRequest(newID(), actor.ID, request)
|
||||
product, err := topupProductFromRequest(newID(), actor.ID, request, 0, 0)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := s.repo.CreateTopupProduct(product); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := s.appendAdminAudit(actor, "topup_product.create", "topup_product", product.ID, "创建积分充值商品", map[string]any{"amountFen": product.AmountFen, "creditsMicrocredits": product.CreditsMicrocredits}); err != nil {
|
||||
if err := s.appendAdminAudit(actor, "topup_product.create", "topup_product", product.ID, "创建积分充值商品", map[string]any{"amountFen": product.AmountFen, "creditsMicrocredits": product.CreditsMicrocredits, "saleStrategy": product.SaleStrategy}); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return product, nil
|
||||
@@ -461,23 +502,32 @@ func (s *Service) UpdateTopupProduct(actor *model.User, id string, request Topup
|
||||
if err := s.RequireAdmin(actor); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if _, err := s.repo.TopupProduct(id); err != nil {
|
||||
existing, err := s.repo.TopupProduct(id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
product, err := topupProductFromRequest(strings.TrimSpace(id), actor.ID, request)
|
||||
consumed := existing.StockTotal - existing.StockRemaining
|
||||
if existing.SaleStrategy != model.TopupSaleStrategyInventory {
|
||||
consumed = 0
|
||||
}
|
||||
product, err := topupProductFromRequest(strings.TrimSpace(id), actor.ID, request, existing.StockTotal, consumed)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := s.repo.UpdateTopupProduct(product); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := s.appendAdminAudit(actor, "topup_product.update", "topup_product", product.ID, "更新积分充值商品", map[string]any{"enabled": product.Enabled}); err != nil {
|
||||
if err := s.appendAdminAudit(actor, "topup_product.update", "topup_product", product.ID, "更新积分充值商品", map[string]any{"enabled": product.Enabled, "saleStrategy": product.SaleStrategy}); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return s.repo.TopupProduct(product.ID)
|
||||
}
|
||||
|
||||
func topupProductFromRequest(id, actorID string, request TopupProductRequest) (*model.TopupProduct, error) {
|
||||
func topupProductFromRequest(id, actorID string, request TopupProductRequest, stockArgs ...int64) (*model.TopupProduct, error) {
|
||||
var consumed int64
|
||||
if len(stockArgs) > 1 {
|
||||
consumed = stockArgs[1]
|
||||
}
|
||||
name := strings.TrimSpace(request.Name)
|
||||
if name == "" || len([]rune(name)) > 120 {
|
||||
return nil, BadAuthRequest("充值商品名称不能为空且不能超过 120 个字符")
|
||||
@@ -488,10 +538,40 @@ func topupProductFromRequest(id, actorID string, request TopupProductRequest) (*
|
||||
if request.CreditsMicrocredits <= 0 || request.CreditsMicrocredits > maxTopupCreditsMicrocredits {
|
||||
return nil, BadAuthRequest("充值积分必须为 0.000001 至 10 亿积分")
|
||||
}
|
||||
strategy := request.SaleStrategy
|
||||
if strategy == "" {
|
||||
strategy = model.TopupSaleStrategyUnlimited
|
||||
}
|
||||
if strategy != model.TopupSaleStrategyUnlimited && strategy != model.TopupSaleStrategyPeriodic && strategy != model.TopupSaleStrategyInventory && strategy != model.TopupSaleStrategyTimed {
|
||||
return nil, BadAuthRequest("上架策略无效")
|
||||
}
|
||||
if strategy == model.TopupSaleStrategyPeriodic && (request.PeriodDays <= 0 || request.PeriodDays > 3650 || request.PeriodPurchaseLimit <= 0 || request.PeriodPurchaseLimit > 100000) {
|
||||
return nil, BadAuthRequest("周期性商品需设置有效的周期天数和购买次数")
|
||||
}
|
||||
if strategy == model.TopupSaleStrategyInventory && request.StockTotal <= 0 {
|
||||
return nil, BadAuthRequest("库存性商品库存必须大于 0")
|
||||
}
|
||||
if strategy == model.TopupSaleStrategyTimed && (request.SaleStartAt == nil || request.SaleEndAt == nil || !request.SaleEndAt.After(*request.SaleStartAt)) {
|
||||
return nil, BadAuthRequest("限时商品必须设置有效的发售和结束时间")
|
||||
}
|
||||
stockRemaining := int64(0)
|
||||
if strategy == model.TopupSaleStrategyInventory {
|
||||
if consumed < 0 {
|
||||
consumed = 0
|
||||
}
|
||||
if request.StockTotal < consumed {
|
||||
return nil, BadAuthRequest(fmt.Sprintf("库存不能低于已售数量(至少 %d)", consumed))
|
||||
}
|
||||
stockRemaining = request.StockTotal - consumed
|
||||
}
|
||||
return &model.TopupProduct{
|
||||
ID: id, Name: name, Description: truncateRunes(strings.TrimSpace(request.Description), 500),
|
||||
AmountFen: request.AmountFen, CreditsMicrocredits: request.CreditsMicrocredits,
|
||||
Enabled: request.Enabled, SortOrder: request.SortOrder, CreatedBy: actorID, UpdatedBy: actorID,
|
||||
Enabled: request.Enabled, SortOrder: request.SortOrder, SaleStrategy: strategy,
|
||||
PeriodDays: request.PeriodDays, PeriodPurchaseLimit: request.PeriodPurchaseLimit,
|
||||
StockTotal: request.StockTotal, StockRemaining: stockRemaining,
|
||||
SaleStartAt: request.SaleStartAt, SaleEndAt: request.SaleEndAt,
|
||||
CreatedBy: actorID, UpdatedBy: actorID,
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -551,7 +631,10 @@ func (s *Service) CreatePaymentOrder(ctx context.Context, actor *model.User, req
|
||||
Status: model.PaymentOrderCreated, CheckoutMode: provider.Descriptor().CheckoutMode,
|
||||
ExpiresAt: now.Add(time.Duration(config.CloseAfterMinutes) * time.Minute),
|
||||
}
|
||||
order, created, err := s.repo.CreatePaymentOrder(order)
|
||||
order, created, err := s.repo.CreatePaymentOrderWithProductReservation(order)
|
||||
if errors.Is(err, repository.ErrTopupUnavailable) {
|
||||
return nil, NewAppError(http.StatusConflict, "充值商品当前不可购买")
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user