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

	_onlineDurationLog.onlineDurationLogDo.UseDB(db, opts...)
	_onlineDurationLog.onlineDurationLogDo.UseModel(&model.OnlineDurationLog{})

	tableName := _onlineDurationLog.onlineDurationLogDo.TableName()
	_onlineDurationLog.ALL = field.NewAsterisk(tableName)
	_onlineDurationLog.ID = field.NewInt64(tableName, "id")
	_onlineDurationLog.ChannelID = field.NewString(tableName, "channel_id")
	_onlineDurationLog.Flag = field.NewInt32(tableName, "flag")
	_onlineDurationLog.UserID = field.NewInt64(tableName, "user_id")
	_onlineDurationLog.ServerID = field.NewInt32(tableName, "server_id")
	_onlineDurationLog.LoginAt = field.NewInt32(tableName, "login_at")
	_onlineDurationLog.LogoutAt = field.NewInt32(tableName, "logout_at")
	_onlineDurationLog.Duration = field.NewInt32(tableName, "duration")
	_onlineDurationLog.UserCreatedAt = field.NewInt32(tableName, "user_created_at")

	_onlineDurationLog.fillFieldMap()

	return _onlineDurationLog
}

type onlineDurationLog struct {
	onlineDurationLogDo

	ALL           field.Asterisk
	ID            field.Int64
	ChannelID     field.String // 渠道ID
	Flag          field.Int32
	UserID        field.Int64 // 用户id
	ServerID      field.Int32 // 服务器ID
	LoginAt       field.Int32
	LogoutAt      field.Int32
	Duration      field.Int32 // 在线时长
	UserCreatedAt field.Int32 // 用户创建时间

	fieldMap map[string]field.Expr
}

func (o onlineDurationLog) Table(newTableName string) *onlineDurationLog {
	o.onlineDurationLogDo.UseTable(newTableName)
	return o.updateTableName(newTableName)
}

func (o onlineDurationLog) As(alias string) *onlineDurationLog {
	o.onlineDurationLogDo.DO = *(o.onlineDurationLogDo.As(alias).(*gen.DO))
	return o.updateTableName(alias)
}

func (o *onlineDurationLog) updateTableName(table string) *onlineDurationLog {
	o.ALL = field.NewAsterisk(table)
	o.ID = field.NewInt64(table, "id")
	o.ChannelID = field.NewString(table, "channel_id")
	o.Flag = field.NewInt32(table, "flag")
	o.UserID = field.NewInt64(table, "user_id")
	o.ServerID = field.NewInt32(table, "server_id")
	o.LoginAt = field.NewInt32(table, "login_at")
	o.LogoutAt = field.NewInt32(table, "logout_at")
	o.Duration = field.NewInt32(table, "duration")
	o.UserCreatedAt = field.NewInt32(table, "user_created_at")

	o.fillFieldMap()

	return o
}

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

func (o *onlineDurationLog) fillFieldMap() {
	o.fieldMap = make(map[string]field.Expr, 9)
	o.fieldMap["id"] = o.ID
	o.fieldMap["channel_id"] = o.ChannelID
	o.fieldMap["flag"] = o.Flag
	o.fieldMap["user_id"] = o.UserID
	o.fieldMap["server_id"] = o.ServerID
	o.fieldMap["login_at"] = o.LoginAt
	o.fieldMap["logout_at"] = o.LogoutAt
	o.fieldMap["duration"] = o.Duration
	o.fieldMap["user_created_at"] = o.UserCreatedAt
}

func (o onlineDurationLog) clone(db *gorm.DB) onlineDurationLog {
	o.onlineDurationLogDo.ReplaceConnPool(db.Statement.ConnPool)
	return o
}

func (o onlineDurationLog) replaceDB(db *gorm.DB) onlineDurationLog {
	o.onlineDurationLogDo.ReplaceDB(db)
	return o
}

type onlineDurationLogDo struct{ gen.DO }

func (o onlineDurationLogDo) Debug() *onlineDurationLogDo {
	return o.withDO(o.DO.Debug())
}

func (o onlineDurationLogDo) WithContext(ctx context.Context) *onlineDurationLogDo {
	return o.withDO(o.DO.WithContext(ctx))
}

func (o onlineDurationLogDo) ReadDB() *onlineDurationLogDo {
	return o.Clauses(dbresolver.Read)
}

func (o onlineDurationLogDo) WriteDB() *onlineDurationLogDo {
	return o.Clauses(dbresolver.Write)
}

func (o onlineDurationLogDo) Session(config *gorm.Session) *onlineDurationLogDo {
	return o.withDO(o.DO.Session(config))
}

func (o onlineDurationLogDo) Clauses(conds ...clause.Expression) *onlineDurationLogDo {
	return o.withDO(o.DO.Clauses(conds...))
}

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

func (o onlineDurationLogDo) Not(conds ...gen.Condition) *onlineDurationLogDo {
	return o.withDO(o.DO.Not(conds...))
}

func (o onlineDurationLogDo) Or(conds ...gen.Condition) *onlineDurationLogDo {
	return o.withDO(o.DO.Or(conds...))
}

func (o onlineDurationLogDo) Select(conds ...field.Expr) *onlineDurationLogDo {
	return o.withDO(o.DO.Select(conds...))
}

func (o onlineDurationLogDo) Where(conds ...gen.Condition) *onlineDurationLogDo {
	return o.withDO(o.DO.Where(conds...))
}

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

func (o onlineDurationLogDo) Order(conds ...field.Expr) *onlineDurationLogDo {
	return o.withDO(o.DO.Order(conds...))
}

func (o onlineDurationLogDo) Distinct(cols ...field.Expr) *onlineDurationLogDo {
	return o.withDO(o.DO.Distinct(cols...))
}

func (o onlineDurationLogDo) Omit(cols ...field.Expr) *onlineDurationLogDo {
	return o.withDO(o.DO.Omit(cols...))
}

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

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

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

func (o onlineDurationLogDo) Group(cols ...field.Expr) *onlineDurationLogDo {
	return o.withDO(o.DO.Group(cols...))
}

func (o onlineDurationLogDo) Having(conds ...gen.Condition) *onlineDurationLogDo {
	return o.withDO(o.DO.Having(conds...))
}

func (o onlineDurationLogDo) Limit(limit int) *onlineDurationLogDo {
	return o.withDO(o.DO.Limit(limit))
}

func (o onlineDurationLogDo) Offset(offset int) *onlineDurationLogDo {
	return o.withDO(o.DO.Offset(offset))
}

func (o onlineDurationLogDo) Scopes(funcs ...func(gen.Dao) gen.Dao) *onlineDurationLogDo {
	return o.withDO(o.DO.Scopes(funcs...))
}

func (o onlineDurationLogDo) Unscoped() *onlineDurationLogDo {
	return o.withDO(o.DO.Unscoped())
}

func (o onlineDurationLogDo) Create(values ...*model.OnlineDurationLog) error {
	if len(values) == 0 {
		return nil
	}
	return o.DO.Create(values)
}

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

func (o onlineDurationLogDo) First() (*model.OnlineDurationLog, error) {
	if result, err := o.DO.First(); err != nil {
		return nil, err
	} else {
		return result.(*model.OnlineDurationLog), nil
	}
}

func (o onlineDurationLogDo) Take() (*model.OnlineDurationLog, error) {
	if result, err := o.DO.Take(); err != nil {
		return nil, err
	} else {
		return result.(*model.OnlineDurationLog), nil
	}
}

func (o onlineDurationLogDo) Last() (*model.OnlineDurationLog, error) {
	if result, err := o.DO.Last(); err != nil {
		return nil, err
	} else {
		return result.(*model.OnlineDurationLog), nil
	}
}

func (o onlineDurationLogDo) Find() ([]*model.OnlineDurationLog, error) {
	result, err := o.DO.Find()
	return result.([]*model.OnlineDurationLog), err
}

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

func (o onlineDurationLogDo) Attrs(attrs ...field.AssignExpr) *onlineDurationLogDo {
	return o.withDO(o.DO.Attrs(attrs...))
}

func (o onlineDurationLogDo) Assign(attrs ...field.AssignExpr) *onlineDurationLogDo {
	return o.withDO(o.DO.Assign(attrs...))
}

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

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

func (o onlineDurationLogDo) FirstOrInit() (*model.OnlineDurationLog, error) {
	if result, err := o.DO.FirstOrInit(); err != nil {
		return nil, err
	} else {
		return result.(*model.OnlineDurationLog), nil
	}
}

func (o onlineDurationLogDo) FirstOrCreate() (*model.OnlineDurationLog, error) {
	if result, err := o.DO.FirstOrCreate(); err != nil {
		return nil, err
	} else {
		return result.(*model.OnlineDurationLog), nil
	}
}

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

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

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

func (o onlineDurationLogDo) Scan(result interface{}) (err error) {
	return o.DO.Scan(result)
}

func (o onlineDurationLogDo) Delete(models ...*model.OnlineDurationLog) (result gen.ResultInfo, err error) {
	return o.DO.Delete(models)
}

func (o *onlineDurationLogDo) withDO(do gen.Dao) *onlineDurationLogDo {
	o.DO = *do.(*gen.DO)
	return o
}
