// 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 newDouyinProductRewards(db *gorm.DB, opts ...gen.DOOption) douyinProductRewards {
	_douyinProductRewards := douyinProductRewards{}

	_douyinProductRewards.douyinProductRewardsDo.UseDB(db, opts...)
	_douyinProductRewards.douyinProductRewardsDo.UseModel(&model.DouyinProductRewards{})

	tableName := _douyinProductRewards.douyinProductRewardsDo.TableName()
	_douyinProductRewards.ALL = field.NewAsterisk(tableName)
	_douyinProductRewards.ID = field.NewInt64(tableName, "id")
	_douyinProductRewards.ProductID = field.NewString(tableName, "product_id")
	_douyinProductRewards.ProductName = field.NewString(tableName, "product_name")
	_douyinProductRewards.RewardType = field.NewString(tableName, "reward_type")
	_douyinProductRewards.RewardPayload = field.NewString(tableName, "reward_payload")
	_douyinProductRewards.Quantity = field.NewInt32(tableName, "quantity")
	_douyinProductRewards.Status = field.NewInt32(tableName, "status")
	_douyinProductRewards.CreatedAt = field.NewTime(tableName, "created_at")
	_douyinProductRewards.UpdatedAt = field.NewTime(tableName, "updated_at")

	_douyinProductRewards.fillFieldMap()

	return _douyinProductRewards
}

// douyinProductRewards 抖店商品奖励规则
type douyinProductRewards struct {
	douyinProductRewardsDo

	ALL           field.Asterisk
	ID            field.Int64
	ProductID     field.String // 抖店商品ID
	ProductName   field.String // 商品名称
	RewardType    field.String // 奖励类型
	RewardPayload field.String // 奖励参数JSON
	Quantity      field.Int32  // 发放数量
	Status        field.Int32  // 状态: 1=启用 0=禁用
	CreatedAt     field.Time
	UpdatedAt     field.Time

	fieldMap map[string]field.Expr
}

func (d douyinProductRewards) Table(newTableName string) *douyinProductRewards {
	d.douyinProductRewardsDo.UseTable(newTableName)
	return d.updateTableName(newTableName)
}

func (d douyinProductRewards) As(alias string) *douyinProductRewards {
	d.douyinProductRewardsDo.DO = *(d.douyinProductRewardsDo.As(alias).(*gen.DO))
	return d.updateTableName(alias)
}

func (d *douyinProductRewards) updateTableName(table string) *douyinProductRewards {
	d.ALL = field.NewAsterisk(table)
	d.ID = field.NewInt64(table, "id")
	d.ProductID = field.NewString(table, "product_id")
	d.ProductName = field.NewString(table, "product_name")
	d.RewardType = field.NewString(table, "reward_type")
	d.RewardPayload = field.NewString(table, "reward_payload")
	d.Quantity = field.NewInt32(table, "quantity")
	d.Status = field.NewInt32(table, "status")
	d.CreatedAt = field.NewTime(table, "created_at")
	d.UpdatedAt = field.NewTime(table, "updated_at")

	d.fillFieldMap()

	return d
}

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

func (d *douyinProductRewards) fillFieldMap() {
	d.fieldMap = make(map[string]field.Expr, 9)
	d.fieldMap["id"] = d.ID
	d.fieldMap["product_id"] = d.ProductID
	d.fieldMap["product_name"] = d.ProductName
	d.fieldMap["reward_type"] = d.RewardType
	d.fieldMap["reward_payload"] = d.RewardPayload
	d.fieldMap["quantity"] = d.Quantity
	d.fieldMap["status"] = d.Status
	d.fieldMap["created_at"] = d.CreatedAt
	d.fieldMap["updated_at"] = d.UpdatedAt
}

func (d douyinProductRewards) clone(db *gorm.DB) douyinProductRewards {
	d.douyinProductRewardsDo.ReplaceConnPool(db.Statement.ConnPool)
	return d
}

func (d douyinProductRewards) replaceDB(db *gorm.DB) douyinProductRewards {
	d.douyinProductRewardsDo.ReplaceDB(db)
	return d
}

type douyinProductRewardsDo struct{ gen.DO }

func (d douyinProductRewardsDo) Debug() *douyinProductRewardsDo {
	return d.withDO(d.DO.Debug())
}

func (d douyinProductRewardsDo) WithContext(ctx context.Context) *douyinProductRewardsDo {
	return d.withDO(d.DO.WithContext(ctx))
}

func (d douyinProductRewardsDo) ReadDB() *douyinProductRewardsDo {
	return d.Clauses(dbresolver.Read)
}

func (d douyinProductRewardsDo) WriteDB() *douyinProductRewardsDo {
	return d.Clauses(dbresolver.Write)
}

func (d douyinProductRewardsDo) Session(config *gorm.Session) *douyinProductRewardsDo {
	return d.withDO(d.DO.Session(config))
}

func (d douyinProductRewardsDo) Clauses(conds ...clause.Expression) *douyinProductRewardsDo {
	return d.withDO(d.DO.Clauses(conds...))
}

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

func (d douyinProductRewardsDo) Not(conds ...gen.Condition) *douyinProductRewardsDo {
	return d.withDO(d.DO.Not(conds...))
}

func (d douyinProductRewardsDo) Or(conds ...gen.Condition) *douyinProductRewardsDo {
	return d.withDO(d.DO.Or(conds...))
}

func (d douyinProductRewardsDo) Select(conds ...field.Expr) *douyinProductRewardsDo {
	return d.withDO(d.DO.Select(conds...))
}

func (d douyinProductRewardsDo) Where(conds ...gen.Condition) *douyinProductRewardsDo {
	return d.withDO(d.DO.Where(conds...))
}

func (d douyinProductRewardsDo) Order(conds ...field.Expr) *douyinProductRewardsDo {
	return d.withDO(d.DO.Order(conds...))
}

func (d douyinProductRewardsDo) Distinct(cols ...field.Expr) *douyinProductRewardsDo {
	return d.withDO(d.DO.Distinct(cols...))
}

func (d douyinProductRewardsDo) Omit(cols ...field.Expr) *douyinProductRewardsDo {
	return d.withDO(d.DO.Omit(cols...))
}

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

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

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

func (d douyinProductRewardsDo) Group(cols ...field.Expr) *douyinProductRewardsDo {
	return d.withDO(d.DO.Group(cols...))
}

func (d douyinProductRewardsDo) Having(conds ...gen.Condition) *douyinProductRewardsDo {
	return d.withDO(d.DO.Having(conds...))
}

func (d douyinProductRewardsDo) Limit(limit int) *douyinProductRewardsDo {
	return d.withDO(d.DO.Limit(limit))
}

func (d douyinProductRewardsDo) Offset(offset int) *douyinProductRewardsDo {
	return d.withDO(d.DO.Offset(offset))
}

func (d douyinProductRewardsDo) Scopes(funcs ...func(gen.Dao) gen.Dao) *douyinProductRewardsDo {
	return d.withDO(d.DO.Scopes(funcs...))
}

func (d douyinProductRewardsDo) Unscoped() *douyinProductRewardsDo {
	return d.withDO(d.DO.Unscoped())
}

func (d douyinProductRewardsDo) Create(values ...*model.DouyinProductRewards) error {
	if len(values) == 0 {
		return nil
	}
	return d.DO.Create(values)
}

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

func (d douyinProductRewardsDo) First() (*model.DouyinProductRewards, error) {
	if result, err := d.DO.First(); err != nil {
		return nil, err
	} else {
		return result.(*model.DouyinProductRewards), nil
	}
}

func (d douyinProductRewardsDo) Take() (*model.DouyinProductRewards, error) {
	if result, err := d.DO.Take(); err != nil {
		return nil, err
	} else {
		return result.(*model.DouyinProductRewards), nil
	}
}

func (d douyinProductRewardsDo) Last() (*model.DouyinProductRewards, error) {
	if result, err := d.DO.Last(); err != nil {
		return nil, err
	} else {
		return result.(*model.DouyinProductRewards), nil
	}
}

func (d douyinProductRewardsDo) Find() ([]*model.DouyinProductRewards, error) {
	result, err := d.DO.Find()
	return result.([]*model.DouyinProductRewards), err
}

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

func (d douyinProductRewardsDo) Attrs(attrs ...field.AssignExpr) *douyinProductRewardsDo {
	return d.withDO(d.DO.Attrs(attrs...))
}

func (d douyinProductRewardsDo) Assign(attrs ...field.AssignExpr) *douyinProductRewardsDo {
	return d.withDO(d.DO.Assign(attrs...))
}

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

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

func (d douyinProductRewardsDo) FirstOrInit() (*model.DouyinProductRewards, error) {
	if result, err := d.DO.FirstOrInit(); err != nil {
		return nil, err
	} else {
		return result.(*model.DouyinProductRewards), nil
	}
}

func (d douyinProductRewardsDo) FirstOrCreate() (*model.DouyinProductRewards, error) {
	if result, err := d.DO.FirstOrCreate(); err != nil {
		return nil, err
	} else {
		return result.(*model.DouyinProductRewards), nil
	}
}

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

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

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

func (d douyinProductRewardsDo) Scan(result interface{}) (err error) {
	return d.DO.Scan(result)
}

func (d douyinProductRewardsDo) Delete(models ...*model.DouyinProductRewards) (result gen.ResultInfo, err error) {
	return d.DO.Delete(models)
}

func (d *douyinProductRewardsDo) withDO(do gen.Dao) *douyinProductRewardsDo {
	d.DO = *do.(*gen.DO)
	return d
}
