@@ -0,0 +1,71 @@
|
||||
package promo_usage
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/redis/go-redis/v9"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type customPromoUsageLogicModel interface {
|
||||
FindByOrderNo(ctx context.Context, orderNo string) ([]*PromoUsage, error)
|
||||
FindByUserAndRule(ctx context.Context, userId, promoRuleId int64) ([]*PromoUsage, error)
|
||||
QueryListByPage(ctx context.Context, page, size int, userId, promoRuleId, subscribeId int64) (int64, []*PromoUsage, error)
|
||||
}
|
||||
|
||||
func NewModel(conn *gorm.DB, c *redis.Client) Model {
|
||||
return &customPromoUsageModel{
|
||||
defaultPromoUsageModel: newPromoUsageModel(conn, c),
|
||||
}
|
||||
}
|
||||
|
||||
func (m *customPromoUsageModel) FindByOrderNo(ctx context.Context, orderNo string) ([]*PromoUsage, error) {
|
||||
var list []*PromoUsage
|
||||
err := m.QueryNoCacheCtx(ctx, &list, func(conn *gorm.DB, v interface{}) error {
|
||||
return conn.Model(&PromoUsage{}).
|
||||
Where("order_no = ?", orderNo).
|
||||
Order("id DESC").
|
||||
Find(v).Error
|
||||
})
|
||||
return list, err
|
||||
}
|
||||
|
||||
func (m *customPromoUsageModel) FindByUserAndRule(ctx context.Context, userId, promoRuleId int64) ([]*PromoUsage, error) {
|
||||
var list []*PromoUsage
|
||||
err := m.QueryNoCacheCtx(ctx, &list, func(conn *gorm.DB, v interface{}) error {
|
||||
return conn.Model(&PromoUsage{}).
|
||||
Where("user_id = ? AND promo_rule_id = ?", userId, promoRuleId).
|
||||
Order("id DESC").
|
||||
Find(v).Error
|
||||
})
|
||||
return list, err
|
||||
}
|
||||
|
||||
func (m *customPromoUsageModel) QueryListByPage(ctx context.Context, page, size int, userId, promoRuleId, subscribeId int64) (int64, []*PromoUsage, error) {
|
||||
var list []*PromoUsage
|
||||
var total int64
|
||||
if page <= 0 {
|
||||
page = 1
|
||||
}
|
||||
if size <= 0 {
|
||||
size = 20
|
||||
}
|
||||
|
||||
err := m.QueryNoCacheCtx(ctx, &list, func(conn *gorm.DB, v interface{}) error {
|
||||
db := conn.Model(&PromoUsage{})
|
||||
if userId > 0 {
|
||||
db = db.Where("user_id = ?", userId)
|
||||
}
|
||||
if promoRuleId > 0 {
|
||||
db = db.Where("promo_rule_id = ?", promoRuleId)
|
||||
}
|
||||
if subscribeId > 0 {
|
||||
db = db.Where("subscribe_id = ?", subscribeId)
|
||||
}
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user