e61988d78f
Co-authored-by: multica-agent <github@multica.ai>
107 lines
2.7 KiB
Go
107 lines
2.7 KiB
Go
package promo_usage
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
|
|
"github.com/perfect-panel/server/pkg/cache"
|
|
"github.com/redis/go-redis/v9"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
var _ Model = (*customPromoUsageModel)(nil)
|
|
|
|
var cachePromoUsageIdPrefix = "cache:promo_usage:id:"
|
|
|
|
type (
|
|
Model interface {
|
|
promoUsageModel
|
|
customPromoUsageLogicModel
|
|
}
|
|
|
|
promoUsageModel interface {
|
|
Insert(ctx context.Context, data *PromoUsage, tx ...*gorm.DB) error
|
|
FindOne(ctx context.Context, id int64) (*PromoUsage, error)
|
|
Update(ctx context.Context, data *PromoUsage, tx ...*gorm.DB) error
|
|
Delete(ctx context.Context, id int64, tx ...*gorm.DB) error
|
|
Transaction(ctx context.Context, fn func(db *gorm.DB) error) error
|
|
}
|
|
|
|
customPromoUsageModel struct {
|
|
*defaultPromoUsageModel
|
|
}
|
|
defaultPromoUsageModel struct {
|
|
cache.CachedConn
|
|
table string
|
|
}
|
|
)
|
|
|
|
func newPromoUsageModel(db *gorm.DB, c *redis.Client) *defaultPromoUsageModel {
|
|
return &defaultPromoUsageModel{
|
|
CachedConn: cache.NewConn(db, c),
|
|
table: "`promo_usage`",
|
|
}
|
|
}
|
|
|
|
func (m *defaultPromoUsageModel) getCacheKeys(data *PromoUsage) []string {
|
|
if data == nil {
|
|
return []string{}
|
|
}
|
|
return []string{fmt.Sprintf("%s%v", cachePromoUsageIdPrefix, data.Id)}
|
|
}
|
|
|
|
func (m *defaultPromoUsageModel) Insert(ctx context.Context, data *PromoUsage, tx ...*gorm.DB) error {
|
|
return m.ExecCtx(ctx, func(conn *gorm.DB) error {
|
|
if len(tx) > 0 {
|
|
conn = tx[0]
|
|
}
|
|
return conn.Create(data).Error
|
|
}, m.getCacheKeys(data)...)
|
|
}
|
|
|
|
func (m *defaultPromoUsageModel) FindOne(ctx context.Context, id int64) (*PromoUsage, error) {
|
|
key := fmt.Sprintf("%s%v", cachePromoUsageIdPrefix, id)
|
|
var resp PromoUsage
|
|
err := m.QueryCtx(ctx, &resp, key, func(conn *gorm.DB, v interface{}) error {
|
|
return conn.Model(&PromoUsage{}).Where("`id` = ?", id).First(v).Error
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &resp, nil
|
|
}
|
|
|
|
func (m *defaultPromoUsageModel) Update(ctx context.Context, data *PromoUsage, tx ...*gorm.DB) error {
|
|
old, err := m.FindOne(ctx, data.Id)
|
|
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
|
|
return err
|
|
}
|
|
return m.ExecCtx(ctx, func(conn *gorm.DB) error {
|
|
if len(tx) > 0 {
|
|
conn = tx[0]
|
|
}
|
|
return conn.Save(data).Error
|
|
}, m.getCacheKeys(old)...)
|
|
}
|
|
|
|
func (m *defaultPromoUsageModel) Delete(ctx context.Context, id int64, tx ...*gorm.DB) error {
|
|
data, err := m.FindOne(ctx, id)
|
|
if err != nil {
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
return nil
|
|
}
|
|
return err
|
|
}
|
|
return m.ExecCtx(ctx, func(conn *gorm.DB) error {
|
|
if len(tx) > 0 {
|
|
conn = tx[0]
|
|
}
|
|
return conn.Delete(&PromoUsage{}, id).Error
|
|
}, m.getCacheKeys(data)...)
|
|
}
|
|
|
|
func (m *defaultPromoUsageModel) Transaction(ctx context.Context, fn func(db *gorm.DB) error) error {
|
|
return m.TransactCtx(ctx, fn)
|
|
}
|