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

	_label.labelDo.UseDB(db, opts...)
	_label.labelDo.UseModel(&model.Label{})

	tableName := _label.labelDo.TableName()
	_label.ALL = field.NewAsterisk(tableName)
	_label.ID = field.NewInt64(tableName, "id")
	_label.CreatedAt = field.NewTime(tableName, "created_at")
	_label.UpdatedAt = field.NewTime(tableName, "updated_at")
	_label.Status = field.NewInt64(tableName, "status")
	_label.DeletedAt = field.NewField(tableName, "deleted_at")
	_label.Type = field.NewInt64(tableName, "type")
	_label.Name = field.NewString(tableName, "name")
	_label.From = field.NewInt64(tableName, "from")
	_label.Mode = field.NewInt64(tableName, "mode")
	_label.Conditions = field.NewString(tableName, "conditions")
	_label.OrganizationID = field.NewInt64(tableName, "organization_id")

	_label.fillFieldMap()

	return _label
}

type label struct {
	labelDo

	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 | 删除日期
	Type           field.Int64  // 标签类型：1好友，2群组，3公众号，4企业微信联系人
	Name           field.String // 标签名称
	From           field.Int64  // 标签来源：1后台创建 2个微同步
	Mode           field.Int64  // 标签模式：1动态 2静态
	Conditions     field.String // 标签的触达条件
	OrganizationID field.Int64  // 机构 ID

	fieldMap map[string]field.Expr
}

func (l label) Table(newTableName string) *label {
	l.labelDo.UseTable(newTableName)
	return l.updateTableName(newTableName)
}

func (l label) As(alias string) *label {
	l.labelDo.DO = *(l.labelDo.As(alias).(*gen.DO))
	return l.updateTableName(alias)
}

func (l *label) updateTableName(table string) *label {
	l.ALL = field.NewAsterisk(table)
	l.ID = field.NewInt64(table, "id")
	l.CreatedAt = field.NewTime(table, "created_at")
	l.UpdatedAt = field.NewTime(table, "updated_at")
	l.Status = field.NewInt64(table, "status")
	l.DeletedAt = field.NewField(table, "deleted_at")
	l.Type = field.NewInt64(table, "type")
	l.Name = field.NewString(table, "name")
	l.From = field.NewInt64(table, "from")
	l.Mode = field.NewInt64(table, "mode")
	l.Conditions = field.NewString(table, "conditions")
	l.OrganizationID = field.NewInt64(table, "organization_id")

	l.fillFieldMap()

	return l
}

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

func (l *label) fillFieldMap() {
	l.fieldMap = make(map[string]field.Expr, 11)
	l.fieldMap["id"] = l.ID
	l.fieldMap["created_at"] = l.CreatedAt
	l.fieldMap["updated_at"] = l.UpdatedAt
	l.fieldMap["status"] = l.Status
	l.fieldMap["deleted_at"] = l.DeletedAt
	l.fieldMap["type"] = l.Type
	l.fieldMap["name"] = l.Name
	l.fieldMap["from"] = l.From
	l.fieldMap["mode"] = l.Mode
	l.fieldMap["conditions"] = l.Conditions
	l.fieldMap["organization_id"] = l.OrganizationID
}

func (l label) clone(db *gorm.DB) label {
	l.labelDo.ReplaceConnPool(db.Statement.ConnPool)
	return l
}

func (l label) replaceDB(db *gorm.DB) label {
	l.labelDo.ReplaceDB(db)
	return l
}

type labelDo struct{ gen.DO }

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

func (l labelDo) Debug() ILabelDo {
	return l.withDO(l.DO.Debug())
}

func (l labelDo) WithContext(ctx context.Context) ILabelDo {
	return l.withDO(l.DO.WithContext(ctx))
}

func (l labelDo) ReadDB() ILabelDo {
	return l.Clauses(dbresolver.Read)
}

func (l labelDo) WriteDB() ILabelDo {
	return l.Clauses(dbresolver.Write)
}

func (l labelDo) Session(config *gorm.Session) ILabelDo {
	return l.withDO(l.DO.Session(config))
}

func (l labelDo) Clauses(conds ...clause.Expression) ILabelDo {
	return l.withDO(l.DO.Clauses(conds...))
}

func (l labelDo) Returning(value interface{}, columns ...string) ILabelDo {
	return l.withDO(l.DO.Returning(value, columns...))
}

func (l labelDo) Not(conds ...gen.Condition) ILabelDo {
	return l.withDO(l.DO.Not(conds...))
}

func (l labelDo) Or(conds ...gen.Condition) ILabelDo {
	return l.withDO(l.DO.Or(conds...))
}

func (l labelDo) Select(conds ...field.Expr) ILabelDo {
	return l.withDO(l.DO.Select(conds...))
}

func (l labelDo) Where(conds ...gen.Condition) ILabelDo {
	return l.withDO(l.DO.Where(conds...))
}

func (l labelDo) Order(conds ...field.Expr) ILabelDo {
	return l.withDO(l.DO.Order(conds...))
}

func (l labelDo) Distinct(cols ...field.Expr) ILabelDo {
	return l.withDO(l.DO.Distinct(cols...))
}

func (l labelDo) Omit(cols ...field.Expr) ILabelDo {
	return l.withDO(l.DO.Omit(cols...))
}

func (l labelDo) Join(table schema.Tabler, on ...field.Expr) ILabelDo {
	return l.withDO(l.DO.Join(table, on...))
}

func (l labelDo) LeftJoin(table schema.Tabler, on ...field.Expr) ILabelDo {
	return l.withDO(l.DO.LeftJoin(table, on...))
}

func (l labelDo) RightJoin(table schema.Tabler, on ...field.Expr) ILabelDo {
	return l.withDO(l.DO.RightJoin(table, on...))
}

func (l labelDo) Group(cols ...field.Expr) ILabelDo {
	return l.withDO(l.DO.Group(cols...))
}

func (l labelDo) Having(conds ...gen.Condition) ILabelDo {
	return l.withDO(l.DO.Having(conds...))
}

func (l labelDo) Limit(limit int) ILabelDo {
	return l.withDO(l.DO.Limit(limit))
}

func (l labelDo) Offset(offset int) ILabelDo {
	return l.withDO(l.DO.Offset(offset))
}

func (l labelDo) Scopes(funcs ...func(gen.Dao) gen.Dao) ILabelDo {
	return l.withDO(l.DO.Scopes(funcs...))
}

func (l labelDo) Unscoped() ILabelDo {
	return l.withDO(l.DO.Unscoped())
}

func (l labelDo) Create(values ...*model.Label) error {
	if len(values) == 0 {
		return nil
	}
	return l.DO.Create(values)
}

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

func (l labelDo) First() (*model.Label, error) {
	if result, err := l.DO.First(); err != nil {
		return nil, err
	} else {
		return result.(*model.Label), nil
	}
}

func (l labelDo) Take() (*model.Label, error) {
	if result, err := l.DO.Take(); err != nil {
		return nil, err
	} else {
		return result.(*model.Label), nil
	}
}

func (l labelDo) Last() (*model.Label, error) {
	if result, err := l.DO.Last(); err != nil {
		return nil, err
	} else {
		return result.(*model.Label), nil
	}
}

func (l labelDo) Find() ([]*model.Label, error) {
	result, err := l.DO.Find()
	return result.([]*model.Label), err
}

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

func (l labelDo) Attrs(attrs ...field.AssignExpr) ILabelDo {
	return l.withDO(l.DO.Attrs(attrs...))
}

func (l labelDo) Assign(attrs ...field.AssignExpr) ILabelDo {
	return l.withDO(l.DO.Assign(attrs...))
}

func (l labelDo) Joins(fields ...field.RelationField) ILabelDo {
	for _, _f := range fields {
		l = *l.withDO(l.DO.Joins(_f))
	}
	return &l
}

func (l labelDo) Preload(fields ...field.RelationField) ILabelDo {
	for _, _f := range fields {
		l = *l.withDO(l.DO.Preload(_f))
	}
	return &l
}

func (l labelDo) FirstOrInit() (*model.Label, error) {
	if result, err := l.DO.FirstOrInit(); err != nil {
		return nil, err
	} else {
		return result.(*model.Label), nil
	}
}

func (l labelDo) FirstOrCreate() (*model.Label, error) {
	if result, err := l.DO.FirstOrCreate(); err != nil {
		return nil, err
	} else {
		return result.(*model.Label), nil
	}
}

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

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

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

func (l labelDo) Scan(result interface{}) (err error) {
	return l.DO.Scan(result)
}

func (l labelDo) Delete(models ...*model.Label) (result gen.ResultInfo, err error) {
	return l.DO.Delete(models)
}

func (l *labelDo) withDO(do gen.Dao) *labelDo {
	l.DO = *do.(*gen.DO)
	return l
}
