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

	_orderSnapshots.orderSnapshotsDo.UseDB(db, opts...)
	_orderSnapshots.orderSnapshotsDo.UseModel(&model.OrderSnapshots{})

	tableName := _orderSnapshots.orderSnapshotsDo.TableName()
	_orderSnapshots.ALL = field.NewAsterisk(tableName)
	_orderSnapshots.ID = field.NewInt64(tableName, "id")
	_orderSnapshots.CreatedAt = field.NewTime(tableName, "created_at")
	_orderSnapshots.OrderID = field.NewInt64(tableName, "order_id")
	_orderSnapshots.OrderNo = field.NewString(tableName, "order_no")
	_orderSnapshots.UserID = field.NewInt64(tableName, "user_id")
	_orderSnapshots.SnapshotType = field.NewInt32(tableName, "snapshot_type")
	_orderSnapshots.SnapshotData = field.NewString(tableName, "snapshot_data")
	_orderSnapshots.SnapshotHash = field.NewString(tableName, "snapshot_hash")

	_orderSnapshots.fillFieldMap()

	return _orderSnapshots
}

// orderSnapshots 订单快照表
type orderSnapshots struct {
	orderSnapshotsDo

	ALL          field.Asterisk
	ID           field.Int64  // 主键ID
	CreatedAt    field.Time   // 创建时间
	OrderID      field.Int64  // 订单ID
	OrderNo      field.String // 订单号
	UserID       field.Int64  // 用户ID
	SnapshotType field.Int32  // 快照类型：1=消费前 2=消费后
	SnapshotData field.String // 完整状态快照(JSON)
	SnapshotHash field.String // 快照数据Hash(可选，用于校验)

	fieldMap map[string]field.Expr
}

func (o orderSnapshots) Table(newTableName string) *orderSnapshots {
	o.orderSnapshotsDo.UseTable(newTableName)
	return o.updateTableName(newTableName)
}

func (o orderSnapshots) As(alias string) *orderSnapshots {
	o.orderSnapshotsDo.DO = *(o.orderSnapshotsDo.As(alias).(*gen.DO))
	return o.updateTableName(alias)
}

func (o *orderSnapshots) updateTableName(table string) *orderSnapshots {
	o.ALL = field.NewAsterisk(table)
	o.ID = field.NewInt64(table, "id")
	o.CreatedAt = field.NewTime(table, "created_at")
	o.OrderID = field.NewInt64(table, "order_id")
	o.OrderNo = field.NewString(table, "order_no")
	o.UserID = field.NewInt64(table, "user_id")
	o.SnapshotType = field.NewInt32(table, "snapshot_type")
	o.SnapshotData = field.NewString(table, "snapshot_data")
	o.SnapshotHash = field.NewString(table, "snapshot_hash")

	o.fillFieldMap()

	return o
}

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

func (o *orderSnapshots) fillFieldMap() {
	o.fieldMap = make(map[string]field.Expr, 8)
	o.fieldMap["id"] = o.ID
	o.fieldMap["created_at"] = o.CreatedAt
	o.fieldMap["order_id"] = o.OrderID
	o.fieldMap["order_no"] = o.OrderNo
	o.fieldMap["user_id"] = o.UserID
	o.fieldMap["snapshot_type"] = o.SnapshotType
	o.fieldMap["snapshot_data"] = o.SnapshotData
	o.fieldMap["snapshot_hash"] = o.SnapshotHash
}

func (o orderSnapshots) clone(db *gorm.DB) orderSnapshots {
	o.orderSnapshotsDo.ReplaceConnPool(db.Statement.ConnPool)
	return o
}

func (o orderSnapshots) replaceDB(db *gorm.DB) orderSnapshots {
	o.orderSnapshotsDo.ReplaceDB(db)
	return o
}

type orderSnapshotsDo struct{ gen.DO }

func (o orderSnapshotsDo) Debug() *orderSnapshotsDo {
	return o.withDO(o.DO.Debug())
}

func (o orderSnapshotsDo) WithContext(ctx context.Context) *orderSnapshotsDo {
	return o.withDO(o.DO.WithContext(ctx))
}

func (o orderSnapshotsDo) ReadDB() *orderSnapshotsDo {
	return o.Clauses(dbresolver.Read)
}

func (o orderSnapshotsDo) WriteDB() *orderSnapshotsDo {
	return o.Clauses(dbresolver.Write)
}

func (o orderSnapshotsDo) Session(config *gorm.Session) *orderSnapshotsDo {
	return o.withDO(o.DO.Session(config))
}

func (o orderSnapshotsDo) Clauses(conds ...clause.Expression) *orderSnapshotsDo {
	return o.withDO(o.DO.Clauses(conds...))
}

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

func (o orderSnapshotsDo) Not(conds ...gen.Condition) *orderSnapshotsDo {
	return o.withDO(o.DO.Not(conds...))
}

func (o orderSnapshotsDo) Or(conds ...gen.Condition) *orderSnapshotsDo {
	return o.withDO(o.DO.Or(conds...))
}

func (o orderSnapshotsDo) Select(conds ...field.Expr) *orderSnapshotsDo {
	return o.withDO(o.DO.Select(conds...))
}

func (o orderSnapshotsDo) Where(conds ...gen.Condition) *orderSnapshotsDo {
	return o.withDO(o.DO.Where(conds...))
}

func (o orderSnapshotsDo) Order(conds ...field.Expr) *orderSnapshotsDo {
	return o.withDO(o.DO.Order(conds...))
}

func (o orderSnapshotsDo) Distinct(cols ...field.Expr) *orderSnapshotsDo {
	return o.withDO(o.DO.Distinct(cols...))
}

func (o orderSnapshotsDo) Omit(cols ...field.Expr) *orderSnapshotsDo {
	return o.withDO(o.DO.Omit(cols...))
}

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

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

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

func (o orderSnapshotsDo) Group(cols ...field.Expr) *orderSnapshotsDo {
	return o.withDO(o.DO.Group(cols...))
}

func (o orderSnapshotsDo) Having(conds ...gen.Condition) *orderSnapshotsDo {
	return o.withDO(o.DO.Having(conds...))
}

func (o orderSnapshotsDo) Limit(limit int) *orderSnapshotsDo {
	return o.withDO(o.DO.Limit(limit))
}

func (o orderSnapshotsDo) Offset(offset int) *orderSnapshotsDo {
	return o.withDO(o.DO.Offset(offset))
}

func (o orderSnapshotsDo) Scopes(funcs ...func(gen.Dao) gen.Dao) *orderSnapshotsDo {
	return o.withDO(o.DO.Scopes(funcs...))
}

func (o orderSnapshotsDo) Unscoped() *orderSnapshotsDo {
	return o.withDO(o.DO.Unscoped())
}

func (o orderSnapshotsDo) Create(values ...*model.OrderSnapshots) error {
	if len(values) == 0 {
		return nil
	}
	return o.DO.Create(values)
}

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

func (o orderSnapshotsDo) First() (*model.OrderSnapshots, error) {
	if result, err := o.DO.First(); err != nil {
		return nil, err
	} else {
		return result.(*model.OrderSnapshots), nil
	}
}

func (o orderSnapshotsDo) Take() (*model.OrderSnapshots, error) {
	if result, err := o.DO.Take(); err != nil {
		return nil, err
	} else {
		return result.(*model.OrderSnapshots), nil
	}
}

func (o orderSnapshotsDo) Last() (*model.OrderSnapshots, error) {
	if result, err := o.DO.Last(); err != nil {
		return nil, err
	} else {
		return result.(*model.OrderSnapshots), nil
	}
}

func (o orderSnapshotsDo) Find() ([]*model.OrderSnapshots, error) {
	result, err := o.DO.Find()
	return result.([]*model.OrderSnapshots), err
}

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

func (o orderSnapshotsDo) Attrs(attrs ...field.AssignExpr) *orderSnapshotsDo {
	return o.withDO(o.DO.Attrs(attrs...))
}

func (o orderSnapshotsDo) Assign(attrs ...field.AssignExpr) *orderSnapshotsDo {
	return o.withDO(o.DO.Assign(attrs...))
}

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

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

func (o orderSnapshotsDo) FirstOrInit() (*model.OrderSnapshots, error) {
	if result, err := o.DO.FirstOrInit(); err != nil {
		return nil, err
	} else {
		return result.(*model.OrderSnapshots), nil
	}
}

func (o orderSnapshotsDo) FirstOrCreate() (*model.OrderSnapshots, error) {
	if result, err := o.DO.FirstOrCreate(); err != nil {
		return nil, err
	} else {
		return result.(*model.OrderSnapshots), nil
	}
}

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

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

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

func (o orderSnapshotsDo) Scan(result interface{}) (err error) {
	return o.DO.Scan(result)
}

func (o orderSnapshotsDo) Delete(models ...*model.OrderSnapshots) (result gen.ResultInfo, err error) {
	return o.DO.Delete(models)
}

func (o *orderSnapshotsDo) withDO(do gen.Dao) *orderSnapshotsDo {
	o.DO = *do.(*gen.DO)
	return o
}
