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

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

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

	"gorm.io/plugin/dbresolver"

	"kra/internal/data/model"
)

func newSysAutoCodePackage(db *gorm.DB, opts ...gen.DOOption) sysAutoCodePackage {
	_sysAutoCodePackage := sysAutoCodePackage{}

	_sysAutoCodePackage.sysAutoCodePackageDo.UseDB(db, opts...)
	_sysAutoCodePackage.sysAutoCodePackageDo.UseModel(&model.SysAutoCodePackage{})

	tableName := _sysAutoCodePackage.sysAutoCodePackageDo.TableName()
	_sysAutoCodePackage.ALL = field.NewAsterisk(tableName)
	_sysAutoCodePackage.ID = field.NewInt64(tableName, "id")
	_sysAutoCodePackage.CreatedAt = field.NewTime(tableName, "created_at")
	_sysAutoCodePackage.UpdatedAt = field.NewTime(tableName, "updated_at")
	_sysAutoCodePackage.DeletedAt = field.NewField(tableName, "deleted_at")
	_sysAutoCodePackage.Desc = field.NewString(tableName, "desc")
	_sysAutoCodePackage.Label = field.NewString(tableName, "label")
	_sysAutoCodePackage.Template = field.NewString(tableName, "template")
	_sysAutoCodePackage.PackageName = field.NewString(tableName, "package_name")
	_sysAutoCodePackage.Module = field.NewString(tableName, "module")

	_sysAutoCodePackage.fillFieldMap()

	return _sysAutoCodePackage
}

type sysAutoCodePackage struct {
	sysAutoCodePackageDo sysAutoCodePackageDo

	ALL         field.Asterisk
	ID          field.Int64
	CreatedAt   field.Time
	UpdatedAt   field.Time
	DeletedAt   field.Field
	Desc        field.String // 描述
	Label       field.String // 展示名
	Template    field.String // 模版
	PackageName field.String // 包名
	Module      field.String

	fieldMap map[string]field.Expr
}

func (s sysAutoCodePackage) Table(newTableName string) *sysAutoCodePackage {
	s.sysAutoCodePackageDo.UseTable(newTableName)
	return s.updateTableName(newTableName)
}

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

func (s *sysAutoCodePackage) updateTableName(table string) *sysAutoCodePackage {
	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.DeletedAt = field.NewField(table, "deleted_at")
	s.Desc = field.NewString(table, "desc")
	s.Label = field.NewString(table, "label")
	s.Template = field.NewString(table, "template")
	s.PackageName = field.NewString(table, "package_name")
	s.Module = field.NewString(table, "module")

	s.fillFieldMap()

	return s
}

func (s *sysAutoCodePackage) WithContext(ctx context.Context) ISysAutoCodePackageDo {
	return s.sysAutoCodePackageDo.WithContext(ctx)
}

func (s sysAutoCodePackage) TableName() string { return s.sysAutoCodePackageDo.TableName() }

func (s sysAutoCodePackage) Alias() string { return s.sysAutoCodePackageDo.Alias() }

func (s sysAutoCodePackage) Columns(cols ...field.Expr) gen.Columns {
	return s.sysAutoCodePackageDo.Columns(cols...)
}

func (s *sysAutoCodePackage) 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 *sysAutoCodePackage) 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["deleted_at"] = s.DeletedAt
	s.fieldMap["desc"] = s.Desc
	s.fieldMap["label"] = s.Label
	s.fieldMap["template"] = s.Template
	s.fieldMap["package_name"] = s.PackageName
	s.fieldMap["module"] = s.Module
}

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

func (s sysAutoCodePackage) replaceDB(db *gorm.DB) sysAutoCodePackage {
	s.sysAutoCodePackageDo.ReplaceDB(db)
	return s
}

type sysAutoCodePackageDo struct{ gen.DO }

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

func (s sysAutoCodePackageDo) Debug() ISysAutoCodePackageDo {
	return s.withDO(s.DO.Debug())
}

func (s sysAutoCodePackageDo) WithContext(ctx context.Context) ISysAutoCodePackageDo {
	return s.withDO(s.DO.WithContext(ctx))
}

func (s sysAutoCodePackageDo) ReadDB() ISysAutoCodePackageDo {
	return s.Clauses(dbresolver.Read)
}

func (s sysAutoCodePackageDo) WriteDB() ISysAutoCodePackageDo {
	return s.Clauses(dbresolver.Write)
}

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

func (s sysAutoCodePackageDo) Clauses(conds ...clause.Expression) ISysAutoCodePackageDo {
	return s.withDO(s.DO.Clauses(conds...))
}

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

func (s sysAutoCodePackageDo) Not(conds ...gen.Condition) ISysAutoCodePackageDo {
	return s.withDO(s.DO.Not(conds...))
}

func (s sysAutoCodePackageDo) Or(conds ...gen.Condition) ISysAutoCodePackageDo {
	return s.withDO(s.DO.Or(conds...))
}

func (s sysAutoCodePackageDo) Select(conds ...field.Expr) ISysAutoCodePackageDo {
	return s.withDO(s.DO.Select(conds...))
}

func (s sysAutoCodePackageDo) Where(conds ...gen.Condition) ISysAutoCodePackageDo {
	return s.withDO(s.DO.Where(conds...))
}

func (s sysAutoCodePackageDo) Order(conds ...field.Expr) ISysAutoCodePackageDo {
	return s.withDO(s.DO.Order(conds...))
}

func (s sysAutoCodePackageDo) Distinct(cols ...field.Expr) ISysAutoCodePackageDo {
	return s.withDO(s.DO.Distinct(cols...))
}

func (s sysAutoCodePackageDo) Omit(cols ...field.Expr) ISysAutoCodePackageDo {
	return s.withDO(s.DO.Omit(cols...))
}

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

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

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

func (s sysAutoCodePackageDo) Group(cols ...field.Expr) ISysAutoCodePackageDo {
	return s.withDO(s.DO.Group(cols...))
}

func (s sysAutoCodePackageDo) Having(conds ...gen.Condition) ISysAutoCodePackageDo {
	return s.withDO(s.DO.Having(conds...))
}

func (s sysAutoCodePackageDo) Limit(limit int) ISysAutoCodePackageDo {
	return s.withDO(s.DO.Limit(limit))
}

func (s sysAutoCodePackageDo) Offset(offset int) ISysAutoCodePackageDo {
	return s.withDO(s.DO.Offset(offset))
}

func (s sysAutoCodePackageDo) Scopes(funcs ...func(gen.Dao) gen.Dao) ISysAutoCodePackageDo {
	return s.withDO(s.DO.Scopes(funcs...))
}

func (s sysAutoCodePackageDo) Unscoped() ISysAutoCodePackageDo {
	return s.withDO(s.DO.Unscoped())
}

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

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

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

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

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

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

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

func (s sysAutoCodePackageDo) Attrs(attrs ...field.AssignExpr) ISysAutoCodePackageDo {
	return s.withDO(s.DO.Attrs(attrs...))
}

func (s sysAutoCodePackageDo) Assign(attrs ...field.AssignExpr) ISysAutoCodePackageDo {
	return s.withDO(s.DO.Assign(attrs...))
}

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

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

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

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

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

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

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