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

	_treasureLog.treasureLogDo.UseDB(db, opts...)
	_treasureLog.treasureLogDo.UseModel(&model.TreasureLog{})

	tableName := _treasureLog.treasureLogDo.TableName()
	_treasureLog.ALL = field.NewAsterisk(tableName)
	_treasureLog.ID = field.NewInt64(tableName, "id")
	_treasureLog.UserID = field.NewInt64(tableName, "user_id")
	_treasureLog.ServerID = field.NewInt32(tableName, "server_id")
	_treasureLog.EventID = field.NewInt32(tableName, "event_id")
	_treasureLog.Type = field.NewInt32(tableName, "type")
	_treasureLog.ItemID = field.NewInt32(tableName, "item_id")
	_treasureLog.ItemCount = field.NewInt64(tableName, "item_count")
	_treasureLog.Level = field.NewInt32(tableName, "level")
	_treasureLog.Star = field.NewInt32(tableName, "star")
	_treasureLog.Date = field.NewString(tableName, "date")
	_treasureLog.EventAt = field.NewInt32(tableName, "event_at")
	_treasureLog.EventAtNs = field.NewInt64(tableName, "event_at_ns")

	_treasureLog.fillFieldMap()

	return _treasureLog
}

type treasureLog struct {
	treasureLogDo

	ALL       field.Asterisk
	ID        field.Int64
	UserID    field.Int64  // 用户ID
	ServerID  field.Int32  // 服务器ID
	EventID   field.Int32  // 事件ID
	Type      field.Int32  // 宝物类型
	ItemID    field.Int32  // 宝物碎片ID
	ItemCount field.Int64  // 宝物碎片数量
	Level     field.Int32  // 升级后等级
	Star      field.Int32  // 升级后星级
	Date      field.String // 触发日期 2005-01-02
	EventAt   field.Int32  // 触发时间戳
	EventAtNs field.Int64  // 发生时间ns

	fieldMap map[string]field.Expr
}

func (t treasureLog) Table(newTableName string) *treasureLog {
	t.treasureLogDo.UseTable(newTableName)
	return t.updateTableName(newTableName)
}

func (t treasureLog) As(alias string) *treasureLog {
	t.treasureLogDo.DO = *(t.treasureLogDo.As(alias).(*gen.DO))
	return t.updateTableName(alias)
}

func (t *treasureLog) updateTableName(table string) *treasureLog {
	t.ALL = field.NewAsterisk(table)
	t.ID = field.NewInt64(table, "id")
	t.UserID = field.NewInt64(table, "user_id")
	t.ServerID = field.NewInt32(table, "server_id")
	t.EventID = field.NewInt32(table, "event_id")
	t.Type = field.NewInt32(table, "type")
	t.ItemID = field.NewInt32(table, "item_id")
	t.ItemCount = field.NewInt64(table, "item_count")
	t.Level = field.NewInt32(table, "level")
	t.Star = field.NewInt32(table, "star")
	t.Date = field.NewString(table, "date")
	t.EventAt = field.NewInt32(table, "event_at")
	t.EventAtNs = field.NewInt64(table, "event_at_ns")

	t.fillFieldMap()

	return t
}

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

func (t *treasureLog) fillFieldMap() {
	t.fieldMap = make(map[string]field.Expr, 12)
	t.fieldMap["id"] = t.ID
	t.fieldMap["user_id"] = t.UserID
	t.fieldMap["server_id"] = t.ServerID
	t.fieldMap["event_id"] = t.EventID
	t.fieldMap["type"] = t.Type
	t.fieldMap["item_id"] = t.ItemID
	t.fieldMap["item_count"] = t.ItemCount
	t.fieldMap["level"] = t.Level
	t.fieldMap["star"] = t.Star
	t.fieldMap["date"] = t.Date
	t.fieldMap["event_at"] = t.EventAt
	t.fieldMap["event_at_ns"] = t.EventAtNs
}

func (t treasureLog) clone(db *gorm.DB) treasureLog {
	t.treasureLogDo.ReplaceConnPool(db.Statement.ConnPool)
	return t
}

func (t treasureLog) replaceDB(db *gorm.DB) treasureLog {
	t.treasureLogDo.ReplaceDB(db)
	return t
}

type treasureLogDo struct{ gen.DO }

func (t treasureLogDo) Debug() *treasureLogDo {
	return t.withDO(t.DO.Debug())
}

func (t treasureLogDo) WithContext(ctx context.Context) *treasureLogDo {
	return t.withDO(t.DO.WithContext(ctx))
}

func (t treasureLogDo) ReadDB() *treasureLogDo {
	return t.Clauses(dbresolver.Read)
}

func (t treasureLogDo) WriteDB() *treasureLogDo {
	return t.Clauses(dbresolver.Write)
}

func (t treasureLogDo) Session(config *gorm.Session) *treasureLogDo {
	return t.withDO(t.DO.Session(config))
}

func (t treasureLogDo) Clauses(conds ...clause.Expression) *treasureLogDo {
	return t.withDO(t.DO.Clauses(conds...))
}

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

func (t treasureLogDo) Not(conds ...gen.Condition) *treasureLogDo {
	return t.withDO(t.DO.Not(conds...))
}

func (t treasureLogDo) Or(conds ...gen.Condition) *treasureLogDo {
	return t.withDO(t.DO.Or(conds...))
}

func (t treasureLogDo) Select(conds ...field.Expr) *treasureLogDo {
	return t.withDO(t.DO.Select(conds...))
}

func (t treasureLogDo) Where(conds ...gen.Condition) *treasureLogDo {
	return t.withDO(t.DO.Where(conds...))
}

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

func (t treasureLogDo) Order(conds ...field.Expr) *treasureLogDo {
	return t.withDO(t.DO.Order(conds...))
}

func (t treasureLogDo) Distinct(cols ...field.Expr) *treasureLogDo {
	return t.withDO(t.DO.Distinct(cols...))
}

func (t treasureLogDo) Omit(cols ...field.Expr) *treasureLogDo {
	return t.withDO(t.DO.Omit(cols...))
}

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

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

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

func (t treasureLogDo) Group(cols ...field.Expr) *treasureLogDo {
	return t.withDO(t.DO.Group(cols...))
}

func (t treasureLogDo) Having(conds ...gen.Condition) *treasureLogDo {
	return t.withDO(t.DO.Having(conds...))
}

func (t treasureLogDo) Limit(limit int) *treasureLogDo {
	return t.withDO(t.DO.Limit(limit))
}

func (t treasureLogDo) Offset(offset int) *treasureLogDo {
	return t.withDO(t.DO.Offset(offset))
}

func (t treasureLogDo) Scopes(funcs ...func(gen.Dao) gen.Dao) *treasureLogDo {
	return t.withDO(t.DO.Scopes(funcs...))
}

func (t treasureLogDo) Unscoped() *treasureLogDo {
	return t.withDO(t.DO.Unscoped())
}

func (t treasureLogDo) Create(values ...*model.TreasureLog) error {
	if len(values) == 0 {
		return nil
	}
	return t.DO.Create(values)
}

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

func (t treasureLogDo) First() (*model.TreasureLog, error) {
	if result, err := t.DO.First(); err != nil {
		return nil, err
	} else {
		return result.(*model.TreasureLog), nil
	}
}

func (t treasureLogDo) Take() (*model.TreasureLog, error) {
	if result, err := t.DO.Take(); err != nil {
		return nil, err
	} else {
		return result.(*model.TreasureLog), nil
	}
}

func (t treasureLogDo) Last() (*model.TreasureLog, error) {
	if result, err := t.DO.Last(); err != nil {
		return nil, err
	} else {
		return result.(*model.TreasureLog), nil
	}
}

func (t treasureLogDo) Find() ([]*model.TreasureLog, error) {
	result, err := t.DO.Find()
	return result.([]*model.TreasureLog), err
}

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

func (t treasureLogDo) Attrs(attrs ...field.AssignExpr) *treasureLogDo {
	return t.withDO(t.DO.Attrs(attrs...))
}

func (t treasureLogDo) Assign(attrs ...field.AssignExpr) *treasureLogDo {
	return t.withDO(t.DO.Assign(attrs...))
}

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

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

func (t treasureLogDo) FirstOrInit() (*model.TreasureLog, error) {
	if result, err := t.DO.FirstOrInit(); err != nil {
		return nil, err
	} else {
		return result.(*model.TreasureLog), nil
	}
}

func (t treasureLogDo) FirstOrCreate() (*model.TreasureLog, error) {
	if result, err := t.DO.FirstOrCreate(); err != nil {
		return nil, err
	} else {
		return result.(*model.TreasureLog), nil
	}
}

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

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

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

func (t treasureLogDo) Scan(result interface{}) (err error) {
	return t.DO.Scan(result)
}

func (t treasureLogDo) Delete(models ...*model.TreasureLog) (result gen.ResultInfo, err error) {
	return t.DO.Delete(models)
}

func (t *treasureLogDo) withDO(do gen.Dao) *treasureLogDo {
	t.DO = *do.(*gen.DO)
	return t
}
