@@ -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
|
||||
}
|
||||
Reference in New Issue
Block a user