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

	_chapterLogsUserDetail.chapterLogsUserDetailDo.UseDB(db, opts...)
	_chapterLogsUserDetail.chapterLogsUserDetailDo.UseModel(&model.ChapterLogsUserDetail{})

	tableName := _chapterLogsUserDetail.chapterLogsUserDetailDo.TableName()
	_chapterLogsUserDetail.ALL = field.NewAsterisk(tableName)
	_chapterLogsUserDetail.ID = field.NewInt64(tableName, "id")
	_chapterLogsUserDetail.UserID = field.NewInt64(tableName, "user_id")
	_chapterLogsUserDetail.ServerID = field.NewInt32(tableName, "server_id")
	_chapterLogsUserDetail.EventID = field.NewInt32(tableName, "event_id")
	_chapterLogsUserDetail.ChapterID = field.NewInt32(tableName, "chapter_id")
	_chapterLogsUserDetail.RoomID = field.NewInt32(tableName, "room_id")
	_chapterLogsUserDetail.UserCreatedAt = field.NewInt32(tableName, "user_created_at")
	_chapterLogsUserDetail.EventAt = field.NewInt32(tableName, "event_at")
	_chapterLogsUserDetail.Extra = field.NewString(tableName, "extra")
	_chapterLogsUserDetail.Token = field.NewString(tableName, "token")
	_chapterLogsUserDetail.Difficulty = field.NewInt32(tableName, "difficulty")
	_chapterLogsUserDetail.EventAtNs = field.NewInt64(tableName, "event_at_ns")

	_chapterLogsUserDetail.fillFieldMap()

	return _chapterLogsUserDetail
}

type chapterLogsUserDetail struct {
	chapterLogsUserDetailDo

	ALL           field.Asterisk
	ID            field.Int64
	UserID        field.Int64  // 用户id
	ServerID      field.Int32  // 服务器ID
	EventID       field.Int32  // 埋点id
	ChapterID     field.Int32  // 关卡id
	RoomID        field.Int32  // 房间id
	UserCreatedAt field.Int32  // 用户注册时间
	EventAt       field.Int32  // 埋点事件触发的时间
	Extra         field.String // 其他参数
	Token         field.String // 登陆的token
	Difficulty    field.Int32  // 困难度
	EventAtNs     field.Int64  // 埋点事件触发的时间

	fieldMap map[string]field.Expr
}

func (c chapterLogsUserDetail) Table(newTableName string) *chapterLogsUserDetail {
	c.chapterLogsUserDetailDo.UseTable(newTableName)
	return c.updateTableName(newTableName)
}

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

func (c *chapterLogsUserDetail) updateTableName(table string) *chapterLogsUserDetail {
	c.ALL = field.NewAsterisk(table)
	c.ID = field.NewInt64(table, "id")
	c.UserID = field.NewInt64(table, "user_id")
	c.ServerID = field.NewInt32(table, "server_id")
	c.EventID = field.NewInt32(table, "event_id")
	c.ChapterID = field.NewInt32(table, "chapter_id")
	c.RoomID = field.NewInt32(table, "room_id")
	c.UserCreatedAt = field.NewInt32(table, "user_created_at")
	c.EventAt = field.NewInt32(table, "event_at")
	c.Extra = field.NewString(table, "extra")
	c.Token = field.NewString(table, "token")
	c.Difficulty = field.NewInt32(table, "difficulty")
	c.EventAtNs = field.NewInt64(table, "event_at_ns")

	c.fillFieldMap()

	return c
}

func (c *chapterLogsUserDetail) 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 *chapterLogsUserDetail) fillFieldMap() {
	c.fieldMap = make(map[string]field.Expr, 12)
	c.fieldMap["id"] = c.ID
	c.fieldMap["user_id"] = c.UserID
	c.fieldMap["server_id"] = c.ServerID
	c.fieldMap["event_id"] = c.EventID
	c.fieldMap["chapter_id"] = c.ChapterID
	c.fieldMap["room_id"] = c.RoomID
	c.fieldMap["user_created_at"] = c.UserCreatedAt
	c.fieldMap["event_at"] = c.EventAt
	c.fieldMap["extra"] = c.Extra
	c.fieldMap["token"] = c.Token
	c.fieldMap["difficulty"] = c.Difficulty
	c.fieldMap["event_at_ns"] = c.EventAtNs
}

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

func (c chapterLogsUserDetail) replaceDB(db *gorm.DB) chapterLogsUserDetail {
	c.chapterLogsUserDetailDo.ReplaceDB(db)
	return c
}

type chapterLogsUserDetailDo struct{ gen.DO }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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