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

	_changedSync.changedSyncDo.UseDB(db, opts...)
	_changedSync.changedSyncDo.UseModel(&model.ChangedSync{})

	tableName := _changedSync.changedSyncDo.TableName()
	_changedSync.ALL = field.NewAsterisk(tableName)
	_changedSync.ID = field.NewInt64(tableName, "id")
	_changedSync.Type = field.NewInt64(tableName, "type")
	_changedSync.ServerID = field.NewInt32(tableName, "server_id")
	_changedSync.LastID = field.NewInt64(tableName, "last_id")
	_changedSync.LastSyncTime = field.NewTime(tableName, "last_sync_time")
	_changedSync.UpdatedAt = field.NewTime(tableName, "updated_at")

	_changedSync.fillFieldMap()

	return _changedSync
}

type changedSync struct {
	changedSyncDo

	ALL          field.Asterisk
	ID           field.Int64
	Type         field.Int64 // 类型，1：累计统计钻石;2：累计统计金币;99：玩家统计
	ServerID     field.Int32 // 服务器ID
	LastID       field.Int64 // 最后同步ID
	LastSyncTime field.Time  // 最后同步时间
	UpdatedAt    field.Time  // 更新时间

	fieldMap map[string]field.Expr
}

func (c changedSync) Table(newTableName string) *changedSync {
	c.changedSyncDo.UseTable(newTableName)
	return c.updateTableName(newTableName)
}

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

func (c *changedSync) updateTableName(table string) *changedSync {
	c.ALL = field.NewAsterisk(table)
	c.ID = field.NewInt64(table, "id")
	c.Type = field.NewInt64(table, "type")
	c.ServerID = field.NewInt32(table, "server_id")
	c.LastID = field.NewInt64(table, "last_id")
	c.LastSyncTime = field.NewTime(table, "last_sync_time")
	c.UpdatedAt = field.NewTime(table, "updated_at")

	c.fillFieldMap()

	return c
}

func (c *changedSync) 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 *changedSync) fillFieldMap() {
	c.fieldMap = make(map[string]field.Expr, 6)
	c.fieldMap["id"] = c.ID
	c.fieldMap["type"] = c.Type
	c.fieldMap["server_id"] = c.ServerID
	c.fieldMap["last_id"] = c.LastID
	c.fieldMap["last_sync_time"] = c.LastSyncTime
	c.fieldMap["updated_at"] = c.UpdatedAt
}

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

func (c changedSync) replaceDB(db *gorm.DB) changedSync {
	c.changedSyncDo.ReplaceDB(db)
	return c
}

type changedSyncDo struct{ gen.DO }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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