添加优惠券功能,实现价格计算

This commit is contained in:
2025-04-19 14:59:19 +08:00
parent 999d0b0a1d
commit 058c4f4313
9 changed files with 581 additions and 19 deletions

359
web/queries/coupon.gen.go Normal file
View File

@@ -0,0 +1,359 @@
// Code generated by gorm.io/gen. DO NOT EDIT.
// Code generated by gorm.io/gen. DO NOT EDIT.
// Code generated by gorm.io/gen. DO NOT EDIT.
package queries
import (
"context"
"gorm.io/gorm"
"gorm.io/gorm/clause"
"gorm.io/gorm/schema"
"gorm.io/gen"
"gorm.io/gen/field"
"gorm.io/plugin/dbresolver"
"platform/web/models"
)
func newCoupon(db *gorm.DB, opts ...gen.DOOption) coupon {
_coupon := coupon{}
_coupon.couponDo.UseDB(db, opts...)
_coupon.couponDo.UseModel(&models.Coupon{})
tableName := _coupon.couponDo.TableName()
_coupon.ALL = field.NewAsterisk(tableName)
_coupon.ExpireAt = field.NewTime(tableName, "expire_at")
_coupon.CreatedAt = field.NewField(tableName, "created_at")
_coupon.UpdatedAt = field.NewField(tableName, "updated_at")
_coupon.DeletedAt = field.NewField(tableName, "deleted_at")
_coupon.ID = field.NewInt32(tableName, "id")
_coupon.UserID = field.NewInt32(tableName, "user_id")
_coupon.Status = field.NewInt32(tableName, "status")
_coupon.Code = field.NewString(tableName, "code")
_coupon.Remark = field.NewString(tableName, "remark")
_coupon.Amount = field.NewFloat64(tableName, "amount")
_coupon.MinAmount = field.NewFloat64(tableName, "min_amount")
_coupon.fillFieldMap()
return _coupon
}
type coupon struct {
couponDo
ALL field.Asterisk
ExpireAt field.Time
CreatedAt field.Field
UpdatedAt field.Field
DeletedAt field.Field
ID field.Int32
UserID field.Int32
Status field.Int32
Code field.String
Remark field.String
Amount field.Float64
MinAmount field.Float64
fieldMap map[string]field.Expr
}
func (c coupon) Table(newTableName string) *coupon {
c.couponDo.UseTable(newTableName)
return c.updateTableName(newTableName)
}
func (c coupon) As(alias string) *coupon {
c.couponDo.DO = *(c.couponDo.As(alias).(*gen.DO))
return c.updateTableName(alias)
}
func (c *coupon) updateTableName(table string) *coupon {
c.ALL = field.NewAsterisk(table)
c.ExpireAt = field.NewTime(table, "expire_at")
c.CreatedAt = field.NewField(table, "created_at")
c.UpdatedAt = field.NewField(table, "updated_at")
c.DeletedAt = field.NewField(table, "deleted_at")
c.ID = field.NewInt32(table, "id")
c.UserID = field.NewInt32(table, "user_id")
c.Status = field.NewInt32(table, "status")
c.Code = field.NewString(table, "code")
c.Remark = field.NewString(table, "remark")
c.Amount = field.NewFloat64(table, "amount")
c.MinAmount = field.NewFloat64(table, "min_amount")
c.fillFieldMap()
return c
}
func (c *coupon) GetFieldByName(fieldName string) (field.OrderExpr, bool) {
_f, ok := c.fieldMap[fieldName]
if !ok || _f == nil {
return nil, false
}
_oe, ok := _f.(field.OrderExpr)
return _oe, ok
}
func (c *coupon) fillFieldMap() {
c.fieldMap = make(map[string]field.Expr, 11)
c.fieldMap["expire_at"] = c.ExpireAt
c.fieldMap["created_at"] = c.CreatedAt
c.fieldMap["updated_at"] = c.UpdatedAt
c.fieldMap["deleted_at"] = c.DeletedAt
c.fieldMap["id"] = c.ID
c.fieldMap["user_id"] = c.UserID
c.fieldMap["status"] = c.Status
c.fieldMap["code"] = c.Code
c.fieldMap["remark"] = c.Remark
c.fieldMap["amount"] = c.Amount
c.fieldMap["min_amount"] = c.MinAmount
}
func (c coupon) clone(db *gorm.DB) coupon {
c.couponDo.ReplaceConnPool(db.Statement.ConnPool)
return c
}
func (c coupon) replaceDB(db *gorm.DB) coupon {
c.couponDo.ReplaceDB(db)
return c
}
type couponDo struct{ gen.DO }
func (c couponDo) Debug() *couponDo {
return c.withDO(c.DO.Debug())
}
func (c couponDo) WithContext(ctx context.Context) *couponDo {
return c.withDO(c.DO.WithContext(ctx))
}
func (c couponDo) ReadDB() *couponDo {
return c.Clauses(dbresolver.Read)
}
func (c couponDo) WriteDB() *couponDo {
return c.Clauses(dbresolver.Write)
}
func (c couponDo) Session(config *gorm.Session) *couponDo {
return c.withDO(c.DO.Session(config))
}
func (c couponDo) Clauses(conds ...clause.Expression) *couponDo {
return c.withDO(c.DO.Clauses(conds...))
}
func (c couponDo) Returning(value interface{}, columns ...string) *couponDo {
return c.withDO(c.DO.Returning(value, columns...))
}
func (c couponDo) Not(conds ...gen.Condition) *couponDo {
return c.withDO(c.DO.Not(conds...))
}
func (c couponDo) Or(conds ...gen.Condition) *couponDo {
return c.withDO(c.DO.Or(conds...))
}
func (c couponDo) Select(conds ...field.Expr) *couponDo {
return c.withDO(c.DO.Select(conds...))
}
func (c couponDo) Where(conds ...gen.Condition) *couponDo {
return c.withDO(c.DO.Where(conds...))
}
func (c couponDo) Order(conds ...field.Expr) *couponDo {
return c.withDO(c.DO.Order(conds...))
}
func (c couponDo) Distinct(cols ...field.Expr) *couponDo {
return c.withDO(c.DO.Distinct(cols...))
}
func (c couponDo) Omit(cols ...field.Expr) *couponDo {
return c.withDO(c.DO.Omit(cols...))
}
func (c couponDo) Join(table schema.Tabler, on ...field.Expr) *couponDo {
return c.withDO(c.DO.Join(table, on...))
}
func (c couponDo) LeftJoin(table schema.Tabler, on ...field.Expr) *couponDo {
return c.withDO(c.DO.LeftJoin(table, on...))
}
func (c couponDo) RightJoin(table schema.Tabler, on ...field.Expr) *couponDo {
return c.withDO(c.DO.RightJoin(table, on...))
}
func (c couponDo) Group(cols ...field.Expr) *couponDo {
return c.withDO(c.DO.Group(cols...))
}
func (c couponDo) Having(conds ...gen.Condition) *couponDo {
return c.withDO(c.DO.Having(conds...))
}
func (c couponDo) Limit(limit int) *couponDo {
return c.withDO(c.DO.Limit(limit))
}
func (c couponDo) Offset(offset int) *couponDo {
return c.withDO(c.DO.Offset(offset))
}
func (c couponDo) Scopes(funcs ...func(gen.Dao) gen.Dao) *couponDo {
return c.withDO(c.DO.Scopes(funcs...))
}
func (c couponDo) Unscoped() *couponDo {
return c.withDO(c.DO.Unscoped())
}
func (c couponDo) Create(values ...*models.Coupon) error {
if len(values) == 0 {
return nil
}
return c.DO.Create(values)
}
func (c couponDo) CreateInBatches(values []*models.Coupon, batchSize int) error {
return c.DO.CreateInBatches(values, batchSize)
}
// Save : !!! underlying implementation is different with GORM
// The method is equivalent to executing the statement: db.Clauses(clause.OnConflict{UpdateAll: true}).Create(values)
func (c couponDo) Save(values ...*models.Coupon) error {
if len(values) == 0 {
return nil
}
return c.DO.Save(values)
}
func (c couponDo) First() (*models.Coupon, error) {
if result, err := c.DO.First(); err != nil {
return nil, err
} else {
return result.(*models.Coupon), nil
}
}
func (c couponDo) Take() (*models.Coupon, error) {
if result, err := c.DO.Take(); err != nil {
return nil, err
} else {
return result.(*models.Coupon), nil
}
}
func (c couponDo) Last() (*models.Coupon, error) {
if result, err := c.DO.Last(); err != nil {
return nil, err
} else {
return result.(*models.Coupon), nil
}
}
func (c couponDo) Find() ([]*models.Coupon, error) {
result, err := c.DO.Find()
return result.([]*models.Coupon), err
}
func (c couponDo) FindInBatch(batchSize int, fc func(tx gen.Dao, batch int) error) (results []*models.Coupon, err error) {
buf := make([]*models.Coupon, 0, batchSize)
err = c.DO.FindInBatches(&buf, batchSize, func(tx gen.Dao, batch int) error {
defer func() { results = append(results, buf...) }()
return fc(tx, batch)
})
return results, err
}
func (c couponDo) FindInBatches(result *[]*models.Coupon, batchSize int, fc func(tx gen.Dao, batch int) error) error {
return c.DO.FindInBatches(result, batchSize, fc)
}
func (c couponDo) Attrs(attrs ...field.AssignExpr) *couponDo {
return c.withDO(c.DO.Attrs(attrs...))
}
func (c couponDo) Assign(attrs ...field.AssignExpr) *couponDo {
return c.withDO(c.DO.Assign(attrs...))
}
func (c couponDo) Joins(fields ...field.RelationField) *couponDo {
for _, _f := range fields {
c = *c.withDO(c.DO.Joins(_f))
}
return &c
}
func (c couponDo) Preload(fields ...field.RelationField) *couponDo {
for _, _f := range fields {
c = *c.withDO(c.DO.Preload(_f))
}
return &c
}
func (c couponDo) FirstOrInit() (*models.Coupon, error) {
if result, err := c.DO.FirstOrInit(); err != nil {
return nil, err
} else {
return result.(*models.Coupon), nil
}
}
func (c couponDo) FirstOrCreate() (*models.Coupon, error) {
if result, err := c.DO.FirstOrCreate(); err != nil {
return nil, err
} else {
return result.(*models.Coupon), nil
}
}
func (c couponDo) FindByPage(offset int, limit int) (result []*models.Coupon, count int64, err error) {
result, err = c.Offset(offset).Limit(limit).Find()
if err != nil {
return
}
if size := len(result); 0 < limit && 0 < size && size < limit {
count = int64(size + offset)
return
}
count, err = c.Offset(-1).Limit(-1).Count()
return
}
func (c couponDo) ScanByPage(result interface{}, offset int, limit int) (count int64, err error) {
count, err = c.Count()
if err != nil {
return
}
err = c.Offset(offset).Limit(limit).Scan(result)
return
}
func (c couponDo) Scan(result interface{}) (err error) {
return c.DO.Scan(result)
}
func (c couponDo) Delete(models ...*models.Coupon) (result gen.ResultInfo, err error) {
return c.DO.Delete(models)
}
func (c *couponDo) withDO(do gen.Dao) *couponDo {
c.DO = *do.(*gen.DO)
return c
}

View File

@@ -25,6 +25,7 @@ var (
Channel *channel
Client *client
ClientPermissionLink *clientPermissionLink
Coupon *coupon
Node *node
Permission *permission
Product *product
@@ -52,6 +53,7 @@ func SetDefault(db *gorm.DB, opts ...gen.DOOption) {
Channel = &Q.Channel
Client = &Q.Client
ClientPermissionLink = &Q.ClientPermissionLink
Coupon = &Q.Coupon
Node = &Q.Node
Permission = &Q.Permission
Product = &Q.Product
@@ -80,6 +82,7 @@ func Use(db *gorm.DB, opts ...gen.DOOption) *Query {
Channel: newChannel(db, opts...),
Client: newClient(db, opts...),
ClientPermissionLink: newClientPermissionLink(db, opts...),
Coupon: newCoupon(db, opts...),
Node: newNode(db, opts...),
Permission: newPermission(db, opts...),
Product: newProduct(db, opts...),
@@ -109,6 +112,7 @@ type Query struct {
Channel channel
Client client
ClientPermissionLink clientPermissionLink
Coupon coupon
Node node
Permission permission
Product product
@@ -139,6 +143,7 @@ func (q *Query) clone(db *gorm.DB) *Query {
Channel: q.Channel.clone(db),
Client: q.Client.clone(db),
ClientPermissionLink: q.ClientPermissionLink.clone(db),
Coupon: q.Coupon.clone(db),
Node: q.Node.clone(db),
Permission: q.Permission.clone(db),
Product: q.Product.clone(db),
@@ -176,6 +181,7 @@ func (q *Query) ReplaceDB(db *gorm.DB) *Query {
Channel: q.Channel.replaceDB(db),
Client: q.Client.replaceDB(db),
ClientPermissionLink: q.ClientPermissionLink.replaceDB(db),
Coupon: q.Coupon.replaceDB(db),
Node: q.Node.replaceDB(db),
Permission: q.Permission.replaceDB(db),
Product: q.Product.replaceDB(db),
@@ -203,6 +209,7 @@ type queryCtx struct {
Channel *channelDo
Client *clientDo
ClientPermissionLink *clientPermissionLinkDo
Coupon *couponDo
Node *nodeDo
Permission *permissionDo
Product *productDo
@@ -230,6 +237,7 @@ func (q *Query) WithContext(ctx context.Context) *queryCtx {
Channel: q.Channel.WithContext(ctx),
Client: q.Client.WithContext(ctx),
ClientPermissionLink: q.ClientPermissionLink.WithContext(ctx),
Coupon: q.Coupon.WithContext(ctx),
Node: q.Node.WithContext(ctx),
Permission: q.Permission.WithContext(ctx),
Product: q.Product.WithContext(ctx),