Files
hi-server/internal/model/promo_rule/model.go
T
2026-05-26 22:11:02 -07:00

45 lines
1.2 KiB
Go

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
}