新功能(#74): 新增促销系统模型层

Co-authored-by: multica-agent <github@multica.ai>
This commit is contained in:
2026-05-26 22:11:02 -07:00
parent 5ebfe0a981
commit e61988d78f
14 changed files with 720 additions and 25 deletions
+44
View File
@@ -0,0 +1,44 @@
package promo_rule
import (
"context"
"time"
"github.com/redis/go-redis/v9"
"gorm.io/gorm"
)
type customPromoRuleLogicModel interface {
QueryEnabledByPriority(ctx context.Context) ([]*PromoRule, error)
QueryAvailableByPriority(ctx context.Context, now time.Time) ([]*PromoRule, error)
}
func NewModel(conn *gorm.DB, c *redis.Client) Model {
return &customPromoRuleModel{
defaultPromoRuleModel: newPromoRuleModel(conn, c),
}
}
func (m *customPromoRuleModel) QueryEnabledByPriority(ctx context.Context) ([]*PromoRule, error) {
var list []*PromoRule
err := m.QueryNoCacheCtx(ctx, &list, func(conn *gorm.DB, v interface{}) error {
return conn.Model(&PromoRule{}).
Where("enabled = ?", true).
Order("priority DESC, id DESC").
Find(v).Error
})
return list, err
}
func (m *customPromoRuleModel) QueryAvailableByPriority(ctx context.Context, now time.Time) ([]*PromoRule, error) {
var list []*PromoRule
err := m.QueryNoCacheCtx(ctx, &list, func(conn *gorm.DB, v interface{}) error {
return conn.Model(&PromoRule{}).
Where("enabled = ?", true).
Where("(start_time IS NULL OR start_time <= ?)", now).
Where("(end_time IS NULL OR end_time >= ?)", now).
Order("priority DESC, id DESC").
Find(v).Error
})
return list, err
}