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

	_treasureChestLog.treasureChestLogDo.UseDB(db, opts...)
	_treasureChestLog.treasureChestLogDo.UseModel(&model.TreasureChestLog{})

	tableName := _treasureChestLog.treasureChestLogDo.TableName()
	_treasureChestLog.ALL = field.NewAsterisk(tableName)
	_treasureChestLog.ID = field.NewInt64(tableName, "id")
	_treasureChestLog.UserID = field.NewInt64(tableName, "user_id")
	_treasureChestLog.ServerID = field.NewInt32(tableName, "server_id")
	_treasureChestLog.EventID = field.NewInt32(tableName, "event_id")
	_treasureChestLog.Type = field.NewInt32(tableName, "type")
	_treasureChestLog.ItemID = field.NewInt32(tableName, "item_id")
	_treasureChestLog.OpenCount = field.NewInt64(tableName, "open_count")
	_treasureChestLog.Date = field.NewString(tableName, "date")
	_treasureChestLog.EventAt = field.NewInt32(tableName, "event_at")
	_treasureChestLog.EventAtNs = field.NewInt64(tableName, "event_at_ns")

	_treasureChestLog.fillFieldMap()

	return _treasureChestLog
}

type treasureChestLog struct {
	treasureChestLogDo

	ALL       field.Asterisk
	ID        field.Int64
	UserID    field.Int64  // 用户ID
	ServerID  field.Int32  // 服务器ID
	EventID   field.Int32  // 事件ID
	Type      field.Int32  // 宝箱类型1 白银 2 黄金
	ItemID    field.Int32  // 宝箱ID
	OpenCount field.Int64  // 开启宝箱数量
	Date      field.String // 触发日期 2005-01-02
	EventAt   field.Int32  // 触发时间戳
	EventAtNs field.Int64  // 发生时间ns

	fieldMap map[string]field.Expr
}

func (t treasureChestLog) Table(newTableName string) *treasureChestLog {
	t.treasureChestLogDo.UseTable(newTableName)
	return t.updateTableName(newTableName)
}

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

func (t *treasureChestLog) updateTableName(table string) *treasureChestLog {
	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.OpenCount = field.NewInt64(table, "open_count")
	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 *treasureChestLog) 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 *treasureChestLog) fillFieldMap() {
	t.fieldMap = make(map[string]field.Expr, 10)
	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["open_count"] = t.OpenCount
	t.fieldMap["date"] = t.Date
	t.fieldMap["event_at"] = t.EventAt
	t.fieldMap["event_at_ns"] = t.EventAtNs
}

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

func (t treasureChestLog) replaceDB(db *gorm.DB) treasureChestLog {
	t.treasureChestLogDo.ReplaceDB(db)
	return t
}

type treasureChestLogDo struct{ gen.DO }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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