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

	_wx.wxDo.UseDB(db, opts...)
	_wx.wxDo.UseModel(&model.Wx{})

	tableName := _wx.wxDo.TableName()
	_wx.ALL = field.NewAsterisk(tableName)
	_wx.ID = field.NewInt64(tableName, "id")
	_wx.CreatedAt = field.NewTime(tableName, "created_at")
	_wx.UpdatedAt = field.NewTime(tableName, "updated_at")
	_wx.Status = field.NewInt64(tableName, "status")
	_wx.DeletedAt = field.NewField(tableName, "deleted_at")
	_wx.Port = field.NewString(tableName, "port")
	_wx.ProcessID = field.NewString(tableName, "process_id")
	_wx.Callback = field.NewString(tableName, "callback")
	_wx.Wxid = field.NewString(tableName, "wxid")
	_wx.Account = field.NewString(tableName, "account")
	_wx.Nickname = field.NewString(tableName, "nickname")
	_wx.Tel = field.NewString(tableName, "tel")
	_wx.HeadBig = field.NewString(tableName, "head_big")
	_wx.ServerID = field.NewInt64(tableName, "server_id")
	_wx.OrganizationID = field.NewInt64(tableName, "organization_id")
	_wx.AgentID = field.NewInt64(tableName, "agent_id")
	_wx.APIBase = field.NewString(tableName, "api_base")
	_wx.APIKey = field.NewString(tableName, "api_key")
	_wx.AllowList = field.NewString(tableName, "allow_list")
	_wx.GroupAllowList = field.NewString(tableName, "group_allow_list")
	_wx.BlockList = field.NewString(tableName, "block_list")
	_wx.GroupBlockList = field.NewString(tableName, "group_block_list")
	_wx.Ctype = field.NewInt64(tableName, "ctype")

	_wx.fillFieldMap()

	return _wx
}

type wx struct {
	wxDo

	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 | 删除日期
	Port           field.String // 端口号
	ProcessID      field.String // 进程号
	Callback       field.String // 回调地址
	Wxid           field.String // 微信id
	Account        field.String // 微信账号
	Nickname       field.String // 微信昵称
	Tel            field.String // 手机号
	HeadBig        field.String // 微信头像
	ServerID       field.Int64  // 服务器id
	OrganizationID field.Int64  // 机构 ID
	AgentID        field.Int64  // 0 fastgpt
	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 字符串时为全部拒绝
	Ctype          field.Int64  // 内容类型：1-个微 3-企微

	fieldMap map[string]field.Expr
}

func (w wx) Table(newTableName string) *wx {
	w.wxDo.UseTable(newTableName)
	return w.updateTableName(newTableName)
}

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

func (w *wx) updateTableName(table string) *wx {
	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.Port = field.NewString(table, "port")
	w.ProcessID = field.NewString(table, "process_id")
	w.Callback = field.NewString(table, "callback")
	w.Wxid = field.NewString(table, "wxid")
	w.Account = field.NewString(table, "account")
	w.Nickname = field.NewString(table, "nickname")
	w.Tel = field.NewString(table, "tel")
	w.HeadBig = field.NewString(table, "head_big")
	w.ServerID = field.NewInt64(table, "server_id")
	w.OrganizationID = field.NewInt64(table, "organization_id")
	w.AgentID = field.NewInt64(table, "agent_id")
	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.Ctype = field.NewInt64(table, "ctype")

	w.fillFieldMap()

	return w
}

func (w *wx) 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 *wx) fillFieldMap() {
	w.fieldMap = make(map[string]field.Expr, 23)
	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["port"] = w.Port
	w.fieldMap["process_id"] = w.ProcessID
	w.fieldMap["callback"] = w.Callback
	w.fieldMap["wxid"] = w.Wxid
	w.fieldMap["account"] = w.Account
	w.fieldMap["nickname"] = w.Nickname
	w.fieldMap["tel"] = w.Tel
	w.fieldMap["head_big"] = w.HeadBig
	w.fieldMap["server_id"] = w.ServerID
	w.fieldMap["organization_id"] = w.OrganizationID
	w.fieldMap["agent_id"] = w.AgentID
	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["ctype"] = w.Ctype
}

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

func (w wx) replaceDB(db *gorm.DB) wx {
	w.wxDo.ReplaceDB(db)
	return w
}

type wxDo struct{ gen.DO }

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

func (w wxDo) Debug() IWxDo {
	return w.withDO(w.DO.Debug())
}

func (w wxDo) WithContext(ctx context.Context) IWxDo {
	return w.withDO(w.DO.WithContext(ctx))
}

func (w wxDo) ReadDB() IWxDo {
	return w.Clauses(dbresolver.Read)
}

func (w wxDo) WriteDB() IWxDo {
	return w.Clauses(dbresolver.Write)
}

func (w wxDo) Session(config *gorm.Session) IWxDo {
	return w.withDO(w.DO.Session(config))
}

func (w wxDo) Clauses(conds ...clause.Expression) IWxDo {
	return w.withDO(w.DO.Clauses(conds...))
}

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

func (w wxDo) Not(conds ...gen.Condition) IWxDo {
	return w.withDO(w.DO.Not(conds...))
}

func (w wxDo) Or(conds ...gen.Condition) IWxDo {
	return w.withDO(w.DO.Or(conds...))
}

func (w wxDo) Select(conds ...field.Expr) IWxDo {
	return w.withDO(w.DO.Select(conds...))
}

func (w wxDo) Where(conds ...gen.Condition) IWxDo {
	return w.withDO(w.DO.Where(conds...))
}

func (w wxDo) Order(conds ...field.Expr) IWxDo {
	return w.withDO(w.DO.Order(conds...))
}

func (w wxDo) Distinct(cols ...field.Expr) IWxDo {
	return w.withDO(w.DO.Distinct(cols...))
}

func (w wxDo) Omit(cols ...field.Expr) IWxDo {
	return w.withDO(w.DO.Omit(cols...))
}

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

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

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

func (w wxDo) Group(cols ...field.Expr) IWxDo {
	return w.withDO(w.DO.Group(cols...))
}

func (w wxDo) Having(conds ...gen.Condition) IWxDo {
	return w.withDO(w.DO.Having(conds...))
}

func (w wxDo) Limit(limit int) IWxDo {
	return w.withDO(w.DO.Limit(limit))
}

func (w wxDo) Offset(offset int) IWxDo {
	return w.withDO(w.DO.Offset(offset))
}

func (w wxDo) Scopes(funcs ...func(gen.Dao) gen.Dao) IWxDo {
	return w.withDO(w.DO.Scopes(funcs...))
}

func (w wxDo) Unscoped() IWxDo {
	return w.withDO(w.DO.Unscoped())
}

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

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

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

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

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

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

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

func (w wxDo) Attrs(attrs ...field.AssignExpr) IWxDo {
	return w.withDO(w.DO.Attrs(attrs...))
}

func (w wxDo) Assign(attrs ...field.AssignExpr) IWxDo {
	return w.withDO(w.DO.Assign(attrs...))
}

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

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

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

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

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

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

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