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

	_userInventory.userInventoryDo.UseDB(db, opts...)
	_userInventory.userInventoryDo.UseModel(&model.UserInventory{})

	tableName := _userInventory.userInventoryDo.TableName()
	_userInventory.ALL = field.NewAsterisk(tableName)
	_userInventory.ID = field.NewInt64(tableName, "id")
	_userInventory.CreatedAt = field.NewTime(tableName, "created_at")
	_userInventory.UpdatedAt = field.NewTime(tableName, "updated_at")
	_userInventory.UserID = field.NewInt64(tableName, "user_id")
	_userInventory.ProductID = field.NewInt64(tableName, "product_id")
	_userInventory.OrderID = field.NewInt64(tableName, "order_id")
	_userInventory.ActivityID = field.NewInt64(tableName, "activity_id")
	_userInventory.RewardID = field.NewInt64(tableName, "reward_id")
	_userInventory.Status = field.NewInt32(tableName, "status")
	_userInventory.ShippingNo = field.NewString(tableName, "shipping_no")
	_userInventory.Remark = field.NewString(tableName, "remark")

	_userInventory.fillFieldMap()

	return _userInventory
}

// userInventory 用户资产（资产）
type userInventory struct {
	userInventoryDo

	ALL        field.Asterisk
	ID         field.Int64  // 主键ID
	CreatedAt  field.Time   // 创建时间
	UpdatedAt  field.Time   // 更新时间
	UserID     field.Int64  // 资产归属用户ID
	ProductID  field.Int64  // 资产对应商品ID（实物奖/商品）
	OrderID    field.Int64  // 来源订单ID
	ActivityID field.Int64  // 来源活动ID
	RewardID   field.Int64  // 来源奖励ID（activity_reward_settings.id）
	Status     field.Int32  // 状态：1持有 2作废 3已使用/发货
	ShippingNo field.String // 发货单号
	Remark     field.String // 备注

	fieldMap map[string]field.Expr
}

func (u userInventory) Table(newTableName string) *userInventory {
	u.userInventoryDo.UseTable(newTableName)
	return u.updateTableName(newTableName)
}

func (u userInventory) As(alias string) *userInventory {
	u.userInventoryDo.DO = *(u.userInventoryDo.As(alias).(*gen.DO))
	return u.updateTableName(alias)
}

func (u *userInventory) updateTableName(table string) *userInventory {
	u.ALL = field.NewAsterisk(table)
	u.ID = field.NewInt64(table, "id")
	u.CreatedAt = field.NewTime(table, "created_at")
	u.UpdatedAt = field.NewTime(table, "updated_at")
	u.UserID = field.NewInt64(table, "user_id")
	u.ProductID = field.NewInt64(table, "product_id")
	u.OrderID = field.NewInt64(table, "order_id")
	u.ActivityID = field.NewInt64(table, "activity_id")
	u.RewardID = field.NewInt64(table, "reward_id")
	u.Status = field.NewInt32(table, "status")
	u.ShippingNo = field.NewString(table, "shipping_no")
	u.Remark = field.NewString(table, "remark")

	u.fillFieldMap()

	return u
}

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

func (u *userInventory) fillFieldMap() {
	u.fieldMap = make(map[string]field.Expr, 11)
	u.fieldMap["id"] = u.ID
	u.fieldMap["created_at"] = u.CreatedAt
	u.fieldMap["updated_at"] = u.UpdatedAt
	u.fieldMap["user_id"] = u.UserID
	u.fieldMap["product_id"] = u.ProductID
	u.fieldMap["order_id"] = u.OrderID
	u.fieldMap["activity_id"] = u.ActivityID
	u.fieldMap["reward_id"] = u.RewardID
	u.fieldMap["status"] = u.Status
	u.fieldMap["shipping_no"] = u.ShippingNo
	u.fieldMap["remark"] = u.Remark
}

func (u userInventory) clone(db *gorm.DB) userInventory {
	u.userInventoryDo.ReplaceConnPool(db.Statement.ConnPool)
	return u
}

func (u userInventory) replaceDB(db *gorm.DB) userInventory {
	u.userInventoryDo.ReplaceDB(db)
	return u
}

type userInventoryDo struct{ gen.DO }

func (u userInventoryDo) Debug() *userInventoryDo {
	return u.withDO(u.DO.Debug())
}

func (u userInventoryDo) WithContext(ctx context.Context) *userInventoryDo {
	return u.withDO(u.DO.WithContext(ctx))
}

func (u userInventoryDo) ReadDB() *userInventoryDo {
	return u.Clauses(dbresolver.Read)
}

func (u userInventoryDo) WriteDB() *userInventoryDo {
	return u.Clauses(dbresolver.Write)
}

func (u userInventoryDo) Session(config *gorm.Session) *userInventoryDo {
	return u.withDO(u.DO.Session(config))
}

func (u userInventoryDo) Clauses(conds ...clause.Expression) *userInventoryDo {
	return u.withDO(u.DO.Clauses(conds...))
}

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

func (u userInventoryDo) Not(conds ...gen.Condition) *userInventoryDo {
	return u.withDO(u.DO.Not(conds...))
}

func (u userInventoryDo) Or(conds ...gen.Condition) *userInventoryDo {
	return u.withDO(u.DO.Or(conds...))
}

func (u userInventoryDo) Select(conds ...field.Expr) *userInventoryDo {
	return u.withDO(u.DO.Select(conds...))
}

func (u userInventoryDo) Where(conds ...gen.Condition) *userInventoryDo {
	return u.withDO(u.DO.Where(conds...))
}

func (u userInventoryDo) Order(conds ...field.Expr) *userInventoryDo {
	return u.withDO(u.DO.Order(conds...))
}

func (u userInventoryDo) Distinct(cols ...field.Expr) *userInventoryDo {
	return u.withDO(u.DO.Distinct(cols...))
}

func (u userInventoryDo) Omit(cols ...field.Expr) *userInventoryDo {
	return u.withDO(u.DO.Omit(cols...))
}

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

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

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

func (u userInventoryDo) Group(cols ...field.Expr) *userInventoryDo {
	return u.withDO(u.DO.Group(cols...))
}

func (u userInventoryDo) Having(conds ...gen.Condition) *userInventoryDo {
	return u.withDO(u.DO.Having(conds...))
}

func (u userInventoryDo) Limit(limit int) *userInventoryDo {
	return u.withDO(u.DO.Limit(limit))
}

func (u userInventoryDo) Offset(offset int) *userInventoryDo {
	return u.withDO(u.DO.Offset(offset))
}

func (u userInventoryDo) Scopes(funcs ...func(gen.Dao) gen.Dao) *userInventoryDo {
	return u.withDO(u.DO.Scopes(funcs...))
}

func (u userInventoryDo) Unscoped() *userInventoryDo {
	return u.withDO(u.DO.Unscoped())
}

func (u userInventoryDo) Create(values ...*model.UserInventory) error {
	if len(values) == 0 {
		return nil
	}
	return u.DO.Create(values)
}

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

func (u userInventoryDo) First() (*model.UserInventory, error) {
	if result, err := u.DO.First(); err != nil {
		return nil, err
	} else {
		return result.(*model.UserInventory), nil
	}
}

func (u userInventoryDo) Take() (*model.UserInventory, error) {
	if result, err := u.DO.Take(); err != nil {
		return nil, err
	} else {
		return result.(*model.UserInventory), nil
	}
}

func (u userInventoryDo) Last() (*model.UserInventory, error) {
	if result, err := u.DO.Last(); err != nil {
		return nil, err
	} else {
		return result.(*model.UserInventory), nil
	}
}

func (u userInventoryDo) Find() ([]*model.UserInventory, error) {
	result, err := u.DO.Find()
	return result.([]*model.UserInventory), err
}

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

func (u userInventoryDo) Attrs(attrs ...field.AssignExpr) *userInventoryDo {
	return u.withDO(u.DO.Attrs(attrs...))
}

func (u userInventoryDo) Assign(attrs ...field.AssignExpr) *userInventoryDo {
	return u.withDO(u.DO.Assign(attrs...))
}

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

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

func (u userInventoryDo) FirstOrInit() (*model.UserInventory, error) {
	if result, err := u.DO.FirstOrInit(); err != nil {
		return nil, err
	} else {
		return result.(*model.UserInventory), nil
	}
}

func (u userInventoryDo) FirstOrCreate() (*model.UserInventory, error) {
	if result, err := u.DO.FirstOrCreate(); err != nil {
		return nil, err
	} else {
		return result.(*model.UserInventory), nil
	}
}

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

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

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

func (u userInventoryDo) Scan(result interface{}) (err error) {
	return u.DO.Scan(result)
}

func (u userInventoryDo) Delete(models ...*model.UserInventory) (result gen.ResultInfo, err error) {
	return u.DO.Delete(models)
}

func (u *userInventoryDo) withDO(do gen.Dao) *userInventoryDo {
	u.DO = *do.(*gen.DO)
	return u
}
