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

	_labelRelationship.labelRelationshipDo.UseDB(db, opts...)
	_labelRelationship.labelRelationshipDo.UseModel(&model.LabelRelationship{})

	tableName := _labelRelationship.labelRelationshipDo.TableName()
	_labelRelationship.ALL = field.NewAsterisk(tableName)
	_labelRelationship.ID = field.NewInt64(tableName, "id")
	_labelRelationship.CreatedAt = field.NewTime(tableName, "created_at")
	_labelRelationship.UpdatedAt = field.NewTime(tableName, "updated_at")
	_labelRelationship.Status = field.NewInt64(tableName, "status")
	_labelRelationship.DeletedAt = field.NewField(tableName, "deleted_at")
	_labelRelationship.LabelID = field.NewInt64(tableName, "label_id")
	_labelRelationship.ContactID = field.NewInt64(tableName, "contact_id")
	_labelRelationship.OrganizationID = field.NewInt64(tableName, "organization_id")

	_labelRelationship.fillFieldMap()

	return _labelRelationship
}

type labelRelationship struct {
	labelRelationshipDo

	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 | 删除日期
	LabelID        field.Int64
	ContactID      field.Int64
	OrganizationID field.Int64 // 机构 ID

	fieldMap map[string]field.Expr
}

func (l labelRelationship) Table(newTableName string) *labelRelationship {
	l.labelRelationshipDo.UseTable(newTableName)
	return l.updateTableName(newTableName)
}

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

func (l *labelRelationship) updateTableName(table string) *labelRelationship {
	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.LabelID = field.NewInt64(table, "label_id")
	l.ContactID = field.NewInt64(table, "contact_id")
	l.OrganizationID = field.NewInt64(table, "organization_id")

	l.fillFieldMap()

	return l
}

func (l *labelRelationship) 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 *labelRelationship) fillFieldMap() {
	l.fieldMap = make(map[string]field.Expr, 8)
	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["label_id"] = l.LabelID
	l.fieldMap["contact_id"] = l.ContactID
	l.fieldMap["organization_id"] = l.OrganizationID
}

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

func (l labelRelationship) replaceDB(db *gorm.DB) labelRelationship {
	l.labelRelationshipDo.ReplaceDB(db)
	return l
}

type labelRelationshipDo struct{ gen.DO }

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

func (l labelRelationshipDo) Debug() ILabelRelationshipDo {
	return l.withDO(l.DO.Debug())
}

func (l labelRelationshipDo) WithContext(ctx context.Context) ILabelRelationshipDo {
	return l.withDO(l.DO.WithContext(ctx))
}

func (l labelRelationshipDo) ReadDB() ILabelRelationshipDo {
	return l.Clauses(dbresolver.Read)
}

func (l labelRelationshipDo) WriteDB() ILabelRelationshipDo {
	return l.Clauses(dbresolver.Write)
}

func (l labelRelationshipDo) Session(config *gorm.Session) ILabelRelationshipDo {
	return l.withDO(l.DO.Session(config))
}

func (l labelRelationshipDo) Clauses(conds ...clause.Expression) ILabelRelationshipDo {
	return l.withDO(l.DO.Clauses(conds...))
}

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

func (l labelRelationshipDo) Not(conds ...gen.Condition) ILabelRelationshipDo {
	return l.withDO(l.DO.Not(conds...))
}

func (l labelRelationshipDo) Or(conds ...gen.Condition) ILabelRelationshipDo {
	return l.withDO(l.DO.Or(conds...))
}

func (l labelRelationshipDo) Select(conds ...field.Expr) ILabelRelationshipDo {
	return l.withDO(l.DO.Select(conds...))
}

func (l labelRelationshipDo) Where(conds ...gen.Condition) ILabelRelationshipDo {
	return l.withDO(l.DO.Where(conds...))
}

func (l labelRelationshipDo) Order(conds ...field.Expr) ILabelRelationshipDo {
	return l.withDO(l.DO.Order(conds...))
}

func (l labelRelationshipDo) Distinct(cols ...field.Expr) ILabelRelationshipDo {
	return l.withDO(l.DO.Distinct(cols...))
}

func (l labelRelationshipDo) Omit(cols ...field.Expr) ILabelRelationshipDo {
	return l.withDO(l.DO.Omit(cols...))
}

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

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

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

func (l labelRelationshipDo) Group(cols ...field.Expr) ILabelRelationshipDo {
	return l.withDO(l.DO.Group(cols...))
}

func (l labelRelationshipDo) Having(conds ...gen.Condition) ILabelRelationshipDo {
	return l.withDO(l.DO.Having(conds...))
}

func (l labelRelationshipDo) Limit(limit int) ILabelRelationshipDo {
	return l.withDO(l.DO.Limit(limit))
}

func (l labelRelationshipDo) Offset(offset int) ILabelRelationshipDo {
	return l.withDO(l.DO.Offset(offset))
}

func (l labelRelationshipDo) Scopes(funcs ...func(gen.Dao) gen.Dao) ILabelRelationshipDo {
	return l.withDO(l.DO.Scopes(funcs...))
}

func (l labelRelationshipDo) Unscoped() ILabelRelationshipDo {
	return l.withDO(l.DO.Unscoped())
}

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

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

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

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

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

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

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

func (l labelRelationshipDo) Attrs(attrs ...field.AssignExpr) ILabelRelationshipDo {
	return l.withDO(l.DO.Attrs(attrs...))
}

func (l labelRelationshipDo) Assign(attrs ...field.AssignExpr) ILabelRelationshipDo {
	return l.withDO(l.DO.Assign(attrs...))
}

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

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

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

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

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

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

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