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

	_duelLog.duelLogDo.UseDB(db, opts...)
	_duelLog.duelLogDo.UseModel(&model.DuelLog{})

	tableName := _duelLog.duelLogDo.TableName()
	_duelLog.ALL = field.NewAsterisk(tableName)
	_duelLog.ID = field.NewInt64(tableName, "id")
	_duelLog.UserID = field.NewInt64(tableName, "user_id")
	_duelLog.ServerID = field.NewInt32(tableName, "server_id")
	_duelLog.EventID = field.NewInt32(tableName, "event_id")
	_duelLog.Cost = field.NewInt32(tableName, "cost")
	_duelLog.BattleID = field.NewString(tableName, "battle_id")
	_duelLog.CostType = field.NewInt32(tableName, "cost_type")
	_duelLog.Date = field.NewString(tableName, "date")
	_duelLog.EventAt = field.NewInt32(tableName, "event_at")
	_duelLog.EventAtNs = field.NewInt64(tableName, "event_at_ns")

	_duelLog.fillFieldMap()

	return _duelLog
}

type duelLog struct {
	duelLogDo

	ALL       field.Asterisk
	ID        field.Int64
	UserID    field.Int64  // 用户id
	ServerID  field.Int32  // 服务器ID
	EventID   field.Int32  // 事件ID
	Cost      field.Int32  // 花费类型【0:免费 1:战帖 ，2复仇】
	BattleID  field.String // 对局id，大于0是复仇类型
	CostType  field.Int32  // 获取类型【1:看广告 2:钻石 】
	Date      field.String // 触发日期 2005-01-02
	EventAt   field.Int32  // 触发时间戳
	EventAtNs field.Int64  // 发生时间ns

	fieldMap map[string]field.Expr
}

func (d duelLog) Table(newTableName string) *duelLog {
	d.duelLogDo.UseTable(newTableName)
	return d.updateTableName(newTableName)
}

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

func (d *duelLog) updateTableName(table string) *duelLog {
	d.ALL = field.NewAsterisk(table)
	d.ID = field.NewInt64(table, "id")
	d.UserID = field.NewInt64(table, "user_id")
	d.ServerID = field.NewInt32(table, "server_id")
	d.EventID = field.NewInt32(table, "event_id")
	d.Cost = field.NewInt32(table, "cost")
	d.BattleID = field.NewString(table, "battle_id")
	d.CostType = field.NewInt32(table, "cost_type")
	d.Date = field.NewString(table, "date")
	d.EventAt = field.NewInt32(table, "event_at")
	d.EventAtNs = field.NewInt64(table, "event_at_ns")

	d.fillFieldMap()

	return d
}

func (d *duelLog) 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 *duelLog) fillFieldMap() {
	d.fieldMap = make(map[string]field.Expr, 10)
	d.fieldMap["id"] = d.ID
	d.fieldMap["user_id"] = d.UserID
	d.fieldMap["server_id"] = d.ServerID
	d.fieldMap["event_id"] = d.EventID
	d.fieldMap["cost"] = d.Cost
	d.fieldMap["battle_id"] = d.BattleID
	d.fieldMap["cost_type"] = d.CostType
	d.fieldMap["date"] = d.Date
	d.fieldMap["event_at"] = d.EventAt
	d.fieldMap["event_at_ns"] = d.EventAtNs
}

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

func (d duelLog) replaceDB(db *gorm.DB) duelLog {
	d.duelLogDo.ReplaceDB(db)
	return d
}

type duelLogDo struct{ gen.DO }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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