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

	_wpChatroom.wpChatroomDo.UseDB(db, opts...)
	_wpChatroom.wpChatroomDo.UseModel(&model.WpChatroom{})

	tableName := _wpChatroom.wpChatroomDo.TableName()
	_wpChatroom.ALL = field.NewAsterisk(tableName)
	_wpChatroom.ID = field.NewInt64(tableName, "id")
	_wpChatroom.CreatedAt = field.NewTime(tableName, "created_at")
	_wpChatroom.UpdatedAt = field.NewTime(tableName, "updated_at")
	_wpChatroom.Status = field.NewInt64(tableName, "status")
	_wpChatroom.DeletedAt = field.NewField(tableName, "deleted_at")
	_wpChatroom.WxWxid = field.NewString(tableName, "wx_wxid")
	_wpChatroom.ChatroomID = field.NewString(tableName, "chatroom_id")
	_wpChatroom.Nickname = field.NewString(tableName, "nickname")
	_wpChatroom.Owner = field.NewString(tableName, "owner")
	_wpChatroom.Avatar = field.NewString(tableName, "avatar")
	_wpChatroom.MemberList = field.NewString(tableName, "member_list")
	_wpChatroom.ShowNameList = field.NewString(tableName, "show_name_list")

	_wpChatroom.fillFieldMap()

	return _wpChatroom
}

type wpChatroom struct {
	wpChatroomDo

	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 | 删除日期
	WxWxid       field.String // 属主微信id
	ChatroomID   field.String // 微信id 公众号微信ID
	Nickname     field.String // 微信昵称 群备注名称
	Owner        field.String // 群主微信id
	Avatar       field.String // 头像
	MemberList   field.String
	ShowNameList field.String

	fieldMap map[string]field.Expr
}

func (w wpChatroom) Table(newTableName string) *wpChatroom {
	w.wpChatroomDo.UseTable(newTableName)
	return w.updateTableName(newTableName)
}

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

func (w *wpChatroom) updateTableName(table string) *wpChatroom {
	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.Status = field.NewInt64(table, "status")
	w.DeletedAt = field.NewField(table, "deleted_at")
	w.WxWxid = field.NewString(table, "wx_wxid")
	w.ChatroomID = field.NewString(table, "chatroom_id")
	w.Nickname = field.NewString(table, "nickname")
	w.Owner = field.NewString(table, "owner")
	w.Avatar = field.NewString(table, "avatar")
	w.MemberList = field.NewString(table, "member_list")
	w.ShowNameList = field.NewString(table, "show_name_list")

	w.fillFieldMap()

	return w
}

func (w *wpChatroom) 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 *wpChatroom) fillFieldMap() {
	w.fieldMap = make(map[string]field.Expr, 12)
	w.fieldMap["id"] = w.ID
	w.fieldMap["created_at"] = w.CreatedAt
	w.fieldMap["updated_at"] = w.UpdatedAt
	w.fieldMap["status"] = w.Status
	w.fieldMap["deleted_at"] = w.DeletedAt
	w.fieldMap["wx_wxid"] = w.WxWxid
	w.fieldMap["chatroom_id"] = w.ChatroomID
	w.fieldMap["nickname"] = w.Nickname
	w.fieldMap["owner"] = w.Owner
	w.fieldMap["avatar"] = w.Avatar
	w.fieldMap["member_list"] = w.MemberList
	w.fieldMap["show_name_list"] = w.ShowNameList
}

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

func (w wpChatroom) replaceDB(db *gorm.DB) wpChatroom {
	w.wpChatroomDo.ReplaceDB(db)
	return w
}

type wpChatroomDo struct{ gen.DO }

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

func (w wpChatroomDo) Debug() IWpChatroomDo {
	return w.withDO(w.DO.Debug())
}

func (w wpChatroomDo) WithContext(ctx context.Context) IWpChatroomDo {
	return w.withDO(w.DO.WithContext(ctx))
}

func (w wpChatroomDo) ReadDB() IWpChatroomDo {
	return w.Clauses(dbresolver.Read)
}

func (w wpChatroomDo) WriteDB() IWpChatroomDo {
	return w.Clauses(dbresolver.Write)
}

func (w wpChatroomDo) Session(config *gorm.Session) IWpChatroomDo {
	return w.withDO(w.DO.Session(config))
}

func (w wpChatroomDo) Clauses(conds ...clause.Expression) IWpChatroomDo {
	return w.withDO(w.DO.Clauses(conds...))
}

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

func (w wpChatroomDo) Not(conds ...gen.Condition) IWpChatroomDo {
	return w.withDO(w.DO.Not(conds...))
}

func (w wpChatroomDo) Or(conds ...gen.Condition) IWpChatroomDo {
	return w.withDO(w.DO.Or(conds...))
}

func (w wpChatroomDo) Select(conds ...field.Expr) IWpChatroomDo {
	return w.withDO(w.DO.Select(conds...))
}

func (w wpChatroomDo) Where(conds ...gen.Condition) IWpChatroomDo {
	return w.withDO(w.DO.Where(conds...))
}

func (w wpChatroomDo) Order(conds ...field.Expr) IWpChatroomDo {
	return w.withDO(w.DO.Order(conds...))
}

func (w wpChatroomDo) Distinct(cols ...field.Expr) IWpChatroomDo {
	return w.withDO(w.DO.Distinct(cols...))
}

func (w wpChatroomDo) Omit(cols ...field.Expr) IWpChatroomDo {
	return w.withDO(w.DO.Omit(cols...))
}

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

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

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

func (w wpChatroomDo) Group(cols ...field.Expr) IWpChatroomDo {
	return w.withDO(w.DO.Group(cols...))
}

func (w wpChatroomDo) Having(conds ...gen.Condition) IWpChatroomDo {
	return w.withDO(w.DO.Having(conds...))
}

func (w wpChatroomDo) Limit(limit int) IWpChatroomDo {
	return w.withDO(w.DO.Limit(limit))
}

func (w wpChatroomDo) Offset(offset int) IWpChatroomDo {
	return w.withDO(w.DO.Offset(offset))
}

func (w wpChatroomDo) Scopes(funcs ...func(gen.Dao) gen.Dao) IWpChatroomDo {
	return w.withDO(w.DO.Scopes(funcs...))
}

func (w wpChatroomDo) Unscoped() IWpChatroomDo {
	return w.withDO(w.DO.Unscoped())
}

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

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

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

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

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

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

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

func (w wpChatroomDo) Attrs(attrs ...field.AssignExpr) IWpChatroomDo {
	return w.withDO(w.DO.Attrs(attrs...))
}

func (w wpChatroomDo) Assign(attrs ...field.AssignExpr) IWpChatroomDo {
	return w.withDO(w.DO.Assign(attrs...))
}

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

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

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

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

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

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

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