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

import (
	"context"

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

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

	"gorm.io/plugin/dbresolver"

	"gadmin/internal/gorm/model"
)

func newAdvertisementSpace(db *gorm.DB, opts ...gen.DOOption) advertisementSpace {
	_advertisementSpace := advertisementSpace{}

	_advertisementSpace.advertisementSpaceDo.UseDB(db, opts...)
	_advertisementSpace.advertisementSpaceDo.UseModel(&model.AdvertisementSpace{})

	tableName := _advertisementSpace.advertisementSpaceDo.TableName()
	_advertisementSpace.ALL = field.NewAsterisk(tableName)
	_advertisementSpace.ID = field.NewInt64(tableName, "id")
	_advertisementSpace.PositionID = field.NewInt32(tableName, "position_id")
	_advertisementSpace.Name = field.NewString(tableName, "name")
	_advertisementSpace.CreatedAt = field.NewTime(tableName, "created_at")
	_advertisementSpace.UpdatedAt = field.NewTime(tableName, "updated_at")

	_advertisementSpace.fillFieldMap()

	return _advertisementSpace
}

type advertisementSpace struct {
	advertisementSpaceDo

	ALL        field.Asterisk
	ID         field.Int64
	PositionID field.Int32  // 广告位id
	Name       field.String // 广告位名称
	CreatedAt  field.Time
	UpdatedAt  field.Time

	fieldMap map[string]field.Expr
}

func (a advertisementSpace) Table(newTableName string) *advertisementSpace {
	a.advertisementSpaceDo.UseTable(newTableName)
	return a.updateTableName(newTableName)
}

func (a advertisementSpace) As(alias string) *advertisementSpace {
	a.advertisementSpaceDo.DO = *(a.advertisementSpaceDo.As(alias).(*gen.DO))
	return a.updateTableName(alias)
}

func (a *advertisementSpace) updateTableName(table string) *advertisementSpace {
	a.ALL = field.NewAsterisk(table)
	a.ID = field.NewInt64(table, "id")
	a.PositionID = field.NewInt32(table, "position_id")
	a.Name = field.NewString(table, "name")
	a.CreatedAt = field.NewTime(table, "created_at")
	a.UpdatedAt = field.NewTime(table, "updated_at")

	a.fillFieldMap()

	return a
}

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

func (a *advertisementSpace) fillFieldMap() {
	a.fieldMap = make(map[string]field.Expr, 5)
	a.fieldMap["id"] = a.ID
	a.fieldMap["position_id"] = a.PositionID
	a.fieldMap["name"] = a.Name
	a.fieldMap["created_at"] = a.CreatedAt
	a.fieldMap["updated_at"] = a.UpdatedAt
}

func (a advertisementSpace) clone(db *gorm.DB) advertisementSpace {
	a.advertisementSpaceDo.ReplaceConnPool(db.Statement.ConnPool)
	return a
}

func (a advertisementSpace) replaceDB(db *gorm.DB) advertisementSpace {
	a.advertisementSpaceDo.ReplaceDB(db)
	return a
}

type advertisementSpaceDo struct{ gen.DO }

func (a advertisementSpaceDo) Debug() *advertisementSpaceDo {
	return a.withDO(a.DO.Debug())
}

func (a advertisementSpaceDo) WithContext(ctx context.Context) *advertisementSpaceDo {
	return a.withDO(a.DO.WithContext(ctx))
}

func (a advertisementSpaceDo) ReadDB() *advertisementSpaceDo {
	return a.Clauses(dbresolver.Read)
}

func (a advertisementSpaceDo) WriteDB() *advertisementSpaceDo {
	return a.Clauses(dbresolver.Write)
}

func (a advertisementSpaceDo) Session(config *gorm.Session) *advertisementSpaceDo {
	return a.withDO(a.DO.Session(config))
}

func (a advertisementSpaceDo) Clauses(conds ...clause.Expression) *advertisementSpaceDo {
	return a.withDO(a.DO.Clauses(conds...))
}

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

func (a advertisementSpaceDo) Not(conds ...gen.Condition) *advertisementSpaceDo {
	return a.withDO(a.DO.Not(conds...))
}

func (a advertisementSpaceDo) Or(conds ...gen.Condition) *advertisementSpaceDo {
	return a.withDO(a.DO.Or(conds...))
}

func (a advertisementSpaceDo) Select(conds ...field.Expr) *advertisementSpaceDo {
	return a.withDO(a.DO.Select(conds...))
}

func (a advertisementSpaceDo) Where(conds ...gen.Condition) *advertisementSpaceDo {
	return a.withDO(a.DO.Where(conds...))
}

func (a advertisementSpaceDo) Exists(subquery interface{ UnderlyingDB() *gorm.DB }) *advertisementSpaceDo {
	return a.Where(field.CompareSubQuery(field.ExistsOp, nil, subquery.UnderlyingDB()))
}

func (a advertisementSpaceDo) Order(conds ...field.Expr) *advertisementSpaceDo {
	return a.withDO(a.DO.Order(conds...))
}

func (a advertisementSpaceDo) Distinct(cols ...field.Expr) *advertisementSpaceDo {
	return a.withDO(a.DO.Distinct(cols...))
}

func (a advertisementSpaceDo) Omit(cols ...field.Expr) *advertisementSpaceDo {
	return a.withDO(a.DO.Omit(cols...))
}

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

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

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

func (a advertisementSpaceDo) Group(cols ...field.Expr) *advertisementSpaceDo {
	return a.withDO(a.DO.Group(cols...))
}

func (a advertisementSpaceDo) Having(conds ...gen.Condition) *advertisementSpaceDo {
	return a.withDO(a.DO.Having(conds...))
}

func (a advertisementSpaceDo) Limit(limit int) *advertisementSpaceDo {
	return a.withDO(a.DO.Limit(limit))
}

func (a advertisementSpaceDo) Offset(offset int) *advertisementSpaceDo {
	return a.withDO(a.DO.Offset(offset))
}

func (a advertisementSpaceDo) Scopes(funcs ...func(gen.Dao) gen.Dao) *advertisementSpaceDo {
	return a.withDO(a.DO.Scopes(funcs...))
}

func (a advertisementSpaceDo) Unscoped() *advertisementSpaceDo {
	return a.withDO(a.DO.Unscoped())
}

func (a advertisementSpaceDo) Create(values ...*model.AdvertisementSpace) error {
	if len(values) == 0 {
		return nil
	}
	return a.DO.Create(values)
}

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

func (a advertisementSpaceDo) First() (*model.AdvertisementSpace, error) {
	if result, err := a.DO.First(); err != nil {
		return nil, err
	} else {
		return result.(*model.AdvertisementSpace), nil
	}
}

func (a advertisementSpaceDo) Take() (*model.AdvertisementSpace, error) {
	if result, err := a.DO.Take(); err != nil {
		return nil, err
	} else {
		return result.(*model.AdvertisementSpace), nil
	}
}

func (a advertisementSpaceDo) Last() (*model.AdvertisementSpace, error) {
	if result, err := a.DO.Last(); err != nil {
		return nil, err
	} else {
		return result.(*model.AdvertisementSpace), nil
	}
}

func (a advertisementSpaceDo) Find() ([]*model.AdvertisementSpace, error) {
	result, err := a.DO.Find()
	return result.([]*model.AdvertisementSpace), err
}

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

func (a advertisementSpaceDo) Attrs(attrs ...field.AssignExpr) *advertisementSpaceDo {
	return a.withDO(a.DO.Attrs(attrs...))
}

func (a advertisementSpaceDo) Assign(attrs ...field.AssignExpr) *advertisementSpaceDo {
	return a.withDO(a.DO.Assign(attrs...))
}

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

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

func (a advertisementSpaceDo) FirstOrInit() (*model.AdvertisementSpace, error) {
	if result, err := a.DO.FirstOrInit(); err != nil {
		return nil, err
	} else {
		return result.(*model.AdvertisementSpace), nil
	}
}

func (a advertisementSpaceDo) FirstOrCreate() (*model.AdvertisementSpace, error) {
	if result, err := a.DO.FirstOrCreate(); err != nil {
		return nil, err
	} else {
		return result.(*model.AdvertisementSpace), nil
	}
}

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

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

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

func (a advertisementSpaceDo) Scan(result interface{}) (err error) {
	return a.DO.Scan(result)
}

func (a advertisementSpaceDo) Delete(models ...*model.AdvertisementSpace) (result gen.ResultInfo, err error) {
	return a.DO.Delete(models)
}

func (a *advertisementSpaceDo) withDO(do gen.Dao) *advertisementSpaceDo {
	a.DO = *do.(*gen.DO)
	return a
}
