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

	_advertisementLog.advertisementLogDo.UseDB(db, opts...)
	_advertisementLog.advertisementLogDo.UseModel(&model.AdvertisementLog{})

	tableName := _advertisementLog.advertisementLogDo.TableName()
	_advertisementLog.ALL = field.NewAsterisk(tableName)
	_advertisementLog.ID = field.NewInt64(tableName, "id")
	_advertisementLog.UserID = field.NewInt64(tableName, "user_id")
	_advertisementLog.ServerID = field.NewInt32(tableName, "server_id")
	_advertisementLog.PositionID = field.NewInt32(tableName, "position_id")
	_advertisementLog.ChapterID = field.NewInt32(tableName, "chapter_id")
	_advertisementLog.EventID = field.NewInt32(tableName, "event_id")
	_advertisementLog.EventAt = field.NewInt32(tableName, "event_at")
	_advertisementLog.UserCreatedAt = field.NewInt32(tableName, "user_created_at")
	_advertisementLog.Extra = field.NewString(tableName, "extra")
	_advertisementLog.Flag = field.NewInt32(tableName, "flag")
	_advertisementLog.ChannelID = field.NewString(tableName, "channel_id")

	_advertisementLog.fillFieldMap()

	return _advertisementLog
}

type advertisementLog struct {
	advertisementLogDo

	ALL           field.Asterisk
	ID            field.Int64
	UserID        field.Int64
	ServerID      field.Int32 // 服务器ID
	PositionID    field.Int32 // 广告位id
	ChapterID     field.Int32
	EventID       field.Int32  // 触发事件 1 展示 2 点击 3 领取奖励
	EventAt       field.Int32  // 事件触发的时间
	UserCreatedAt field.Int32  // 用户注册时间
	Extra         field.String // 奖励
	Flag          field.Int32
	ChannelID     field.String

	fieldMap map[string]field.Expr
}

func (a advertisementLog) Table(newTableName string) *advertisementLog {
	a.advertisementLogDo.UseTable(newTableName)
	return a.updateTableName(newTableName)
}

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

func (a *advertisementLog) updateTableName(table string) *advertisementLog {
	a.ALL = field.NewAsterisk(table)
	a.ID = field.NewInt64(table, "id")
	a.UserID = field.NewInt64(table, "user_id")
	a.ServerID = field.NewInt32(table, "server_id")
	a.PositionID = field.NewInt32(table, "position_id")
	a.ChapterID = field.NewInt32(table, "chapter_id")
	a.EventID = field.NewInt32(table, "event_id")
	a.EventAt = field.NewInt32(table, "event_at")
	a.UserCreatedAt = field.NewInt32(table, "user_created_at")
	a.Extra = field.NewString(table, "extra")
	a.Flag = field.NewInt32(table, "flag")
	a.ChannelID = field.NewString(table, "channel_id")

	a.fillFieldMap()

	return a
}

func (a *advertisementLog) 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 *advertisementLog) fillFieldMap() {
	a.fieldMap = make(map[string]field.Expr, 11)
	a.fieldMap["id"] = a.ID
	a.fieldMap["user_id"] = a.UserID
	a.fieldMap["server_id"] = a.ServerID
	a.fieldMap["position_id"] = a.PositionID
	a.fieldMap["chapter_id"] = a.ChapterID
	a.fieldMap["event_id"] = a.EventID
	a.fieldMap["event_at"] = a.EventAt
	a.fieldMap["user_created_at"] = a.UserCreatedAt
	a.fieldMap["extra"] = a.Extra
	a.fieldMap["flag"] = a.Flag
	a.fieldMap["channel_id"] = a.ChannelID
}

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

func (a advertisementLog) replaceDB(db *gorm.DB) advertisementLog {
	a.advertisementLogDo.ReplaceDB(db)
	return a
}

type advertisementLogDo struct{ gen.DO }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

func (a advertisementLogDo) FindByPage(offset int, limit int) (result []*model.AdvertisementLog, 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 advertisementLogDo) 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 advertisementLogDo) Scan(result interface{}) (err error) {
	return a.DO.Scan(result)
}

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

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