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

	_siteConfig.siteConfigDo.UseDB(db, opts...)
	_siteConfig.siteConfigDo.UseModel(&model.SiteConfig{})

	tableName := _siteConfig.siteConfigDo.TableName()
	_siteConfig.ALL = field.NewAsterisk(tableName)
	_siteConfig.ID = field.NewUint64(tableName, "id")
	_siteConfig.CreatedAt = field.NewTime(tableName, "created_at")
	_siteConfig.UpdatedAt = field.NewTime(tableName, "updated_at")
	_siteConfig.DeletedAt = field.NewField(tableName, "deleted_at")
	_siteConfig.Host = field.NewString(tableName, "host")
	_siteConfig.Port = field.NewInt(tableName, "port")
	_siteConfig.Scheme = field.NewString(tableName, "scheme")
	_siteConfig.DisplayURL = field.NewString(tableName, "display_url")
	_siteConfig.CustomOrder = field.NewInt(tableName, "custom_order")
	_siteConfig.HealthCheckEnabled = field.NewBool(tableName, "health_check_enabled")
	_siteConfig.CheckInterval = field.NewInt(tableName, "check_interval")
	_siteConfig.Timeout = field.NewInt(tableName, "timeout")
	_siteConfig.UserAgent = field.NewString(tableName, "user_agent")
	_siteConfig.MaxRedirects = field.NewInt(tableName, "max_redirects")
	_siteConfig.FollowRedirects = field.NewBool(tableName, "follow_redirects")
	_siteConfig.CheckFavicon = field.NewBool(tableName, "check_favicon")
	_siteConfig.HealthCheckConfig = field.NewField(tableName, "health_check_config")

	_siteConfig.fillFieldMap()

	return _siteConfig
}

type siteConfig struct {
	siteConfigDo

	ALL                field.Asterisk
	ID                 field.Uint64
	CreatedAt          field.Time
	UpdatedAt          field.Time
	DeletedAt          field.Field
	Host               field.String
	Port               field.Int
	Scheme             field.String
	DisplayURL         field.String
	CustomOrder        field.Int
	HealthCheckEnabled field.Bool
	CheckInterval      field.Int
	Timeout            field.Int
	UserAgent          field.String
	MaxRedirects       field.Int
	FollowRedirects    field.Bool
	CheckFavicon       field.Bool
	HealthCheckConfig  field.Field

	fieldMap map[string]field.Expr
}

func (s siteConfig) Table(newTableName string) *siteConfig {
	s.siteConfigDo.UseTable(newTableName)
	return s.updateTableName(newTableName)
}

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

func (s *siteConfig) updateTableName(table string) *siteConfig {
	s.ALL = field.NewAsterisk(table)
	s.ID = field.NewUint64(table, "id")
	s.CreatedAt = field.NewTime(table, "created_at")
	s.UpdatedAt = field.NewTime(table, "updated_at")
	s.DeletedAt = field.NewField(table, "deleted_at")
	s.Host = field.NewString(table, "host")
	s.Port = field.NewInt(table, "port")
	s.Scheme = field.NewString(table, "scheme")
	s.DisplayURL = field.NewString(table, "display_url")
	s.CustomOrder = field.NewInt(table, "custom_order")
	s.HealthCheckEnabled = field.NewBool(table, "health_check_enabled")
	s.CheckInterval = field.NewInt(table, "check_interval")
	s.Timeout = field.NewInt(table, "timeout")
	s.UserAgent = field.NewString(table, "user_agent")
	s.MaxRedirects = field.NewInt(table, "max_redirects")
	s.FollowRedirects = field.NewBool(table, "follow_redirects")
	s.CheckFavicon = field.NewBool(table, "check_favicon")
	s.HealthCheckConfig = field.NewField(table, "health_check_config")

	s.fillFieldMap()

	return s
}

func (s *siteConfig) 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 *siteConfig) fillFieldMap() {
	s.fieldMap = make(map[string]field.Expr, 17)
	s.fieldMap["id"] = s.ID
	s.fieldMap["created_at"] = s.CreatedAt
	s.fieldMap["updated_at"] = s.UpdatedAt
	s.fieldMap["deleted_at"] = s.DeletedAt
	s.fieldMap["host"] = s.Host
	s.fieldMap["port"] = s.Port
	s.fieldMap["scheme"] = s.Scheme
	s.fieldMap["display_url"] = s.DisplayURL
	s.fieldMap["custom_order"] = s.CustomOrder
	s.fieldMap["health_check_enabled"] = s.HealthCheckEnabled
	s.fieldMap["check_interval"] = s.CheckInterval
	s.fieldMap["timeout"] = s.Timeout
	s.fieldMap["user_agent"] = s.UserAgent
	s.fieldMap["max_redirects"] = s.MaxRedirects
	s.fieldMap["follow_redirects"] = s.FollowRedirects
	s.fieldMap["check_favicon"] = s.CheckFavicon
	s.fieldMap["health_check_config"] = s.HealthCheckConfig
}

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

func (s siteConfig) replaceDB(db *gorm.DB) siteConfig {
	s.siteConfigDo.ReplaceDB(db)
	return s
}

type siteConfigDo struct{ gen.DO }

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

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

	var executeSQL *gorm.DB
	executeSQL = s.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 (s siteConfigDo) DeleteByID(id uint64) (err error) {
	var params []interface{}

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

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

	return
}

func (s siteConfigDo) Debug() *siteConfigDo {
	return s.withDO(s.DO.Debug())
}

func (s siteConfigDo) WithContext(ctx context.Context) *siteConfigDo {
	return s.withDO(s.DO.WithContext(ctx))
}

func (s siteConfigDo) ReadDB() *siteConfigDo {
	return s.Clauses(dbresolver.Read)
}

func (s siteConfigDo) WriteDB() *siteConfigDo {
	return s.Clauses(dbresolver.Write)
}

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

func (s siteConfigDo) Clauses(conds ...clause.Expression) *siteConfigDo {
	return s.withDO(s.DO.Clauses(conds...))
}

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

func (s siteConfigDo) Not(conds ...gen.Condition) *siteConfigDo {
	return s.withDO(s.DO.Not(conds...))
}

func (s siteConfigDo) Or(conds ...gen.Condition) *siteConfigDo {
	return s.withDO(s.DO.Or(conds...))
}

func (s siteConfigDo) Select(conds ...field.Expr) *siteConfigDo {
	return s.withDO(s.DO.Select(conds...))
}

func (s siteConfigDo) Where(conds ...gen.Condition) *siteConfigDo {
	return s.withDO(s.DO.Where(conds...))
}

func (s siteConfigDo) Order(conds ...field.Expr) *siteConfigDo {
	return s.withDO(s.DO.Order(conds...))
}

func (s siteConfigDo) Distinct(cols ...field.Expr) *siteConfigDo {
	return s.withDO(s.DO.Distinct(cols...))
}

func (s siteConfigDo) Omit(cols ...field.Expr) *siteConfigDo {
	return s.withDO(s.DO.Omit(cols...))
}

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

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

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

func (s siteConfigDo) Group(cols ...field.Expr) *siteConfigDo {
	return s.withDO(s.DO.Group(cols...))
}

func (s siteConfigDo) Having(conds ...gen.Condition) *siteConfigDo {
	return s.withDO(s.DO.Having(conds...))
}

func (s siteConfigDo) Limit(limit int) *siteConfigDo {
	return s.withDO(s.DO.Limit(limit))
}

func (s siteConfigDo) Offset(offset int) *siteConfigDo {
	return s.withDO(s.DO.Offset(offset))
}

func (s siteConfigDo) Scopes(funcs ...func(gen.Dao) gen.Dao) *siteConfigDo {
	return s.withDO(s.DO.Scopes(funcs...))
}

func (s siteConfigDo) Unscoped() *siteConfigDo {
	return s.withDO(s.DO.Unscoped())
}

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

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

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

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

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

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

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

func (s siteConfigDo) Attrs(attrs ...field.AssignExpr) *siteConfigDo {
	return s.withDO(s.DO.Attrs(attrs...))
}

func (s siteConfigDo) Assign(attrs ...field.AssignExpr) *siteConfigDo {
	return s.withDO(s.DO.Assign(attrs...))
}

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

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

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

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

func (s siteConfigDo) FindByPage(offset int, limit int) (result []*model.SiteConfig, 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 siteConfigDo) 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 siteConfigDo) Scan(result interface{}) (err error) {
	return s.DO.Scan(result)
}

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

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