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

	"gorm.io/gorm"
	"gorm.io/gorm/clause"
	"gorm.io/gorm/schema"

	"gorm.io/gen"
	"gorm.io/gen/field"

	"gorm.io/plugin/dbresolver"

	"github.com/0xJacky/Nginx-UI/model"
)

func newUpstreamConfig(db *gorm.DB, opts ...gen.DOOption) upstreamConfig {
	_upstreamConfig := upstreamConfig{}

	_upstreamConfig.upstreamConfigDo.UseDB(db, opts...)
	_upstreamConfig.upstreamConfigDo.UseModel(&model.UpstreamConfig{})

	tableName := _upstreamConfig.upstreamConfigDo.TableName()
	_upstreamConfig.ALL = field.NewAsterisk(tableName)
	_upstreamConfig.ID = field.NewUint64(tableName, "id")
	_upstreamConfig.CreatedAt = field.NewTime(tableName, "created_at")
	_upstreamConfig.UpdatedAt = field.NewTime(tableName, "updated_at")
	_upstreamConfig.DeletedAt = field.NewField(tableName, "deleted_at")
	_upstreamConfig.Socket = field.NewString(tableName, "socket")
	_upstreamConfig.Enabled = field.NewBool(tableName, "enabled")

	_upstreamConfig.fillFieldMap()

	return _upstreamConfig
}

type upstreamConfig struct {
	upstreamConfigDo

	ALL       field.Asterisk
	ID        field.Uint64
	CreatedAt field.Time
	UpdatedAt field.Time
	DeletedAt field.Field
	Socket    field.String
	Enabled   field.Bool

	fieldMap map[string]field.Expr
}

func (u upstreamConfig) Table(newTableName string) *upstreamConfig {
	u.upstreamConfigDo.UseTable(newTableName)
	return u.updateTableName(newTableName)
}

func (u upstreamConfig) As(alias string) *upstreamConfig {
	u.upstreamConfigDo.DO = *(u.upstreamConfigDo.As(alias).(*gen.DO))
	return u.updateTableName(alias)
}

func (u *upstreamConfig) updateTableName(table string) *upstreamConfig {
	u.ALL = field.NewAsterisk(table)
	u.ID = field.NewUint64(table, "id")
	u.CreatedAt = field.NewTime(table, "created_at")
	u.UpdatedAt = field.NewTime(table, "updated_at")
	u.DeletedAt = field.NewField(table, "deleted_at")
	u.Socket = field.NewString(table, "socket")
	u.Enabled = field.NewBool(table, "enabled")

	u.fillFieldMap()

	return u
}

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

func (u *upstreamConfig) fillFieldMap() {
	u.fieldMap = make(map[string]field.Expr, 6)
	u.fieldMap["id"] = u.ID
	u.fieldMap["created_at"] = u.CreatedAt
	u.fieldMap["updated_at"] = u.UpdatedAt
	u.fieldMap["deleted_at"] = u.DeletedAt
	u.fieldMap["socket"] = u.Socket
	u.fieldMap["enabled"] = u.Enabled
}

func (u upstreamConfig) clone(db *gorm.DB) upstreamConfig {
	u.upstreamConfigDo.ReplaceConnPool(db.Statement.ConnPool)
	return u
}

func (u upstreamConfig) replaceDB(db *gorm.DB) upstreamConfig {
	u.upstreamConfigDo.ReplaceDB(db)
	return u
}

type upstreamConfigDo struct{ gen.DO }

// FirstByID Where("id=@id")
func (u upstreamConfigDo) FirstByID(id uint64) (result *model.UpstreamConfig, err error) {
	var params []interface{}

	var generateSQL strings.Builder
	params = append(params, id)
	generateSQL.WriteString("id=? ")

	var executeSQL *gorm.DB
	executeSQL = u.UnderlyingDB().Where(generateSQL.String(), params...).Take(&result) // ignore_security_alert
	err = executeSQL.Error

	return
}

// DeleteByID update @@table set deleted_at=strftime('%Y-%m-%d %H:%M:%S','now') where id=@id
func (u upstreamConfigDo) DeleteByID(id uint64) (err error) {
	var params []interface{}

	var generateSQL strings.Builder
	params = append(params, id)
	generateSQL.WriteString("update upstream_configs set deleted_at=strftime('%Y-%m-%d %H:%M:%S','now') where id=? ")

	var executeSQL *gorm.DB
	executeSQL = u.UnderlyingDB().Exec(generateSQL.String(), params...) // ignore_security_alert
	err = executeSQL.Error

	return
}

func (u upstreamConfigDo) Debug() *upstreamConfigDo {
	return u.withDO(u.DO.Debug())
}

func (u upstreamConfigDo) WithContext(ctx context.Context) *upstreamConfigDo {
	return u.withDO(u.DO.WithContext(ctx))
}

func (u upstreamConfigDo) ReadDB() *upstreamConfigDo {
	return u.Clauses(dbresolver.Read)
}

func (u upstreamConfigDo) WriteDB() *upstreamConfigDo {
	return u.Clauses(dbresolver.Write)
}

func (u upstreamConfigDo) Session(config *gorm.Session) *upstreamConfigDo {
	return u.withDO(u.DO.Session(config))
}

func (u upstreamConfigDo) Clauses(conds ...clause.Expression) *upstreamConfigDo {
	return u.withDO(u.DO.Clauses(conds...))
}

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

func (u upstreamConfigDo) Not(conds ...gen.Condition) *upstreamConfigDo {
	return u.withDO(u.DO.Not(conds...))
}

func (u upstreamConfigDo) Or(conds ...gen.Condition) *upstreamConfigDo {
	return u.withDO(u.DO.Or(conds...))
}

func (u upstreamConfigDo) Select(conds ...field.Expr) *upstreamConfigDo {
	return u.withDO(u.DO.Select(conds...))
}

func (u upstreamConfigDo) Where(conds ...gen.Condition) *upstreamConfigDo {
	return u.withDO(u.DO.Where(conds...))
}

func (u upstreamConfigDo) Order(conds ...field.Expr) *upstreamConfigDo {
	return u.withDO(u.DO.Order(conds...))
}

func (u upstreamConfigDo) Distinct(cols ...field.Expr) *upstreamConfigDo {
	return u.withDO(u.DO.Distinct(cols...))
}

func (u upstreamConfigDo) Omit(cols ...field.Expr) *upstreamConfigDo {
	return u.withDO(u.DO.Omit(cols...))
}

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

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

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

func (u upstreamConfigDo) Group(cols ...field.Expr) *upstreamConfigDo {
	return u.withDO(u.DO.Group(cols...))
}

func (u upstreamConfigDo) Having(conds ...gen.Condition) *upstreamConfigDo {
	return u.withDO(u.DO.Having(conds...))
}

func (u upstreamConfigDo) Limit(limit int) *upstreamConfigDo {
	return u.withDO(u.DO.Limit(limit))
}

func (u upstreamConfigDo) Offset(offset int) *upstreamConfigDo {
	return u.withDO(u.DO.Offset(offset))
}

func (u upstreamConfigDo) Scopes(funcs ...func(gen.Dao) gen.Dao) *upstreamConfigDo {
	return u.withDO(u.DO.Scopes(funcs...))
}

func (u upstreamConfigDo) Unscoped() *upstreamConfigDo {
	return u.withDO(u.DO.Unscoped())
}

func (u upstreamConfigDo) Create(values ...*model.UpstreamConfig) error {
	if len(values) == 0 {
		return nil
	}
	return u.DO.Create(values)
}

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

func (u upstreamConfigDo) First() (*model.UpstreamConfig, error) {
	if result, err := u.DO.First(); err != nil {
		return nil, err
	} else {
		return result.(*model.UpstreamConfig), nil
	}
}

func (u upstreamConfigDo) Take() (*model.UpstreamConfig, error) {
	if result, err := u.DO.Take(); err != nil {
		return nil, err
	} else {
		return result.(*model.UpstreamConfig), nil
	}
}

func (u upstreamConfigDo) Last() (*model.UpstreamConfig, error) {
	if result, err := u.DO.Last(); err != nil {
		return nil, err
	} else {
		return result.(*model.UpstreamConfig), nil
	}
}

func (u upstreamConfigDo) Find() ([]*model.UpstreamConfig, error) {
	result, err := u.DO.Find()
	return result.([]*model.UpstreamConfig), err
}

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

func (u upstreamConfigDo) Attrs(attrs ...field.AssignExpr) *upstreamConfigDo {
	return u.withDO(u.DO.Attrs(attrs...))
}

func (u upstreamConfigDo) Assign(attrs ...field.AssignExpr) *upstreamConfigDo {
	return u.withDO(u.DO.Assign(attrs...))
}

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

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

func (u upstreamConfigDo) FirstOrInit() (*model.UpstreamConfig, error) {
	if result, err := u.DO.FirstOrInit(); err != nil {
		return nil, err
	} else {
		return result.(*model.UpstreamConfig), nil
	}
}

func (u upstreamConfigDo) FirstOrCreate() (*model.UpstreamConfig, error) {
	if result, err := u.DO.FirstOrCreate(); err != nil {
		return nil, err
	} else {
		return result.(*model.UpstreamConfig), nil
	}
}

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

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

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

func (u upstreamConfigDo) Scan(result interface{}) (err error) {
	return u.DO.Scan(result)
}

func (u upstreamConfigDo) Delete(models ...*model.UpstreamConfig) (result gen.ResultInfo, err error) {
	return u.DO.Delete(models)
}

func (u *upstreamConfigDo) withDO(do gen.Dao) *upstreamConfigDo {
	u.DO = *do.(*gen.DO)
	return u
}
