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

	_serverDeploy.serverDeployDo.UseDB(db, opts...)
	_serverDeploy.serverDeployDo.UseModel(&model.ServerDeploy{})

	tableName := _serverDeploy.serverDeployDo.TableName()
	_serverDeploy.ALL = field.NewAsterisk(tableName)
	_serverDeploy.ID = field.NewInt64(tableName, "id")
	_serverDeploy.Environment = field.NewString(tableName, "environment")
	_serverDeploy.AdminID = field.NewInt64(tableName, "admin_id")
	_serverDeploy.GroupID = field.NewInt32(tableName, "group_id")
	_serverDeploy.ServerType = field.NewString(tableName, "server_type")
	_serverDeploy.ContainerName = field.NewString(tableName, "container_name")
	_serverDeploy.Name = field.NewString(tableName, "name")
	_serverDeploy.DockerAddr = field.NewString(tableName, "docker_addr")
	_serverDeploy.CaPem = field.NewString(tableName, "ca_pem")
	_serverDeploy.CertPem = field.NewString(tableName, "cert_pem")
	_serverDeploy.KeyPem = field.NewString(tableName, "key_pem")
	_serverDeploy.Remark = field.NewString(tableName, "remark")
	_serverDeploy.RunStatus = field.NewInt32(tableName, "run_status")
	_serverDeploy.LastSyncAt = field.NewTime(tableName, "last_sync_at")
	_serverDeploy.DeployStatus = field.NewInt32(tableName, "deploy_status")
	_serverDeploy.CreatedAt = field.NewTime(tableName, "created_at")
	_serverDeploy.AlarmSwitch = field.NewInt64(tableName, "alarm_switch")
	_serverDeploy.IsMproom = field.NewInt32(tableName, "is_mproom")

	_serverDeploy.fillFieldMap()

	return _serverDeploy
}

type serverDeploy struct {
	serverDeployDo

	ALL           field.Asterisk
	ID            field.Int64
	Environment   field.String // 所在环境
	AdminID       field.Int64  // 管理员ID
	GroupID       field.Int32  // 分组,1微信，2抖音
	ServerType    field.String
	ContainerName field.String // 容器名称
	Name          field.String // 应用名称
	DockerAddr    field.String // dockerAPI地址
	CaPem         field.String // ca.pem路径
	CertPem       field.String // cert.pem路径
	KeyPem        field.String // key.pem路径
	Remark        field.String // 备注
	RunStatus     field.Int32
	LastSyncAt    field.Time
	DeployStatus  field.Int32 // 部署状态
	CreatedAt     field.Time  // 创建时间
	AlarmSwitch   field.Int64 // 报警开关
	IsMproom      field.Int32

	fieldMap map[string]field.Expr
}

func (s serverDeploy) Table(newTableName string) *serverDeploy {
	s.serverDeployDo.UseTable(newTableName)
	return s.updateTableName(newTableName)
}

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

func (s *serverDeploy) updateTableName(table string) *serverDeploy {
	s.ALL = field.NewAsterisk(table)
	s.ID = field.NewInt64(table, "id")
	s.Environment = field.NewString(table, "environment")
	s.AdminID = field.NewInt64(table, "admin_id")
	s.GroupID = field.NewInt32(table, "group_id")
	s.ServerType = field.NewString(table, "server_type")
	s.ContainerName = field.NewString(table, "container_name")
	s.Name = field.NewString(table, "name")
	s.DockerAddr = field.NewString(table, "docker_addr")
	s.CaPem = field.NewString(table, "ca_pem")
	s.CertPem = field.NewString(table, "cert_pem")
	s.KeyPem = field.NewString(table, "key_pem")
	s.Remark = field.NewString(table, "remark")
	s.RunStatus = field.NewInt32(table, "run_status")
	s.LastSyncAt = field.NewTime(table, "last_sync_at")
	s.DeployStatus = field.NewInt32(table, "deploy_status")
	s.CreatedAt = field.NewTime(table, "created_at")
	s.AlarmSwitch = field.NewInt64(table, "alarm_switch")
	s.IsMproom = field.NewInt32(table, "is_mproom")

	s.fillFieldMap()

	return s
}

func (s *serverDeploy) 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 *serverDeploy) fillFieldMap() {
	s.fieldMap = make(map[string]field.Expr, 18)
	s.fieldMap["id"] = s.ID
	s.fieldMap["environment"] = s.Environment
	s.fieldMap["admin_id"] = s.AdminID
	s.fieldMap["group_id"] = s.GroupID
	s.fieldMap["server_type"] = s.ServerType
	s.fieldMap["container_name"] = s.ContainerName
	s.fieldMap["name"] = s.Name
	s.fieldMap["docker_addr"] = s.DockerAddr
	s.fieldMap["ca_pem"] = s.CaPem
	s.fieldMap["cert_pem"] = s.CertPem
	s.fieldMap["key_pem"] = s.KeyPem
	s.fieldMap["remark"] = s.Remark
	s.fieldMap["run_status"] = s.RunStatus
	s.fieldMap["last_sync_at"] = s.LastSyncAt
	s.fieldMap["deploy_status"] = s.DeployStatus
	s.fieldMap["created_at"] = s.CreatedAt
	s.fieldMap["alarm_switch"] = s.AlarmSwitch
	s.fieldMap["is_mproom"] = s.IsMproom
}

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

func (s serverDeploy) replaceDB(db *gorm.DB) serverDeploy {
	s.serverDeployDo.ReplaceDB(db)
	return s
}

type serverDeployDo struct{ gen.DO }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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