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

	_gemLog.gemLogDo.UseDB(db, opts...)
	_gemLog.gemLogDo.UseModel(&model.GemLog{})

	tableName := _gemLog.gemLogDo.TableName()
	_gemLog.ALL = field.NewAsterisk(tableName)
	_gemLog.ID = field.NewInt64(tableName, "id")
	_gemLog.UserID = field.NewInt64(tableName, "user_id")
	_gemLog.ServerID = field.NewInt32(tableName, "server_id")
	_gemLog.EventID = field.NewInt32(tableName, "event_id")
	_gemLog.Part = field.NewInt64(tableName, "part")
	_gemLog.Level = field.NewInt64(tableName, "level")
	_gemLog.Body = field.NewString(tableName, "body")
	_gemLog.Date = field.NewString(tableName, "date")
	_gemLog.EventAt = field.NewInt32(tableName, "event_at")
	_gemLog.EventAtNs = field.NewInt64(tableName, "event_at_ns")

	_gemLog.fillFieldMap()

	return _gemLog
}

type gemLog struct {
	gemLogDo

	ALL       field.Asterisk
	ID        field.Int64
	UserID    field.Int64  // 用户ID
	ServerID  field.Int32  // 服务器ID
	EventID   field.Int32  // 事件ID
	Part      field.Int64  // 部位（宝石）类型
	Level     field.Int64  // 宝石等级
	Body      field.String // 身体部位镶嵌
	Date      field.String // 触发日期 2005-01-02
	EventAt   field.Int32  // 触发时间戳
	EventAtNs field.Int64  // 发生时间ns

	fieldMap map[string]field.Expr
}

func (g gemLog) Table(newTableName string) *gemLog {
	g.gemLogDo.UseTable(newTableName)
	return g.updateTableName(newTableName)
}

func (g gemLog) As(alias string) *gemLog {
	g.gemLogDo.DO = *(g.gemLogDo.As(alias).(*gen.DO))
	return g.updateTableName(alias)
}

func (g *gemLog) updateTableName(table string) *gemLog {
	g.ALL = field.NewAsterisk(table)
	g.ID = field.NewInt64(table, "id")
	g.UserID = field.NewInt64(table, "user_id")
	g.ServerID = field.NewInt32(table, "server_id")
	g.EventID = field.NewInt32(table, "event_id")
	g.Part = field.NewInt64(table, "part")
	g.Level = field.NewInt64(table, "level")
	g.Body = field.NewString(table, "body")
	g.Date = field.NewString(table, "date")
	g.EventAt = field.NewInt32(table, "event_at")
	g.EventAtNs = field.NewInt64(table, "event_at_ns")

	g.fillFieldMap()

	return g
}

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

func (g *gemLog) fillFieldMap() {
	g.fieldMap = make(map[string]field.Expr, 10)
	g.fieldMap["id"] = g.ID
	g.fieldMap["user_id"] = g.UserID
	g.fieldMap["server_id"] = g.ServerID
	g.fieldMap["event_id"] = g.EventID
	g.fieldMap["part"] = g.Part
	g.fieldMap["level"] = g.Level
	g.fieldMap["body"] = g.Body
	g.fieldMap["date"] = g.Date
	g.fieldMap["event_at"] = g.EventAt
	g.fieldMap["event_at_ns"] = g.EventAtNs
}

func (g gemLog) clone(db *gorm.DB) gemLog {
	g.gemLogDo.ReplaceConnPool(db.Statement.ConnPool)
	return g
}

func (g gemLog) replaceDB(db *gorm.DB) gemLog {
	g.gemLogDo.ReplaceDB(db)
	return g
}

type gemLogDo struct{ gen.DO }

func (g gemLogDo) Debug() *gemLogDo {
	return g.withDO(g.DO.Debug())
}

func (g gemLogDo) WithContext(ctx context.Context) *gemLogDo {
	return g.withDO(g.DO.WithContext(ctx))
}

func (g gemLogDo) ReadDB() *gemLogDo {
	return g.Clauses(dbresolver.Read)
}

func (g gemLogDo) WriteDB() *gemLogDo {
	return g.Clauses(dbresolver.Write)
}

func (g gemLogDo) Session(config *gorm.Session) *gemLogDo {
	return g.withDO(g.DO.Session(config))
}

func (g gemLogDo) Clauses(conds ...clause.Expression) *gemLogDo {
	return g.withDO(g.DO.Clauses(conds...))
}

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

func (g gemLogDo) Not(conds ...gen.Condition) *gemLogDo {
	return g.withDO(g.DO.Not(conds...))
}

func (g gemLogDo) Or(conds ...gen.Condition) *gemLogDo {
	return g.withDO(g.DO.Or(conds...))
}

func (g gemLogDo) Select(conds ...field.Expr) *gemLogDo {
	return g.withDO(g.DO.Select(conds...))
}

func (g gemLogDo) Where(conds ...gen.Condition) *gemLogDo {
	return g.withDO(g.DO.Where(conds...))
}

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

func (g gemLogDo) Order(conds ...field.Expr) *gemLogDo {
	return g.withDO(g.DO.Order(conds...))
}

func (g gemLogDo) Distinct(cols ...field.Expr) *gemLogDo {
	return g.withDO(g.DO.Distinct(cols...))
}

func (g gemLogDo) Omit(cols ...field.Expr) *gemLogDo {
	return g.withDO(g.DO.Omit(cols...))
}

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

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

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

func (g gemLogDo) Group(cols ...field.Expr) *gemLogDo {
	return g.withDO(g.DO.Group(cols...))
}

func (g gemLogDo) Having(conds ...gen.Condition) *gemLogDo {
	return g.withDO(g.DO.Having(conds...))
}

func (g gemLogDo) Limit(limit int) *gemLogDo {
	return g.withDO(g.DO.Limit(limit))
}

func (g gemLogDo) Offset(offset int) *gemLogDo {
	return g.withDO(g.DO.Offset(offset))
}

func (g gemLogDo) Scopes(funcs ...func(gen.Dao) gen.Dao) *gemLogDo {
	return g.withDO(g.DO.Scopes(funcs...))
}

func (g gemLogDo) Unscoped() *gemLogDo {
	return g.withDO(g.DO.Unscoped())
}

func (g gemLogDo) Create(values ...*model.GemLog) error {
	if len(values) == 0 {
		return nil
	}
	return g.DO.Create(values)
}

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

func (g gemLogDo) First() (*model.GemLog, error) {
	if result, err := g.DO.First(); err != nil {
		return nil, err
	} else {
		return result.(*model.GemLog), nil
	}
}

func (g gemLogDo) Take() (*model.GemLog, error) {
	if result, err := g.DO.Take(); err != nil {
		return nil, err
	} else {
		return result.(*model.GemLog), nil
	}
}

func (g gemLogDo) Last() (*model.GemLog, error) {
	if result, err := g.DO.Last(); err != nil {
		return nil, err
	} else {
		return result.(*model.GemLog), nil
	}
}

func (g gemLogDo) Find() ([]*model.GemLog, error) {
	result, err := g.DO.Find()
	return result.([]*model.GemLog), err
}

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

func (g gemLogDo) Attrs(attrs ...field.AssignExpr) *gemLogDo {
	return g.withDO(g.DO.Attrs(attrs...))
}

func (g gemLogDo) Assign(attrs ...field.AssignExpr) *gemLogDo {
	return g.withDO(g.DO.Assign(attrs...))
}

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

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

func (g gemLogDo) FirstOrInit() (*model.GemLog, error) {
	if result, err := g.DO.FirstOrInit(); err != nil {
		return nil, err
	} else {
		return result.(*model.GemLog), nil
	}
}

func (g gemLogDo) FirstOrCreate() (*model.GemLog, error) {
	if result, err := g.DO.FirstOrCreate(); err != nil {
		return nil, err
	} else {
		return result.(*model.GemLog), nil
	}
}

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

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

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

func (g gemLogDo) Scan(result interface{}) (err error) {
	return g.DO.Scan(result)
}

func (g gemLogDo) Delete(models ...*model.GemLog) (result gen.ResultInfo, err error) {
	return g.DO.Delete(models)
}

func (g *gemLogDo) withDO(do gen.Dao) *gemLogDo {
	g.DO = *do.(*gen.DO)
	return g
}
