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

	_labelTagging.labelTaggingDo.UseDB(db, opts...)
	_labelTagging.labelTaggingDo.UseModel(&model.LabelTagging{})

	tableName := _labelTagging.labelTaggingDo.TableName()
	_labelTagging.ALL = field.NewAsterisk(tableName)
	_labelTagging.ID = field.NewInt64(tableName, "id")
	_labelTagging.CreatedAt = field.NewTime(tableName, "created_at")
	_labelTagging.UpdatedAt = field.NewTime(tableName, "updated_at")
	_labelTagging.Status = field.NewInt64(tableName, "status")
	_labelTagging.DeletedAt = field.NewField(tableName, "deleted_at")
	_labelTagging.OrganizationID = field.NewInt64(tableName, "organization_id")
	_labelTagging.Type = field.NewInt64(tableName, "type")
	_labelTagging.Conditions = field.NewString(tableName, "conditions")
	_labelTagging.ActionLabelAdd = field.NewString(tableName, "action_label_add")
	_labelTagging.ActionLabelDel = field.NewString(tableName, "action_label_del")

	_labelTagging.fillFieldMap()

	return _labelTagging
}

type labelTagging struct {
	labelTaggingDo

	ALL            field.Asterisk
	ID             field.Int64
	CreatedAt      field.Time   // Create Time | 创建日期
	UpdatedAt      field.Time   // Update Time | 修改日期
	Status         field.Int64  // 状态 1 正常 2 禁用
	DeletedAt      field.Field  // Delete Time | 删除日期
	OrganizationID field.Int64  // 机构 ID
	Type           field.Int64  // 标签类型：1好友，2群组，3公众号，4企业微信联系人
	Conditions     field.String // 标签的触达条件
	ActionLabelAdd field.String // 命中后需要打的标签
	ActionLabelDel field.String // 命中后需要打的标签

	fieldMap map[string]field.Expr
}

func (l labelTagging) Table(newTableName string) *labelTagging {
	l.labelTaggingDo.UseTable(newTableName)
	return l.updateTableName(newTableName)
}

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

func (l *labelTagging) updateTableName(table string) *labelTagging {
	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.OrganizationID = field.NewInt64(table, "organization_id")
	l.Type = field.NewInt64(table, "type")
	l.Conditions = field.NewString(table, "conditions")
	l.ActionLabelAdd = field.NewString(table, "action_label_add")
	l.ActionLabelDel = field.NewString(table, "action_label_del")

	l.fillFieldMap()

	return l
}

func (l *labelTagging) 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 *labelTagging) fillFieldMap() {
	l.fieldMap = make(map[string]field.Expr, 10)
	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["organization_id"] = l.OrganizationID
	l.fieldMap["type"] = l.Type
	l.fieldMap["conditions"] = l.Conditions
	l.fieldMap["action_label_add"] = l.ActionLabelAdd
	l.fieldMap["action_label_del"] = l.ActionLabelDel
}

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

func (l labelTagging) replaceDB(db *gorm.DB) labelTagging {
	l.labelTaggingDo.ReplaceDB(db)
	return l
}

type labelTaggingDo struct{ gen.DO }

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

func (l labelTaggingDo) Debug() ILabelTaggingDo {
	return l.withDO(l.DO.Debug())
}

func (l labelTaggingDo) WithContext(ctx context.Context) ILabelTaggingDo {
	return l.withDO(l.DO.WithContext(ctx))
}

func (l labelTaggingDo) ReadDB() ILabelTaggingDo {
	return l.Clauses(dbresolver.Read)
}

func (l labelTaggingDo) WriteDB() ILabelTaggingDo {
	return l.Clauses(dbresolver.Write)
}

func (l labelTaggingDo) Session(config *gorm.Session) ILabelTaggingDo {
	return l.withDO(l.DO.Session(config))
}

func (l labelTaggingDo) Clauses(conds ...clause.Expression) ILabelTaggingDo {
	return l.withDO(l.DO.Clauses(conds...))
}

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

func (l labelTaggingDo) Not(conds ...gen.Condition) ILabelTaggingDo {
	return l.withDO(l.DO.Not(conds...))
}

func (l labelTaggingDo) Or(conds ...gen.Condition) ILabelTaggingDo {
	return l.withDO(l.DO.Or(conds...))
}

func (l labelTaggingDo) Select(conds ...field.Expr) ILabelTaggingDo {
	return l.withDO(l.DO.Select(conds...))
}

func (l labelTaggingDo) Where(conds ...gen.Condition) ILabelTaggingDo {
	return l.withDO(l.DO.Where(conds...))
}

func (l labelTaggingDo) Order(conds ...field.Expr) ILabelTaggingDo {
	return l.withDO(l.DO.Order(conds...))
}

func (l labelTaggingDo) Distinct(cols ...field.Expr) ILabelTaggingDo {
	return l.withDO(l.DO.Distinct(cols...))
}

func (l labelTaggingDo) Omit(cols ...field.Expr) ILabelTaggingDo {
	return l.withDO(l.DO.Omit(cols...))
}

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

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

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

func (l labelTaggingDo) Group(cols ...field.Expr) ILabelTaggingDo {
	return l.withDO(l.DO.Group(cols...))
}

func (l labelTaggingDo) Having(conds ...gen.Condition) ILabelTaggingDo {
	return l.withDO(l.DO.Having(conds...))
}

func (l labelTaggingDo) Limit(limit int) ILabelTaggingDo {
	return l.withDO(l.DO.Limit(limit))
}

func (l labelTaggingDo) Offset(offset int) ILabelTaggingDo {
	return l.withDO(l.DO.Offset(offset))
}

func (l labelTaggingDo) Scopes(funcs ...func(gen.Dao) gen.Dao) ILabelTaggingDo {
	return l.withDO(l.DO.Scopes(funcs...))
}

func (l labelTaggingDo) Unscoped() ILabelTaggingDo {
	return l.withDO(l.DO.Unscoped())
}

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

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

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

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

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

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

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

func (l labelTaggingDo) Attrs(attrs ...field.AssignExpr) ILabelTaggingDo {
	return l.withDO(l.DO.Attrs(attrs...))
}

func (l labelTaggingDo) Assign(attrs ...field.AssignExpr) ILabelTaggingDo {
	return l.withDO(l.DO.Assign(attrs...))
}

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

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

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

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

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

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

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