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

	_changedLog.changedLogDo.UseDB(db, opts...)
	_changedLog.changedLogDo.UseModel(&model.ChangedLog{})

	tableName := _changedLog.changedLogDo.TableName()
	_changedLog.ALL = field.NewAsterisk(tableName)
	_changedLog.ID = field.NewInt64(tableName, "id")
	_changedLog.ChannelID = field.NewString(tableName, "channel_id")
	_changedLog.UserID = field.NewInt64(tableName, "user_id")
	_changedLog.ServerID = field.NewInt32(tableName, "server_id")
	_changedLog.Source = field.NewInt32(tableName, "source")
	_changedLog.Coin = field.NewInt64(tableName, "coin")
	_changedLog.Diamond = field.NewInt64(tableName, "diamond")
	_changedLog.CoinSum = field.NewInt64(tableName, "coin_sum")
	_changedLog.DiamondSum = field.NewInt64(tableName, "diamond_sum")
	_changedLog.Times = field.NewInt64(tableName, "times")
	_changedLog.Time = field.NewInt32(tableName, "time")
	_changedLog.Date = field.NewTime(tableName, "date")

	_changedLog.fillFieldMap()

	return _changedLog
}

type changedLog struct {
	changedLogDo

	ALL        field.Asterisk
	ID         field.Int64
	ChannelID  field.String // 渠道ID
	UserID     field.Int64  // 用户ID
	ServerID   field.Int32  // 服务器ID
	Source     field.Int32  // 来源类型
	Coin       field.Int64  // 变动金币
	Diamond    field.Int64  // 变动钻石
	CoinSum    field.Int64  // 变动后剩余金币
	DiamondSum field.Int64  // 变动后剩余钻石
	Times      field.Int64  // 变动数量
	Time       field.Int32  // 变动时间
	Date       field.Time   // 日期

	fieldMap map[string]field.Expr
}

func (c changedLog) Table(newTableName string) *changedLog {
	c.changedLogDo.UseTable(newTableName)
	return c.updateTableName(newTableName)
}

func (c changedLog) As(alias string) *changedLog {
	c.changedLogDo.DO = *(c.changedLogDo.As(alias).(*gen.DO))
	return c.updateTableName(alias)
}

func (c *changedLog) updateTableName(table string) *changedLog {
	c.ALL = field.NewAsterisk(table)
	c.ID = field.NewInt64(table, "id")
	c.ChannelID = field.NewString(table, "channel_id")
	c.UserID = field.NewInt64(table, "user_id")
	c.ServerID = field.NewInt32(table, "server_id")
	c.Source = field.NewInt32(table, "source")
	c.Coin = field.NewInt64(table, "coin")
	c.Diamond = field.NewInt64(table, "diamond")
	c.CoinSum = field.NewInt64(table, "coin_sum")
	c.DiamondSum = field.NewInt64(table, "diamond_sum")
	c.Times = field.NewInt64(table, "times")
	c.Time = field.NewInt32(table, "time")
	c.Date = field.NewTime(table, "date")

	c.fillFieldMap()

	return c
}

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

func (c *changedLog) fillFieldMap() {
	c.fieldMap = make(map[string]field.Expr, 12)
	c.fieldMap["id"] = c.ID
	c.fieldMap["channel_id"] = c.ChannelID
	c.fieldMap["user_id"] = c.UserID
	c.fieldMap["server_id"] = c.ServerID
	c.fieldMap["source"] = c.Source
	c.fieldMap["coin"] = c.Coin
	c.fieldMap["diamond"] = c.Diamond
	c.fieldMap["coin_sum"] = c.CoinSum
	c.fieldMap["diamond_sum"] = c.DiamondSum
	c.fieldMap["times"] = c.Times
	c.fieldMap["time"] = c.Time
	c.fieldMap["date"] = c.Date
}

func (c changedLog) clone(db *gorm.DB) changedLog {
	c.changedLogDo.ReplaceConnPool(db.Statement.ConnPool)
	return c
}

func (c changedLog) replaceDB(db *gorm.DB) changedLog {
	c.changedLogDo.ReplaceDB(db)
	return c
}

type changedLogDo struct{ gen.DO }

func (c changedLogDo) Debug() *changedLogDo {
	return c.withDO(c.DO.Debug())
}

func (c changedLogDo) WithContext(ctx context.Context) *changedLogDo {
	return c.withDO(c.DO.WithContext(ctx))
}

func (c changedLogDo) ReadDB() *changedLogDo {
	return c.Clauses(dbresolver.Read)
}

func (c changedLogDo) WriteDB() *changedLogDo {
	return c.Clauses(dbresolver.Write)
}

func (c changedLogDo) Session(config *gorm.Session) *changedLogDo {
	return c.withDO(c.DO.Session(config))
}

func (c changedLogDo) Clauses(conds ...clause.Expression) *changedLogDo {
	return c.withDO(c.DO.Clauses(conds...))
}

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

func (c changedLogDo) Not(conds ...gen.Condition) *changedLogDo {
	return c.withDO(c.DO.Not(conds...))
}

func (c changedLogDo) Or(conds ...gen.Condition) *changedLogDo {
	return c.withDO(c.DO.Or(conds...))
}

func (c changedLogDo) Select(conds ...field.Expr) *changedLogDo {
	return c.withDO(c.DO.Select(conds...))
}

func (c changedLogDo) Where(conds ...gen.Condition) *changedLogDo {
	return c.withDO(c.DO.Where(conds...))
}

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

func (c changedLogDo) Order(conds ...field.Expr) *changedLogDo {
	return c.withDO(c.DO.Order(conds...))
}

func (c changedLogDo) Distinct(cols ...field.Expr) *changedLogDo {
	return c.withDO(c.DO.Distinct(cols...))
}

func (c changedLogDo) Omit(cols ...field.Expr) *changedLogDo {
	return c.withDO(c.DO.Omit(cols...))
}

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

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

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

func (c changedLogDo) Group(cols ...field.Expr) *changedLogDo {
	return c.withDO(c.DO.Group(cols...))
}

func (c changedLogDo) Having(conds ...gen.Condition) *changedLogDo {
	return c.withDO(c.DO.Having(conds...))
}

func (c changedLogDo) Limit(limit int) *changedLogDo {
	return c.withDO(c.DO.Limit(limit))
}

func (c changedLogDo) Offset(offset int) *changedLogDo {
	return c.withDO(c.DO.Offset(offset))
}

func (c changedLogDo) Scopes(funcs ...func(gen.Dao) gen.Dao) *changedLogDo {
	return c.withDO(c.DO.Scopes(funcs...))
}

func (c changedLogDo) Unscoped() *changedLogDo {
	return c.withDO(c.DO.Unscoped())
}

func (c changedLogDo) Create(values ...*model.ChangedLog) error {
	if len(values) == 0 {
		return nil
	}
	return c.DO.Create(values)
}

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

func (c changedLogDo) First() (*model.ChangedLog, error) {
	if result, err := c.DO.First(); err != nil {
		return nil, err
	} else {
		return result.(*model.ChangedLog), nil
	}
}

func (c changedLogDo) Take() (*model.ChangedLog, error) {
	if result, err := c.DO.Take(); err != nil {
		return nil, err
	} else {
		return result.(*model.ChangedLog), nil
	}
}

func (c changedLogDo) Last() (*model.ChangedLog, error) {
	if result, err := c.DO.Last(); err != nil {
		return nil, err
	} else {
		return result.(*model.ChangedLog), nil
	}
}

func (c changedLogDo) Find() ([]*model.ChangedLog, error) {
	result, err := c.DO.Find()
	return result.([]*model.ChangedLog), err
}

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

func (c changedLogDo) Attrs(attrs ...field.AssignExpr) *changedLogDo {
	return c.withDO(c.DO.Attrs(attrs...))
}

func (c changedLogDo) Assign(attrs ...field.AssignExpr) *changedLogDo {
	return c.withDO(c.DO.Assign(attrs...))
}

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

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

func (c changedLogDo) FirstOrInit() (*model.ChangedLog, error) {
	if result, err := c.DO.FirstOrInit(); err != nil {
		return nil, err
	} else {
		return result.(*model.ChangedLog), nil
	}
}

func (c changedLogDo) FirstOrCreate() (*model.ChangedLog, error) {
	if result, err := c.DO.FirstOrCreate(); err != nil {
		return nil, err
	} else {
		return result.(*model.ChangedLog), nil
	}
}

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

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

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

func (c changedLogDo) Scan(result interface{}) (err error) {
	return c.DO.Scan(result)
}

func (c changedLogDo) Delete(models ...*model.ChangedLog) (result gen.ResultInfo, err error) {
	return c.DO.Delete(models)
}

func (c *changedLogDo) withDO(do gen.Dao) *changedLogDo {
	c.DO = *do.(*gen.DO)
	return c
}
