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

	_retrofit.retrofitDo.UseDB(db, opts...)
	_retrofit.retrofitDo.UseModel(&model.Retrofit{})

	tableName := _retrofit.retrofitDo.TableName()
	_retrofit.ALL = field.NewAsterisk(tableName)
	_retrofit.ID = field.NewInt64(tableName, "id")
	_retrofit.GroupID = field.NewInt64(tableName, "group_id")
	_retrofit.Name = field.NewString(tableName, "name")
	_retrofit.Type = field.NewInt64(tableName, "type")
	_retrofit.Mid = field.NewInt64(tableName, "mid")
	_retrofit.Nums = field.NewInt64(tableName, "nums")
	_retrofit.Status = field.NewInt32(tableName, "status")
	_retrofit.CreatedAt = field.NewTime(tableName, "created_at")
	_retrofit.UpdatedAt = field.NewTime(tableName, "updated_at")
	_retrofit.DeletedAt = field.NewField(tableName, "deleted_at")

	_retrofit.fillFieldMap()

	return _retrofit
}

type retrofit struct {
	retrofitDo

	ALL       field.Asterisk
	ID        field.Int64
	GroupID   field.Int64  // 模板ID
	Name      field.String // 别名
	Type      field.Int64  // 加装类型
	Mid       field.Int64  // 关联ID
	Nums      field.Int64  // 数量
	Status    field.Int32  // 状态
	CreatedAt field.Time   // 创建时间
	UpdatedAt field.Time   // 更新时间
	DeletedAt field.Field  // 删除时间

	fieldMap map[string]field.Expr
}

func (r retrofit) Table(newTableName string) *retrofit {
	r.retrofitDo.UseTable(newTableName)
	return r.updateTableName(newTableName)
}

func (r retrofit) As(alias string) *retrofit {
	r.retrofitDo.DO = *(r.retrofitDo.As(alias).(*gen.DO))
	return r.updateTableName(alias)
}

func (r *retrofit) updateTableName(table string) *retrofit {
	r.ALL = field.NewAsterisk(table)
	r.ID = field.NewInt64(table, "id")
	r.GroupID = field.NewInt64(table, "group_id")
	r.Name = field.NewString(table, "name")
	r.Type = field.NewInt64(table, "type")
	r.Mid = field.NewInt64(table, "mid")
	r.Nums = field.NewInt64(table, "nums")
	r.Status = field.NewInt32(table, "status")
	r.CreatedAt = field.NewTime(table, "created_at")
	r.UpdatedAt = field.NewTime(table, "updated_at")
	r.DeletedAt = field.NewField(table, "deleted_at")

	r.fillFieldMap()

	return r
}

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

func (r *retrofit) fillFieldMap() {
	r.fieldMap = make(map[string]field.Expr, 10)
	r.fieldMap["id"] = r.ID
	r.fieldMap["group_id"] = r.GroupID
	r.fieldMap["name"] = r.Name
	r.fieldMap["type"] = r.Type
	r.fieldMap["mid"] = r.Mid
	r.fieldMap["nums"] = r.Nums
	r.fieldMap["status"] = r.Status
	r.fieldMap["created_at"] = r.CreatedAt
	r.fieldMap["updated_at"] = r.UpdatedAt
	r.fieldMap["deleted_at"] = r.DeletedAt
}

func (r retrofit) clone(db *gorm.DB) retrofit {
	r.retrofitDo.ReplaceConnPool(db.Statement.ConnPool)
	return r
}

func (r retrofit) replaceDB(db *gorm.DB) retrofit {
	r.retrofitDo.ReplaceDB(db)
	return r
}

type retrofitDo struct{ gen.DO }

func (r retrofitDo) Debug() *retrofitDo {
	return r.withDO(r.DO.Debug())
}

func (r retrofitDo) WithContext(ctx context.Context) *retrofitDo {
	return r.withDO(r.DO.WithContext(ctx))
}

func (r retrofitDo) ReadDB() *retrofitDo {
	return r.Clauses(dbresolver.Read)
}

func (r retrofitDo) WriteDB() *retrofitDo {
	return r.Clauses(dbresolver.Write)
}

func (r retrofitDo) Session(config *gorm.Session) *retrofitDo {
	return r.withDO(r.DO.Session(config))
}

func (r retrofitDo) Clauses(conds ...clause.Expression) *retrofitDo {
	return r.withDO(r.DO.Clauses(conds...))
}

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

func (r retrofitDo) Not(conds ...gen.Condition) *retrofitDo {
	return r.withDO(r.DO.Not(conds...))
}

func (r retrofitDo) Or(conds ...gen.Condition) *retrofitDo {
	return r.withDO(r.DO.Or(conds...))
}

func (r retrofitDo) Select(conds ...field.Expr) *retrofitDo {
	return r.withDO(r.DO.Select(conds...))
}

func (r retrofitDo) Where(conds ...gen.Condition) *retrofitDo {
	return r.withDO(r.DO.Where(conds...))
}

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

func (r retrofitDo) Order(conds ...field.Expr) *retrofitDo {
	return r.withDO(r.DO.Order(conds...))
}

func (r retrofitDo) Distinct(cols ...field.Expr) *retrofitDo {
	return r.withDO(r.DO.Distinct(cols...))
}

func (r retrofitDo) Omit(cols ...field.Expr) *retrofitDo {
	return r.withDO(r.DO.Omit(cols...))
}

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

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

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

func (r retrofitDo) Group(cols ...field.Expr) *retrofitDo {
	return r.withDO(r.DO.Group(cols...))
}

func (r retrofitDo) Having(conds ...gen.Condition) *retrofitDo {
	return r.withDO(r.DO.Having(conds...))
}

func (r retrofitDo) Limit(limit int) *retrofitDo {
	return r.withDO(r.DO.Limit(limit))
}

func (r retrofitDo) Offset(offset int) *retrofitDo {
	return r.withDO(r.DO.Offset(offset))
}

func (r retrofitDo) Scopes(funcs ...func(gen.Dao) gen.Dao) *retrofitDo {
	return r.withDO(r.DO.Scopes(funcs...))
}

func (r retrofitDo) Unscoped() *retrofitDo {
	return r.withDO(r.DO.Unscoped())
}

func (r retrofitDo) Create(values ...*model.Retrofit) error {
	if len(values) == 0 {
		return nil
	}
	return r.DO.Create(values)
}

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

func (r retrofitDo) First() (*model.Retrofit, error) {
	if result, err := r.DO.First(); err != nil {
		return nil, err
	} else {
		return result.(*model.Retrofit), nil
	}
}

func (r retrofitDo) Take() (*model.Retrofit, error) {
	if result, err := r.DO.Take(); err != nil {
		return nil, err
	} else {
		return result.(*model.Retrofit), nil
	}
}

func (r retrofitDo) Last() (*model.Retrofit, error) {
	if result, err := r.DO.Last(); err != nil {
		return nil, err
	} else {
		return result.(*model.Retrofit), nil
	}
}

func (r retrofitDo) Find() ([]*model.Retrofit, error) {
	result, err := r.DO.Find()
	return result.([]*model.Retrofit), err
}

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

func (r retrofitDo) Attrs(attrs ...field.AssignExpr) *retrofitDo {
	return r.withDO(r.DO.Attrs(attrs...))
}

func (r retrofitDo) Assign(attrs ...field.AssignExpr) *retrofitDo {
	return r.withDO(r.DO.Assign(attrs...))
}

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

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

func (r retrofitDo) FirstOrInit() (*model.Retrofit, error) {
	if result, err := r.DO.FirstOrInit(); err != nil {
		return nil, err
	} else {
		return result.(*model.Retrofit), nil
	}
}

func (r retrofitDo) FirstOrCreate() (*model.Retrofit, error) {
	if result, err := r.DO.FirstOrCreate(); err != nil {
		return nil, err
	} else {
		return result.(*model.Retrofit), nil
	}
}

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

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

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

func (r retrofitDo) Scan(result interface{}) (err error) {
	return r.DO.Scan(result)
}

func (r retrofitDo) Delete(models ...*model.Retrofit) (result gen.ResultInfo, err error) {
	return r.DO.Delete(models)
}

func (r *retrofitDo) withDO(do gen.Dao) *retrofitDo {
	r.DO = *do.(*gen.DO)
	return r
}
