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

	_whatsapp.whatsappDo.UseDB(db, opts...)
	_whatsapp.whatsappDo.UseModel(&model.Whatsapp{})

	tableName := _whatsapp.whatsappDo.TableName()
	_whatsapp.ALL = field.NewAsterisk(tableName)
	_whatsapp.ID = field.NewInt64(tableName, "id")
	_whatsapp.CreatedAt = field.NewTime(tableName, "created_at")
	_whatsapp.UpdatedAt = field.NewTime(tableName, "updated_at")
	_whatsapp.Status = field.NewInt64(tableName, "status")
	_whatsapp.DeletedAt = field.NewField(tableName, "deleted_at")
	_whatsapp.WaID = field.NewString(tableName, "wa_id")
	_whatsapp.WaName = field.NewString(tableName, "wa_name")
	_whatsapp.Callback = field.NewString(tableName, "callback")
	_whatsapp.AgentID = field.NewInt64(tableName, "agent_id")
	_whatsapp.Account = field.NewString(tableName, "account")
	_whatsapp.Cc = field.NewString(tableName, "cc")
	_whatsapp.Phone = field.NewString(tableName, "phone")
	_whatsapp.CcPhone = field.NewString(tableName, "cc_phone")
	_whatsapp.PhoneName = field.NewString(tableName, "phone_name")
	_whatsapp.PhoneStatus = field.NewInt64(tableName, "phone_status")
	_whatsapp.APIBase = field.NewString(tableName, "api_base")
	_whatsapp.APIKey = field.NewString(tableName, "api_key")
	_whatsapp.AllowList = field.NewString(tableName, "allow_list")
	_whatsapp.GroupAllowList = field.NewString(tableName, "group_allow_list")
	_whatsapp.BlockList = field.NewString(tableName, "block_list")
	_whatsapp.GroupBlockList = field.NewString(tableName, "group_block_list")
	_whatsapp.OrganizationID = field.NewInt64(tableName, "organization_id")

	_whatsapp.fillFieldMap()

	return _whatsapp
}

// whatsapp whatsapp手机号表
type whatsapp struct {
	whatsappDo

	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 | 删除日期
	WaID           field.String // wa通道ID
	WaName         field.String // 通道名
	Callback       field.String // 回调地址
	AgentID        field.Int64  // AI角色ID
	Account        field.String // 账户
	Cc             field.String // 国家区号
	Phone          field.String // 手机号
	CcPhone        field.String // 国家区号 + 手机号
	PhoneName      field.String // 号码名称
	PhoneStatus    field.Int64  // 号码状态
	APIBase        field.String
	APIKey         field.String
	AllowList      field.String // 白名单，以数组存储wxid。当包含 ALL 字符串时为全部允许
	GroupAllowList field.String // 群白名单，以数组存储wxid。当包含 ALL 字符串时为全部允许
	BlockList      field.String // 黑名单，以数组存储wxid。当包含 ALL 字符串时为全部拒绝
	GroupBlockList field.String // 群黑名单，以数组存储wxid。当包含 ALL 字符串时为全部拒绝
	OrganizationID field.Int64  // 机构ID

	fieldMap map[string]field.Expr
}

func (w whatsapp) Table(newTableName string) *whatsapp {
	w.whatsappDo.UseTable(newTableName)
	return w.updateTableName(newTableName)
}

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

func (w *whatsapp) updateTableName(table string) *whatsapp {
	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.WaID = field.NewString(table, "wa_id")
	w.WaName = field.NewString(table, "wa_name")
	w.Callback = field.NewString(table, "callback")
	w.AgentID = field.NewInt64(table, "agent_id")
	w.Account = field.NewString(table, "account")
	w.Cc = field.NewString(table, "cc")
	w.Phone = field.NewString(table, "phone")
	w.CcPhone = field.NewString(table, "cc_phone")
	w.PhoneName = field.NewString(table, "phone_name")
	w.PhoneStatus = field.NewInt64(table, "phone_status")
	w.APIBase = field.NewString(table, "api_base")
	w.APIKey = field.NewString(table, "api_key")
	w.AllowList = field.NewString(table, "allow_list")
	w.GroupAllowList = field.NewString(table, "group_allow_list")
	w.BlockList = field.NewString(table, "block_list")
	w.GroupBlockList = field.NewString(table, "group_block_list")
	w.OrganizationID = field.NewInt64(table, "organization_id")

	w.fillFieldMap()

	return w
}

func (w *whatsapp) 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 *whatsapp) fillFieldMap() {
	w.fieldMap = make(map[string]field.Expr, 22)
	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["wa_id"] = w.WaID
	w.fieldMap["wa_name"] = w.WaName
	w.fieldMap["callback"] = w.Callback
	w.fieldMap["agent_id"] = w.AgentID
	w.fieldMap["account"] = w.Account
	w.fieldMap["cc"] = w.Cc
	w.fieldMap["phone"] = w.Phone
	w.fieldMap["cc_phone"] = w.CcPhone
	w.fieldMap["phone_name"] = w.PhoneName
	w.fieldMap["phone_status"] = w.PhoneStatus
	w.fieldMap["api_base"] = w.APIBase
	w.fieldMap["api_key"] = w.APIKey
	w.fieldMap["allow_list"] = w.AllowList
	w.fieldMap["group_allow_list"] = w.GroupAllowList
	w.fieldMap["block_list"] = w.BlockList
	w.fieldMap["group_block_list"] = w.GroupBlockList
	w.fieldMap["organization_id"] = w.OrganizationID
}

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

func (w whatsapp) replaceDB(db *gorm.DB) whatsapp {
	w.whatsappDo.ReplaceDB(db)
	return w
}

type whatsappDo struct{ gen.DO }

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

func (w whatsappDo) Debug() IWhatsappDo {
	return w.withDO(w.DO.Debug())
}

func (w whatsappDo) WithContext(ctx context.Context) IWhatsappDo {
	return w.withDO(w.DO.WithContext(ctx))
}

func (w whatsappDo) ReadDB() IWhatsappDo {
	return w.Clauses(dbresolver.Read)
}

func (w whatsappDo) WriteDB() IWhatsappDo {
	return w.Clauses(dbresolver.Write)
}

func (w whatsappDo) Session(config *gorm.Session) IWhatsappDo {
	return w.withDO(w.DO.Session(config))
}

func (w whatsappDo) Clauses(conds ...clause.Expression) IWhatsappDo {
	return w.withDO(w.DO.Clauses(conds...))
}

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

func (w whatsappDo) Not(conds ...gen.Condition) IWhatsappDo {
	return w.withDO(w.DO.Not(conds...))
}

func (w whatsappDo) Or(conds ...gen.Condition) IWhatsappDo {
	return w.withDO(w.DO.Or(conds...))
}

func (w whatsappDo) Select(conds ...field.Expr) IWhatsappDo {
	return w.withDO(w.DO.Select(conds...))
}

func (w whatsappDo) Where(conds ...gen.Condition) IWhatsappDo {
	return w.withDO(w.DO.Where(conds...))
}

func (w whatsappDo) Order(conds ...field.Expr) IWhatsappDo {
	return w.withDO(w.DO.Order(conds...))
}

func (w whatsappDo) Distinct(cols ...field.Expr) IWhatsappDo {
	return w.withDO(w.DO.Distinct(cols...))
}

func (w whatsappDo) Omit(cols ...field.Expr) IWhatsappDo {
	return w.withDO(w.DO.Omit(cols...))
}

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

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

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

func (w whatsappDo) Group(cols ...field.Expr) IWhatsappDo {
	return w.withDO(w.DO.Group(cols...))
}

func (w whatsappDo) Having(conds ...gen.Condition) IWhatsappDo {
	return w.withDO(w.DO.Having(conds...))
}

func (w whatsappDo) Limit(limit int) IWhatsappDo {
	return w.withDO(w.DO.Limit(limit))
}

func (w whatsappDo) Offset(offset int) IWhatsappDo {
	return w.withDO(w.DO.Offset(offset))
}

func (w whatsappDo) Scopes(funcs ...func(gen.Dao) gen.Dao) IWhatsappDo {
	return w.withDO(w.DO.Scopes(funcs...))
}

func (w whatsappDo) Unscoped() IWhatsappDo {
	return w.withDO(w.DO.Unscoped())
}

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

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

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

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

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

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

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

func (w whatsappDo) Attrs(attrs ...field.AssignExpr) IWhatsappDo {
	return w.withDO(w.DO.Attrs(attrs...))
}

func (w whatsappDo) Assign(attrs ...field.AssignExpr) IWhatsappDo {
	return w.withDO(w.DO.Assign(attrs...))
}

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

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

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

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

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

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

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