@@ -0,0 +1,84 @@
|
||||
package subscribe_promo
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/perfect-panel/server/internal/model/promo_rule"
|
||||
"github.com/redis/go-redis/v9"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type RulePromo struct {
|
||||
Id int64
|
||||
SubscribeId int64
|
||||
PromoRuleId int64
|
||||
PromoPrice int64
|
||||
RuleName string
|
||||
RuleType string
|
||||
Params promo_rule.JSONRawMessage
|
||||
Priority int64
|
||||
StartTime *time.Time
|
||||
EndTime *time.Time
|
||||
}
|
||||
|
||||
type customSubscribePromoLogicModel interface {
|
||||
FindOneBySubscribeAndRule(ctx context.Context, subscribeId, promoRuleId int64) (*SubscribePromo, error)
|
||||
FindBySubscribeId(ctx context.Context, subscribeId int64) ([]*SubscribePromo, error)
|
||||
FindByPromoRuleId(ctx context.Context, promoRuleId int64) ([]*SubscribePromo, error)
|
||||
QueryEnabledBySubscribeId(ctx context.Context, subscribeId int64) ([]*RulePromo, error)
|
||||
}
|
||||
|
||||
func NewModel(conn *gorm.DB, c *redis.Client) Model {
|
||||
return &customSubscribePromoModel{
|
||||
defaultSubscribePromoModel: newSubscribePromoModel(conn, c),
|
||||
}
|
||||
}
|
||||
|
||||
func (m *customSubscribePromoModel) FindOneBySubscribeAndRule(ctx context.Context, subscribeId, promoRuleId int64) (*SubscribePromo, error) {
|
||||
var resp SubscribePromo
|
||||
err := m.QueryNoCacheCtx(ctx, &resp, func(conn *gorm.DB, v interface{}) error {
|
||||
return conn.Model(&SubscribePromo{}).
|
||||
Where("subscribe_id = ? AND promo_rule_id = ?", subscribeId, promoRuleId).
|
||||
First(v).Error
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &resp, nil
|
||||
}
|
||||
|
||||
func (m *customSubscribePromoModel) FindBySubscribeId(ctx context.Context, subscribeId int64) ([]*SubscribePromo, error) {
|
||||
var list []*SubscribePromo
|
||||
err := m.QueryNoCacheCtx(ctx, &list, func(conn *gorm.DB, v interface{}) error {
|
||||
return conn.Model(&SubscribePromo{}).
|
||||
Where("subscribe_id = ?", subscribeId).
|
||||
Order("id DESC").
|
||||
Find(v).Error
|
||||
})
|
||||
return list, err
|
||||
}
|
||||
|
||||
func (m *customSubscribePromoModel) FindByPromoRuleId(ctx context.Context, promoRuleId int64) ([]*SubscribePromo, error) {
|
||||
var list []*SubscribePromo
|
||||
err := m.QueryNoCacheCtx(ctx, &list, func(conn *gorm.DB, v interface{}) error {
|
||||
return conn.Model(&SubscribePromo{}).
|
||||
Where("promo_rule_id = ?", promoRuleId).
|
||||
Order("id DESC").
|
||||
Find(v).Error
|
||||
})
|
||||
return list, err
|
||||
}
|
||||
|
||||
func (m *customSubscribePromoModel) QueryEnabledBySubscribeId(ctx context.Context, subscribeId int64) ([]*RulePromo, error) {
|
||||
var list []*RulePromo
|
||||
err := m.QueryNoCacheCtx(ctx, &list, func(conn *gorm.DB, v interface{}) error {
|
||||
return conn.Table("subscribe_promo AS sp").
|
||||
Select("sp.id, sp.subscribe_id, sp.promo_rule_id, sp.promo_price, pr.name AS rule_name, pr.type AS rule_type, pr.params, pr.priority, 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 = ? AND pr.enabled = ?", subscribeId, true).
|
||||
Order("pr.priority DESC, pr.id DESC").
|
||||
Scan(v).Error
|
||||
})
|
||||
return list, err
|
||||
}
|
||||
Reference in New Issue
Block a user