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

	_intentOrg.intentOrgDo.UseDB(db, opts...)
	_intentOrg.intentOrgDo.UseModel(&model.IntentOrg{})

	tableName := _intentOrg.intentOrgDo.TableName()
	_intentOrg.ALL = field.NewAsterisk(tableName)
	_intentOrg.ID = field.NewInt64(tableName, "id")
	_intentOrg.Name = field.NewString(tableName, "name")
	_intentOrg.APIKey = field.NewString(tableName, "api_key")
	_intentOrg.OpenaiBase = field.NewString(tableName, "openai_base")
	_intentOrg.OpenaiKey = field.NewString(tableName, "openai_key")
	_intentOrg.IntentCallback = field.NewString(tableName, "intent_callback")
	_intentOrg.MismatchCallback = field.NewString(tableName, "mismatch_callback")
	_intentOrg.Status = field.NewInt64(tableName, "status")
	_intentOrg.DeletedAt = field.NewField(tableName, "deleted_at")
	_intentOrg.CreatedAt = field.NewTime(tableName, "created_at")
	_intentOrg.UpdatedAt = field.NewTime(tableName, "updated_at")

	_intentOrg.fillFieldMap()

	return _intentOrg
}

type intentOrg struct {
	intentOrgDo

	ALL              field.Asterisk
	ID               field.Int64
	Name             field.String // 机构名称
	APIKey           field.String
	OpenaiBase       field.String
	OpenaiKey        field.String
	IntentCallback   field.String // 意向度结果推送地址
	MismatchCallback field.String // 未匹配结果推送地址
	Status           field.Int64  // 状态 1 正常 2 禁用
	DeletedAt        field.Field  // Delete Time | 删除日期
	CreatedAt        field.Time   // Create Time | 创建日期
	UpdatedAt        field.Time   // Update Time | 修改日期

	fieldMap map[string]field.Expr
}

func (i intentOrg) Table(newTableName string) *intentOrg {
	i.intentOrgDo.UseTable(newTableName)
	return i.updateTableName(newTableName)
}

func (i intentOrg) As(alias string) *intentOrg {
	i.intentOrgDo.DO = *(i.intentOrgDo.As(alias).(*gen.DO))
	return i.updateTableName(alias)
}

func (i *intentOrg) updateTableName(table string) *intentOrg {
	i.ALL = field.NewAsterisk(table)
	i.ID = field.NewInt64(table, "id")
	i.Name = field.NewString(table, "name")
	i.APIKey = field.NewString(table, "api_key")
	i.OpenaiBase = field.NewString(table, "openai_base")
	i.OpenaiKey = field.NewString(table, "openai_key")
	i.IntentCallback = field.NewString(table, "intent_callback")
	i.MismatchCallback = field.NewString(table, "mismatch_callback")
	i.Status = field.NewInt64(table, "status")
	i.DeletedAt = field.NewField(table, "deleted_at")
	i.CreatedAt = field.NewTime(table, "created_at")
	i.UpdatedAt = field.NewTime(table, "updated_at")

	i.fillFieldMap()

	return i
}

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

func (i *intentOrg) fillFieldMap() {
	i.fieldMap = make(map[string]field.Expr, 11)
	i.fieldMap["id"] = i.ID
	i.fieldMap["name"] = i.Name
	i.fieldMap["api_key"] = i.APIKey
	i.fieldMap["openai_base"] = i.OpenaiBase
	i.fieldMap["openai_key"] = i.OpenaiKey
	i.fieldMap["intent_callback"] = i.IntentCallback
	i.fieldMap["mismatch_callback"] = i.MismatchCallback
	i.fieldMap["status"] = i.Status
	i.fieldMap["deleted_at"] = i.DeletedAt
	i.fieldMap["created_at"] = i.CreatedAt
	i.fieldMap["updated_at"] = i.UpdatedAt
}

func (i intentOrg) clone(db *gorm.DB) intentOrg {
	i.intentOrgDo.ReplaceConnPool(db.Statement.ConnPool)
	return i
}

func (i intentOrg) replaceDB(db *gorm.DB) intentOrg {
	i.intentOrgDo.ReplaceDB(db)
	return i
}

type intentOrgDo struct{ gen.DO }

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

func (i intentOrgDo) Debug() IIntentOrgDo {
	return i.withDO(i.DO.Debug())
}

func (i intentOrgDo) WithContext(ctx context.Context) IIntentOrgDo {
	return i.withDO(i.DO.WithContext(ctx))
}

func (i intentOrgDo) ReadDB() IIntentOrgDo {
	return i.Clauses(dbresolver.Read)
}

func (i intentOrgDo) WriteDB() IIntentOrgDo {
	return i.Clauses(dbresolver.Write)
}

func (i intentOrgDo) Session(config *gorm.Session) IIntentOrgDo {
	return i.withDO(i.DO.Session(config))
}

func (i intentOrgDo) Clauses(conds ...clause.Expression) IIntentOrgDo {
	return i.withDO(i.DO.Clauses(conds...))
}

func (i intentOrgDo) Returning(value interface{}, columns ...string) IIntentOrgDo {
	return i.withDO(i.DO.Returning(value, columns...))
}

func (i intentOrgDo) Not(conds ...gen.Condition) IIntentOrgDo {
	return i.withDO(i.DO.Not(conds...))
}

func (i intentOrgDo) Or(conds ...gen.Condition) IIntentOrgDo {
	return i.withDO(i.DO.Or(conds...))
}

func (i intentOrgDo) Select(conds ...field.Expr) IIntentOrgDo {
	return i.withDO(i.DO.Select(conds...))
}

func (i intentOrgDo) Where(conds ...gen.Condition) IIntentOrgDo {
	return i.withDO(i.DO.Where(conds...))
}

func (i intentOrgDo) Order(conds ...field.Expr) IIntentOrgDo {
	return i.withDO(i.DO.Order(conds...))
}

func (i intentOrgDo) Distinct(cols ...field.Expr) IIntentOrgDo {
	return i.withDO(i.DO.Distinct(cols...))
}

func (i intentOrgDo) Omit(cols ...field.Expr) IIntentOrgDo {
	return i.withDO(i.DO.Omit(cols...))
}

func (i intentOrgDo) Join(table schema.Tabler, on ...field.Expr) IIntentOrgDo {
	return i.withDO(i.DO.Join(table, on...))
}

func (i intentOrgDo) LeftJoin(table schema.Tabler, on ...field.Expr) IIntentOrgDo {
	return i.withDO(i.DO.LeftJoin(table, on...))
}

func (i intentOrgDo) RightJoin(table schema.Tabler, on ...field.Expr) IIntentOrgDo {
	return i.withDO(i.DO.RightJoin(table, on...))
}

func (i intentOrgDo) Group(cols ...field.Expr) IIntentOrgDo {
	return i.withDO(i.DO.Group(cols...))
}

func (i intentOrgDo) Having(conds ...gen.Condition) IIntentOrgDo {
	return i.withDO(i.DO.Having(conds...))
}

func (i intentOrgDo) Limit(limit int) IIntentOrgDo {
	return i.withDO(i.DO.Limit(limit))
}

func (i intentOrgDo) Offset(offset int) IIntentOrgDo {
	return i.withDO(i.DO.Offset(offset))
}

func (i intentOrgDo) Scopes(funcs ...func(gen.Dao) gen.Dao) IIntentOrgDo {
	return i.withDO(i.DO.Scopes(funcs...))
}

func (i intentOrgDo) Unscoped() IIntentOrgDo {
	return i.withDO(i.DO.Unscoped())
}

func (i intentOrgDo) Create(values ...*model.IntentOrg) error {
	if len(values) == 0 {
		return nil
	}
	return i.DO.Create(values)
}

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

func (i intentOrgDo) First() (*model.IntentOrg, error) {
	if result, err := i.DO.First(); err != nil {
		return nil, err
	} else {
		return result.(*model.IntentOrg), nil
	}
}

func (i intentOrgDo) Take() (*model.IntentOrg, error) {
	if result, err := i.DO.Take(); err != nil {
		return nil, err
	} else {
		return result.(*model.IntentOrg), nil
	}
}

func (i intentOrgDo) Last() (*model.IntentOrg, error) {
	if result, err := i.DO.Last(); err != nil {
		return nil, err
	} else {
		return result.(*model.IntentOrg), nil
	}
}

func (i intentOrgDo) Find() ([]*model.IntentOrg, error) {
	result, err := i.DO.Find()
	return result.([]*model.IntentOrg), err
}

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

func (i intentOrgDo) Attrs(attrs ...field.AssignExpr) IIntentOrgDo {
	return i.withDO(i.DO.Attrs(attrs...))
}

func (i intentOrgDo) Assign(attrs ...field.AssignExpr) IIntentOrgDo {
	return i.withDO(i.DO.Assign(attrs...))
}

func (i intentOrgDo) Joins(fields ...field.RelationField) IIntentOrgDo {
	for _, _f := range fields {
		i = *i.withDO(i.DO.Joins(_f))
	}
	return &i
}

func (i intentOrgDo) Preload(fields ...field.RelationField) IIntentOrgDo {
	for _, _f := range fields {
		i = *i.withDO(i.DO.Preload(_f))
	}
	return &i
}

func (i intentOrgDo) FirstOrInit() (*model.IntentOrg, error) {
	if result, err := i.DO.FirstOrInit(); err != nil {
		return nil, err
	} else {
		return result.(*model.IntentOrg), nil
	}
}

func (i intentOrgDo) FirstOrCreate() (*model.IntentOrg, error) {
	if result, err := i.DO.FirstOrCreate(); err != nil {
		return nil, err
	} else {
		return result.(*model.IntentOrg), nil
	}
}

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

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

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

func (i intentOrgDo) Scan(result interface{}) (err error) {
	return i.DO.Scan(result)
}

func (i intentOrgDo) Delete(models ...*model.IntentOrg) (result gen.ResultInfo, err error) {
	return i.DO.Delete(models)
}

func (i *intentOrgDo) withDO(do gen.Dao) *intentOrgDo {
	i.DO = *do.(*gen.DO)
	return i
}
