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

	_goodsLog.goodsLogDo.UseDB(db, opts...)
	_goodsLog.goodsLogDo.UseModel(&model.GoodsLog{})

	tableName := _goodsLog.goodsLogDo.TableName()
	_goodsLog.ALL = field.NewAsterisk(tableName)
	_goodsLog.ID = field.NewInt64(tableName, "id")
	_goodsLog.UserID = field.NewInt64(tableName, "user_id")
	_goodsLog.ServerID = field.NewInt32(tableName, "server_id")
	_goodsLog.PositionID = field.NewInt32(tableName, "position_id")
	_goodsLog.GoodType = field.NewInt32(tableName, "good_type")
	_goodsLog.EventID = field.NewInt32(tableName, "event_id")
	_goodsLog.EventAt = field.NewInt32(tableName, "event_at")
	_goodsLog.UserCreatedAt = field.NewInt32(tableName, "user_created_at")
	_goodsLog.Extra = field.NewString(tableName, "extra")

	_goodsLog.fillFieldMap()

	return _goodsLog
}

type goodsLog struct {
	goodsLogDo

	ALL           field.Asterisk
	ID            field.Int64
	UserID        field.Int64
	ServerID      field.Int32  // 服务器ID
	PositionID    field.Int32  // 商品[位]id
	GoodType      field.Int32  // 商品 1 礼包 2 充值
	EventID       field.Int32  // 触发事件 1 展示 2 点击 3 支付成功
	EventAt       field.Int32  // 事件触发的时间
	UserCreatedAt field.Int32  // 用户创建时间
	Extra         field.String // 额外信息

	fieldMap map[string]field.Expr
}

func (g goodsLog) Table(newTableName string) *goodsLog {
	g.goodsLogDo.UseTable(newTableName)
	return g.updateTableName(newTableName)
}

func (g goodsLog) As(alias string) *goodsLog {
	g.goodsLogDo.DO = *(g.goodsLogDo.As(alias).(*gen.DO))
	return g.updateTableName(alias)
}

func (g *goodsLog) updateTableName(table string) *goodsLog {
	g.ALL = field.NewAsterisk(table)
	g.ID = field.NewInt64(table, "id")
	g.UserID = field.NewInt64(table, "user_id")
	g.ServerID = field.NewInt32(table, "server_id")
	g.PositionID = field.NewInt32(table, "position_id")
	g.GoodType = field.NewInt32(table, "good_type")
	g.EventID = field.NewInt32(table, "event_id")
	g.EventAt = field.NewInt32(table, "event_at")
	g.UserCreatedAt = field.NewInt32(table, "user_created_at")
	g.Extra = field.NewString(table, "extra")

	g.fillFieldMap()

	return g
}

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

func (g *goodsLog) fillFieldMap() {
	g.fieldMap = make(map[string]field.Expr, 9)
	g.fieldMap["id"] = g.ID
	g.fieldMap["user_id"] = g.UserID
	g.fieldMap["server_id"] = g.ServerID
	g.fieldMap["position_id"] = g.PositionID
	g.fieldMap["good_type"] = g.GoodType
	g.fieldMap["event_id"] = g.EventID
	g.fieldMap["event_at"] = g.EventAt
	g.fieldMap["user_created_at"] = g.UserCreatedAt
	g.fieldMap["extra"] = g.Extra
}

func (g goodsLog) clone(db *gorm.DB) goodsLog {
	g.goodsLogDo.ReplaceConnPool(db.Statement.ConnPool)
	return g
}

func (g goodsLog) replaceDB(db *gorm.DB) goodsLog {
	g.goodsLogDo.ReplaceDB(db)
	return g
}

type goodsLogDo struct{ gen.DO }

func (g goodsLogDo) Debug() *goodsLogDo {
	return g.withDO(g.DO.Debug())
}

func (g goodsLogDo) WithContext(ctx context.Context) *goodsLogDo {
	return g.withDO(g.DO.WithContext(ctx))
}

func (g goodsLogDo) ReadDB() *goodsLogDo {
	return g.Clauses(dbresolver.Read)
}

func (g goodsLogDo) WriteDB() *goodsLogDo {
	return g.Clauses(dbresolver.Write)
}

func (g goodsLogDo) Session(config *gorm.Session) *goodsLogDo {
	return g.withDO(g.DO.Session(config))
}

func (g goodsLogDo) Clauses(conds ...clause.Expression) *goodsLogDo {
	return g.withDO(g.DO.Clauses(conds...))
}

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

func (g goodsLogDo) Not(conds ...gen.Condition) *goodsLogDo {
	return g.withDO(g.DO.Not(conds...))
}

func (g goodsLogDo) Or(conds ...gen.Condition) *goodsLogDo {
	return g.withDO(g.DO.Or(conds...))
}

func (g goodsLogDo) Select(conds ...field.Expr) *goodsLogDo {
	return g.withDO(g.DO.Select(conds...))
}

func (g goodsLogDo) Where(conds ...gen.Condition) *goodsLogDo {
	return g.withDO(g.DO.Where(conds...))
}

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

func (g goodsLogDo) Order(conds ...field.Expr) *goodsLogDo {
	return g.withDO(g.DO.Order(conds...))
}

func (g goodsLogDo) Distinct(cols ...field.Expr) *goodsLogDo {
	return g.withDO(g.DO.Distinct(cols...))
}

func (g goodsLogDo) Omit(cols ...field.Expr) *goodsLogDo {
	return g.withDO(g.DO.Omit(cols...))
}

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

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

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

func (g goodsLogDo) Group(cols ...field.Expr) *goodsLogDo {
	return g.withDO(g.DO.Group(cols...))
}

func (g goodsLogDo) Having(conds ...gen.Condition) *goodsLogDo {
	return g.withDO(g.DO.Having(conds...))
}

func (g goodsLogDo) Limit(limit int) *goodsLogDo {
	return g.withDO(g.DO.Limit(limit))
}

func (g goodsLogDo) Offset(offset int) *goodsLogDo {
	return g.withDO(g.DO.Offset(offset))
}

func (g goodsLogDo) Scopes(funcs ...func(gen.Dao) gen.Dao) *goodsLogDo {
	return g.withDO(g.DO.Scopes(funcs...))
}

func (g goodsLogDo) Unscoped() *goodsLogDo {
	return g.withDO(g.DO.Unscoped())
}

func (g goodsLogDo) Create(values ...*model.GoodsLog) error {
	if len(values) == 0 {
		return nil
	}
	return g.DO.Create(values)
}

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

func (g goodsLogDo) First() (*model.GoodsLog, error) {
	if result, err := g.DO.First(); err != nil {
		return nil, err
	} else {
		return result.(*model.GoodsLog), nil
	}
}

func (g goodsLogDo) Take() (*model.GoodsLog, error) {
	if result, err := g.DO.Take(); err != nil {
		return nil, err
	} else {
		return result.(*model.GoodsLog), nil
	}
}

func (g goodsLogDo) Last() (*model.GoodsLog, error) {
	if result, err := g.DO.Last(); err != nil {
		return nil, err
	} else {
		return result.(*model.GoodsLog), nil
	}
}

func (g goodsLogDo) Find() ([]*model.GoodsLog, error) {
	result, err := g.DO.Find()
	return result.([]*model.GoodsLog), err
}

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

func (g goodsLogDo) Attrs(attrs ...field.AssignExpr) *goodsLogDo {
	return g.withDO(g.DO.Attrs(attrs...))
}

func (g goodsLogDo) Assign(attrs ...field.AssignExpr) *goodsLogDo {
	return g.withDO(g.DO.Assign(attrs...))
}

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

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

func (g goodsLogDo) FirstOrInit() (*model.GoodsLog, error) {
	if result, err := g.DO.FirstOrInit(); err != nil {
		return nil, err
	} else {
		return result.(*model.GoodsLog), nil
	}
}

func (g goodsLogDo) FirstOrCreate() (*model.GoodsLog, error) {
	if result, err := g.DO.FirstOrCreate(); err != nil {
		return nil, err
	} else {
		return result.(*model.GoodsLog), nil
	}
}

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

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

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

func (g goodsLogDo) Scan(result interface{}) (err error) {
	return g.DO.Scan(result)
}

func (g goodsLogDo) Delete(models ...*model.GoodsLog) (result gen.ResultInfo, err error) {
	return g.DO.Delete(models)
}

func (g *goodsLogDo) withDO(do gen.Dao) *goodsLogDo {
	g.DO = *do.(*gen.DO)
	return g
}
