e61988d78f
Co-authored-by: multica-agent <github@multica.ai>
107 lines
2.7 KiB
Go
107 lines
2.7 KiB
Go
package promo_rule
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
|
|
"github.com/perfect-panel/server/pkg/cache"
|
|
"github.com/redis/go-redis/v9"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
var _ Model = (*customPromoRuleModel)(nil)
|
|
|
|
var cachePromoRuleIdPrefix = "cache:promo_rule:id:"
|
|
|
|
type (
|
|
Model interface {
|
|
promoRuleModel
|
|
customPromoRuleLogicModel
|
|
}
|
|
|
|
promoRuleModel interface {
|
|
Insert(ctx context.Context, data *PromoRule, tx ...*gorm.DB) error
|
|
FindOne(ctx context.Context, id int64) (*PromoRule, error)
|
|
Update(ctx context.Context, data *PromoRule, 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
|
|
}
|
|
|
|
customPromoRuleModel struct {
|
|
*defaultPromoRuleModel
|
|
}
|
|
defaultPromoRuleModel struct {
|
|
cache.CachedConn
|
|
table string
|
|
}
|
|
)
|
|
|
|
func newPromoRuleModel(db *gorm.DB, c *redis.Client) *defaultPromoRuleModel {
|
|
return &defaultPromoRuleModel{
|
|
CachedConn: cache.NewConn(db, c),
|
|
table: "`promo_rule`",
|
|
}
|
|
}
|
|
|
|
func (m *defaultPromoRuleModel) getCacheKeys(data *PromoRule) []string {
|
|
if data == nil {
|
|
return []string{}
|
|
}
|
|
return []string{fmt.Sprintf("%s%v", cachePromoRuleIdPrefix, data.Id)}
|
|
}
|
|
|
|
func (m *defaultPromoRuleModel) Insert(ctx context.Context, data *PromoRule, 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 *defaultPromoRuleModel) FindOne(ctx context.Context, id int64) (*PromoRule, error) {
|
|
key := fmt.Sprintf("%s%v", cachePromoRuleIdPrefix, id)
|
|
var resp PromoRule
|
|
err := m.QueryCtx(ctx, &resp, key, func(conn *gorm.DB, v interface{}) error {
|
|
return conn.Model(&PromoRule{}).Where("`id` = ?", id).First(v).Error
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &resp, nil
|
|
}
|
|
|
|
func (m *defaultPromoRuleModel) Update(ctx context.Context, data *PromoRule, 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 *defaultPromoRuleModel) 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(&PromoRule{}, id).Error
|
|
}, m.getCacheKeys(data)...)
|
|
}
|
|
|
|
func (m *defaultPromoRuleModel) Transaction(ctx context.Context, fn func(db *gorm.DB) error) error {
|
|
return m.TransactCtx(ctx, fn)
|
|
}
|