// 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"

	"wechat-api/database/dao/wechat/model"
)

func newServer(db *gorm.DB, opts ...gen.DOOption) server {
	_server := server{}

	_server.serverDo.UseDB(db, opts...)
	_server.serverDo.UseModel(&model.Server{})

	tableName := _server.serverDo.TableName()
	_server.ALL = field.NewAsterisk(tableName)
	_server.ID = field.NewInt64(tableName, "id")
	_server.CreatedAt = field.NewTime(tableName, "created_at")
	_server.UpdatedAt = field.NewTime(tableName, "updated_at")
	_server.Status = field.NewInt64(tableName, "status")
	_server.DeletedAt = field.NewField(tableName, "deleted_at")
	_server.Name = field.NewString(tableName, "name")
	_server.PublicIP = field.NewString(tableName, "public_ip")
	_server.PrivateIP = field.NewString(tableName, "private_ip")
	_server.AdminPort = field.NewString(tableName, "admin_port")

	_server.fillFieldMap()

	return _server
}

type server struct {
	serverDo

	ALL       field.Asterisk
	ID        field.Int64
	CreatedAt field.Time   // Create Time | 创建日期
	UpdatedAt field.Time   // Update Time | 修改日期
	Status    field.Int64  // Status 1: normal 2: ban | 状态 1 正常 2 禁用
	DeletedAt field.Field  // Delete Time | 删除日期
	Name      field.String // 名称
	PublicIP  field.String // 公网ip
	PrivateIP field.String // 内网ip
	AdminPort field.String // 管理端口

	fieldMap map[string]field.Expr
}

func (s server) Table(newTableName string) *server {
	s.serverDo.UseTable(newTableName)
	return s.updateTableName(newTableName)
}

func (s server) As(alias string) *server {
	s.serverDo.DO = *(s.serverDo.As(alias).(*gen.DO))
	return s.updateTableName(alias)
}

func (s *server) updateTableName(table string) *server {
	s.ALL = field.NewAsterisk(table)
	s.ID = field.NewInt64(table, "id")
	s.CreatedAt = field.NewTime(table, "created_at")
	s.UpdatedAt = field.NewTime(table, "updated_at")
	s.Status = field.NewInt64(table, "status")
	s.DeletedAt = field.NewField(table, "deleted_at")
	s.Name = field.NewString(table, "name")
	s.PublicIP = field.NewString(table, "public_ip")
	s.PrivateIP = field.NewString(table, "private_ip")
	s.AdminPort = field.NewString(table, "admin_port")

	s.fillFieldMap()

	return s
}

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

func (s *server) fillFieldMap() {
	s.fieldMap = make(map[string]field.Expr, 9)
	s.fieldMap["id"] = s.ID
	s.fieldMap["created_at"] = s.CreatedAt
	s.fieldMap["updated_at"] = s.UpdatedAt
	s.fieldMap["status"] = s.Status
	s.fieldMap["deleted_at"] = s.DeletedAt
	s.fieldMap["name"] = s.Name
	s.fieldMap["public_ip"] = s.PublicIP
	s.fieldMap["private_ip"] = s.PrivateIP
	s.fieldMap["admin_port"] = s.AdminPort
}

func (s server) clone(db *gorm.DB) server {
	s.serverDo.ReplaceConnPool(db.Statement.ConnPool)
	return s
}

func (s server) replaceDB(db *gorm.DB) server {
	s.serverDo.ReplaceDB(db)
	return s
}

type serverDo struct{ gen.DO }

type IServerDo interface {
	gen.SubQuery
	Debug() IServerDo
	WithContext(ctx context.Context) IServerDo
	WithResult(fc func(tx gen.Dao)) gen.ResultInfo
	ReplaceDB(db *gorm.DB)
	ReadDB() IServerDo
	WriteDB() IServerDo
	As(alias string) gen.Dao
	Session(config *gorm.Session) IServerDo
	Columns(cols ...field.Expr) gen.Columns
	Clauses(conds ...clause.Expression) IServerDo
	Not(conds ...gen.Condition) IServerDo
	Or(conds ...gen.Condition) IServerDo
	Select(conds ...field.Expr) IServerDo
	Where(conds ...gen.Condition) IServerDo
	Order(conds ...field.Expr) IServerDo
	Distinct(cols ...field.Expr) IServerDo
	Omit(cols ...field.Expr) IServerDo
	Join(table schema.Tabler, on ...field.Expr) IServerDo
	LeftJoin(table schema.Tabler, on ...field.Expr) IServerDo
	RightJoin(table schema.Tabler, on ...field.Expr) IServerDo
	Group(cols ...field.Expr) IServerDo
	Having(conds ...gen.Condition) IServerDo
	Limit(limit int) IServerDo
	Offset(offset int) IServerDo
	Count() (count int64, err error)
	Scopes(funcs ...func(gen.Dao) gen.Dao) IServerDo
	Unscoped() IServerDo
	Create(values ...*model.Server) error
	CreateInBatches(values []*model.Server, batchSize int) error
	Save(values ...*model.Server) error
	First() (*model.Server, error)
	Take() (*model.Server, error)
	Last() (*model.Server, error)
	Find() ([]*model.Server, error)
	FindInBatch(batchSize int, fc func(tx gen.Dao, batch int) error) (results []*model.Server, err error)
	FindInBatches(result *[]*model.Server, batchSize int, fc func(tx gen.Dao, batch int) error) error
	Pluck(column field.Expr, dest interface{}) error
	Delete(...*model.Server) (info gen.ResultInfo, err error)
	Update(column field.Expr, value interface{}) (info gen.ResultInfo, err error)
	UpdateSimple(columns ...field.AssignExpr) (info gen.ResultInfo, err error)
	Updates(value interface{}) (info gen.ResultInfo, err error)
	UpdateColumn(column field.Expr, value interface{}) (info gen.ResultInfo, err error)
	UpdateColumnSimple(columns ...field.AssignExpr) (info gen.ResultInfo, err error)
	UpdateColumns(value interface{}) (info gen.ResultInfo, err error)
	UpdateFrom(q gen.SubQuery) gen.Dao
	Attrs(attrs ...field.AssignExpr) IServerDo
	Assign(attrs ...field.AssignExpr) IServerDo
	Joins(fields ...field.RelationField) IServerDo
	Preload(fields ...field.RelationField) IServerDo
	FirstOrInit() (*model.Server, error)
	FirstOrCreate() (*model.Server, error)
	FindByPage(offset int, limit int) (result []*model.Server, count int64, err error)
	ScanByPage(result interface{}, offset int, limit int) (count int64, err error)
	Scan(result interface{}) (err error)
	Returning(value interface{}, columns ...string) IServerDo
	UnderlyingDB() *gorm.DB
	schema.Tabler
}

func (s serverDo) Debug() IServerDo {
	return s.withDO(s.DO.Debug())
}

func (s serverDo) WithContext(ctx context.Context) IServerDo {
	return s.withDO(s.DO.WithContext(ctx))
}

func (s serverDo) ReadDB() IServerDo {
	return s.Clauses(dbresolver.Read)
}

func (s serverDo) WriteDB() IServerDo {
	return s.Clauses(dbresolver.Write)
}

func (s serverDo) Session(config *gorm.Session) IServerDo {
	return s.withDO(s.DO.Session(config))
}

func (s serverDo) Clauses(conds ...clause.Expression) IServerDo {
	return s.withDO(s.DO.Clauses(conds...))
}

func (s serverDo) Returning(value interface{}, columns ...string) IServerDo {
	return s.withDO(s.DO.Returning(value, columns...))
}

func (s serverDo) Not(conds ...gen.Condition) IServerDo {
	return s.withDO(s.DO.Not(conds...))
}

func (s serverDo) Or(conds ...gen.Condition) IServerDo {
	return s.withDO(s.DO.Or(conds...))
}

func (s serverDo) Select(conds ...field.Expr) IServerDo {
	return s.withDO(s.DO.Select(conds...))
}

func (s serverDo) Where(conds ...gen.Condition) IServerDo {
	return s.withDO(s.DO.Where(conds...))
}

func (s serverDo) Order(conds ...field.Expr) IServerDo {
	return s.withDO(s.DO.Order(conds...))
}

func (s serverDo) Distinct(cols ...field.Expr) IServerDo {
	return s.withDO(s.DO.Distinct(cols...))
}

func (s serverDo) Omit(cols ...field.Expr) IServerDo {
	return s.withDO(s.DO.Omit(cols...))
}

func (s serverDo) Join(table schema.Tabler, on ...field.Expr) IServerDo {
	return s.withDO(s.DO.Join(table, on...))
}

func (s serverDo) LeftJoin(table schema.Tabler, on ...field.Expr) IServerDo {
	return s.withDO(s.DO.LeftJoin(table, on...))
}

func (s serverDo) RightJoin(table schema.Tabler, on ...field.Expr) IServerDo {
	return s.withDO(s.DO.RightJoin(table, on...))
}

func (s serverDo) Group(cols ...field.Expr) IServerDo {
	return s.withDO(s.DO.Group(cols...))
}

func (s serverDo) Having(conds ...gen.Condition) IServerDo {
	return s.withDO(s.DO.Having(conds...))
}

func (s serverDo) Limit(limit int) IServerDo {
	return s.withDO(s.DO.Limit(limit))
}

func (s serverDo) Offset(offset int) IServerDo {
	return s.withDO(s.DO.Offset(offset))
}

func (s serverDo) Scopes(funcs ...func(gen.Dao) gen.Dao) IServerDo {
	return s.withDO(s.DO.Scopes(funcs...))
}

func (s serverDo) Unscoped() IServerDo {
	return s.withDO(s.DO.Unscoped())
}

func (s serverDo) Create(values ...*model.Server) error {
	if len(values) == 0 {
		return nil
	}
	return s.DO.Create(values)
}

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

func (s serverDo) First() (*model.Server, error) {
	if result, err := s.DO.First(); err != nil {
		return nil, err
	} else {
		return result.(*model.Server), nil
	}
}

func (s serverDo) Take() (*model.Server, error) {
	if result, err := s.DO.Take(); err != nil {
		return nil, err
	} else {
		return result.(*model.Server), nil
	}
}

func (s serverDo) Last() (*model.Server, error) {
	if result, err := s.DO.Last(); err != nil {
		return nil, err
	} else {
		return result.(*model.Server), nil
	}
}

func (s serverDo) Find() ([]*model.Server, error) {
	result, err := s.DO.Find()
	return result.([]*model.Server), err
}

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

func (s serverDo) Attrs(attrs ...field.AssignExpr) IServerDo {
	return s.withDO(s.DO.Attrs(attrs...))
}

func (s serverDo) Assign(attrs ...field.AssignExpr) IServerDo {
	return s.withDO(s.DO.Assign(attrs...))
}

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

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

func (s serverDo) FirstOrInit() (*model.Server, error) {
	if result, err := s.DO.FirstOrInit(); err != nil {
		return nil, err
	} else {
		return result.(*model.Server), nil
	}
}

func (s serverDo) FirstOrCreate() (*model.Server, error) {
	if result, err := s.DO.FirstOrCreate(); err != nil {
		return nil, err
	} else {
		return result.(*model.Server), nil
	}
}

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

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

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

func (s serverDo) Scan(result interface{}) (err error) {
	return s.DO.Scan(result)
}

func (s serverDo) Delete(models ...*model.Server) (result gen.ResultInfo, err error) {
	return s.DO.Delete(models)
}

func (s *serverDo) withDO(do gen.Dao) *serverDo {
	s.DO = *do.(*gen.DO)
	return s
}
