修复(#79): 合并internal并适配数量配价

Co-authored-by: multica-agent <github@multica.ai>
This commit is contained in:
2026-05-27 01:14:32 -07:00
37 changed files with 1813 additions and 431 deletions
+3 -4
View File
@@ -1011,17 +1011,16 @@ func RegisterHandlers(router *gin.Engine, serverCtx *svc.ServiceContext) {
}
publicSubscribeGroupRouter := router.Group("/v1/public/subscribe")
publicSubscribeGroupRouter.Use(middleware.AuthMiddleware(serverCtx), middleware.DeviceMiddleware(serverCtx))
{
// Get subscribe list
publicSubscribeGroupRouter.GET("/list", publicSubscribe.QuerySubscribeListHandler(serverCtx))
publicSubscribeGroupRouter.GET("/list", middleware.OptionalAuthMiddleware(serverCtx), middleware.DeviceMiddleware(serverCtx), publicSubscribe.QuerySubscribeListHandler(serverCtx))
// Get user subscribe node info
publicSubscribeGroupRouter.GET("/node/list", publicSubscribe.QueryUserSubscribeNodeListHandler(serverCtx))
publicSubscribeGroupRouter.GET("/node/list", middleware.AuthMiddleware(serverCtx), middleware.DeviceMiddleware(serverCtx), publicSubscribe.QueryUserSubscribeNodeListHandler(serverCtx))
// Get subscribe group list
publicSubscribeGroupRouter.GET("/group/list", publicSubscribe.QuerySubscribeGroupListHandler(serverCtx))
publicSubscribeGroupRouter.GET("/group/list", middleware.AuthMiddleware(serverCtx), middleware.DeviceMiddleware(serverCtx), publicSubscribe.QuerySubscribeGroupListHandler(serverCtx))
}
publicTicketGroupRouter := router.Group("/v1/public/ticket")
@@ -29,7 +29,7 @@ func (l *DeleteSubscribeApplicationLogic) DeleteSubscribeApplication(req *types.
err := l.svcCtx.ClientModel.Delete(l.ctx, req.Id)
if err != nil {
l.Errorf("Failed to delete subscribe application with ID %d: %v", req.Id, err)
return errors.Wrapf(xerr.NewErrCode(xerr.DatabaseDeletedError), err.Error())
return errors.Wrap(xerr.NewErrCode(xerr.DatabaseDeletedError), err.Error())
}
return nil
}
@@ -42,7 +42,7 @@ func (l *CreateRuleLogic) CreateRule(req *types.CreatePromoRuleRequest) (*types.
Type: req.Type,
Params: params,
Priority: req.Priority,
Enabled: &enabled,
Enabled: enabled,
StartTime: unixPtrToTimePtr(req.StartTime),
EndTime: unixPtrToTimePtr(req.EndTime),
}
+1 -5
View File
@@ -113,17 +113,13 @@ func convertRule(item *promomodel.Rule) types.PromoRule {
if item == nil {
return types.PromoRule{}
}
enabled := false
if item.Enabled != nil {
enabled = *item.Enabled
}
return types.PromoRule{
Id: item.Id,
Name: item.Name,
Type: item.Type,
Params: parseParams(item.Params),
Priority: item.Priority,
Enabled: enabled,
Enabled: item.Enabled,
StartTime: timePtrToUnixPtr(item.StartTime),
EndTime: timePtrToUnixPtr(item.EndTime),
CreatedAt: item.CreatedAt.Unix(),
@@ -37,10 +37,7 @@ func (l *UpdateRuleLogic) UpdateRule(req *types.UpdatePromoRuleRequest) (*types.
if err != nil {
return nil, err
}
enabled := true
if rule.Enabled != nil {
enabled = *rule.Enabled
}
enabled := rule.Enabled
if req.Enabled != nil {
enabled = *req.Enabled
}
@@ -48,7 +45,7 @@ func (l *UpdateRuleLogic) UpdateRule(req *types.UpdatePromoRuleRequest) (*types.
rule.Type = req.Type
rule.Params = params
rule.Priority = req.Priority
rule.Enabled = &enabled
rule.Enabled = enabled
rule.StartTime = unixPtrToTimePtr(req.StartTime)
rule.EndTime = unixPtrToTimePtr(req.EndTime)
if err := l.svcCtx.PromoModel.UpdateRule(l.ctx, rule); err != nil {
@@ -80,7 +80,7 @@ func (l *ResetSortWithNodeLogic) ResetSortWithNode(req *types.ResetSortRequest)
})
if err != nil {
l.Errorw("[NodeSort] Update Database Error: ", logger.Field("error", err.Error()))
return errors.Wrapf(xerr.NewErrCode(xerr.DatabaseUpdateError), err.Error())
return errors.Wrap(xerr.NewErrCode(xerr.DatabaseUpdateError), err.Error())
}
return nil
}
@@ -80,7 +80,7 @@ func (l *ResetSortWithServerLogic) ResetSortWithServer(req *types.ResetSortReque
})
if err != nil {
l.Errorw("[NodeSort] Update Database Error: ", logger.Field("error", err.Error()))
return errors.Wrapf(xerr.NewErrCode(xerr.DatabaseUpdateError), err.Error())
return errors.Wrap(xerr.NewErrCode(xerr.DatabaseUpdateError), err.Error())
}
return nil
}
+170
View File
@@ -0,0 +1,170 @@
package common
import (
"context"
"encoding/json"
"time"
"github.com/perfect-panel/server/internal/model/promo"
"github.com/perfect-panel/server/internal/model/user"
"github.com/perfect-panel/server/internal/svc"
"github.com/perfect-panel/server/pkg/xerr"
"github.com/pkg/errors"
"gorm.io/gorm"
)
type PromoResult struct {
Eligible bool
RuleID int64
RuleName string
RuleType string
PromoPrice int64
ExpiresAt time.Time
}
type promoRuleParams struct {
WindowHours int `json:"window_hours"`
InactiveMonths int `json:"inactive_months"`
}
func EvaluatePromo(ctx context.Context, svcCtx *svc.ServiceContext, userID int64, subscribeID int64, quantity int64) (*PromoResult, error) {
result := &PromoResult{}
if svcCtx == nil || svcCtx.PromoModel == nil || svcCtx.DB == nil || userID <= 0 || subscribeID <= 0 || quantity <= 0 {
return result, nil
}
rules, err := svcCtx.PromoModel.QueryEligibleRules(ctx, subscribeID, quantity)
if err != nil {
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DatabaseQueryError), "query promo rules failed: %v", err.Error())
}
if len(rules) == 0 {
return result, nil
}
var currentUser user.User
now := time.Now()
for _, rule := range rules {
if rule == nil || !isPromoRuleInTimeWindow(rule, now) {
continue
}
if rule.PromoPrice <= 0 {
continue
}
params := promoRuleParams{}
if rule.Params != "" {
if err = json.Unmarshal([]byte(rule.Params), &params); err != nil {
continue
}
}
eligible, expiresAt, err := evaluatePromoRule(ctx, svcCtx.DB, rule, params, userID, &currentUser, now)
if err != nil {
return nil, err
}
if !eligible {
continue
}
return &PromoResult{
Eligible: true,
RuleID: rule.Id,
RuleName: rule.Name,
RuleType: rule.Type,
PromoPrice: rule.PromoPrice,
ExpiresAt: expiresAt,
}, nil
}
return result, nil
}
func isPromoRuleInTimeWindow(rule *promo.RuleWithPrice, now time.Time) bool {
if rule.StartTime != nil && !rule.StartTime.IsZero() && now.Before(*rule.StartTime) {
return false
}
if rule.EndTime != nil && !rule.EndTime.IsZero() && now.After(*rule.EndTime) {
return false
}
return true
}
func evaluatePromoRule(
ctx context.Context,
db *gorm.DB,
rule *promo.RuleWithPrice,
params promoRuleParams,
userID int64,
currentUser *user.User,
now time.Time,
) (bool, time.Time, error) {
switch rule.Type {
case promo.RuleTypeNewUser:
return evaluateNewUserPromo(ctx, db, params, userID, currentUser, now)
case promo.RuleTypeInactiveUser:
return evaluateInactiveUserPromo(ctx, db, params, userID, promoRuleExpiresAt(rule), now)
case promo.RuleTypeCampaign:
return true, promoRuleExpiresAt(rule), nil
default:
return false, time.Time{}, nil
}
}
func evaluateNewUserPromo(
ctx context.Context,
db *gorm.DB,
params promoRuleParams,
userID int64,
currentUser *user.User,
now time.Time,
) (bool, time.Time, error) {
if params.WindowHours <= 0 {
return false, time.Time{}, nil
}
if currentUser.Id == 0 {
if err := db.WithContext(ctx).Model(&user.User{}).Where("id = ?", userID).First(currentUser).Error; err != nil {
return false, time.Time{}, errors.Wrapf(xerr.NewErrCode(xerr.DatabaseQueryError), "query promo user failed")
}
}
expiresAt := currentUser.CreatedAt.Add(time.Duration(params.WindowHours) * time.Hour)
return now.Before(expiresAt), expiresAt, nil
}
func evaluateInactiveUserPromo(
ctx context.Context,
db *gorm.DB,
params promoRuleParams,
userID int64,
ruleExpiresAt time.Time,
now time.Time,
) (bool, time.Time, error) {
if params.InactiveMonths <= 0 {
return false, time.Time{}, nil
}
var lastSub user.Subscribe
err := db.WithContext(ctx).
Model(&user.Subscribe{}).
Where("user_id = ?", userID).
Order("expire_time DESC").
Limit(1).
Take(&lastSub).Error
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return true, ruleExpiresAt, nil
}
return false, time.Time{}, errors.Wrapf(xerr.NewErrCode(xerr.DatabaseQueryError), "query promo inactive user subscription failed")
}
threshold := now.AddDate(0, -params.InactiveMonths, 0)
return lastSub.ExpireTime.Before(threshold) || lastSub.ExpireTime.Equal(threshold), ruleExpiresAt, nil
}
func promoRuleExpiresAt(rule *promo.RuleWithPrice) time.Time {
if rule != nil && rule.EndTime != nil {
return *rule.EndTime
}
return time.Time{}
}
@@ -61,11 +61,6 @@ func (l *FileUploadCompleteLogic) FileUploadComplete(req *types.FileUploadComple
}
return &types.FileUploadCompleteResponse{
FileId: meta.FileID,
ObjectKey: meta.ObjectKey,
Size: head.ContentLength,
ContentType: head.ContentType,
Etag: head.ETag,
Status: meta.Status,
Url: l.svcCtx.S3Store.BuildObjectURL(meta.ObjectKey),
}, nil
}
@@ -52,20 +52,13 @@ func (l *FileUploadLogic) FileUpload(req *types.FileUploadRequest, fileHeader *m
fileID := buildFileID(u.Id, req.BizType, fileHeader.Filename)
objectKey := buildObjectKey(l.svcCtx.Config.S3.Prefix, u.Id, req.BizType, fileID, fileHeader.Filename, now)
putResult, err := l.svcCtx.S3Store.PutObject(l.ctx, objectKey, file, fileHeader.Size, contentType)
if err != nil {
if _, err := l.svcCtx.S3Store.PutObject(l.ctx, objectKey, file, fileHeader.Size, contentType); err != nil {
l.Errorw("put object failed", logger.Field("error", err.Error()), logger.Field("user_id", u.Id), logger.Field("file_id", fileID))
return nil, err
}
return &types.FileUploadResponse{
FileId: fileID,
FileName: fileHeader.Filename,
ObjectKey: objectKey,
Size: fileHeader.Size,
ContentType: contentType,
Etag: putResult.ETag,
Status: fileUploadCompleteStatus,
Url: l.svcCtx.S3Store.BuildObjectURL(objectKey),
}, nil
}
@@ -2,7 +2,6 @@ package order
import (
"context"
"math"
commonLogic "github.com/perfect-panel/server/internal/logic/common"
"github.com/perfect-panel/server/internal/model/order"
@@ -115,14 +114,28 @@ func (l *PreCreateOrderLogic) PreCreateOrder(req *types.PurchaseOrderRequest) (r
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SubscribeNewUserOnly), "not a new user")
}
var discount float64 = 1
if len(newUserDiscount.Discounts) > 0 {
discount = getDiscount(newUserDiscount.Discounts, req.Quantity, newUserDiscount.EligibleForDiscount)
priceResult, err := calculatePurchasePrice(
l.ctx,
l.svcCtx,
u.Id,
targetSubscribeID,
sub.UnitPrice,
req.Quantity,
newUserDiscount.Discounts,
newUserDiscount.EligibleForDiscount,
!isSingleModeRenewal,
)
if err != nil {
l.Errorw("[PreCreateOrder] Promo price calculation error",
logger.Field("error", err.Error()),
logger.Field("user_id", u.Id),
logger.Field("subscribe_id", targetSubscribeID),
)
return nil, err
}
price := sub.UnitPrice * req.Quantity
amount := int64(math.Round(float64(price) * discount))
discountAmount := price - amount
price := priceResult.OriginalPrice
amount := priceResult.PayableBase
discountAmount := priceResult.DiscountAmount
var couponAmount int64
if req.Coupon != "" {
couponInfo, err := l.svcCtx.CouponModel.FindOneByCode(l.ctx, req.Coupon)
@@ -185,6 +198,7 @@ func (l *PreCreateOrderLogic) PreCreateOrder(req *types.PurchaseOrderRequest) (r
Price: price,
Amount: amount,
Discount: discountAmount,
PromoDiscount: priceResult.PromoDiscount,
GiftAmount: deductionAmount,
Coupon: req.Coupon,
CouponDiscount: couponAmount,
@@ -0,0 +1,62 @@
package order
import (
"context"
"math"
commonLogic "github.com/perfect-panel/server/internal/logic/common"
"github.com/perfect-panel/server/internal/svc"
"github.com/perfect-panel/server/internal/types"
)
type orderPriceResult struct {
OriginalPrice int64
PayableBase int64
DiscountAmount int64
PromoRuleId int64
PromoDiscount int64
PromoPrice int64
}
func calculatePurchasePrice(
ctx context.Context,
svcCtx *svc.ServiceContext,
userID int64,
subscribeID int64,
unitPrice int64,
quantity int64,
discounts []types.SubscribeDiscount,
eligibleForDiscount bool,
allowPromo bool,
) (*orderPriceResult, error) {
originalPrice := unitPrice * quantity
result := &orderPriceResult{
OriginalPrice: originalPrice,
PayableBase: originalPrice,
}
if allowPromo {
promoResult, err := commonLogic.EvaluatePromo(ctx, svcCtx, userID, subscribeID, quantity)
if err != nil {
return nil, err
}
if promoResult != nil && promoResult.Eligible && promoResult.PromoPrice < unitPrice {
result.PayableBase = promoResult.PromoPrice * quantity
result.PromoRuleId = promoResult.RuleID
result.PromoDiscount = originalPrice - result.PayableBase
result.PromoPrice = promoResult.PromoPrice
if result.PromoDiscount < 0 {
result.PromoDiscount = 0
}
return result, nil
}
}
discount := float64(1)
if len(discounts) > 0 {
discount = getDiscount(discounts, quantity, eligibleForDiscount)
}
result.PayableBase = int64(math.Round(float64(originalPrice) * discount))
result.DiscountAmount = originalPrice - result.PayableBase
return result, nil
}
@@ -0,0 +1,157 @@
package order
import (
"context"
"testing"
"github.com/perfect-panel/server/internal/model/promo"
"github.com/perfect-panel/server/internal/svc"
"github.com/perfect-panel/server/internal/types"
"gorm.io/gorm"
)
type fakePromoModel struct {
rules []*promo.RuleWithPrice
}
func (m fakePromoModel) QueryEligibleRules(context.Context, int64, int64) ([]*promo.RuleWithPrice, error) {
return m.rules, nil
}
func (m fakePromoModel) InsertUsage(context.Context, *promo.Usage, ...*gorm.DB) error {
return nil
}
func (m fakePromoModel) InsertRule(context.Context, *promo.Rule) error {
return nil
}
func (m fakePromoModel) FindRule(context.Context, int64) (*promo.Rule, error) {
return nil, gorm.ErrRecordNotFound
}
func (m fakePromoModel) UpdateRule(context.Context, *promo.Rule) error {
return nil
}
func (m fakePromoModel) DeleteRule(context.Context, int64) error {
return nil
}
func (m fakePromoModel) QueryRuleList(context.Context, int, int, string, *bool, string) (int64, []*promo.Rule, error) {
return 0, nil, nil
}
func (m fakePromoModel) UpsertPrices(context.Context, int64, []*promo.SubscribePromo) error {
return nil
}
func (m fakePromoModel) FindPrice(context.Context, int64) (*promo.SubscribePromo, error) {
return nil, gorm.ErrRecordNotFound
}
func (m fakePromoModel) DeletePrice(context.Context, int64) error {
return nil
}
func (m fakePromoModel) QueryPriceList(context.Context, int64, int, int) (int64, []*promo.SubscribePromo, error) {
return 0, nil, nil
}
func (m fakePromoModel) QueryUsageList(context.Context, promo.UsageFilter) (int64, []*promo.Usage, error) {
return 0, nil, nil
}
func (m fakePromoModel) Transaction(context.Context, func(*gorm.DB) error) error {
return nil
}
func TestCalculatePurchasePricePromoSkipsPercentDiscount(t *testing.T) {
svcCtx := &svc.ServiceContext{
DB: &gorm.DB{},
PromoModel: fakePromoModel{rules: []*promo.RuleWithPrice{
{
Rule: promo.Rule{
Id: 9,
Name: "campaign",
Type: promo.RuleTypeCampaign,
Enabled: true,
},
PromoPrice: 600,
},
}},
}
result, err := calculatePurchasePrice(
context.Background(),
svcCtx,
1,
2,
1000,
3,
[]types.SubscribeDiscount{{Quantity: 3, Discount: 50}},
true,
true,
)
if err != nil {
t.Fatalf("calculatePurchasePrice returned error: %v", err)
}
if result.OriginalPrice != 3000 {
t.Fatalf("OriginalPrice = %d, want 3000", result.OriginalPrice)
}
if result.PayableBase != 1800 {
t.Fatalf("PayableBase = %d, want 1800", result.PayableBase)
}
if result.DiscountAmount != 0 {
t.Fatalf("DiscountAmount = %d, want 0", result.DiscountAmount)
}
if result.PromoRuleId != 9 {
t.Fatalf("PromoRuleId = %d, want 9", result.PromoRuleId)
}
if result.PromoDiscount != 1200 {
t.Fatalf("PromoDiscount = %d, want 1200", result.PromoDiscount)
}
}
func TestCalculatePurchasePriceIgnoresInvalidPromoPrice(t *testing.T) {
svcCtx := &svc.ServiceContext{
DB: &gorm.DB{},
PromoModel: fakePromoModel{rules: []*promo.RuleWithPrice{
{
Rule: promo.Rule{
Id: 10,
Name: "invalid campaign",
Type: promo.RuleTypeCampaign,
Enabled: true,
},
PromoPrice: 1000,
},
}},
}
result, err := calculatePurchasePrice(
context.Background(),
svcCtx,
1,
2,
1000,
3,
[]types.SubscribeDiscount{{Quantity: 3, Discount: 50}},
true,
true,
)
if err != nil {
t.Fatalf("calculatePurchasePrice returned error: %v", err)
}
if result.PayableBase != 1500 {
t.Fatalf("PayableBase = %d, want 1500", result.PayableBase)
}
if result.DiscountAmount != 1500 {
t.Fatalf("DiscountAmount = %d, want 1500", result.DiscountAmount)
}
if result.PromoRuleId != 0 || result.PromoDiscount != 0 {
t.Fatalf("promo fields = (%d, %d), want (0, 0)", result.PromoRuleId, result.PromoDiscount)
}
}
+23 -8
View File
@@ -3,7 +3,6 @@ package order
import (
"context"
"encoding/json"
"math"
"strings"
"time"
@@ -204,14 +203,28 @@ func (l *PurchaseLogic) Purchase(req *types.PurchaseOrderRequest) (resp *types.P
return nil, err
}
var discount float64 = 1
if len(newUserDiscount.Discounts) > 0 {
discount = getDiscount(newUserDiscount.Discounts, req.Quantity, newUserDiscount.EligibleForDiscount)
priceResult, err := calculatePurchasePrice(
l.ctx,
l.svcCtx,
u.Id,
targetSubscribeID,
sub.UnitPrice,
req.Quantity,
newUserDiscount.Discounts,
newUserDiscount.EligibleForDiscount,
orderType == 1,
)
if err != nil {
l.Errorw("[Purchase] Promo price calculation error",
logger.Field("error", err.Error()),
logger.Field("user_id", u.Id),
logger.Field("subscribe_id", targetSubscribeID),
)
return nil, err
}
price := sub.UnitPrice * req.Quantity
// discount amount
amount := int64(math.Round(float64(price) * discount))
discountAmount := price - amount
price := priceResult.OriginalPrice
amount := priceResult.PayableBase
discountAmount := priceResult.DiscountAmount
// Validate amount to prevent overflow
if amount > MaxOrderAmount {
@@ -306,6 +319,8 @@ func (l *PurchaseLogic) Purchase(req *types.PurchaseOrderRequest) (resp *types.P
Price: price,
Amount: amount,
Discount: discountAmount,
PromoRuleId: priceResult.PromoRuleId,
PromoDiscount: priceResult.PromoDiscount,
GiftAmount: deductionAmount,
Coupon: req.Coupon,
CouponDiscount: coupon,
+224
View File
@@ -0,0 +1,224 @@
package subscribe
import (
"context"
"encoding/json"
stderrors "errors"
"strings"
"time"
"github.com/go-sql-driver/mysql"
"github.com/perfect-panel/server/internal/model/user"
"github.com/perfect-panel/server/internal/svc"
"github.com/perfect-panel/server/internal/types"
"github.com/perfect-panel/server/pkg/constant"
"github.com/perfect-panel/server/pkg/xerr"
"github.com/pkg/errors"
"gorm.io/gorm"
)
const (
promoRuleTypeNewUser = "new_user"
promoRuleTypeInactiveUser = "inactive_user"
promoRuleTypeCampaign = "campaign"
)
type subscribePromoCandidate struct {
SubscribeId int64 `gorm:"column:subscribe_id"`
RuleName string `gorm:"column:rule_name"`
RuleType string `gorm:"column:rule_type"`
PromoPrice int64 `gorm:"column:promo_price"`
Params string `gorm:"column:params"`
StartTime *time.Time `gorm:"column:start_time"`
EndTime *time.Time `gorm:"column:end_time"`
}
type promoRuleParams struct {
WindowHours int64 `json:"window_hours"`
InactiveMonths int `json:"inactive_months"`
}
func loadSubscribePromoMap(ctx context.Context, svcCtx *svc.ServiceContext, subscribeIDs []int64) (map[int64]*types.SubscribePromo, error) {
result := make(map[int64]*types.SubscribePromo)
if len(subscribeIDs) == 0 || svcCtx == nil || svcCtx.DB == nil {
return result, nil
}
userInfo, _ := ctx.Value(constant.CtxKeyUser).(*user.User)
candidates, err := querySubscribePromoCandidates(ctx, svcCtx, subscribeIDs, userInfo != nil)
if err != nil {
if isMissingPromoTableError(err) {
return result, nil
}
return nil, err
}
evaluator := promoEligibilityEvaluator{ctx: ctx, db: svcCtx.DB, userInfo: userInfo}
now := time.Now()
for _, candidate := range candidates {
if _, exists := result[candidate.SubscribeId]; exists {
continue
}
if !candidate.isActive(now) {
continue
}
ok, expiresAt, err := evaluator.match(candidate, now)
if err != nil {
return nil, err
}
if !ok {
continue
}
result[candidate.SubscribeId] = &types.SubscribePromo{
RuleName: candidate.RuleName,
RuleType: candidate.RuleType,
PromoPrice: candidate.PromoPrice,
ExpiresAt: unixSeconds(expiresAt),
}
}
return result, nil
}
func querySubscribePromoCandidates(ctx context.Context, svcCtx *svc.ServiceContext, subscribeIDs []int64, loggedIn bool) ([]subscribePromoCandidate, error) {
var candidates []subscribePromoCandidate
query := svcCtx.DB.WithContext(ctx).
Table("subscribe_promo AS sp").
Select("sp.subscribe_id, sp.promo_price, pr.name AS rule_name, pr.type AS rule_type, pr.params, pr.start_time, pr.end_time").
Joins("JOIN promo_rule AS pr ON pr.id = sp.promo_rule_id AND pr.deleted_at IS NULL").
Where("sp.subscribe_id IN ? AND sp.promo_price > 0 AND pr.enabled = ?", subscribeIDs, true)
if !loggedIn {
query = query.Where("pr.type = ?", promoRuleTypeCampaign)
}
err := query.
Order("sp.subscribe_id ASC").
Order("pr.priority DESC").
Order("pr.id ASC").
Scan(&candidates).Error
if err != nil {
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DatabaseQueryError), "query subscribe promo candidates failed: %v", err)
}
return candidates, nil
}
func (c subscribePromoCandidate) isActive(now time.Time) bool {
if c.PromoPrice <= 0 {
return false
}
if c.StartTime != nil && now.Before(*c.StartTime) {
return false
}
if c.EndTime != nil && now.After(*c.EndTime) {
return false
}
return true
}
type promoEligibilityEvaluator struct {
ctx context.Context
db *gorm.DB
userInfo *user.User
lastExpire *time.Time
}
func (e *promoEligibilityEvaluator) match(candidate subscribePromoCandidate, now time.Time) (bool, time.Time, error) {
switch candidate.RuleType {
case promoRuleTypeCampaign:
return true, candidate.expiresAt(), nil
case promoRuleTypeNewUser:
if e.userInfo == nil {
return false, time.Time{}, nil
}
params, err := candidate.params()
if err != nil {
return false, time.Time{}, err
}
if params.WindowHours <= 0 || e.userInfo.CreatedAt.IsZero() {
return false, time.Time{}, nil
}
expiresAt := e.userInfo.CreatedAt.Add(time.Duration(params.WindowHours) * time.Hour)
return now.Before(expiresAt), expiresAt, nil
case promoRuleTypeInactiveUser:
if e.userInfo == nil {
return false, time.Time{}, nil
}
params, err := candidate.params()
if err != nil {
return false, time.Time{}, err
}
if params.InactiveMonths <= 0 {
return false, time.Time{}, nil
}
lastExpire, err := e.lastSubscribeExpireAt()
if err != nil {
return false, time.Time{}, err
}
if lastExpire.Equal(time.UnixMilli(0)) || lastExpire.After(now) {
return false, time.Time{}, nil
}
if lastExpire.IsZero() {
return true, candidate.expiresAt(), nil
}
threshold := now.AddDate(0, -params.InactiveMonths, 0)
return !lastExpire.After(threshold), candidate.expiresAt(), nil
default:
return false, time.Time{}, nil
}
}
func (e *promoEligibilityEvaluator) lastSubscribeExpireAt() (time.Time, error) {
if e.lastExpire != nil {
return *e.lastExpire, nil
}
var item user.Subscribe
err := e.db.WithContext(e.ctx).
Model(&user.Subscribe{}).
Where("user_id = ?", e.userInfo.Id).
Where("expire_time != ?", time.UnixMilli(0)).
Order("expire_time DESC").
Limit(1).
Take(&item).Error
if err != nil {
if stderrors.Is(err, gorm.ErrRecordNotFound) {
zero := time.Time{}
e.lastExpire = &zero
return zero, nil
}
return time.Time{}, errors.Wrapf(xerr.NewErrCode(xerr.DatabaseQueryError), "query promo user last subscription failed")
}
e.lastExpire = &item.ExpireTime
return item.ExpireTime, nil
}
func (c subscribePromoCandidate) expiresAt() time.Time {
if c.EndTime == nil {
return time.Time{}
}
return *c.EndTime
}
func (c subscribePromoCandidate) params() (promoRuleParams, error) {
if c.Params == "" {
return promoRuleParams{}, nil
}
var params promoRuleParams
if err := json.Unmarshal([]byte(c.Params), &params); err != nil {
return promoRuleParams{}, errors.Wrapf(xerr.NewErrCode(xerr.InvalidParams), "parse promo rule params failed")
}
return params, nil
}
func unixSeconds(t time.Time) int64 {
if t.IsZero() {
return 0
}
return t.Unix()
}
func isMissingPromoTableError(err error) bool {
var mysqlErr *mysql.MySQLError
if stderrors.As(err, &mysqlErr) {
return mysqlErr.Number == 1146
}
return strings.Contains(err.Error(), "Error 1146")
}
@@ -0,0 +1,75 @@
package subscribe
import (
"testing"
"time"
"github.com/perfect-panel/server/internal/model/user"
)
func TestPromoEligibilityEvaluatorMatch(t *testing.T) {
now := time.Unix(1710000000, 0)
campaignEnd := now.Add(2 * time.Hour)
campaign := subscribePromoCandidate{
RuleName: "限时活动",
RuleType: promoRuleTypeCampaign,
PromoPrice: 99,
EndTime: &campaignEnd,
}
ok, expiresAt, err := (&promoEligibilityEvaluator{}).match(campaign, now)
if err != nil {
t.Fatalf("campaign match error: %v", err)
}
if !ok {
t.Fatal("campaign promo should match without login")
}
if got, want := unixSeconds(expiresAt), campaignEnd.Unix(); got != want {
t.Fatalf("campaign expires_at = %d, want %d", got, want)
}
newUser := subscribePromoCandidate{
RuleName: "新客7天优惠",
RuleType: promoRuleTypeNewUser,
PromoPrice: 279,
Params: `{"window_hours":168}`,
}
ok, _, err = (&promoEligibilityEvaluator{}).match(newUser, now)
if err != nil {
t.Fatalf("anonymous new_user match error: %v", err)
}
if ok {
t.Fatal("new_user promo should not match without login")
}
userInfo := &user.User{Id: 1, CreatedAt: now.Add(-24 * time.Hour)}
ok, expiresAt, err = (&promoEligibilityEvaluator{userInfo: userInfo}).match(newUser, now)
if err != nil {
t.Fatalf("logged-in new_user match error: %v", err)
}
if !ok {
t.Fatal("new_user promo should match inside window")
}
if got, want := unixSeconds(expiresAt), userInfo.CreatedAt.Add(168*time.Hour).Unix(); got != want {
t.Fatalf("new_user expires_at = %d, want %d", got, want)
}
}
func TestSubscribePromoCandidateActiveWindow(t *testing.T) {
now := time.Unix(1710000000, 0)
start := now.Add(-time.Hour)
end := now.Add(time.Hour)
if !(subscribePromoCandidate{PromoPrice: 1, StartTime: &start, EndTime: &end}).isActive(now) {
t.Fatal("candidate inside active window should be active")
}
if (subscribePromoCandidate{PromoPrice: 0, StartTime: &start, EndTime: &end}).isActive(now) {
t.Fatal("candidate with zero promo price should not be active")
}
if (subscribePromoCandidate{PromoPrice: 1, StartTime: &end}).isActive(now) {
t.Fatal("candidate before start time should not be active")
}
if (subscribePromoCandidate{PromoPrice: 1, EndTime: &start}).isActive(now) {
t.Fatal("candidate after end time should not be active")
}
}
@@ -47,9 +47,11 @@ func (l *QuerySubscribeListLogic) QuerySubscribeList(req *types.QuerySubscribeLi
Total: total,
}
list := make([]types.Subscribe, len(data))
subscribeIDs := make([]int64, 0, len(data))
for i, item := range data {
var sub types.Subscribe
tool.DeepCopy(&sub, item)
subscribeIDs = append(subscribeIDs, sub.Id)
if item.Discount != "" {
var discount []types.SubscribeDiscount
_ = json.Unmarshal([]byte(item.Discount), &discount)
@@ -69,6 +71,15 @@ func (l *QuerySubscribeListLogic) QuerySubscribeList(req *types.QuerySubscribeLi
}
}
promos, err := loadSubscribePromoMap(l.ctx, l.svcCtx, subscribeIDs)
if err != nil {
l.Errorw("[QuerySubscribeListLogic] Query Promo Error", logger.Field("error", err.Error()))
return nil, err
}
for i := range list {
list[i].Promo = promos[list[i].Id]
}
resp.List = list
resp.Total = int64(len(list))
return
@@ -46,8 +46,8 @@ func (l *CommissionWithdrawLogic) CommissionWithdraw(req *types.CommissionWithdr
return nil, errors.Wrapf(xerr.NewErrCode(xerr.InvalidParams), "account is required for bank transfer")
}
default: // WithdrawalMethodOther
if req.Account == "" && req.Content == "" {
return nil, errors.Wrapf(xerr.NewErrCode(xerr.InvalidParams), "account or content is required for other methods")
if req.Account == "" {
return nil, errors.Wrapf(xerr.NewErrCode(xerr.InvalidParams), "account is required for other methods")
}
}
@@ -39,14 +39,14 @@ func (l *DeviceWsConnectLogic) DeviceWsConnect(c *gin.Context) error {
value, _ = c.GetQuery("identifier")
if value == nil || value.(string) == "" {
l.Errorf("DeviceWsConnectLogic DeviceWsConnect identifier is empty")
return errors.Wrapf(xerr.NewErrCode(xerr.InvalidParams), "identifier is empty")
return errors.Wrap(xerr.NewErrCode(xerr.InvalidParams), "identifier is empty")
}
}
identifier := value.(string)
_, err := l.svcCtx.UserModel.FindOneDeviceByIdentifier(l.ctx, identifier)
if err != nil && !sysErr.Is(err, gorm.ErrRecordNotFound) {
l.Errorf("DeviceWsConnectLogic DeviceWsConnect FindOneDeviceByIdentifier err: %v", err)
return errors.Wrapf(xerr.NewErrCode(xerr.DatabaseQueryError), err.Error())
return errors.Wrap(xerr.NewErrCode(xerr.DatabaseQueryError), err.Error())
}
value = l.ctx.Value(constant.CtxKeyUser)
@@ -67,7 +67,7 @@ func (l *DeviceWsConnectLogic) DeviceWsConnect(c *gin.Context) error {
err := l.svcCtx.UserModel.InsertDevice(l.ctx, &device)
if err != nil {
l.Errorf("DeviceWsConnectLogic DeviceWsConnect InsertDevice err: %v", err)
return errors.Wrapf(xerr.NewErrCode(xerr.DatabaseInsertError), err.Error())
return errors.Wrap(xerr.NewErrCode(xerr.DatabaseInsertError), err.Error())
}
}
//默认在线设备1
+78 -63
View File
@@ -22,77 +22,92 @@ import (
func AuthMiddleware(svc *svc.ServiceContext) func(c *gin.Context) {
return func(c *gin.Context) {
ctx := c.Request.Context()
if !authenticateRequest(c, svc, c.GetHeader("Authorization"), true) {
return
}
c.Next()
}
}
jwtConfig := svc.Config.JwtAuth
// get token from header
func OptionalAuthMiddleware(svc *svc.ServiceContext) func(c *gin.Context) {
return func(c *gin.Context) {
token := c.GetHeader("Authorization")
if token == "" {
c.Next()
return
}
if !authenticateRequest(c, svc, token, false) {
return
}
c.Next()
}
}
func authenticateRequest(c *gin.Context, svc *svc.ServiceContext, token string, requireToken bool) bool {
ctx := c.Request.Context()
if token == "" {
if requireToken {
logger.WithContext(c.Request.Context()).Debug("[AuthMiddleware] Token Empty")
result.HttpResult(c, nil, errors.Wrapf(xerr.NewErrCode(xerr.ErrorTokenEmpty), "Token Empty"))
c.Abort()
return
}
// parse token
claims, err := jwt.ParseJwtToken(token, jwtConfig.AccessSecret)
if err != nil {
logger.WithContext(c.Request.Context()).Debug("[AuthMiddleware] ParseJwtToken", logger.Field("error", err.Error()), logger.Field("token", token))
result.HttpResult(c, nil, errors.Wrapf(xerr.NewErrCode(xerr.ErrorTokenExpire), "Token Invalid"))
c.Abort()
return
}
loginType := parseLoginType(claims)
if claims["identifier"] != nil {
ctx = context.WithValue(ctx, constant.CtxKeyIdentifier, claims["identifier"].(string))
}
// get user id from token
userId := int64(claims["UserId"].(float64))
// get session id from token
sessionId := claims["SessionId"].(string)
// get session id from redis
sessionIdCacheKey := fmt.Sprintf("%v:%v", config.SessionIdKey, sessionId)
value, err := svc.Redis.Get(c, sessionIdCacheKey).Result()
if err != nil {
logger.WithContext(c.Request.Context()).Debug("[AuthMiddleware] Redis Get", logger.Field("error", err.Error()), logger.Field("sessionId", sessionId))
result.HttpResult(c, nil, errors.Wrapf(xerr.NewErrCode(xerr.InvalidAccess), "Invalid Access"))
c.Abort()
return
}
//verify user id
if value != fmt.Sprintf("%v", userId) {
logger.WithContext(c.Request.Context()).Debug("[AuthMiddleware] Invalid Access", logger.Field("userId", userId), logger.Field("sessionId", sessionId))
result.HttpResult(c, nil, errors.Wrapf(xerr.NewErrCode(xerr.InvalidAccess), "Invalid Access"))
c.Abort()
return
}
// sliding session: refresh TTL on every active request
svc.Redis.Expire(c, sessionIdCacheKey, time.Duration(svc.Config.JwtAuth.AccessExpire)*time.Second)
userInfo, err := svc.UserModel.FindOne(c, userId)
if err != nil {
logger.WithContext(c.Request.Context()).Debug("[AuthMiddleware] UserModel FindOne", logger.Field("error", err.Error()), logger.Field("userId", userId))
result.HttpResult(c, nil, errors.Wrapf(xerr.NewErrCode(xerr.DatabaseQueryError), "Database Query Error"))
c.Abort()
return
}
// admin verify
paths := strings.Split(c.Request.URL.Path, "/")
if tool.StringSliceContains(paths, "admin") && !*userInfo.IsAdmin {
logger.WithContext(c.Request.Context()).Debug("[AuthMiddleware] Not Admin User", logger.Field("userId", userId), logger.Field("sessionId", sessionId))
result.HttpResult(c, nil, errors.Wrapf(xerr.NewErrCode(xerr.InvalidAccess), "Invalid Access"))
c.Abort()
return
}
ctx = context.WithValue(ctx, constant.CtxLoginType, loginType)
ctx = context.WithValue(ctx, constant.CtxKeyUser, userInfo)
ctx = context.WithValue(ctx, constant.CtxKeySessionID, sessionId)
c.Request = c.Request.WithContext(ctx)
c.Next()
return !requireToken
}
claims, err := jwt.ParseJwtToken(token, svc.Config.JwtAuth.AccessSecret)
if err != nil {
logger.WithContext(c.Request.Context()).Debug("[AuthMiddleware] ParseJwtToken", logger.Field("error", err.Error()), logger.Field("token", token))
result.HttpResult(c, nil, errors.Wrapf(xerr.NewErrCode(xerr.ErrorTokenExpire), "Token Invalid"))
c.Abort()
return false
}
loginType := parseLoginType(claims)
if claims["identifier"] != nil {
ctx = context.WithValue(ctx, constant.CtxKeyIdentifier, claims["identifier"].(string))
}
userId := int64(claims["UserId"].(float64))
sessionId := claims["SessionId"].(string)
sessionIdCacheKey := fmt.Sprintf("%v:%v", config.SessionIdKey, sessionId)
value, err := svc.Redis.Get(c, sessionIdCacheKey).Result()
if err != nil {
logger.WithContext(c.Request.Context()).Debug("[AuthMiddleware] Redis Get", logger.Field("error", err.Error()), logger.Field("sessionId", sessionId))
result.HttpResult(c, nil, errors.Wrapf(xerr.NewErrCode(xerr.InvalidAccess), "Invalid Access"))
c.Abort()
return false
}
if value != fmt.Sprintf("%v", userId) {
logger.WithContext(c.Request.Context()).Debug("[AuthMiddleware] Invalid Access", logger.Field("userId", userId), logger.Field("sessionId", sessionId))
result.HttpResult(c, nil, errors.Wrapf(xerr.NewErrCode(xerr.InvalidAccess), "Invalid Access"))
c.Abort()
return false
}
svc.Redis.Expire(c, sessionIdCacheKey, time.Duration(svc.Config.JwtAuth.AccessExpire)*time.Second)
userInfo, err := svc.UserModel.FindOne(c, userId)
if err != nil {
logger.WithContext(c.Request.Context()).Debug("[AuthMiddleware] UserModel FindOne", logger.Field("error", err.Error()), logger.Field("userId", userId))
result.HttpResult(c, nil, errors.Wrapf(xerr.NewErrCode(xerr.DatabaseQueryError), "Database Query Error"))
c.Abort()
return false
}
paths := strings.Split(c.Request.URL.Path, "/")
if tool.StringSliceContains(paths, "admin") && !*userInfo.IsAdmin {
logger.WithContext(c.Request.Context()).Debug("[AuthMiddleware] Not Admin User", logger.Field("userId", userId), logger.Field("sessionId", sessionId))
result.HttpResult(c, nil, errors.Wrapf(xerr.NewErrCode(xerr.InvalidAccess), "Invalid Access"))
c.Abort()
return false
}
ctx = context.WithValue(ctx, constant.CtxLoginType, loginType)
ctx = context.WithValue(ctx, constant.CtxKeyUser, userInfo)
ctx = context.WithValue(ctx, constant.CtxKeySessionID, sessionId)
c.Request = c.Request.WithContext(ctx)
return true
}
func parseLoginType(claims map[string]interface{}) string {
+2
View File
@@ -22,6 +22,8 @@ type Details struct {
Price int64 `gorm:"type:int;not null;default:0;comment:Original price"`
Amount int64 `gorm:"type:int;not null;default:0;comment:Order Amount"`
Discount int64 `gorm:"type:int;not null;default:0;comment:Order Discount"`
PromoRuleId int64 `gorm:"type:bigint unsigned;not null;default:0;comment:Promo Rule ID"`
PromoDiscount int64 `gorm:"type:bigint;not null;default:0;comment:Promo Discount Amount"`
Coupon string `gorm:"type:varchar(255);default:null;comment:Coupon"`
CouponDiscount int64 `gorm:"type:int;not null;default:0;comment:Coupon Discount"`
PaymentId int64 `gorm:"type:bigint;not null;default:0;comment:Payment Id"`
+26 -24
View File
@@ -3,32 +3,34 @@ package order
import "time"
type Order struct {
Id int64 `gorm:"primaryKey"`
ParentId int64 `gorm:"type:bigint;default:null;comment:Parent Order Id"`
Id int64 `gorm:"primaryKey"`
ParentId int64 `gorm:"type:bigint;default:null;comment:Parent Order Id"`
UserId int64 `gorm:"type:bigint;not null;default:0;comment:User Id"`
SubscriptionUserId int64 `gorm:"type:bigint;not null;default:0;comment:Target user ID for subscription (0=same as UserId)"`
OrderNo string `gorm:"type:varchar(255);not null;default:'';unique;comment:Order No"`
Type uint8 `gorm:"type:tinyint(1);not null;default:1;comment:Order Type: 1: Subscribe, 2: Renewal, 3: ResetTraffic, 4: Recharge"`
Quantity int64 `gorm:"type:bigint;not null;default:1;comment:Quantity"`
Price int64 `gorm:"type:int;not null;default:0;comment:Original price"`
Amount int64 `gorm:"type:int;not null;default:0;comment:Order Amount"`
GiftAmount int64 `gorm:"type:int;not null;default:0;comment:User Gift Amount"`
Discount int64 `gorm:"type:int;not null;default:0;comment:Discount Amount"`
Coupon string `gorm:"type:varchar(255);default:null;comment:Coupon"`
CouponDiscount int64 `gorm:"type:int;not null;default:0;comment:Coupon Discount Amount"`
Commission int64 `gorm:"type:int;not null;default:0;comment:Order Commission"`
PaymentId int64 `gorm:"type:bigint;not null;default:0;comment:Payment Method Id"`
Method string `gorm:"type:varchar(255);not null;default:'';comment:Payment Method"`
FeeAmount int64 `gorm:"type:int;not null;default:0;comment:Fee Amount"`
TradeNo string `gorm:"type:varchar(255);default:null;comment:Trade No"`
Status uint8 `gorm:"type:tinyint(1);not null;default:1;comment:Order Status: 1: Pending, 2: Paid, 3:Close, 4: Failed, 5:Finished;"`
SubscribeId int64 `gorm:"type:bigint;not null;default:0;comment:Subscribe Id"`
SubscribeToken string `gorm:"type:varchar(255);default:null;comment:Renewal Subscribe Token"`
AppAccountToken string `gorm:"type:varchar(36);default:null;comment:Apple IAP App Account Token (UUID)"`
ActivationContext string `gorm:"type:text;default:null;comment:Activation context JSON (guest/redemption info for DB fallback)"`
IsNew bool `gorm:"type:tinyint(1);not null;default:0;comment:Is New Order"`
CreatedAt time.Time `gorm:"<-:create;comment:Create Time"`
UpdatedAt time.Time `gorm:"comment:Update Time"`
OrderNo string `gorm:"type:varchar(255);not null;default:'';unique;comment:Order No"`
Type uint8 `gorm:"type:tinyint(1);not null;default:1;comment:Order Type: 1: Subscribe, 2: Renewal, 3: ResetTraffic, 4: Recharge"`
Quantity int64 `gorm:"type:bigint;not null;default:1;comment:Quantity"`
Price int64 `gorm:"type:int;not null;default:0;comment:Original price"`
Amount int64 `gorm:"type:int;not null;default:0;comment:Order Amount"`
GiftAmount int64 `gorm:"type:int;not null;default:0;comment:User Gift Amount"`
Discount int64 `gorm:"type:int;not null;default:0;comment:Discount Amount"`
PromoRuleId int64 `gorm:"type:bigint unsigned;not null;default:0;comment:Promo Rule ID"`
PromoDiscount int64 `gorm:"type:bigint;not null;default:0;comment:Promo Discount Amount"`
Coupon string `gorm:"type:varchar(255);default:null;comment:Coupon"`
CouponDiscount int64 `gorm:"type:int;not null;default:0;comment:Coupon Discount Amount"`
Commission int64 `gorm:"type:int;not null;default:0;comment:Order Commission"`
PaymentId int64 `gorm:"type:bigint;not null;default:0;comment:Payment Method Id"`
Method string `gorm:"type:varchar(255);not null;default:'';comment:Payment Method"`
FeeAmount int64 `gorm:"type:int;not null;default:0;comment:Fee Amount"`
TradeNo string `gorm:"type:varchar(255);default:null;comment:Trade No"`
Status uint8 `gorm:"type:tinyint(1);not null;default:1;comment:Order Status: 1: Pending, 2: Paid, 3:Close, 4: Failed, 5:Finished;"`
SubscribeId int64 `gorm:"type:bigint;not null;default:0;comment:Subscribe Id"`
SubscribeToken string `gorm:"type:varchar(255);default:null;comment:Renewal Subscribe Token"`
AppAccountToken string `gorm:"type:varchar(36);default:null;comment:Apple IAP App Account Token (UUID)"`
ActivationContext string `gorm:"type:text;default:null;comment:Activation context JSON (guest/redemption info for DB fallback)"`
IsNew bool `gorm:"type:tinyint(1);not null;default:0;comment:Is New Order"`
CreatedAt time.Time `gorm:"<-:create;comment:Create Time"`
UpdatedAt time.Time `gorm:"comment:Update Time"`
}
type OrdersTotal struct {
-239
View File
@@ -1,239 +0,0 @@
package promo
import (
"context"
"errors"
"fmt"
"github.com/perfect-panel/server/pkg/cache"
"github.com/redis/go-redis/v9"
"gorm.io/gorm"
)
const (
cachePromoRuleIdPrefix = "cache:promo_rule:id:"
cacheSubscribePromoIdPrefix = "cache:subscribe_promo:id:"
)
type Model interface {
InsertRule(ctx context.Context, data *Rule) error
FindRule(ctx context.Context, id int64) (*Rule, error)
UpdateRule(ctx context.Context, data *Rule) error
DeleteRule(ctx context.Context, id int64) error
QueryRuleList(ctx context.Context, page, size int, ruleType string, enabled *bool, search string) (int64, []*Rule, error)
UpsertPrices(ctx context.Context, ruleId int64, items []*SubscribePromo) error
FindPrice(ctx context.Context, id int64) (*SubscribePromo, error)
DeletePrice(ctx context.Context, id int64) error
QueryPriceList(ctx context.Context, ruleId int64, page, size int) (int64, []*SubscribePromo, error)
QueryUsageList(ctx context.Context, params UsageFilter) (int64, []*Usage, error)
Transaction(ctx context.Context, fn func(db *gorm.DB) error) error
}
type UsageFilter struct {
Page int
Size int
RuleId int64
UserId int64
SubscribeId int64
OrderNo string
}
type customPromoModel struct {
cache.CachedConn
table string
}
func NewModel(db *gorm.DB, c *redis.Client) Model {
return &customPromoModel{
CachedConn: cache.NewConn(db, c),
table: "`promo_rule`",
}
}
func (m *customPromoModel) ruleCacheKey(id int64) string {
return fmt.Sprintf("%s%d", cachePromoRuleIdPrefix, id)
}
func (m *customPromoModel) priceCacheKey(id int64) string {
return fmt.Sprintf("%s%d", cacheSubscribePromoIdPrefix, id)
}
func (m *customPromoModel) InsertRule(ctx context.Context, data *Rule) error {
return m.ExecCtx(ctx, func(conn *gorm.DB) error {
return conn.Create(data).Error
}, m.ruleCacheKey(data.Id))
}
func (m *customPromoModel) FindRule(ctx context.Context, id int64) (*Rule, error) {
var resp Rule
err := m.QueryCtx(ctx, &resp, m.ruleCacheKey(id), func(conn *gorm.DB, v interface{}) error {
return conn.Model(&Rule{}).Where("id = ?", id).First(v).Error
})
if err != nil {
return nil, err
}
return &resp, nil
}
func (m *customPromoModel) UpdateRule(ctx context.Context, data *Rule) error {
old, err := m.FindRule(ctx, data.Id)
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
return err
}
keys := []string{m.ruleCacheKey(data.Id)}
if old != nil {
keys = append(keys, m.ruleCacheKey(old.Id))
}
return m.ExecCtx(ctx, func(conn *gorm.DB) error {
return conn.Save(data).Error
}, keys...)
}
func (m *customPromoModel) DeleteRule(ctx context.Context, id int64) error {
data, err := m.FindRule(ctx, id)
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil
}
return err
}
return m.ExecCtx(ctx, func(conn *gorm.DB) error {
return conn.Delete(&Rule{}, id).Error
}, m.ruleCacheKey(data.Id))
}
func (m *customPromoModel) QueryRuleList(ctx context.Context, page, size int, ruleType string, enabled *bool, search string) (int64, []*Rule, error) {
if page <= 0 {
page = 1
}
if size <= 0 {
size = 10
}
var total int64
var list []*Rule
err := m.QueryNoCacheCtx(ctx, &list, func(conn *gorm.DB, v interface{}) error {
db := conn.Model(&Rule{})
if ruleType != "" {
db = db.Where("type = ?", ruleType)
}
if enabled != nil {
db = db.Where("enabled = ?", *enabled)
}
if search != "" {
db = db.Where("name LIKE ?", "%"+search+"%")
}
if err := db.Count(&total).Error; err != nil {
return err
}
return db.Order("priority DESC").Order("id DESC").Limit(size).Offset((page - 1) * size).Find(v).Error
})
return total, list, err
}
func (m *customPromoModel) UpsertPrices(ctx context.Context, ruleId int64, items []*SubscribePromo) error {
return m.ExecCtx(ctx, func(conn *gorm.DB) error {
for _, item := range items {
if item == nil {
continue
}
item.PromoRuleId = ruleId
var existing SubscribePromo
err := conn.Model(&SubscribePromo{}).
Where("subscribe_id = ? AND quantity = ? AND promo_rule_id = ?", item.SubscribeId, item.Quantity, ruleId).
First(&existing).Error
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
return err
}
if errors.Is(err, gorm.ErrRecordNotFound) {
if err := conn.Create(item).Error; err != nil {
return err
}
continue
}
existing.Quantity = item.Quantity
existing.PromoPrice = item.PromoPrice
if err := conn.Save(&existing).Error; err != nil {
return err
}
}
return nil
})
}
func (m *customPromoModel) FindPrice(ctx context.Context, id int64) (*SubscribePromo, error) {
var resp SubscribePromo
err := m.QueryCtx(ctx, &resp, m.priceCacheKey(id), func(conn *gorm.DB, v interface{}) error {
return conn.Model(&SubscribePromo{}).Where("id = ?", id).First(v).Error
})
if err != nil {
return nil, err
}
return &resp, nil
}
func (m *customPromoModel) DeletePrice(ctx context.Context, id int64) error {
data, err := m.FindPrice(ctx, id)
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil
}
return err
}
return m.ExecCtx(ctx, func(conn *gorm.DB) error {
return conn.Delete(&SubscribePromo{}, id).Error
}, m.priceCacheKey(data.Id))
}
func (m *customPromoModel) QueryPriceList(ctx context.Context, ruleId int64, page, size int) (int64, []*SubscribePromo, error) {
if page <= 0 {
page = 1
}
if size <= 0 {
size = 10
}
var total int64
var list []*SubscribePromo
err := m.QueryNoCacheCtx(ctx, &list, func(conn *gorm.DB, v interface{}) error {
db := conn.Model(&SubscribePromo{}).Where("promo_rule_id = ?", ruleId)
if err := db.Count(&total).Error; err != nil {
return err
}
return db.Order("id DESC").Limit(size).Offset((page - 1) * size).Find(v).Error
})
return total, list, err
}
func (m *customPromoModel) QueryUsageList(ctx context.Context, params UsageFilter) (int64, []*Usage, error) {
if params.Page <= 0 {
params.Page = 1
}
if params.Size <= 0 {
params.Size = 10
}
var total int64
var list []*Usage
err := m.QueryNoCacheCtx(ctx, &list, func(conn *gorm.DB, v interface{}) error {
db := conn.Model(&Usage{})
if params.RuleId > 0 {
db = db.Where("promo_rule_id = ?", params.RuleId)
}
if params.UserId > 0 {
db = db.Where("user_id = ?", params.UserId)
}
if params.SubscribeId > 0 {
db = db.Where("subscribe_id = ?", params.SubscribeId)
}
if params.OrderNo != "" {
db = db.Where("order_no = ?", params.OrderNo)
}
if err := db.Count(&total).Error; err != nil {
return err
}
return db.Order("id DESC").Limit(params.Size).Offset((params.Page - 1) * params.Size).Find(v).Error
})
return total, list, err
}
func (m *customPromoModel) Transaction(ctx context.Context, fn func(db *gorm.DB) error) error {
return m.TransactCtx(ctx, fn)
}
+204
View File
@@ -0,0 +1,204 @@
package promo
import (
"context"
"errors"
"github.com/redis/go-redis/v9"
"gorm.io/gorm"
)
type RuleWithPrice struct {
Rule
PromoPrice int64 `gorm:"column:promo_price"`
}
type Model interface {
QueryEligibleRules(ctx context.Context, subscribeId int64, quantity int64) ([]*RuleWithPrice, error)
InsertUsage(ctx context.Context, data *Usage, tx ...*gorm.DB) error
InsertRule(ctx context.Context, data *Rule) error
FindRule(ctx context.Context, id int64) (*Rule, error)
UpdateRule(ctx context.Context, data *Rule) error
DeleteRule(ctx context.Context, id int64) error
QueryRuleList(ctx context.Context, page, size int, ruleType string, enabled *bool, search string) (int64, []*Rule, error)
UpsertPrices(ctx context.Context, ruleId int64, items []*SubscribePromo) error
FindPrice(ctx context.Context, id int64) (*SubscribePromo, error)
DeletePrice(ctx context.Context, id int64) error
QueryPriceList(ctx context.Context, ruleId int64, page, size int) (int64, []*SubscribePromo, error)
QueryUsageList(ctx context.Context, params UsageFilter) (int64, []*Usage, error)
Transaction(ctx context.Context, fn func(db *gorm.DB) error) error
}
type UsageFilter struct {
Page int
Size int
RuleId int64
UserId int64
SubscribeId int64
OrderNo string
}
type defaultPromoModel struct {
db *gorm.DB
}
func NewModel(db *gorm.DB, _ *redis.Client) Model {
return &defaultPromoModel{db: db}
}
func (m *defaultPromoModel) QueryEligibleRules(ctx context.Context, subscribeId int64, quantity int64) ([]*RuleWithPrice, error) {
var list []*RuleWithPrice
err := m.db.WithContext(ctx).
Table("promo_rule AS pr").
Select("pr.*, sp.promo_price").
Joins("JOIN subscribe_promo AS sp ON sp.promo_rule_id = pr.id").
Where("sp.subscribe_id = ? AND sp.quantity = ? AND sp.promo_price > 0 AND pr.enabled = ?", subscribeId, quantity, true).
Where("pr.deleted_at IS NULL").
Order("pr.priority DESC").
Order("pr.id ASC").
Find(&list).Error
return list, err
}
func (m *defaultPromoModel) InsertUsage(ctx context.Context, data *Usage, tx ...*gorm.DB) error {
db := m.db.WithContext(ctx)
if len(tx) > 0 {
db = tx[0].WithContext(ctx)
}
return db.Model(&Usage{}).Create(data).Error
}
func (m *defaultPromoModel) InsertRule(ctx context.Context, data *Rule) error {
return m.db.WithContext(ctx).Create(data).Error
}
func (m *defaultPromoModel) FindRule(ctx context.Context, id int64) (*Rule, error) {
var resp Rule
if err := m.db.WithContext(ctx).Model(&Rule{}).Where("id = ?", id).First(&resp).Error; err != nil {
return nil, err
}
return &resp, nil
}
func (m *defaultPromoModel) UpdateRule(ctx context.Context, data *Rule) error {
return m.db.WithContext(ctx).Save(data).Error
}
func (m *defaultPromoModel) DeleteRule(ctx context.Context, id int64) error {
return m.db.WithContext(ctx).Delete(&Rule{}, id).Error
}
func (m *defaultPromoModel) QueryRuleList(ctx context.Context, page, size int, ruleType string, enabled *bool, search string) (int64, []*Rule, error) {
if page <= 0 {
page = 1
}
if size <= 0 {
size = 10
}
var total int64
var list []*Rule
db := m.db.WithContext(ctx).Model(&Rule{})
if ruleType != "" {
db = db.Where("type = ?", ruleType)
}
if enabled != nil {
db = db.Where("enabled = ?", *enabled)
}
if search != "" {
db = db.Where("name LIKE ?", "%"+search+"%")
}
if err := db.Count(&total).Error; err != nil {
return 0, nil, err
}
err := db.Order("priority DESC").Order("id DESC").Limit(size).Offset((page - 1) * size).Find(&list).Error
return total, list, err
}
func (m *defaultPromoModel) UpsertPrices(ctx context.Context, ruleId int64, items []*SubscribePromo) error {
return m.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
for _, item := range items {
if item == nil {
continue
}
item.PromoRuleId = ruleId
var existing SubscribePromo
err := tx.Model(&SubscribePromo{}).
Where("subscribe_id = ? AND quantity = ? AND promo_rule_id = ?", item.SubscribeId, item.Quantity, ruleId).
First(&existing).Error
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
return err
}
if errors.Is(err, gorm.ErrRecordNotFound) {
return tx.Create(item).Error
}
existing.Quantity = item.Quantity
existing.PromoPrice = item.PromoPrice
if err := tx.Save(&existing).Error; err != nil {
return err
}
}
return nil
})
}
func (m *defaultPromoModel) FindPrice(ctx context.Context, id int64) (*SubscribePromo, error) {
var resp SubscribePromo
if err := m.db.WithContext(ctx).Model(&SubscribePromo{}).Where("id = ?", id).First(&resp).Error; err != nil {
return nil, err
}
return &resp, nil
}
func (m *defaultPromoModel) DeletePrice(ctx context.Context, id int64) error {
return m.db.WithContext(ctx).Delete(&SubscribePromo{}, id).Error
}
func (m *defaultPromoModel) QueryPriceList(ctx context.Context, ruleId int64, page, size int) (int64, []*SubscribePromo, error) {
if page <= 0 {
page = 1
}
if size <= 0 {
size = 10
}
var total int64
var list []*SubscribePromo
db := m.db.WithContext(ctx).Model(&SubscribePromo{}).Where("promo_rule_id = ?", ruleId)
if err := db.Count(&total).Error; err != nil {
return 0, nil, err
}
err := db.Order("id DESC").Limit(size).Offset((page - 1) * size).Find(&list).Error
return total, list, err
}
func (m *defaultPromoModel) QueryUsageList(ctx context.Context, params UsageFilter) (int64, []*Usage, error) {
if params.Page <= 0 {
params.Page = 1
}
if params.Size <= 0 {
params.Size = 10
}
var total int64
var list []*Usage
db := m.db.WithContext(ctx).Model(&Usage{})
if params.RuleId > 0 {
db = db.Where("promo_rule_id = ?", params.RuleId)
}
if params.UserId > 0 {
db = db.Where("user_id = ?", params.UserId)
}
if params.SubscribeId > 0 {
db = db.Where("subscribe_id = ?", params.SubscribeId)
}
if params.OrderNo != "" {
db = db.Where("order_no = ?", params.OrderNo)
}
if err := db.Count(&total).Error; err != nil {
return 0, nil, err
}
err := db.Order("id DESC").Limit(params.Size).Offset((params.Page - 1) * params.Size).Find(&list).Error
return total, list, err
}
func (m *defaultPromoModel) Transaction(ctx context.Context, fn func(db *gorm.DB) error) error {
return m.db.WithContext(ctx).Transaction(fn)
}
+19 -13
View File
@@ -6,13 +6,19 @@ import (
"gorm.io/gorm"
)
const (
RuleTypeNewUser = "new_user"
RuleTypeInactiveUser = "inactive_user"
RuleTypeCampaign = "campaign"
)
type Rule struct {
Id int64 `gorm:"primaryKey"`
Name string `gorm:"type:varchar(100);not null;default:'';comment:Promo Rule Name"`
Type string `gorm:"type:varchar(32);not null;default:'';comment:Promo Rule Type"`
Name string `gorm:"type:varchar(100);not null;default:'';comment:Rule Name"`
Type string `gorm:"type:varchar(32);not null;default:'';comment:Rule Type"`
Params string `gorm:"type:json;not null;comment:Rule Params"`
Priority int64 `gorm:"type:int;not null;default:0;comment:Priority"`
Enabled *bool `gorm:"type:tinyint(1);not null;default:1;comment:Enabled"`
Enabled bool `gorm:"type:tinyint(1);not null;default:1;comment:Enabled"`
StartTime *time.Time `gorm:"default:null;comment:Start Time"`
EndTime *time.Time `gorm:"default:null;comment:End Time"`
CreatedAt time.Time `gorm:"<-:create;comment:Create Time"`
@@ -20,34 +26,34 @@ type Rule struct {
DeletedAt gorm.DeletedAt `gorm:"index;comment:Delete Time"`
}
func (*Rule) TableName() string {
func (Rule) TableName() string {
return "promo_rule"
}
type SubscribePromo struct {
Id int64 `gorm:"primaryKey"`
SubscribeId int64 `gorm:"not null;index:idx_subscribe_qty_rule,unique;comment:Subscribe ID"`
Quantity int64 `gorm:"not null;default:0;index:idx_subscribe_qty_rule,unique;comment:Quantity"`
PromoRuleId int64 `gorm:"not null;index:idx_subscribe_qty_rule,unique;index:idx_promo_rule_id;comment:Promo Rule ID"`
SubscribeId int64 `gorm:"type:bigint unsigned;not null;comment:Subscribe ID"`
Quantity int64 `gorm:"type:bigint;not null;default:1;comment:Quantity"`
PromoRuleId int64 `gorm:"type:bigint unsigned;not null;comment:Promo Rule ID"`
PromoPrice int64 `gorm:"type:bigint;not null;default:0;comment:Promo Price"`
CreatedAt time.Time `gorm:"<-:create;comment:Create Time"`
UpdatedAt time.Time `gorm:"comment:Update Time"`
}
func (*SubscribePromo) TableName() string {
func (SubscribePromo) TableName() string {
return "subscribe_promo"
}
type Usage struct {
Id int64 `gorm:"primaryKey"`
UserId int64 `gorm:"not null;index:idx_user_rule;comment:User ID"`
PromoRuleId int64 `gorm:"not null;index:idx_user_rule;comment:Promo Rule ID"`
SubscribeId int64 `gorm:"not null;comment:Subscribe ID"`
OrderNo string `gorm:"type:varchar(255);not null;default:'';index:idx_order_no;comment:Order No"`
UserId int64 `gorm:"type:bigint unsigned;not null;comment:User ID"`
PromoRuleId int64 `gorm:"type:bigint unsigned;not null;comment:Promo Rule ID"`
SubscribeId int64 `gorm:"type:bigint unsigned;not null;comment:Subscribe ID"`
OrderNo string `gorm:"type:varchar(255);not null;default:'';comment:Order No"`
PromoPrice int64 `gorm:"type:bigint;not null;default:0;comment:Promo Price"`
CreatedAt time.Time `gorm:"<-:create;comment:Create Time"`
}
func (*Usage) TableName() string {
func (Usage) TableName() string {
return "promo_usage"
}
+15 -13
View File
@@ -828,13 +828,7 @@ type FileUploadRequest struct {
}
type FileUploadResponse struct {
FileId string `json:"file_id"`
FileName string `json:"file_name"`
ObjectKey string `json:"object_key"`
Size int64 `json:"size"`
ContentType string `json:"content_type"`
Etag string `json:"etag"`
Status string `json:"status"`
Url string `json:"url"`
}
type FileUploadCompleteRequest struct {
@@ -842,12 +836,7 @@ type FileUploadCompleteRequest struct {
}
type FileUploadCompleteResponse struct {
FileId string `json:"file_id"`
ObjectKey string `json:"object_key"`
Size int64 `json:"size"`
ContentType string `json:"content_type"`
Etag string `json:"etag"`
Status string `json:"status"`
Url string `json:"url"`
}
type FileUploadInitRequest struct {
@@ -1959,6 +1948,8 @@ type Order struct {
Amount int64 `json:"amount"`
GiftAmount int64 `json:"gift_amount"`
Discount int64 `json:"discount"`
PromoRuleId int64 `json:"promo_rule_id"`
PromoDiscount int64 `json:"promo_discount"`
Coupon string `json:"coupon"`
CouponDiscount int64 `json:"coupon_discount"`
Commission int64 `json:"commission,omitempty"`
@@ -1982,6 +1973,8 @@ type OrderDetail struct {
Amount int64 `json:"amount"`
GiftAmount int64 `json:"gift_amount"`
Discount int64 `json:"discount"`
PromoRuleId int64 `json:"promo_rule_id"`
PromoDiscount int64 `json:"promo_discount"`
Coupon string `json:"coupon"`
CouponDiscount int64 `json:"coupon_discount"`
Commission int64 `json:"commission,omitempty"`
@@ -2103,6 +2096,7 @@ type PreOrderResponse struct {
Price int64 `json:"price"`
Amount int64 `json:"amount"`
Discount int64 `json:"discount"`
PromoDiscount int64 `json:"promo_discount"`
GiftAmount int64 `json:"gift_amount"`
Coupon string `json:"coupon"`
CouponDiscount int64 `json:"coupon_discount"`
@@ -2871,6 +2865,13 @@ type StripePayment struct {
PublishableKey string `json:"publishable_key"`
}
type SubscribePromo struct {
RuleName string `json:"rule_name"`
RuleType string `json:"rule_type"`
PromoPrice int64 `json:"promo_price"`
ExpiresAt int64 `json:"expires_at"`
}
type Subscribe struct {
Id int64 `json:"id"`
Name string `json:"name"`
@@ -2879,6 +2880,7 @@ type Subscribe struct {
UnitPrice int64 `json:"unit_price"`
UnitTime string `json:"unit_time"`
Discount []SubscribeDiscount `json:"discount"`
Promo *SubscribePromo `json:"promo"`
NodeCount int64 `json:"node_count"`
Replacement int64 `json:"replacement"`
Inventory int64 `json:"inventory"`