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

	_chapter.chapterDo.UseDB(db, opts...)
	_chapter.chapterDo.UseModel(&model.Chapter{})

	tableName := _chapter.chapterDo.TableName()
	_chapter.ALL = field.NewAsterisk(tableName)
	_chapter.ID = field.NewInt64(tableName, "id")
	_chapter.PlayerID = field.NewInt64(tableName, "player_id")
	_chapter.ServerID = field.NewInt32(tableName, "server_id")
	_chapter.ChapterID = field.NewInt32(tableName, "chapter_id")
	_chapter.Degree = field.NewString(tableName, "degree")
	_chapter.ChallengeCount = field.NewInt32(tableName, "challenge_count")
	_chapter.ClearanceCount = field.NewInt32(tableName, "clearance_count")
	_chapter.FailCount = field.NewInt32(tableName, "fail_count")
	_chapter.Difficulty = field.NewInt32(tableName, "difficulty")
	_chapter.IsPass = field.NewInt32(tableName, "is_pass")
	_chapter.PassCount = field.NewInt32(tableName, "pass_count")
	_chapter.CreatedAt = field.NewTime(tableName, "created_at")
	_chapter.UpdatedAt = field.NewTime(tableName, "updated_at")

	_chapter.fillFieldMap()

	return _chapter
}

type chapter struct {
	chapterDo

	ALL            field.Asterisk
	ID             field.Int64
	PlayerID       field.Int64  // 玩家ID
	ServerID       field.Int32  // 服务器ID
	ChapterID      field.Int32  // 章节ID
	Degree         field.String // 难度系数
	ChallengeCount field.Int32  // 总挑战次数
	ClearanceCount field.Int32  // 通关次数
	FailCount      field.Int32  // 失败次数
	Difficulty     field.Int32  // 难度
	IsPass         field.Int32  // 是否通关
	PassCount      field.Int32  // 通关房间数
	CreatedAt      field.Time   // 创建时间
	UpdatedAt      field.Time   // 更新时间

	fieldMap map[string]field.Expr
}

func (c chapter) Table(newTableName string) *chapter {
	c.chapterDo.UseTable(newTableName)
	return c.updateTableName(newTableName)
}

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

func (c *chapter) updateTableName(table string) *chapter {
	c.ALL = field.NewAsterisk(table)
	c.ID = field.NewInt64(table, "id")
	c.PlayerID = field.NewInt64(table, "player_id")
	c.ServerID = field.NewInt32(table, "server_id")
	c.ChapterID = field.NewInt32(table, "chapter_id")
	c.Degree = field.NewString(table, "degree")
	c.ChallengeCount = field.NewInt32(table, "challenge_count")
	c.ClearanceCount = field.NewInt32(table, "clearance_count")
	c.FailCount = field.NewInt32(table, "fail_count")
	c.Difficulty = field.NewInt32(table, "difficulty")
	c.IsPass = field.NewInt32(table, "is_pass")
	c.PassCount = field.NewInt32(table, "pass_count")
	c.CreatedAt = field.NewTime(table, "created_at")
	c.UpdatedAt = field.NewTime(table, "updated_at")

	c.fillFieldMap()

	return c
}

func (c *chapter) 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 *chapter) fillFieldMap() {
	c.fieldMap = make(map[string]field.Expr, 13)
	c.fieldMap["id"] = c.ID
	c.fieldMap["player_id"] = c.PlayerID
	c.fieldMap["server_id"] = c.ServerID
	c.fieldMap["chapter_id"] = c.ChapterID
	c.fieldMap["degree"] = c.Degree
	c.fieldMap["challenge_count"] = c.ChallengeCount
	c.fieldMap["clearance_count"] = c.ClearanceCount
	c.fieldMap["fail_count"] = c.FailCount
	c.fieldMap["difficulty"] = c.Difficulty
	c.fieldMap["is_pass"] = c.IsPass
	c.fieldMap["pass_count"] = c.PassCount
	c.fieldMap["created_at"] = c.CreatedAt
	c.fieldMap["updated_at"] = c.UpdatedAt
}

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

func (c chapter) replaceDB(db *gorm.DB) chapter {
	c.chapterDo.ReplaceDB(db)
	return c
}

type chapterDo struct{ gen.DO }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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