// 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 dal

import (
	"app/internal/models"
	"context"

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

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

	"gorm.io/plugin/dbresolver"
)

func newProductType(db *gorm.DB, opts ...gen.DOOption) productType {
	_productType := productType{}

	_productType.productTypeDo.UseDB(db, opts...)
	_productType.productTypeDo.UseModel(&models.ProductType{})

	tableName := _productType.productTypeDo.TableName()
	_productType.ALL = field.NewAsterisk(tableName)
	_productType.Id = field.NewUint(tableName, "id")
	_productType.Name = field.NewString(tableName, "name")

	_productType.fillFieldMap()

	return _productType
}

type productType struct {
	productTypeDo

	ALL  field.Asterisk
	Id   field.Uint
	Name field.String

	fieldMap map[string]field.Expr
}

func (p productType) Table(newTableName string) *productType {
	p.productTypeDo.UseTable(newTableName)
	return p.updateTableName(newTableName)
}

func (p productType) As(alias string) *productType {
	p.productTypeDo.DO = *(p.productTypeDo.As(alias).(*gen.DO))
	return p.updateTableName(alias)
}

func (p *productType) updateTableName(table string) *productType {
	p.ALL = field.NewAsterisk(table)
	p.Id = field.NewUint(table, "id")
	p.Name = field.NewString(table, "name")

	p.fillFieldMap()

	return p
}

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

func (p *productType) fillFieldMap() {
	p.fieldMap = make(map[string]field.Expr, 2)
	p.fieldMap["id"] = p.Id
	p.fieldMap["name"] = p.Name
}

func (p productType) clone(db *gorm.DB) productType {
	p.productTypeDo.ReplaceConnPool(db.Statement.ConnPool)
	return p
}

func (p productType) replaceDB(db *gorm.DB) productType {
	p.productTypeDo.ReplaceDB(db)
	return p
}

type productTypeDo struct{ gen.DO }

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

func (p productTypeDo) Debug() IProductTypeDo {
	return p.withDO(p.DO.Debug())
}

func (p productTypeDo) WithContext(ctx context.Context) IProductTypeDo {
	return p.withDO(p.DO.WithContext(ctx))
}

func (p productTypeDo) ReadDB() IProductTypeDo {
	return p.Clauses(dbresolver.Read)
}

func (p productTypeDo) WriteDB() IProductTypeDo {
	return p.Clauses(dbresolver.Write)
}

func (p productTypeDo) Session(config *gorm.Session) IProductTypeDo {
	return p.withDO(p.DO.Session(config))
}

func (p productTypeDo) Clauses(conds ...clause.Expression) IProductTypeDo {
	return p.withDO(p.DO.Clauses(conds...))
}

func (p productTypeDo) Returning(value interface{}, columns ...string) IProductTypeDo {
	return p.withDO(p.DO.Returning(value, columns...))
}

func (p productTypeDo) Not(conds ...gen.Condition) IProductTypeDo {
	return p.withDO(p.DO.Not(conds...))
}

func (p productTypeDo) Or(conds ...gen.Condition) IProductTypeDo {
	return p.withDO(p.DO.Or(conds...))
}

func (p productTypeDo) Select(conds ...field.Expr) IProductTypeDo {
	return p.withDO(p.DO.Select(conds...))
}

func (p productTypeDo) Where(conds ...gen.Condition) IProductTypeDo {
	return p.withDO(p.DO.Where(conds...))
}

func (p productTypeDo) Order(conds ...field.Expr) IProductTypeDo {
	return p.withDO(p.DO.Order(conds...))
}

func (p productTypeDo) Distinct(cols ...field.Expr) IProductTypeDo {
	return p.withDO(p.DO.Distinct(cols...))
}

func (p productTypeDo) Omit(cols ...field.Expr) IProductTypeDo {
	return p.withDO(p.DO.Omit(cols...))
}

func (p productTypeDo) Join(table schema.Tabler, on ...field.Expr) IProductTypeDo {
	return p.withDO(p.DO.Join(table, on...))
}

func (p productTypeDo) LeftJoin(table schema.Tabler, on ...field.Expr) IProductTypeDo {
	return p.withDO(p.DO.LeftJoin(table, on...))
}

func (p productTypeDo) RightJoin(table schema.Tabler, on ...field.Expr) IProductTypeDo {
	return p.withDO(p.DO.RightJoin(table, on...))
}

func (p productTypeDo) Group(cols ...field.Expr) IProductTypeDo {
	return p.withDO(p.DO.Group(cols...))
}

func (p productTypeDo) Having(conds ...gen.Condition) IProductTypeDo {
	return p.withDO(p.DO.Having(conds...))
}

func (p productTypeDo) Limit(limit int) IProductTypeDo {
	return p.withDO(p.DO.Limit(limit))
}

func (p productTypeDo) Offset(offset int) IProductTypeDo {
	return p.withDO(p.DO.Offset(offset))
}

func (p productTypeDo) Scopes(funcs ...func(gen.Dao) gen.Dao) IProductTypeDo {
	return p.withDO(p.DO.Scopes(funcs...))
}

func (p productTypeDo) Unscoped() IProductTypeDo {
	return p.withDO(p.DO.Unscoped())
}

func (p productTypeDo) Create(values ...*models.ProductType) error {
	if len(values) == 0 {
		return nil
	}
	return p.DO.Create(values)
}

func (p productTypeDo) CreateInBatches(values []*models.ProductType, batchSize int) error {
	return p.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 (p productTypeDo) Save(values ...*models.ProductType) error {
	if len(values) == 0 {
		return nil
	}
	return p.DO.Save(values)
}

func (p productTypeDo) First() (*models.ProductType, error) {
	if result, err := p.DO.First(); err != nil {
		return nil, err
	} else {
		return result.(*models.ProductType), nil
	}
}

func (p productTypeDo) Take() (*models.ProductType, error) {
	if result, err := p.DO.Take(); err != nil {
		return nil, err
	} else {
		return result.(*models.ProductType), nil
	}
}

func (p productTypeDo) Last() (*models.ProductType, error) {
	if result, err := p.DO.Last(); err != nil {
		return nil, err
	} else {
		return result.(*models.ProductType), nil
	}
}

func (p productTypeDo) Find() ([]*models.ProductType, error) {
	result, err := p.DO.Find()
	return result.([]*models.ProductType), err
}

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

func (p productTypeDo) Attrs(attrs ...field.AssignExpr) IProductTypeDo {
	return p.withDO(p.DO.Attrs(attrs...))
}

func (p productTypeDo) Assign(attrs ...field.AssignExpr) IProductTypeDo {
	return p.withDO(p.DO.Assign(attrs...))
}

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

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

func (p productTypeDo) FirstOrInit() (*models.ProductType, error) {
	if result, err := p.DO.FirstOrInit(); err != nil {
		return nil, err
	} else {
		return result.(*models.ProductType), nil
	}
}

func (p productTypeDo) FirstOrCreate() (*models.ProductType, error) {
	if result, err := p.DO.FirstOrCreate(); err != nil {
		return nil, err
	} else {
		return result.(*models.ProductType), nil
	}
}

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

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

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

func (p productTypeDo) Scan(result interface{}) (err error) {
	return p.DO.Scan(result)
}

func (p productTypeDo) Delete(models ...*models.ProductType) (result gen.ResultInfo, err error) {
	return p.DO.Delete(models)
}

func (p *productTypeDo) withDO(do gen.Dao) *productTypeDo {
	p.DO = *do.(*gen.DO)
	return p
}
