init
This commit is contained in:
@@ -0,0 +1,24 @@
|
||||
package coupon
|
||||
|
||||
import "time"
|
||||
|
||||
type Coupon struct {
|
||||
Id int64 `gorm:"primaryKey"`
|
||||
Name string `gorm:"type:varchar(255);not null;default:'';comment:Coupon Name"`
|
||||
Code string `gorm:"type:varchar(255);not null;default:'';unique;comment:Coupon Code"`
|
||||
Count int64 `gorm:"type:int;not null;default:0;comment:Count Limit"`
|
||||
Type uint8 `gorm:"type:tinyint(1);not null;default:1;comment:Coupon Type: 1: Percentage 2: Fixed Amount"`
|
||||
Discount int64 `gorm:"type:int;not null;default:0;comment:Coupon Discount"`
|
||||
StartTime int64 `gorm:"type:int;not null;default:0;comment:Start Time"`
|
||||
ExpireTime int64 `gorm:"type:int;not null;default:0;comment:Expire Time"`
|
||||
UserLimit int64 `gorm:"type:int;not null;default:0;comment:User Limit"`
|
||||
Subscribe string `gorm:"type:varchar(255);not null;default:'';comment:Subscribe Limit"`
|
||||
UsedCount int64 `gorm:"type:int;not null;default:0;comment:Used Count"`
|
||||
Enable *bool `gorm:"type:tinyint(1);not null;default:1;comment:Enable"`
|
||||
CreatedAt time.Time `gorm:"<-:create;comment:Create Time"`
|
||||
UpdatedAt time.Time `gorm:"comment:Update Time"`
|
||||
}
|
||||
|
||||
func (Coupon) TableName() string {
|
||||
return "coupon"
|
||||
}
|
||||
@@ -0,0 +1,135 @@
|
||||
package coupon
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/perfect-panel/ppanel-server/pkg/cache"
|
||||
"github.com/redis/go-redis/v9"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
var _ Model = (*customCouponModel)(nil)
|
||||
var (
|
||||
cacheCouponIdPrefix = "cache:coupon:id:"
|
||||
cacheCouponCodePrefix = "cache:coupon:code:"
|
||||
)
|
||||
|
||||
type (
|
||||
Model interface {
|
||||
couponModel
|
||||
customCouponLogicModel
|
||||
}
|
||||
couponModel interface {
|
||||
Insert(ctx context.Context, data *Coupon) error
|
||||
FindOne(ctx context.Context, id int64) (*Coupon, error)
|
||||
FindOneByCode(ctx context.Context, code string) (*Coupon, error)
|
||||
Update(ctx context.Context, data *Coupon) error
|
||||
Delete(ctx context.Context, id int64) error
|
||||
Transaction(ctx context.Context, fn func(db *gorm.DB) error) error
|
||||
}
|
||||
|
||||
customCouponModel struct {
|
||||
*defaultCouponModel
|
||||
}
|
||||
defaultCouponModel struct {
|
||||
cache.CachedConn
|
||||
table string
|
||||
}
|
||||
)
|
||||
|
||||
func newCouponModel(db *gorm.DB, c *redis.Client) *defaultCouponModel {
|
||||
return &defaultCouponModel{
|
||||
CachedConn: cache.NewConn(db, c),
|
||||
table: "`coupon`",
|
||||
}
|
||||
}
|
||||
|
||||
//nolint:unused
|
||||
func (m *defaultCouponModel) batchGetCacheKeys(Coupons ...*Coupon) []string {
|
||||
var keys []string
|
||||
for _, coupon := range Coupons {
|
||||
keys = append(keys, m.getCacheKeys(coupon)...)
|
||||
}
|
||||
return keys
|
||||
|
||||
}
|
||||
func (m *defaultCouponModel) getCacheKeys(data *Coupon) []string {
|
||||
if data == nil {
|
||||
return []string{}
|
||||
}
|
||||
couponIdKey := fmt.Sprintf("%s%v", cacheCouponIdPrefix, data.Id)
|
||||
couponCodeKey := fmt.Sprintf("%s%v", cacheCouponCodePrefix, data.Code)
|
||||
cacheKeys := []string{
|
||||
couponIdKey,
|
||||
couponCodeKey,
|
||||
}
|
||||
return cacheKeys
|
||||
}
|
||||
|
||||
func (m *defaultCouponModel) Insert(ctx context.Context, data *Coupon) error {
|
||||
err := m.ExecCtx(ctx, func(conn *gorm.DB) error {
|
||||
return conn.Create(&data).Error
|
||||
}, m.getCacheKeys(data)...)
|
||||
return err
|
||||
}
|
||||
|
||||
func (m *defaultCouponModel) FindOne(ctx context.Context, id int64) (*Coupon, error) {
|
||||
CouponIdKey := fmt.Sprintf("%s%v", cacheCouponIdPrefix, id)
|
||||
var resp Coupon
|
||||
err := m.QueryCtx(ctx, &resp, CouponIdKey, func(conn *gorm.DB, v interface{}) error {
|
||||
return conn.Model(&Coupon{}).Where("`id` = ?", id).First(&resp).Error
|
||||
})
|
||||
switch {
|
||||
case err == nil:
|
||||
return &resp, nil
|
||||
default:
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
func (m *defaultCouponModel) FindOneByCode(ctx context.Context, code string) (*Coupon, error) {
|
||||
CouponCodeKey := fmt.Sprintf("%s%v", cacheCouponCodePrefix, code)
|
||||
var resp Coupon
|
||||
err := m.QueryCtx(ctx, &resp, CouponCodeKey, func(conn *gorm.DB, v interface{}) error {
|
||||
return conn.Model(&Coupon{}).Where("`code` = ?", code).First(&resp).Error
|
||||
})
|
||||
switch {
|
||||
case err == nil:
|
||||
return &resp, nil
|
||||
default:
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
func (m *defaultCouponModel) Update(ctx context.Context, data *Coupon) error {
|
||||
old, err := m.FindOne(ctx, data.Id)
|
||||
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return err
|
||||
}
|
||||
err = m.ExecCtx(ctx, func(conn *gorm.DB) error {
|
||||
db := conn
|
||||
return db.Save(data).Error
|
||||
}, m.getCacheKeys(old)...)
|
||||
return err
|
||||
}
|
||||
|
||||
func (m *defaultCouponModel) Delete(ctx context.Context, id int64) error {
|
||||
data, err := m.FindOne(ctx, id)
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil
|
||||
}
|
||||
return err
|
||||
}
|
||||
err = m.ExecCtx(ctx, func(conn *gorm.DB) error {
|
||||
db := conn
|
||||
return db.Delete(&Coupon{}, id).Error
|
||||
}, m.getCacheKeys(data)...)
|
||||
return err
|
||||
}
|
||||
|
||||
func (m *defaultCouponModel) Transaction(ctx context.Context, fn func(db *gorm.DB) error) error {
|
||||
return m.TransactCtx(ctx, fn)
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
package coupon
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/redis/go-redis/v9"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type customCouponLogicModel interface {
|
||||
UpdateCount(ctx context.Context, code string) error
|
||||
QueryCouponListByPage(ctx context.Context, page, size int, subscribe int64, search string) (total int64, list []*Coupon, err error)
|
||||
BatchDelete(ctx context.Context, ids []int64) error
|
||||
}
|
||||
|
||||
// NewModel returns a model for the database table.
|
||||
func NewModel(conn *gorm.DB, c *redis.Client) Model {
|
||||
return &customCouponModel{
|
||||
defaultCouponModel: newCouponModel(conn, c),
|
||||
}
|
||||
}
|
||||
|
||||
// QueryCouponListByPage query coupon list by page
|
||||
func (m *customCouponModel) QueryCouponListByPage(ctx context.Context, page, size int, subscribe int64, search string) (total int64, list []*Coupon, err error) {
|
||||
err = m.QueryNoCacheCtx(ctx, &list, func(conn *gorm.DB, v interface{}) error {
|
||||
db := conn.Model(&Coupon{})
|
||||
if subscribe != 0 {
|
||||
db = db.Where("FIND_IN_SET(?, subscribe)", subscribe)
|
||||
}
|
||||
if search != "" {
|
||||
db = db.Where("name like ? or code like ?", "%"+search+"%", "%"+search+"%")
|
||||
}
|
||||
return db.Count(&total).Limit(size).Offset((page - 1) * size).Find(v).Error
|
||||
})
|
||||
return total, list, err
|
||||
}
|
||||
|
||||
func (m *customCouponModel) BatchDelete(ctx context.Context, ids []int64) error {
|
||||
var err error
|
||||
for _, id := range ids {
|
||||
if err = m.Delete(ctx, id); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *customCouponModel) UpdateCount(ctx context.Context, code string) error {
|
||||
data, err := m.FindOneByCode(ctx, code)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
data.UsedCount++
|
||||
return m.Update(ctx, data)
|
||||
}
|
||||
Reference in New Issue
Block a user