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

	_envGroup.envGroupDo.UseDB(db, opts...)
	_envGroup.envGroupDo.UseModel(&model.EnvGroup{})

	tableName := _envGroup.envGroupDo.TableName()
	_envGroup.ALL = field.NewAsterisk(tableName)
	_envGroup.ID = field.NewUint64(tableName, "id")
	_envGroup.CreatedAt = field.NewTime(tableName, "created_at")
	_envGroup.UpdatedAt = field.NewTime(tableName, "updated_at")
	_envGroup.DeletedAt = field.NewField(tableName, "deleted_at")
	_envGroup.Name = field.NewString(tableName, "name")
	_envGroup.SyncNodeIds = field.NewField(tableName, "sync_node_ids")
	_envGroup.OrderID = field.NewInt(tableName, "order_id")
	_envGroup.PostSyncAction = field.NewString(tableName, "post_sync_action")

	_envGroup.fillFieldMap()

	return _envGroup
}

type envGroup struct {
	envGroupDo

	ALL            field.Asterisk
	ID             field.Uint64
	CreatedAt      field.Time
	UpdatedAt      field.Time
	DeletedAt      field.Field
	Name           field.String
	SyncNodeIds    field.Field
	OrderID        field.Int
	PostSyncAction field.String

	fieldMap map[string]field.Expr
}

func (e envGroup) Table(newTableName string) *envGroup {
	e.envGroupDo.UseTable(newTableName)
	return e.updateTableName(newTableName)
}

func (e envGroup) As(alias string) *envGroup {
	e.envGroupDo.DO = *(e.envGroupDo.As(alias).(*gen.DO))
	return e.updateTableName(alias)
}

func (e *envGroup) updateTableName(table string) *envGroup {
	e.ALL = field.NewAsterisk(table)
	e.ID = field.NewUint64(table, "id")
	e.CreatedAt = field.NewTime(table, "created_at")
	e.UpdatedAt = field.NewTime(table, "updated_at")
	e.DeletedAt = field.NewField(table, "deleted_at")
	e.Name = field.NewString(table, "name")
	e.SyncNodeIds = field.NewField(table, "sync_node_ids")
	e.OrderID = field.NewInt(table, "order_id")
	e.PostSyncAction = field.NewString(table, "post_sync_action")

	e.fillFieldMap()

	return e
}

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

func (e *envGroup) fillFieldMap() {
	e.fieldMap = make(map[string]field.Expr, 8)
	e.fieldMap["id"] = e.ID
	e.fieldMap["created_at"] = e.CreatedAt
	e.fieldMap["updated_at"] = e.UpdatedAt
	e.fieldMap["deleted_at"] = e.DeletedAt
	e.fieldMap["name"] = e.Name
	e.fieldMap["sync_node_ids"] = e.SyncNodeIds
	e.fieldMap["order_id"] = e.OrderID
	e.fieldMap["post_sync_action"] = e.PostSyncAction
}

func (e envGroup) clone(db *gorm.DB) envGroup {
	e.envGroupDo.ReplaceConnPool(db.Statement.ConnPool)
	return e
}

func (e envGroup) replaceDB(db *gorm.DB) envGroup {
	e.envGroupDo.ReplaceDB(db)
	return e
}

type envGroupDo struct{ gen.DO }

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

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

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

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

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

	return
}

func (e envGroupDo) Debug() *envGroupDo {
	return e.withDO(e.DO.Debug())
}

func (e envGroupDo) WithContext(ctx context.Context) *envGroupDo {
	return e.withDO(e.DO.WithContext(ctx))
}

func (e envGroupDo) ReadDB() *envGroupDo {
	return e.Clauses(dbresolver.Read)
}

func (e envGroupDo) WriteDB() *envGroupDo {
	return e.Clauses(dbresolver.Write)
}

func (e envGroupDo) Session(config *gorm.Session) *envGroupDo {
	return e.withDO(e.DO.Session(config))
}

func (e envGroupDo) Clauses(conds ...clause.Expression) *envGroupDo {
	return e.withDO(e.DO.Clauses(conds...))
}

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

func (e envGroupDo) Not(conds ...gen.Condition) *envGroupDo {
	return e.withDO(e.DO.Not(conds...))
}

func (e envGroupDo) Or(conds ...gen.Condition) *envGroupDo {
	return e.withDO(e.DO.Or(conds...))
}

func (e envGroupDo) Select(conds ...field.Expr) *envGroupDo {
	return e.withDO(e.DO.Select(conds...))
}

func (e envGroupDo) Where(conds ...gen.Condition) *envGroupDo {
	return e.withDO(e.DO.Where(conds...))
}

func (e envGroupDo) Order(conds ...field.Expr) *envGroupDo {
	return e.withDO(e.DO.Order(conds...))
}

func (e envGroupDo) Distinct(cols ...field.Expr) *envGroupDo {
	return e.withDO(e.DO.Distinct(cols...))
}

func (e envGroupDo) Omit(cols ...field.Expr) *envGroupDo {
	return e.withDO(e.DO.Omit(cols...))
}

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

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

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

func (e envGroupDo) Group(cols ...field.Expr) *envGroupDo {
	return e.withDO(e.DO.Group(cols...))
}

func (e envGroupDo) Having(conds ...gen.Condition) *envGroupDo {
	return e.withDO(e.DO.Having(conds...))
}

func (e envGroupDo) Limit(limit int) *envGroupDo {
	return e.withDO(e.DO.Limit(limit))
}

func (e envGroupDo) Offset(offset int) *envGroupDo {
	return e.withDO(e.DO.Offset(offset))
}

func (e envGroupDo) Scopes(funcs ...func(gen.Dao) gen.Dao) *envGroupDo {
	return e.withDO(e.DO.Scopes(funcs...))
}

func (e envGroupDo) Unscoped() *envGroupDo {
	return e.withDO(e.DO.Unscoped())
}

func (e envGroupDo) Create(values ...*model.EnvGroup) error {
	if len(values) == 0 {
		return nil
	}
	return e.DO.Create(values)
}

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

func (e envGroupDo) First() (*model.EnvGroup, error) {
	if result, err := e.DO.First(); err != nil {
		return nil, err
	} else {
		return result.(*model.EnvGroup), nil
	}
}

func (e envGroupDo) Take() (*model.EnvGroup, error) {
	if result, err := e.DO.Take(); err != nil {
		return nil, err
	} else {
		return result.(*model.EnvGroup), nil
	}
}

func (e envGroupDo) Last() (*model.EnvGroup, error) {
	if result, err := e.DO.Last(); err != nil {
		return nil, err
	} else {
		return result.(*model.EnvGroup), nil
	}
}

func (e envGroupDo) Find() ([]*model.EnvGroup, error) {
	result, err := e.DO.Find()
	return result.([]*model.EnvGroup), err
}

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

func (e envGroupDo) Attrs(attrs ...field.AssignExpr) *envGroupDo {
	return e.withDO(e.DO.Attrs(attrs...))
}

func (e envGroupDo) Assign(attrs ...field.AssignExpr) *envGroupDo {
	return e.withDO(e.DO.Assign(attrs...))
}

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

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

func (e envGroupDo) FirstOrInit() (*model.EnvGroup, error) {
	if result, err := e.DO.FirstOrInit(); err != nil {
		return nil, err
	} else {
		return result.(*model.EnvGroup), nil
	}
}

func (e envGroupDo) FirstOrCreate() (*model.EnvGroup, error) {
	if result, err := e.DO.FirstOrCreate(); err != nil {
		return nil, err
	} else {
		return result.(*model.EnvGroup), nil
	}
}

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

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

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

func (e envGroupDo) Scan(result interface{}) (err error) {
	return e.DO.Scan(result)
}

func (e envGroupDo) Delete(models ...*model.EnvGroup) (result gen.ResultInfo, err error) {
	return e.DO.Delete(models)
}

func (e *envGroupDo) withDO(do gen.Dao) *envGroupDo {
	e.DO = *do.(*gen.DO)
	return e
}
