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

	_loginLog.loginLogDo.UseDB(db, opts...)
	_loginLog.loginLogDo.UseModel(&model.LoginLog{})

	tableName := _loginLog.loginLogDo.TableName()
	_loginLog.ALL = field.NewAsterisk(tableName)
	_loginLog.ID = field.NewInt64(tableName, "id")
	_loginLog.UserID = field.NewInt64(tableName, "user_id")
	_loginLog.ServerID = field.NewInt32(tableName, "server_id")
	_loginLog.EventID = field.NewInt32(tableName, "event_id")
	_loginLog.UserCreatedAt = field.NewInt32(tableName, "user_created_at")
	_loginLog.EventAt = field.NewInt32(tableName, "event_at")
	_loginLog.CostTime = field.NewInt64(tableName, "cost_time")
	_loginLog.ChannelID = field.NewString(tableName, "channel_id")
	_loginLog.Flag = field.NewInt32(tableName, "flag")

	_loginLog.fillFieldMap()

	return _loginLog
}

type loginLog struct {
	loginLogDo

	ALL           field.Asterisk
	ID            field.Int64
	UserID        field.Int64  // 用户id
	ServerID      field.Int32  // 服务器ID
	EventID       field.Int32  // 埋点id
	UserCreatedAt field.Int32  // 用户注册时间
	EventAt       field.Int32  // 埋点事件触发的时间
	CostTime      field.Int64  // 耗时，微秒
	ChannelID     field.String // 渠道ID
	Flag          field.Int32

	fieldMap map[string]field.Expr
}

func (l loginLog) Table(newTableName string) *loginLog {
	l.loginLogDo.UseTable(newTableName)
	return l.updateTableName(newTableName)
}

func (l loginLog) As(alias string) *loginLog {
	l.loginLogDo.DO = *(l.loginLogDo.As(alias).(*gen.DO))
	return l.updateTableName(alias)
}

func (l *loginLog) updateTableName(table string) *loginLog {
	l.ALL = field.NewAsterisk(table)
	l.ID = field.NewInt64(table, "id")
	l.UserID = field.NewInt64(table, "user_id")
	l.ServerID = field.NewInt32(table, "server_id")
	l.EventID = field.NewInt32(table, "event_id")
	l.UserCreatedAt = field.NewInt32(table, "user_created_at")
	l.EventAt = field.NewInt32(table, "event_at")
	l.CostTime = field.NewInt64(table, "cost_time")
	l.ChannelID = field.NewString(table, "channel_id")
	l.Flag = field.NewInt32(table, "flag")

	l.fillFieldMap()

	return l
}

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

func (l *loginLog) fillFieldMap() {
	l.fieldMap = make(map[string]field.Expr, 9)
	l.fieldMap["id"] = l.ID
	l.fieldMap["user_id"] = l.UserID
	l.fieldMap["server_id"] = l.ServerID
	l.fieldMap["event_id"] = l.EventID
	l.fieldMap["user_created_at"] = l.UserCreatedAt
	l.fieldMap["event_at"] = l.EventAt
	l.fieldMap["cost_time"] = l.CostTime
	l.fieldMap["channel_id"] = l.ChannelID
	l.fieldMap["flag"] = l.Flag
}

func (l loginLog) clone(db *gorm.DB) loginLog {
	l.loginLogDo.ReplaceConnPool(db.Statement.ConnPool)
	return l
}

func (l loginLog) replaceDB(db *gorm.DB) loginLog {
	l.loginLogDo.ReplaceDB(db)
	return l
}

type loginLogDo struct{ gen.DO }

func (l loginLogDo) Debug() *loginLogDo {
	return l.withDO(l.DO.Debug())
}

func (l loginLogDo) WithContext(ctx context.Context) *loginLogDo {
	return l.withDO(l.DO.WithContext(ctx))
}

func (l loginLogDo) ReadDB() *loginLogDo {
	return l.Clauses(dbresolver.Read)
}

func (l loginLogDo) WriteDB() *loginLogDo {
	return l.Clauses(dbresolver.Write)
}

func (l loginLogDo) Session(config *gorm.Session) *loginLogDo {
	return l.withDO(l.DO.Session(config))
}

func (l loginLogDo) Clauses(conds ...clause.Expression) *loginLogDo {
	return l.withDO(l.DO.Clauses(conds...))
}

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

func (l loginLogDo) Not(conds ...gen.Condition) *loginLogDo {
	return l.withDO(l.DO.Not(conds...))
}

func (l loginLogDo) Or(conds ...gen.Condition) *loginLogDo {
	return l.withDO(l.DO.Or(conds...))
}

func (l loginLogDo) Select(conds ...field.Expr) *loginLogDo {
	return l.withDO(l.DO.Select(conds...))
}

func (l loginLogDo) Where(conds ...gen.Condition) *loginLogDo {
	return l.withDO(l.DO.Where(conds...))
}

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

func (l loginLogDo) Order(conds ...field.Expr) *loginLogDo {
	return l.withDO(l.DO.Order(conds...))
}

func (l loginLogDo) Distinct(cols ...field.Expr) *loginLogDo {
	return l.withDO(l.DO.Distinct(cols...))
}

func (l loginLogDo) Omit(cols ...field.Expr) *loginLogDo {
	return l.withDO(l.DO.Omit(cols...))
}

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

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

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

func (l loginLogDo) Group(cols ...field.Expr) *loginLogDo {
	return l.withDO(l.DO.Group(cols...))
}

func (l loginLogDo) Having(conds ...gen.Condition) *loginLogDo {
	return l.withDO(l.DO.Having(conds...))
}

func (l loginLogDo) Limit(limit int) *loginLogDo {
	return l.withDO(l.DO.Limit(limit))
}

func (l loginLogDo) Offset(offset int) *loginLogDo {
	return l.withDO(l.DO.Offset(offset))
}

func (l loginLogDo) Scopes(funcs ...func(gen.Dao) gen.Dao) *loginLogDo {
	return l.withDO(l.DO.Scopes(funcs...))
}

func (l loginLogDo) Unscoped() *loginLogDo {
	return l.withDO(l.DO.Unscoped())
}

func (l loginLogDo) Create(values ...*model.LoginLog) error {
	if len(values) == 0 {
		return nil
	}
	return l.DO.Create(values)
}

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

func (l loginLogDo) First() (*model.LoginLog, error) {
	if result, err := l.DO.First(); err != nil {
		return nil, err
	} else {
		return result.(*model.LoginLog), nil
	}
}

func (l loginLogDo) Take() (*model.LoginLog, error) {
	if result, err := l.DO.Take(); err != nil {
		return nil, err
	} else {
		return result.(*model.LoginLog), nil
	}
}

func (l loginLogDo) Last() (*model.LoginLog, error) {
	if result, err := l.DO.Last(); err != nil {
		return nil, err
	} else {
		return result.(*model.LoginLog), nil
	}
}

func (l loginLogDo) Find() ([]*model.LoginLog, error) {
	result, err := l.DO.Find()
	return result.([]*model.LoginLog), err
}

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

func (l loginLogDo) Attrs(attrs ...field.AssignExpr) *loginLogDo {
	return l.withDO(l.DO.Attrs(attrs...))
}

func (l loginLogDo) Assign(attrs ...field.AssignExpr) *loginLogDo {
	return l.withDO(l.DO.Assign(attrs...))
}

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

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

func (l loginLogDo) FirstOrInit() (*model.LoginLog, error) {
	if result, err := l.DO.FirstOrInit(); err != nil {
		return nil, err
	} else {
		return result.(*model.LoginLog), nil
	}
}

func (l loginLogDo) FirstOrCreate() (*model.LoginLog, error) {
	if result, err := l.DO.FirstOrCreate(); err != nil {
		return nil, err
	} else {
		return result.(*model.LoginLog), nil
	}
}

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

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

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

func (l loginLogDo) Scan(result interface{}) (err error) {
	return l.DO.Scan(result)
}

func (l loginLogDo) Delete(models ...*model.LoginLog) (result gen.ResultInfo, err error) {
	return l.DO.Delete(models)
}

func (l *loginLogDo) withDO(do gen.Dao) *loginLogDo {
	l.DO = *do.(*gen.DO)
	return l
}
