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

	"wechat-api/database/dao/wechat/model"
)

func newWxCardVisit(db *gorm.DB, opts ...gen.DOOption) wxCardVisit {
	_wxCardVisit := wxCardVisit{}

	_wxCardVisit.wxCardVisitDo.UseDB(db, opts...)
	_wxCardVisit.wxCardVisitDo.UseModel(&model.WxCardVisit{})

	tableName := _wxCardVisit.wxCardVisitDo.TableName()
	_wxCardVisit.ALL = field.NewAsterisk(tableName)
	_wxCardVisit.ID = field.NewInt64(tableName, "id")
	_wxCardVisit.CreatedAt = field.NewTime(tableName, "created_at")
	_wxCardVisit.UpdatedAt = field.NewTime(tableName, "updated_at")
	_wxCardVisit.DeletedAt = field.NewField(tableName, "deleted_at")
	_wxCardVisit.UserID = field.NewInt64(tableName, "user_id")
	_wxCardVisit.BotID = field.NewInt64(tableName, "bot_id")
	_wxCardVisit.BotType = field.NewInt64(tableName, "bot_type")

	_wxCardVisit.fillFieldMap()

	return _wxCardVisit
}

// wxCardVisit 名片访问记录表
type wxCardVisit struct {
	wxCardVisitDo

	ALL       field.Asterisk
	ID        field.Int64
	CreatedAt field.Time  // Create Time | 创建日期
	UpdatedAt field.Time  // Update Time | 修改日期
	DeletedAt field.Field // Delete Time | 删除日期
	UserID    field.Int64 // 访客 ID
	BotID     field.Int64 // 被访ID
	BotType   field.Int64 // 类型:1-微信 2-小程序 3-智能体

	fieldMap map[string]field.Expr
}

func (w wxCardVisit) Table(newTableName string) *wxCardVisit {
	w.wxCardVisitDo.UseTable(newTableName)
	return w.updateTableName(newTableName)
}

func (w wxCardVisit) As(alias string) *wxCardVisit {
	w.wxCardVisitDo.DO = *(w.wxCardVisitDo.As(alias).(*gen.DO))
	return w.updateTableName(alias)
}

func (w *wxCardVisit) updateTableName(table string) *wxCardVisit {
	w.ALL = field.NewAsterisk(table)
	w.ID = field.NewInt64(table, "id")
	w.CreatedAt = field.NewTime(table, "created_at")
	w.UpdatedAt = field.NewTime(table, "updated_at")
	w.DeletedAt = field.NewField(table, "deleted_at")
	w.UserID = field.NewInt64(table, "user_id")
	w.BotID = field.NewInt64(table, "bot_id")
	w.BotType = field.NewInt64(table, "bot_type")

	w.fillFieldMap()

	return w
}

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

func (w *wxCardVisit) fillFieldMap() {
	w.fieldMap = make(map[string]field.Expr, 7)
	w.fieldMap["id"] = w.ID
	w.fieldMap["created_at"] = w.CreatedAt
	w.fieldMap["updated_at"] = w.UpdatedAt
	w.fieldMap["deleted_at"] = w.DeletedAt
	w.fieldMap["user_id"] = w.UserID
	w.fieldMap["bot_id"] = w.BotID
	w.fieldMap["bot_type"] = w.BotType
}

func (w wxCardVisit) clone(db *gorm.DB) wxCardVisit {
	w.wxCardVisitDo.ReplaceConnPool(db.Statement.ConnPool)
	return w
}

func (w wxCardVisit) replaceDB(db *gorm.DB) wxCardVisit {
	w.wxCardVisitDo.ReplaceDB(db)
	return w
}

type wxCardVisitDo struct{ gen.DO }

type IWxCardVisitDo interface {
	gen.SubQuery
	Debug() IWxCardVisitDo
	WithContext(ctx context.Context) IWxCardVisitDo
	WithResult(fc func(tx gen.Dao)) gen.ResultInfo
	ReplaceDB(db *gorm.DB)
	ReadDB() IWxCardVisitDo
	WriteDB() IWxCardVisitDo
	As(alias string) gen.Dao
	Session(config *gorm.Session) IWxCardVisitDo
	Columns(cols ...field.Expr) gen.Columns
	Clauses(conds ...clause.Expression) IWxCardVisitDo
	Not(conds ...gen.Condition) IWxCardVisitDo
	Or(conds ...gen.Condition) IWxCardVisitDo
	Select(conds ...field.Expr) IWxCardVisitDo
	Where(conds ...gen.Condition) IWxCardVisitDo
	Order(conds ...field.Expr) IWxCardVisitDo
	Distinct(cols ...field.Expr) IWxCardVisitDo
	Omit(cols ...field.Expr) IWxCardVisitDo
	Join(table schema.Tabler, on ...field.Expr) IWxCardVisitDo
	LeftJoin(table schema.Tabler, on ...field.Expr) IWxCardVisitDo
	RightJoin(table schema.Tabler, on ...field.Expr) IWxCardVisitDo
	Group(cols ...field.Expr) IWxCardVisitDo
	Having(conds ...gen.Condition) IWxCardVisitDo
	Limit(limit int) IWxCardVisitDo
	Offset(offset int) IWxCardVisitDo
	Count() (count int64, err error)
	Scopes(funcs ...func(gen.Dao) gen.Dao) IWxCardVisitDo
	Unscoped() IWxCardVisitDo
	Create(values ...*model.WxCardVisit) error
	CreateInBatches(values []*model.WxCardVisit, batchSize int) error
	Save(values ...*model.WxCardVisit) error
	First() (*model.WxCardVisit, error)
	Take() (*model.WxCardVisit, error)
	Last() (*model.WxCardVisit, error)
	Find() ([]*model.WxCardVisit, error)
	FindInBatch(batchSize int, fc func(tx gen.Dao, batch int) error) (results []*model.WxCardVisit, err error)
	FindInBatches(result *[]*model.WxCardVisit, batchSize int, fc func(tx gen.Dao, batch int) error) error
	Pluck(column field.Expr, dest interface{}) error
	Delete(...*model.WxCardVisit) (info gen.ResultInfo, err error)
	Update(column field.Expr, value interface{}) (info gen.ResultInfo, err error)
	UpdateSimple(columns ...field.AssignExpr) (info gen.ResultInfo, err error)
	Updates(value interface{}) (info gen.ResultInfo, err error)
	UpdateColumn(column field.Expr, value interface{}) (info gen.ResultInfo, err error)
	UpdateColumnSimple(columns ...field.AssignExpr) (info gen.ResultInfo, err error)
	UpdateColumns(value interface{}) (info gen.ResultInfo, err error)
	UpdateFrom(q gen.SubQuery) gen.Dao
	Attrs(attrs ...field.AssignExpr) IWxCardVisitDo
	Assign(attrs ...field.AssignExpr) IWxCardVisitDo
	Joins(fields ...field.RelationField) IWxCardVisitDo
	Preload(fields ...field.RelationField) IWxCardVisitDo
	FirstOrInit() (*model.WxCardVisit, error)
	FirstOrCreate() (*model.WxCardVisit, error)
	FindByPage(offset int, limit int) (result []*model.WxCardVisit, count int64, err error)
	ScanByPage(result interface{}, offset int, limit int) (count int64, err error)
	Scan(result interface{}) (err error)
	Returning(value interface{}, columns ...string) IWxCardVisitDo
	UnderlyingDB() *gorm.DB
	schema.Tabler
}

func (w wxCardVisitDo) Debug() IWxCardVisitDo {
	return w.withDO(w.DO.Debug())
}

func (w wxCardVisitDo) WithContext(ctx context.Context) IWxCardVisitDo {
	return w.withDO(w.DO.WithContext(ctx))
}

func (w wxCardVisitDo) ReadDB() IWxCardVisitDo {
	return w.Clauses(dbresolver.Read)
}

func (w wxCardVisitDo) WriteDB() IWxCardVisitDo {
	return w.Clauses(dbresolver.Write)
}

func (w wxCardVisitDo) Session(config *gorm.Session) IWxCardVisitDo {
	return w.withDO(w.DO.Session(config))
}

func (w wxCardVisitDo) Clauses(conds ...clause.Expression) IWxCardVisitDo {
	return w.withDO(w.DO.Clauses(conds...))
}

func (w wxCardVisitDo) Returning(value interface{}, columns ...string) IWxCardVisitDo {
	return w.withDO(w.DO.Returning(value, columns...))
}

func (w wxCardVisitDo) Not(conds ...gen.Condition) IWxCardVisitDo {
	return w.withDO(w.DO.Not(conds...))
}

func (w wxCardVisitDo) Or(conds ...gen.Condition) IWxCardVisitDo {
	return w.withDO(w.DO.Or(conds...))
}

func (w wxCardVisitDo) Select(conds ...field.Expr) IWxCardVisitDo {
	return w.withDO(w.DO.Select(conds...))
}

func (w wxCardVisitDo) Where(conds ...gen.Condition) IWxCardVisitDo {
	return w.withDO(w.DO.Where(conds...))
}

func (w wxCardVisitDo) Order(conds ...field.Expr) IWxCardVisitDo {
	return w.withDO(w.DO.Order(conds...))
}

func (w wxCardVisitDo) Distinct(cols ...field.Expr) IWxCardVisitDo {
	return w.withDO(w.DO.Distinct(cols...))
}

func (w wxCardVisitDo) Omit(cols ...field.Expr) IWxCardVisitDo {
	return w.withDO(w.DO.Omit(cols...))
}

func (w wxCardVisitDo) Join(table schema.Tabler, on ...field.Expr) IWxCardVisitDo {
	return w.withDO(w.DO.Join(table, on...))
}

func (w wxCardVisitDo) LeftJoin(table schema.Tabler, on ...field.Expr) IWxCardVisitDo {
	return w.withDO(w.DO.LeftJoin(table, on...))
}

func (w wxCardVisitDo) RightJoin(table schema.Tabler, on ...field.Expr) IWxCardVisitDo {
	return w.withDO(w.DO.RightJoin(table, on...))
}

func (w wxCardVisitDo) Group(cols ...field.Expr) IWxCardVisitDo {
	return w.withDO(w.DO.Group(cols...))
}

func (w wxCardVisitDo) Having(conds ...gen.Condition) IWxCardVisitDo {
	return w.withDO(w.DO.Having(conds...))
}

func (w wxCardVisitDo) Limit(limit int) IWxCardVisitDo {
	return w.withDO(w.DO.Limit(limit))
}

func (w wxCardVisitDo) Offset(offset int) IWxCardVisitDo {
	return w.withDO(w.DO.Offset(offset))
}

func (w wxCardVisitDo) Scopes(funcs ...func(gen.Dao) gen.Dao) IWxCardVisitDo {
	return w.withDO(w.DO.Scopes(funcs...))
}

func (w wxCardVisitDo) Unscoped() IWxCardVisitDo {
	return w.withDO(w.DO.Unscoped())
}

func (w wxCardVisitDo) Create(values ...*model.WxCardVisit) error {
	if len(values) == 0 {
		return nil
	}
	return w.DO.Create(values)
}

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

func (w wxCardVisitDo) First() (*model.WxCardVisit, error) {
	if result, err := w.DO.First(); err != nil {
		return nil, err
	} else {
		return result.(*model.WxCardVisit), nil
	}
}

func (w wxCardVisitDo) Take() (*model.WxCardVisit, error) {
	if result, err := w.DO.Take(); err != nil {
		return nil, err
	} else {
		return result.(*model.WxCardVisit), nil
	}
}

func (w wxCardVisitDo) Last() (*model.WxCardVisit, error) {
	if result, err := w.DO.Last(); err != nil {
		return nil, err
	} else {
		return result.(*model.WxCardVisit), nil
	}
}

func (w wxCardVisitDo) Find() ([]*model.WxCardVisit, error) {
	result, err := w.DO.Find()
	return result.([]*model.WxCardVisit), err
}

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

func (w wxCardVisitDo) Attrs(attrs ...field.AssignExpr) IWxCardVisitDo {
	return w.withDO(w.DO.Attrs(attrs...))
}

func (w wxCardVisitDo) Assign(attrs ...field.AssignExpr) IWxCardVisitDo {
	return w.withDO(w.DO.Assign(attrs...))
}

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

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

func (w wxCardVisitDo) FirstOrInit() (*model.WxCardVisit, error) {
	if result, err := w.DO.FirstOrInit(); err != nil {
		return nil, err
	} else {
		return result.(*model.WxCardVisit), nil
	}
}

func (w wxCardVisitDo) FirstOrCreate() (*model.WxCardVisit, error) {
	if result, err := w.DO.FirstOrCreate(); err != nil {
		return nil, err
	} else {
		return result.(*model.WxCardVisit), nil
	}
}

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

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

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

func (w wxCardVisitDo) Scan(result interface{}) (err error) {
	return w.DO.Scan(result)
}

func (w wxCardVisitDo) Delete(models ...*model.WxCardVisit) (result gen.ResultInfo, err error) {
	return w.DO.Delete(models)
}

func (w *wxCardVisitDo) withDO(do gen.Dao) *wxCardVisitDo {
	w.DO = *do.(*gen.DO)
	return w
}
