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

import (
	"context"

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

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

	"gorm.io/plugin/dbresolver"

	"bindbox-game/internal/repository/mysql/model"
)

func newProducts(db *gorm.DB, opts ...gen.DOOption) products {
	_products := products{}

	_products.productsDo.UseDB(db, opts...)
	_products.productsDo.UseModel(&model.Products{})

	tableName := _products.productsDo.TableName()
	_products.ALL = field.NewAsterisk(tableName)
	_products.ID = field.NewInt64(tableName, "id")
	_products.CreatedAt = field.NewTime(tableName, "created_at")
	_products.UpdatedAt = field.NewTime(tableName, "updated_at")
	_products.Name = field.NewString(tableName, "name")
	_products.CategoryID = field.NewInt64(tableName, "category_id")
	_products.ImagesJSON = field.NewString(tableName, "images_json")
	_products.Price = field.NewInt64(tableName, "price")
	_products.Stock = field.NewInt64(tableName, "stock")
	_products.Sales = field.NewInt64(tableName, "sales")
	_products.Status = field.NewInt32(tableName, "status")
	_products.DeletedAt = field.NewField(tableName, "deleted_at")
	_products.Description = field.NewString(tableName, "description")

	_products.fillFieldMap()

	return _products
}

// products 商品
type products struct {
	productsDo

	ALL         field.Asterisk
	ID          field.Int64  // 主键ID
	CreatedAt   field.Time   // 创建时间
	UpdatedAt   field.Time   // 更新时间
	Name        field.String // 商品名称
	CategoryID  field.Int64  // 单一主分类ID（product_categories.id）
	ImagesJSON  field.String // 商品图片JSON（数组）
	Price       field.Int64  // 商品售价（分）
	Stock       field.Int64  // 可售库存
	Sales       field.Int64  // 已售数量
	Status      field.Int32  // 上下架状态：1上架 2下架
	DeletedAt   field.Field
	Description field.String // 商品详情

	fieldMap map[string]field.Expr
}

func (p products) Table(newTableName string) *products {
	p.productsDo.UseTable(newTableName)
	return p.updateTableName(newTableName)
}

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

func (p *products) updateTableName(table string) *products {
	p.ALL = field.NewAsterisk(table)
	p.ID = field.NewInt64(table, "id")
	p.CreatedAt = field.NewTime(table, "created_at")
	p.UpdatedAt = field.NewTime(table, "updated_at")
	p.Name = field.NewString(table, "name")
	p.CategoryID = field.NewInt64(table, "category_id")
	p.ImagesJSON = field.NewString(table, "images_json")
	p.Price = field.NewInt64(table, "price")
	p.Stock = field.NewInt64(table, "stock")
	p.Sales = field.NewInt64(table, "sales")
	p.Status = field.NewInt32(table, "status")
	p.DeletedAt = field.NewField(table, "deleted_at")
	p.Description = field.NewString(table, "description")

	p.fillFieldMap()

	return p
}

func (p *products) 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 *products) fillFieldMap() {
	p.fieldMap = make(map[string]field.Expr, 12)
	p.fieldMap["id"] = p.ID
	p.fieldMap["created_at"] = p.CreatedAt
	p.fieldMap["updated_at"] = p.UpdatedAt
	p.fieldMap["name"] = p.Name
	p.fieldMap["category_id"] = p.CategoryID
	p.fieldMap["images_json"] = p.ImagesJSON
	p.fieldMap["price"] = p.Price
	p.fieldMap["stock"] = p.Stock
	p.fieldMap["sales"] = p.Sales
	p.fieldMap["status"] = p.Status
	p.fieldMap["deleted_at"] = p.DeletedAt
	p.fieldMap["description"] = p.Description
}

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

func (p products) replaceDB(db *gorm.DB) products {
	p.productsDo.ReplaceDB(db)
	return p
}

type productsDo struct{ gen.DO }

func (p productsDo) Debug() *productsDo {
	return p.withDO(p.DO.Debug())
}

func (p productsDo) WithContext(ctx context.Context) *productsDo {
	return p.withDO(p.DO.WithContext(ctx))
}

func (p productsDo) ReadDB() *productsDo {
	return p.Clauses(dbresolver.Read)
}

func (p productsDo) WriteDB() *productsDo {
	return p.Clauses(dbresolver.Write)
}

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

func (p productsDo) Clauses(conds ...clause.Expression) *productsDo {
	return p.withDO(p.DO.Clauses(conds...))
}

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

func (p productsDo) Not(conds ...gen.Condition) *productsDo {
	return p.withDO(p.DO.Not(conds...))
}

func (p productsDo) Or(conds ...gen.Condition) *productsDo {
	return p.withDO(p.DO.Or(conds...))
}

func (p productsDo) Select(conds ...field.Expr) *productsDo {
	return p.withDO(p.DO.Select(conds...))
}

func (p productsDo) Where(conds ...gen.Condition) *productsDo {
	return p.withDO(p.DO.Where(conds...))
}

func (p productsDo) Order(conds ...field.Expr) *productsDo {
	return p.withDO(p.DO.Order(conds...))
}

func (p productsDo) Distinct(cols ...field.Expr) *productsDo {
	return p.withDO(p.DO.Distinct(cols...))
}

func (p productsDo) Omit(cols ...field.Expr) *productsDo {
	return p.withDO(p.DO.Omit(cols...))
}

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

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

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

func (p productsDo) Group(cols ...field.Expr) *productsDo {
	return p.withDO(p.DO.Group(cols...))
}

func (p productsDo) Having(conds ...gen.Condition) *productsDo {
	return p.withDO(p.DO.Having(conds...))
}

func (p productsDo) Limit(limit int) *productsDo {
	return p.withDO(p.DO.Limit(limit))
}

func (p productsDo) Offset(offset int) *productsDo {
	return p.withDO(p.DO.Offset(offset))
}

func (p productsDo) Scopes(funcs ...func(gen.Dao) gen.Dao) *productsDo {
	return p.withDO(p.DO.Scopes(funcs...))
}

func (p productsDo) Unscoped() *productsDo {
	return p.withDO(p.DO.Unscoped())
}

func (p productsDo) Create(values ...*model.Products) error {
	if len(values) == 0 {
		return nil
	}
	return p.DO.Create(values)
}

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

func (p productsDo) First() (*model.Products, error) {
	if result, err := p.DO.First(); err != nil {
		return nil, err
	} else {
		return result.(*model.Products), nil
	}
}

func (p productsDo) Take() (*model.Products, error) {
	if result, err := p.DO.Take(); err != nil {
		return nil, err
	} else {
		return result.(*model.Products), nil
	}
}

func (p productsDo) Last() (*model.Products, error) {
	if result, err := p.DO.Last(); err != nil {
		return nil, err
	} else {
		return result.(*model.Products), nil
	}
}

func (p productsDo) Find() ([]*model.Products, error) {
	result, err := p.DO.Find()
	return result.([]*model.Products), err
}

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

func (p productsDo) Attrs(attrs ...field.AssignExpr) *productsDo {
	return p.withDO(p.DO.Attrs(attrs...))
}

func (p productsDo) Assign(attrs ...field.AssignExpr) *productsDo {
	return p.withDO(p.DO.Assign(attrs...))
}

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

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

func (p productsDo) FirstOrInit() (*model.Products, error) {
	if result, err := p.DO.FirstOrInit(); err != nil {
		return nil, err
	} else {
		return result.(*model.Products), nil
	}
}

func (p productsDo) FirstOrCreate() (*model.Products, error) {
	if result, err := p.DO.FirstOrCreate(); err != nil {
		return nil, err
	} else {
		return result.(*model.Products), nil
	}
}

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

func (p productsDo) Delete(models ...*model.Products) (result gen.ResultInfo, err error) {
	return p.DO.Delete(models)
}

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