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

	_xunjiContact.xunjiContactDo.UseDB(db, opts...)
	_xunjiContact.xunjiContactDo.UseModel(&model.XunjiContact{})

	tableName := _xunjiContact.xunjiContactDo.TableName()
	_xunjiContact.ALL = field.NewAsterisk(tableName)
	_xunjiContact.ID = field.NewInt64(tableName, "id")
	_xunjiContact.CreatedAt = field.NewTime(tableName, "created_at")
	_xunjiContact.UpdatedAt = field.NewTime(tableName, "updated_at")
	_xunjiContact.Status = field.NewInt64(tableName, "status")
	_xunjiContact.DeletedAt = field.NewField(tableName, "deleted_at")
	_xunjiContact.RobotID = field.NewString(tableName, "robot_id")
	_xunjiContact.AccountID = field.NewString(tableName, "account_id")
	_xunjiContact.Nickname = field.NewString(tableName, "nickname")
	_xunjiContact.Avatar = field.NewString(tableName, "avatar")

	_xunjiContact.fillFieldMap()

	return _xunjiContact
}

type xunjiContact struct {
	xunjiContactDo

	ALL       field.Asterisk
	ID        field.Int64
	CreatedAt field.Time   // Create Time | 创建日期
	UpdatedAt field.Time   // Update Time | 修改日期
	Status    field.Int64  // Status 1: normal 2: ban | 状态 1 正常 2 禁用
	DeletedAt field.Field  // Delete Time | 删除日期
	RobotID   field.String // 机器人ID
	AccountID field.String // 客户ID
	Nickname  field.String // 昵称
	Avatar    field.String // 头像

	fieldMap map[string]field.Expr
}

func (x xunjiContact) Table(newTableName string) *xunjiContact {
	x.xunjiContactDo.UseTable(newTableName)
	return x.updateTableName(newTableName)
}

func (x xunjiContact) As(alias string) *xunjiContact {
	x.xunjiContactDo.DO = *(x.xunjiContactDo.As(alias).(*gen.DO))
	return x.updateTableName(alias)
}

func (x *xunjiContact) updateTableName(table string) *xunjiContact {
	x.ALL = field.NewAsterisk(table)
	x.ID = field.NewInt64(table, "id")
	x.CreatedAt = field.NewTime(table, "created_at")
	x.UpdatedAt = field.NewTime(table, "updated_at")
	x.Status = field.NewInt64(table, "status")
	x.DeletedAt = field.NewField(table, "deleted_at")
	x.RobotID = field.NewString(table, "robot_id")
	x.AccountID = field.NewString(table, "account_id")
	x.Nickname = field.NewString(table, "nickname")
	x.Avatar = field.NewString(table, "avatar")

	x.fillFieldMap()

	return x
}

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

func (x *xunjiContact) fillFieldMap() {
	x.fieldMap = make(map[string]field.Expr, 9)
	x.fieldMap["id"] = x.ID
	x.fieldMap["created_at"] = x.CreatedAt
	x.fieldMap["updated_at"] = x.UpdatedAt
	x.fieldMap["status"] = x.Status
	x.fieldMap["deleted_at"] = x.DeletedAt
	x.fieldMap["robot_id"] = x.RobotID
	x.fieldMap["account_id"] = x.AccountID
	x.fieldMap["nickname"] = x.Nickname
	x.fieldMap["avatar"] = x.Avatar
}

func (x xunjiContact) clone(db *gorm.DB) xunjiContact {
	x.xunjiContactDo.ReplaceConnPool(db.Statement.ConnPool)
	return x
}

func (x xunjiContact) replaceDB(db *gorm.DB) xunjiContact {
	x.xunjiContactDo.ReplaceDB(db)
	return x
}

type xunjiContactDo struct{ gen.DO }

type IXunjiContactDo interface {
	gen.SubQuery
	Debug() IXunjiContactDo
	WithContext(ctx context.Context) IXunjiContactDo
	WithResult(fc func(tx gen.Dao)) gen.ResultInfo
	ReplaceDB(db *gorm.DB)
	ReadDB() IXunjiContactDo
	WriteDB() IXunjiContactDo
	As(alias string) gen.Dao
	Session(config *gorm.Session) IXunjiContactDo
	Columns(cols ...field.Expr) gen.Columns
	Clauses(conds ...clause.Expression) IXunjiContactDo
	Not(conds ...gen.Condition) IXunjiContactDo
	Or(conds ...gen.Condition) IXunjiContactDo
	Select(conds ...field.Expr) IXunjiContactDo
	Where(conds ...gen.Condition) IXunjiContactDo
	Order(conds ...field.Expr) IXunjiContactDo
	Distinct(cols ...field.Expr) IXunjiContactDo
	Omit(cols ...field.Expr) IXunjiContactDo
	Join(table schema.Tabler, on ...field.Expr) IXunjiContactDo
	LeftJoin(table schema.Tabler, on ...field.Expr) IXunjiContactDo
	RightJoin(table schema.Tabler, on ...field.Expr) IXunjiContactDo
	Group(cols ...field.Expr) IXunjiContactDo
	Having(conds ...gen.Condition) IXunjiContactDo
	Limit(limit int) IXunjiContactDo
	Offset(offset int) IXunjiContactDo
	Count() (count int64, err error)
	Scopes(funcs ...func(gen.Dao) gen.Dao) IXunjiContactDo
	Unscoped() IXunjiContactDo
	Create(values ...*model.XunjiContact) error
	CreateInBatches(values []*model.XunjiContact, batchSize int) error
	Save(values ...*model.XunjiContact) error
	First() (*model.XunjiContact, error)
	Take() (*model.XunjiContact, error)
	Last() (*model.XunjiContact, error)
	Find() ([]*model.XunjiContact, error)
	FindInBatch(batchSize int, fc func(tx gen.Dao, batch int) error) (results []*model.XunjiContact, err error)
	FindInBatches(result *[]*model.XunjiContact, batchSize int, fc func(tx gen.Dao, batch int) error) error
	Pluck(column field.Expr, dest interface{}) error
	Delete(...*model.XunjiContact) (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) IXunjiContactDo
	Assign(attrs ...field.AssignExpr) IXunjiContactDo
	Joins(fields ...field.RelationField) IXunjiContactDo
	Preload(fields ...field.RelationField) IXunjiContactDo
	FirstOrInit() (*model.XunjiContact, error)
	FirstOrCreate() (*model.XunjiContact, error)
	FindByPage(offset int, limit int) (result []*model.XunjiContact, 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) IXunjiContactDo
	UnderlyingDB() *gorm.DB
	schema.Tabler
}

func (x xunjiContactDo) Debug() IXunjiContactDo {
	return x.withDO(x.DO.Debug())
}

func (x xunjiContactDo) WithContext(ctx context.Context) IXunjiContactDo {
	return x.withDO(x.DO.WithContext(ctx))
}

func (x xunjiContactDo) ReadDB() IXunjiContactDo {
	return x.Clauses(dbresolver.Read)
}

func (x xunjiContactDo) WriteDB() IXunjiContactDo {
	return x.Clauses(dbresolver.Write)
}

func (x xunjiContactDo) Session(config *gorm.Session) IXunjiContactDo {
	return x.withDO(x.DO.Session(config))
}

func (x xunjiContactDo) Clauses(conds ...clause.Expression) IXunjiContactDo {
	return x.withDO(x.DO.Clauses(conds...))
}

func (x xunjiContactDo) Returning(value interface{}, columns ...string) IXunjiContactDo {
	return x.withDO(x.DO.Returning(value, columns...))
}

func (x xunjiContactDo) Not(conds ...gen.Condition) IXunjiContactDo {
	return x.withDO(x.DO.Not(conds...))
}

func (x xunjiContactDo) Or(conds ...gen.Condition) IXunjiContactDo {
	return x.withDO(x.DO.Or(conds...))
}

func (x xunjiContactDo) Select(conds ...field.Expr) IXunjiContactDo {
	return x.withDO(x.DO.Select(conds...))
}

func (x xunjiContactDo) Where(conds ...gen.Condition) IXunjiContactDo {
	return x.withDO(x.DO.Where(conds...))
}

func (x xunjiContactDo) Order(conds ...field.Expr) IXunjiContactDo {
	return x.withDO(x.DO.Order(conds...))
}

func (x xunjiContactDo) Distinct(cols ...field.Expr) IXunjiContactDo {
	return x.withDO(x.DO.Distinct(cols...))
}

func (x xunjiContactDo) Omit(cols ...field.Expr) IXunjiContactDo {
	return x.withDO(x.DO.Omit(cols...))
}

func (x xunjiContactDo) Join(table schema.Tabler, on ...field.Expr) IXunjiContactDo {
	return x.withDO(x.DO.Join(table, on...))
}

func (x xunjiContactDo) LeftJoin(table schema.Tabler, on ...field.Expr) IXunjiContactDo {
	return x.withDO(x.DO.LeftJoin(table, on...))
}

func (x xunjiContactDo) RightJoin(table schema.Tabler, on ...field.Expr) IXunjiContactDo {
	return x.withDO(x.DO.RightJoin(table, on...))
}

func (x xunjiContactDo) Group(cols ...field.Expr) IXunjiContactDo {
	return x.withDO(x.DO.Group(cols...))
}

func (x xunjiContactDo) Having(conds ...gen.Condition) IXunjiContactDo {
	return x.withDO(x.DO.Having(conds...))
}

func (x xunjiContactDo) Limit(limit int) IXunjiContactDo {
	return x.withDO(x.DO.Limit(limit))
}

func (x xunjiContactDo) Offset(offset int) IXunjiContactDo {
	return x.withDO(x.DO.Offset(offset))
}

func (x xunjiContactDo) Scopes(funcs ...func(gen.Dao) gen.Dao) IXunjiContactDo {
	return x.withDO(x.DO.Scopes(funcs...))
}

func (x xunjiContactDo) Unscoped() IXunjiContactDo {
	return x.withDO(x.DO.Unscoped())
}

func (x xunjiContactDo) Create(values ...*model.XunjiContact) error {
	if len(values) == 0 {
		return nil
	}
	return x.DO.Create(values)
}

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

func (x xunjiContactDo) First() (*model.XunjiContact, error) {
	if result, err := x.DO.First(); err != nil {
		return nil, err
	} else {
		return result.(*model.XunjiContact), nil
	}
}

func (x xunjiContactDo) Take() (*model.XunjiContact, error) {
	if result, err := x.DO.Take(); err != nil {
		return nil, err
	} else {
		return result.(*model.XunjiContact), nil
	}
}

func (x xunjiContactDo) Last() (*model.XunjiContact, error) {
	if result, err := x.DO.Last(); err != nil {
		return nil, err
	} else {
		return result.(*model.XunjiContact), nil
	}
}

func (x xunjiContactDo) Find() ([]*model.XunjiContact, error) {
	result, err := x.DO.Find()
	return result.([]*model.XunjiContact), err
}

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

func (x xunjiContactDo) Attrs(attrs ...field.AssignExpr) IXunjiContactDo {
	return x.withDO(x.DO.Attrs(attrs...))
}

func (x xunjiContactDo) Assign(attrs ...field.AssignExpr) IXunjiContactDo {
	return x.withDO(x.DO.Assign(attrs...))
}

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

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

func (x xunjiContactDo) FirstOrInit() (*model.XunjiContact, error) {
	if result, err := x.DO.FirstOrInit(); err != nil {
		return nil, err
	} else {
		return result.(*model.XunjiContact), nil
	}
}

func (x xunjiContactDo) FirstOrCreate() (*model.XunjiContact, error) {
	if result, err := x.DO.FirstOrCreate(); err != nil {
		return nil, err
	} else {
		return result.(*model.XunjiContact), nil
	}
}

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

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

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

func (x xunjiContactDo) Scan(result interface{}) (err error) {
	return x.DO.Scan(result)
}

func (x xunjiContactDo) Delete(models ...*model.XunjiContact) (result gen.ResultInfo, err error) {
	return x.DO.Delete(models)
}

func (x *xunjiContactDo) withDO(do gen.Dao) *xunjiContactDo {
	x.DO = *do.(*gen.DO)
	return x
}
