// 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 dao

import (
	"context"

	"gorm.io/gorm"
	"gorm.io/gorm/clause"
	"gorm.io/gorm/schema"

	"gorm.io/gen"
	"gorm.io/gen/field"

	"gorm.io/plugin/dbresolver"

	"bindbox-game/internal/repository/mysql/model"
)

func newSystemCoupons(db *gorm.DB, opts ...gen.DOOption) systemCoupons {
	_systemCoupons := systemCoupons{}

	_systemCoupons.systemCouponsDo.UseDB(db, opts...)
	_systemCoupons.systemCouponsDo.UseModel(&model.SystemCoupons{})

	tableName := _systemCoupons.systemCouponsDo.TableName()
	_systemCoupons.ALL = field.NewAsterisk(tableName)
	_systemCoupons.ID = field.NewInt64(tableName, "id")
	_systemCoupons.CreatedAt = field.NewTime(tableName, "created_at")
	_systemCoupons.UpdatedAt = field.NewTime(tableName, "updated_at")
	_systemCoupons.Name = field.NewString(tableName, "name")
	_systemCoupons.ScopeType = field.NewInt32(tableName, "scope_type")
	_systemCoupons.ActivityID = field.NewInt64(tableName, "activity_id")
	_systemCoupons.ProductID = field.NewInt64(tableName, "product_id")
	_systemCoupons.DiscountType = field.NewInt32(tableName, "discount_type")
	_systemCoupons.DiscountValue = field.NewInt64(tableName, "discount_value")
	_systemCoupons.MinSpend = field.NewInt64(tableName, "min_spend")
	_systemCoupons.ValidStart = field.NewTime(tableName, "valid_start")
	_systemCoupons.ValidEnd = field.NewTime(tableName, "valid_end")
	_systemCoupons.Status = field.NewInt32(tableName, "status")
	_systemCoupons.TotalQuantity = field.NewInt64(tableName, "total_quantity")
	_systemCoupons.DeletedAt = field.NewField(tableName, "deleted_at")

	_systemCoupons.fillFieldMap()

	return _systemCoupons
}

// systemCoupons 优惠券模板
type systemCoupons struct {
	systemCouponsDo

	ALL           field.Asterisk
	ID            field.Int64  // 主键ID
	CreatedAt     field.Time   // 创建时间
	UpdatedAt     field.Time   // 更新时间
	Name          field.String // 券名称
	ScopeType     field.Int32  // 适用范围：1全局 2活动 3商品
	ActivityID    field.Int64  // 指定活动ID（可空）
	ProductID     field.Int64  // 指定商品ID（可空）
	DiscountType  field.Int32  // 优惠类型：1直减 2满减 3折扣
	DiscountValue field.Int64  // 优惠面值（直减/满减为分；折扣为千分比）
	MinSpend      field.Int64  // 使用门槛金额（分）
	ValidStart    field.Time   // 有效期开始
	ValidEnd      field.Time   // 有效期结束
	Status        field.Int32  // 状态：1启用 2停用
	TotalQuantity field.Int64
	DeletedAt     field.Field

	fieldMap map[string]field.Expr
}

func (s systemCoupons) Table(newTableName string) *systemCoupons {
	s.systemCouponsDo.UseTable(newTableName)
	return s.updateTableName(newTableName)
}

func (s systemCoupons) As(alias string) *systemCoupons {
	s.systemCouponsDo.DO = *(s.systemCouponsDo.As(alias).(*gen.DO))
	return s.updateTableName(alias)
}

func (s *systemCoupons) updateTableName(table string) *systemCoupons {
	s.ALL = field.NewAsterisk(table)
	s.ID = field.NewInt64(table, "id")
	s.CreatedAt = field.NewTime(table, "created_at")
	s.UpdatedAt = field.NewTime(table, "updated_at")
	s.Name = field.NewString(table, "name")
	s.ScopeType = field.NewInt32(table, "scope_type")
	s.ActivityID = field.NewInt64(table, "activity_id")
	s.ProductID = field.NewInt64(table, "product_id")
	s.DiscountType = field.NewInt32(table, "discount_type")
	s.DiscountValue = field.NewInt64(table, "discount_value")
	s.MinSpend = field.NewInt64(table, "min_spend")
	s.ValidStart = field.NewTime(table, "valid_start")
	s.ValidEnd = field.NewTime(table, "valid_end")
	s.Status = field.NewInt32(table, "status")
	s.TotalQuantity = field.NewInt64(table, "total_quantity")
	s.DeletedAt = field.NewField(table, "deleted_at")

	s.fillFieldMap()

	return s
}

func (s *systemCoupons) GetFieldByName(fieldName string) (field.OrderExpr, bool) {
	_f, ok := s.fieldMap[fieldName]
	if !ok || _f == nil {
		return nil, false
	}
	_oe, ok := _f.(field.OrderExpr)
	return _oe, ok
}

func (s *systemCoupons) fillFieldMap() {
	s.fieldMap = make(map[string]field.Expr, 15)
	s.fieldMap["id"] = s.ID
	s.fieldMap["created_at"] = s.CreatedAt
	s.fieldMap["updated_at"] = s.UpdatedAt
	s.fieldMap["name"] = s.Name
	s.fieldMap["scope_type"] = s.ScopeType
	s.fieldMap["activity_id"] = s.ActivityID
	s.fieldMap["product_id"] = s.ProductID
	s.fieldMap["discount_type"] = s.DiscountType
	s.fieldMap["discount_value"] = s.DiscountValue
	s.fieldMap["min_spend"] = s.MinSpend
	s.fieldMap["valid_start"] = s.ValidStart
	s.fieldMap["valid_end"] = s.ValidEnd
	s.fieldMap["status"] = s.Status
	s.fieldMap["total_quantity"] = s.TotalQuantity
	s.fieldMap["deleted_at"] = s.DeletedAt
}

func (s systemCoupons) clone(db *gorm.DB) systemCoupons {
	s.systemCouponsDo.ReplaceConnPool(db.Statement.ConnPool)
	return s
}

func (s systemCoupons) replaceDB(db *gorm.DB) systemCoupons {
	s.systemCouponsDo.ReplaceDB(db)
	return s
}

type systemCouponsDo struct{ gen.DO }

func (s systemCouponsDo) Debug() *systemCouponsDo {
	return s.withDO(s.DO.Debug())
}

func (s systemCouponsDo) WithContext(ctx context.Context) *systemCouponsDo {
	return s.withDO(s.DO.WithContext(ctx))
}

func (s systemCouponsDo) ReadDB() *systemCouponsDo {
	return s.Clauses(dbresolver.Read)
}

func (s systemCouponsDo) WriteDB() *systemCouponsDo {
	return s.Clauses(dbresolver.Write)
}

func (s systemCouponsDo) Session(config *gorm.Session) *systemCouponsDo {
	return s.withDO(s.DO.Session(config))
}

func (s systemCouponsDo) Clauses(conds ...clause.Expression) *systemCouponsDo {
	return s.withDO(s.DO.Clauses(conds...))
}

func (s systemCouponsDo) Returning(value interface{}, columns ...string) *systemCouponsDo {
	return s.withDO(s.DO.Returning(value, columns...))
}

func (s systemCouponsDo) Not(conds ...gen.Condition) *systemCouponsDo {
	return s.withDO(s.DO.Not(conds...))
}

func (s systemCouponsDo) Or(conds ...gen.Condition) *systemCouponsDo {
	return s.withDO(s.DO.Or(conds...))
}

func (s systemCouponsDo) Select(conds ...field.Expr) *systemCouponsDo {
	return s.withDO(s.DO.Select(conds...))
}

func (s systemCouponsDo) Where(conds ...gen.Condition) *systemCouponsDo {
	return s.withDO(s.DO.Where(conds...))
}

func (s systemCouponsDo) Order(conds ...field.Expr) *systemCouponsDo {
	return s.withDO(s.DO.Order(conds...))
}

func (s systemCouponsDo) Distinct(cols ...field.Expr) *systemCouponsDo {
	return s.withDO(s.DO.Distinct(cols...))
}

func (s systemCouponsDo) Omit(cols ...field.Expr) *systemCouponsDo {
	return s.withDO(s.DO.Omit(cols...))
}

func (s systemCouponsDo) Join(table schema.Tabler, on ...field.Expr) *systemCouponsDo {
	return s.withDO(s.DO.Join(table, on...))
}

func (s systemCouponsDo) LeftJoin(table schema.Tabler, on ...field.Expr) *systemCouponsDo {
	return s.withDO(s.DO.LeftJoin(table, on...))
}

func (s systemCouponsDo) RightJoin(table schema.Tabler, on ...field.Expr) *systemCouponsDo {
	return s.withDO(s.DO.RightJoin(table, on...))
}

func (s systemCouponsDo) Group(cols ...field.Expr) *systemCouponsDo {
	return s.withDO(s.DO.Group(cols...))
}

func (s systemCouponsDo) Having(conds ...gen.Condition) *systemCouponsDo {
	return s.withDO(s.DO.Having(conds...))
}

func (s systemCouponsDo) Limit(limit int) *systemCouponsDo {
	return s.withDO(s.DO.Limit(limit))
}

func (s systemCouponsDo) Offset(offset int) *systemCouponsDo {
	return s.withDO(s.DO.Offset(offset))
}

func (s systemCouponsDo) Scopes(funcs ...func(gen.Dao) gen.Dao) *systemCouponsDo {
	return s.withDO(s.DO.Scopes(funcs...))
}

func (s systemCouponsDo) Unscoped() *systemCouponsDo {
	return s.withDO(s.DO.Unscoped())
}

func (s systemCouponsDo) Create(values ...*model.SystemCoupons) error {
	if len(values) == 0 {
		return nil
	}
	return s.DO.Create(values)
}

func (s systemCouponsDo) CreateInBatches(values []*model.SystemCoupons, batchSize int) error {
	return s.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 (s systemCouponsDo) Save(values ...*model.SystemCoupons) error {
	if len(values) == 0 {
		return nil
	}
	return s.DO.Save(values)
}

func (s systemCouponsDo) First() (*model.SystemCoupons, error) {
	if result, err := s.DO.First(); err != nil {
		return nil, err
	} else {
		return result.(*model.SystemCoupons), nil
	}
}

func (s systemCouponsDo) Take() (*model.SystemCoupons, error) {
	if result, err := s.DO.Take(); err != nil {
		return nil, err
	} else {
		return result.(*model.SystemCoupons), nil
	}
}

func (s systemCouponsDo) Last() (*model.SystemCoupons, error) {
	if result, err := s.DO.Last(); err != nil {
		return nil, err
	} else {
		return result.(*model.SystemCoupons), nil
	}
}

func (s systemCouponsDo) Find() ([]*model.SystemCoupons, error) {
	result, err := s.DO.Find()
	return result.([]*model.SystemCoupons), err
}

func (s systemCouponsDo) FindInBatch(batchSize int, fc func(tx gen.Dao, batch int) error) (results []*model.SystemCoupons, err error) {
	buf := make([]*model.SystemCoupons, 0, batchSize)
	err = s.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 (s systemCouponsDo) FindInBatches(result *[]*model.SystemCoupons, batchSize int, fc func(tx gen.Dao, batch int) error) error {
	return s.DO.FindInBatches(result, batchSize, fc)
}

func (s systemCouponsDo) Attrs(attrs ...field.AssignExpr) *systemCouponsDo {
	return s.withDO(s.DO.Attrs(attrs...))
}

func (s systemCouponsDo) Assign(attrs ...field.AssignExpr) *systemCouponsDo {
	return s.withDO(s.DO.Assign(attrs...))
}

func (s systemCouponsDo) Joins(fields ...field.RelationField) *systemCouponsDo {
	for _, _f := range fields {
		s = *s.withDO(s.DO.Joins(_f))
	}
	return &s
}

func (s systemCouponsDo) Preload(fields ...field.RelationField) *systemCouponsDo {
	for _, _f := range fields {
		s = *s.withDO(s.DO.Preload(_f))
	}
	return &s
}

func (s systemCouponsDo) FirstOrInit() (*model.SystemCoupons, error) {
	if result, err := s.DO.FirstOrInit(); err != nil {
		return nil, err
	} else {
		return result.(*model.SystemCoupons), nil
	}
}

func (s systemCouponsDo) FirstOrCreate() (*model.SystemCoupons, error) {
	if result, err := s.DO.FirstOrCreate(); err != nil {
		return nil, err
	} else {
		return result.(*model.SystemCoupons), nil
	}
}

func (s systemCouponsDo) FindByPage(offset int, limit int) (result []*model.SystemCoupons, count int64, err error) {
	result, err = s.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 = s.Offset(-1).Limit(-1).Count()
	return
}

func (s systemCouponsDo) ScanByPage(result interface{}, offset int, limit int) (count int64, err error) {
	count, err = s.Count()
	if err != nil {
		return
	}

	err = s.Offset(offset).Limit(limit).Scan(result)
	return
}

func (s systemCouponsDo) Scan(result interface{}) (err error) {
	return s.DO.Scan(result)
}

func (s systemCouponsDo) Delete(models ...*model.SystemCoupons) (result gen.ResultInfo, err error) {
	return s.DO.Delete(models)
}

func (s *systemCouponsDo) withDO(do gen.Dao) *systemCouponsDo {
	s.DO = *do.(*gen.DO)
	return s
}
