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

import (
	"context"

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

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

	"gorm.io/plugin/dbresolver"

	"gadmin/internal/gorm/model"
)

func newOrder(db *gorm.DB, opts ...gen.DOOption) order {
	_order := order{}

	_order.orderDo.UseDB(db, opts...)
	_order.orderDo.UseModel(&model.Order{})

	tableName := _order.orderDo.TableName()
	_order.ALL = field.NewAsterisk(tableName)
	_order.ID = field.NewInt64(tableName, "id")
	_order.Platform = field.NewInt32(tableName, "platform")
	_order.RelatID = field.NewInt32(tableName, "relat_id")
	_order.PaymentType = field.NewInt32(tableName, "payment_type")
	_order.PlayerID = field.NewInt64(tableName, "player_id")
	_order.ServerID = field.NewInt32(tableName, "server_id")
	_order.GoodsID = field.NewInt32(tableName, "goods_id")
	_order.OrderSn = field.NewString(tableName, "order_sn")
	_order.OutTradeNo = field.NewString(tableName, "out_trade_no")
	_order.Money = field.NewFloat64(tableName, "money")
	_order.IsNew = field.NewInt32(tableName, "is_new")
	_order.Status = field.NewInt32(tableName, "status")
	_order.Date = field.NewString(tableName, "date")
	_order.PayAt = field.NewTime(tableName, "pay_at")
	_order.CreatedAt = field.NewTime(tableName, "created_at")
	_order.ChannelID = field.NewString(tableName, "channel_id")
	_order.UserCreatedAt = field.NewInt32(tableName, "user_created_at")
	_order.Flag = field.NewInt32(tableName, "flag")

	_order.fillFieldMap()

	return _order
}

type order struct {
	orderDo

	ALL           field.Asterisk
	ID            field.Int64
	Platform      field.Int32   // 订单平台(1：安卓，2：苹果)
	RelatID       field.Int32   // 关联订单ID
	PaymentType   field.Int32   // 支付方式(1:微信，2抖音 其他待定)
	PlayerID      field.Int64   // 玩家ID
	ServerID      field.Int32   // 服务器ID
	GoodsID       field.Int32   // 商品ID
	OrderSn       field.String  // 订单号
	OutTradeNo    field.String  // 外部订单号
	Money         field.Float64 // 订单实付金额
	IsNew         field.Int32   // 是否为首次下单
	Status        field.Int32   // 订单状态
	Date          field.String  // 订单日期
	PayAt         field.Time    // 支付时间
	CreatedAt     field.Time    // 创建时间
	ChannelID     field.String  // 渠道ID
	UserCreatedAt field.Int32   // 用户注册时间
	Flag          field.Int32

	fieldMap map[string]field.Expr
}

func (o order) Table(newTableName string) *order {
	o.orderDo.UseTable(newTableName)
	return o.updateTableName(newTableName)
}

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

func (o *order) updateTableName(table string) *order {
	o.ALL = field.NewAsterisk(table)
	o.ID = field.NewInt64(table, "id")
	o.Platform = field.NewInt32(table, "platform")
	o.RelatID = field.NewInt32(table, "relat_id")
	o.PaymentType = field.NewInt32(table, "payment_type")
	o.PlayerID = field.NewInt64(table, "player_id")
	o.ServerID = field.NewInt32(table, "server_id")
	o.GoodsID = field.NewInt32(table, "goods_id")
	o.OrderSn = field.NewString(table, "order_sn")
	o.OutTradeNo = field.NewString(table, "out_trade_no")
	o.Money = field.NewFloat64(table, "money")
	o.IsNew = field.NewInt32(table, "is_new")
	o.Status = field.NewInt32(table, "status")
	o.Date = field.NewString(table, "date")
	o.PayAt = field.NewTime(table, "pay_at")
	o.CreatedAt = field.NewTime(table, "created_at")
	o.ChannelID = field.NewString(table, "channel_id")
	o.UserCreatedAt = field.NewInt32(table, "user_created_at")
	o.Flag = field.NewInt32(table, "flag")

	o.fillFieldMap()

	return o
}

func (o *order) 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 *order) fillFieldMap() {
	o.fieldMap = make(map[string]field.Expr, 18)
	o.fieldMap["id"] = o.ID
	o.fieldMap["platform"] = o.Platform
	o.fieldMap["relat_id"] = o.RelatID
	o.fieldMap["payment_type"] = o.PaymentType
	o.fieldMap["player_id"] = o.PlayerID
	o.fieldMap["server_id"] = o.ServerID
	o.fieldMap["goods_id"] = o.GoodsID
	o.fieldMap["order_sn"] = o.OrderSn
	o.fieldMap["out_trade_no"] = o.OutTradeNo
	o.fieldMap["money"] = o.Money
	o.fieldMap["is_new"] = o.IsNew
	o.fieldMap["status"] = o.Status
	o.fieldMap["date"] = o.Date
	o.fieldMap["pay_at"] = o.PayAt
	o.fieldMap["created_at"] = o.CreatedAt
	o.fieldMap["channel_id"] = o.ChannelID
	o.fieldMap["user_created_at"] = o.UserCreatedAt
	o.fieldMap["flag"] = o.Flag
}

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

func (o order) replaceDB(db *gorm.DB) order {
	o.orderDo.ReplaceDB(db)
	return o
}

type orderDo struct{ gen.DO }

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

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

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

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

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

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

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

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

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

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

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

func (o orderDo) Exists(subquery interface{ UnderlyingDB() *gorm.DB }) *orderDo {
	return o.Where(field.CompareSubQuery(field.ExistsOp, nil, subquery.UnderlyingDB()))
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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