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

	_chapterSync.chapterSyncDo.UseDB(db, opts...)
	_chapterSync.chapterSyncDo.UseModel(&model.ChapterSync{})

	tableName := _chapterSync.chapterSyncDo.TableName()
	_chapterSync.ALL = field.NewAsterisk(tableName)
	_chapterSync.ID = field.NewInt64(tableName, "id")
	_chapterSync.ChapterID = field.NewInt64(tableName, "chapter_id")
	_chapterSync.ServerID = field.NewInt32(tableName, "server_id")
	_chapterSync.LastID = field.NewInt64(tableName, "last_id")
	_chapterSync.LastSyncTime = field.NewTime(tableName, "last_sync_time")
	_chapterSync.UpdatedAt = field.NewTime(tableName, "updated_at")
	_chapterSync.Difficulty = field.NewInt32(tableName, "difficulty")

	_chapterSync.fillFieldMap()

	return _chapterSync
}

type chapterSync struct {
	chapterSyncDo

	ALL          field.Asterisk
	ID           field.Int64
	ChapterID    field.Int64 // 章节ID
	ServerID     field.Int32 // 服务器ID
	LastID       field.Int64 // 最后同步ID
	LastSyncTime field.Time  // 最后同步时间
	UpdatedAt    field.Time  // 更新时间
	Difficulty   field.Int32

	fieldMap map[string]field.Expr
}

func (c chapterSync) Table(newTableName string) *chapterSync {
	c.chapterSyncDo.UseTable(newTableName)
	return c.updateTableName(newTableName)
}

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

func (c *chapterSync) updateTableName(table string) *chapterSync {
	c.ALL = field.NewAsterisk(table)
	c.ID = field.NewInt64(table, "id")
	c.ChapterID = field.NewInt64(table, "chapter_id")
	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.Difficulty = field.NewInt32(table, "difficulty")

	c.fillFieldMap()

	return c
}

func (c *chapterSync) 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 *chapterSync) fillFieldMap() {
	c.fieldMap = make(map[string]field.Expr, 7)
	c.fieldMap["id"] = c.ID
	c.fieldMap["chapter_id"] = c.ChapterID
	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
	c.fieldMap["difficulty"] = c.Difficulty
}

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

func (c chapterSync) replaceDB(db *gorm.DB) chapterSync {
	c.chapterSyncDo.ReplaceDB(db)
	return c
}

type chapterSyncDo struct{ gen.DO }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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