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

	_postType.postTypeDo.UseDB(db, opts...)
	_postType.postTypeDo.UseModel(&models.PostType{})

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

	_postType.fillFieldMap()

	return _postType
}

type postType struct {
	postTypeDo

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

	fieldMap map[string]field.Expr
}

func (p postType) Table(newTableName string) *postType {
	p.postTypeDo.UseTable(newTableName)
	return p.updateTableName(newTableName)
}

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

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

	p.fillFieldMap()

	return p
}

func (p *postType) 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 *postType) fillFieldMap() {
	p.fieldMap = make(map[string]field.Expr, 2)
	p.fieldMap["id"] = p.Id
	p.fieldMap["name"] = p.Name
}

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

func (p postType) replaceDB(db *gorm.DB) postType {
	p.postTypeDo.ReplaceDB(db)
	return p
}

type postTypeDo struct{ gen.DO }

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

func (p postTypeDo) Debug() IPostTypeDo {
	return p.withDO(p.DO.Debug())
}

func (p postTypeDo) WithContext(ctx context.Context) IPostTypeDo {
	return p.withDO(p.DO.WithContext(ctx))
}

func (p postTypeDo) ReadDB() IPostTypeDo {
	return p.Clauses(dbresolver.Read)
}

func (p postTypeDo) WriteDB() IPostTypeDo {
	return p.Clauses(dbresolver.Write)
}

func (p postTypeDo) Session(config *gorm.Session) IPostTypeDo {
	return p.withDO(p.DO.Session(config))
}

func (p postTypeDo) Clauses(conds ...clause.Expression) IPostTypeDo {
	return p.withDO(p.DO.Clauses(conds...))
}

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

func (p postTypeDo) Not(conds ...gen.Condition) IPostTypeDo {
	return p.withDO(p.DO.Not(conds...))
}

func (p postTypeDo) Or(conds ...gen.Condition) IPostTypeDo {
	return p.withDO(p.DO.Or(conds...))
}

func (p postTypeDo) Select(conds ...field.Expr) IPostTypeDo {
	return p.withDO(p.DO.Select(conds...))
}

func (p postTypeDo) Where(conds ...gen.Condition) IPostTypeDo {
	return p.withDO(p.DO.Where(conds...))
}

func (p postTypeDo) Order(conds ...field.Expr) IPostTypeDo {
	return p.withDO(p.DO.Order(conds...))
}

func (p postTypeDo) Distinct(cols ...field.Expr) IPostTypeDo {
	return p.withDO(p.DO.Distinct(cols...))
}

func (p postTypeDo) Omit(cols ...field.Expr) IPostTypeDo {
	return p.withDO(p.DO.Omit(cols...))
}

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

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

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

func (p postTypeDo) Group(cols ...field.Expr) IPostTypeDo {
	return p.withDO(p.DO.Group(cols...))
}

func (p postTypeDo) Having(conds ...gen.Condition) IPostTypeDo {
	return p.withDO(p.DO.Having(conds...))
}

func (p postTypeDo) Limit(limit int) IPostTypeDo {
	return p.withDO(p.DO.Limit(limit))
}

func (p postTypeDo) Offset(offset int) IPostTypeDo {
	return p.withDO(p.DO.Offset(offset))
}

func (p postTypeDo) Scopes(funcs ...func(gen.Dao) gen.Dao) IPostTypeDo {
	return p.withDO(p.DO.Scopes(funcs...))
}

func (p postTypeDo) Unscoped() IPostTypeDo {
	return p.withDO(p.DO.Unscoped())
}

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

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

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

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

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

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

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

func (p postTypeDo) Attrs(attrs ...field.AssignExpr) IPostTypeDo {
	return p.withDO(p.DO.Attrs(attrs...))
}

func (p postTypeDo) Assign(attrs ...field.AssignExpr) IPostTypeDo {
	return p.withDO(p.DO.Assign(attrs...))
}

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

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

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

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

func (p postTypeDo) FindByPage(offset int, limit int) (result []*models.PostType, 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 postTypeDo) 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 postTypeDo) Scan(result interface{}) (err error) {
	return p.DO.Scan(result)
}

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

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