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

	_orders.ordersDo.UseDB(db, opts...)
	_orders.ordersDo.UseModel(&model.Orders{})

	tableName := _orders.ordersDo.TableName()
	_orders.ALL = field.NewAsterisk(tableName)
	_orders.ID = field.NewInt64(tableName, "id")
	_orders.CreatedAt = field.NewTime(tableName, "created_at")
	_orders.UpdatedAt = field.NewTime(tableName, "updated_at")
	_orders.UserID = field.NewInt64(tableName, "user_id")
	_orders.OrderNo = field.NewString(tableName, "order_no")
	_orders.SourceType = field.NewInt32(tableName, "source_type")
	_orders.TotalAmount = field.NewInt64(tableName, "total_amount")
	_orders.DiscountAmount = field.NewInt64(tableName, "discount_amount")
	_orders.PointsAmount = field.NewInt64(tableName, "points_amount")
	_orders.ActualAmount = field.NewInt64(tableName, "actual_amount")
	_orders.Status = field.NewInt32(tableName, "status")
	_orders.PayPreorderID = field.NewInt64(tableName, "pay_preorder_id")
	_orders.PaidAt = field.NewTime(tableName, "paid_at")
	_orders.CancelledAt = field.NewTime(tableName, "cancelled_at")
	_orders.UserAddressID = field.NewInt64(tableName, "user_address_id")
	_orders.IsConsumed = field.NewInt32(tableName, "is_consumed")
	_orders.PointsLedgerID = field.NewInt64(tableName, "points_ledger_id")
	_orders.CouponID = field.NewInt64(tableName, "coupon_id")
	_orders.ItemCardID = field.NewInt64(tableName, "item_card_id")
	_orders.Remark = field.NewString(tableName, "remark")

	_orders.fillFieldMap()

	return _orders
}

// orders 订单
type orders struct {
	ordersDo

	ALL            field.Asterisk
	ID             field.Int64  // 主键ID
	CreatedAt      field.Time   // 创建时间
	UpdatedAt      field.Time   // 更新时间
	UserID         field.Int64  // 下单用户ID（user_members.id）
	OrderNo        field.String // 业务订单号（唯一）
	SourceType     field.Int32  // 来源：1商城直购 2抽奖票据 3其他
	TotalAmount    field.Int64  // 订单总金额（分）
	DiscountAmount field.Int64  // 优惠券抵扣金额（分）
	PointsAmount   field.Int64  // 积分抵扣金额（分）
	ActualAmount   field.Int64  // 实际支付金额（分）
	Status         field.Int32  // 订单状态：1待支付 2已支付 3已取消 4已退款
	PayPreorderID  field.Int64  // 关联预支付单ID（payment_preorder.id）
	PaidAt         field.Time   // 支付完成时间
	CancelledAt    field.Time   // 取消时间
	UserAddressID  field.Int64  // 收货地址ID（user_addresses.id）
	IsConsumed     field.Int32  // 是否已履约/消耗（对虚拟资产）
	PointsLedgerID field.Int64  // 积分扣减流水ID（user_points_ledger.id）
	CouponID       field.Int64  // 使用的优惠券ID
	ItemCardID     field.Int64  // 使用的道具卡ID
	Remark         field.String // 备注

	fieldMap map[string]field.Expr
}

func (o orders) Table(newTableName string) *orders {
	o.ordersDo.UseTable(newTableName)
	return o.updateTableName(newTableName)
}

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

func (o *orders) updateTableName(table string) *orders {
	o.ALL = field.NewAsterisk(table)
	o.ID = field.NewInt64(table, "id")
	o.CreatedAt = field.NewTime(table, "created_at")
	o.UpdatedAt = field.NewTime(table, "updated_at")
	o.UserID = field.NewInt64(table, "user_id")
	o.OrderNo = field.NewString(table, "order_no")
	o.SourceType = field.NewInt32(table, "source_type")
	o.TotalAmount = field.NewInt64(table, "total_amount")
	o.DiscountAmount = field.NewInt64(table, "discount_amount")
	o.PointsAmount = field.NewInt64(table, "points_amount")
	o.ActualAmount = field.NewInt64(table, "actual_amount")
	o.Status = field.NewInt32(table, "status")
	o.PayPreorderID = field.NewInt64(table, "pay_preorder_id")
	o.PaidAt = field.NewTime(table, "paid_at")
	o.CancelledAt = field.NewTime(table, "cancelled_at")
	o.UserAddressID = field.NewInt64(table, "user_address_id")
	o.IsConsumed = field.NewInt32(table, "is_consumed")
	o.PointsLedgerID = field.NewInt64(table, "points_ledger_id")
	o.CouponID = field.NewInt64(table, "coupon_id")
	o.ItemCardID = field.NewInt64(table, "item_card_id")
	o.Remark = field.NewString(table, "remark")

	o.fillFieldMap()

	return o
}

func (o *orders) 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 *orders) fillFieldMap() {
	o.fieldMap = make(map[string]field.Expr, 20)
	o.fieldMap["id"] = o.ID
	o.fieldMap["created_at"] = o.CreatedAt
	o.fieldMap["updated_at"] = o.UpdatedAt
	o.fieldMap["user_id"] = o.UserID
	o.fieldMap["order_no"] = o.OrderNo
	o.fieldMap["source_type"] = o.SourceType
	o.fieldMap["total_amount"] = o.TotalAmount
	o.fieldMap["discount_amount"] = o.DiscountAmount
	o.fieldMap["points_amount"] = o.PointsAmount
	o.fieldMap["actual_amount"] = o.ActualAmount
	o.fieldMap["status"] = o.Status
	o.fieldMap["pay_preorder_id"] = o.PayPreorderID
	o.fieldMap["paid_at"] = o.PaidAt
	o.fieldMap["cancelled_at"] = o.CancelledAt
	o.fieldMap["user_address_id"] = o.UserAddressID
	o.fieldMap["is_consumed"] = o.IsConsumed
	o.fieldMap["points_ledger_id"] = o.PointsLedgerID
	o.fieldMap["coupon_id"] = o.CouponID
	o.fieldMap["item_card_id"] = o.ItemCardID
	o.fieldMap["remark"] = o.Remark
}

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

func (o orders) replaceDB(db *gorm.DB) orders {
	o.ordersDo.ReplaceDB(db)
	return o
}

type ordersDo struct{ gen.DO }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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