// 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 dao

import (
	"context"

	"gorm.io/gorm"
	"gorm.io/gorm/clause"
	"gorm.io/gorm/schema"

	"gorm.io/gen"
	"gorm.io/gen/field"

	"gorm.io/plugin/dbresolver"

	"mini-chat/internal/repository/mysql/model"
)

func newMiniProgram(db *gorm.DB, opts ...gen.DOOption) miniProgram {
	_miniProgram := miniProgram{}

	_miniProgram.miniProgramDo.UseDB(db, opts...)
	_miniProgram.miniProgramDo.UseModel(&model.MiniProgram{})

	tableName := _miniProgram.miniProgramDo.TableName()
	_miniProgram.ALL = field.NewAsterisk(tableName)
	_miniProgram.ID = field.NewInt32(tableName, "id")
	_miniProgram.AppID = field.NewString(tableName, "app_id")
	_miniProgram.AppSecret = field.NewString(tableName, "app_secret")
	_miniProgram.AdminID = field.NewInt32(tableName, "admin_id")
	_miniProgram.TemplateID = field.NewString(tableName, "template_id")
	_miniProgram.Name = field.NewString(tableName, "name")
	_miniProgram.Description = field.NewString(tableName, "description")
	_miniProgram.Avatar = field.NewString(tableName, "avatar")
	_miniProgram.CreatedUser = field.NewString(tableName, "created_user")
	_miniProgram.CreatedAt = field.NewTime(tableName, "created_at")
	_miniProgram.UpdatedUser = field.NewString(tableName, "updated_user")
	_miniProgram.UpdatedAt = field.NewTime(tableName, "updated_at")

	_miniProgram.fillFieldMap()

	return _miniProgram
}

// miniProgram 小程序表
type miniProgram struct {
	miniProgramDo

	ALL         field.Asterisk
	ID          field.Int32  // 主键ID
	AppID       field.String // 小程序ID
	AppSecret   field.String // 小程序密钥
	AdminID     field.Int32  // 管理员ID
	TemplateID  field.String // 模版ID
	Name        field.String // 名称
	Description field.String // 描述
	Avatar      field.String // 头像
	CreatedUser field.String // 创建人
	CreatedAt   field.Time   // 创建时间
	UpdatedUser field.String // 更新人
	UpdatedAt   field.Time   // 更新时间

	fieldMap map[string]field.Expr
}

func (m miniProgram) Table(newTableName string) *miniProgram {
	m.miniProgramDo.UseTable(newTableName)
	return m.updateTableName(newTableName)
}

func (m miniProgram) As(alias string) *miniProgram {
	m.miniProgramDo.DO = *(m.miniProgramDo.As(alias).(*gen.DO))
	return m.updateTableName(alias)
}

func (m *miniProgram) updateTableName(table string) *miniProgram {
	m.ALL = field.NewAsterisk(table)
	m.ID = field.NewInt32(table, "id")
	m.AppID = field.NewString(table, "app_id")
	m.AppSecret = field.NewString(table, "app_secret")
	m.AdminID = field.NewInt32(table, "admin_id")
	m.TemplateID = field.NewString(table, "template_id")
	m.Name = field.NewString(table, "name")
	m.Description = field.NewString(table, "description")
	m.Avatar = field.NewString(table, "avatar")
	m.CreatedUser = field.NewString(table, "created_user")
	m.CreatedAt = field.NewTime(table, "created_at")
	m.UpdatedUser = field.NewString(table, "updated_user")
	m.UpdatedAt = field.NewTime(table, "updated_at")

	m.fillFieldMap()

	return m
}

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

func (m *miniProgram) fillFieldMap() {
	m.fieldMap = make(map[string]field.Expr, 12)
	m.fieldMap["id"] = m.ID
	m.fieldMap["app_id"] = m.AppID
	m.fieldMap["app_secret"] = m.AppSecret
	m.fieldMap["admin_id"] = m.AdminID
	m.fieldMap["template_id"] = m.TemplateID
	m.fieldMap["name"] = m.Name
	m.fieldMap["description"] = m.Description
	m.fieldMap["avatar"] = m.Avatar
	m.fieldMap["created_user"] = m.CreatedUser
	m.fieldMap["created_at"] = m.CreatedAt
	m.fieldMap["updated_user"] = m.UpdatedUser
	m.fieldMap["updated_at"] = m.UpdatedAt
}

func (m miniProgram) clone(db *gorm.DB) miniProgram {
	m.miniProgramDo.ReplaceConnPool(db.Statement.ConnPool)
	return m
}

func (m miniProgram) replaceDB(db *gorm.DB) miniProgram {
	m.miniProgramDo.ReplaceDB(db)
	return m
}

type miniProgramDo struct{ gen.DO }

func (m miniProgramDo) Debug() *miniProgramDo {
	return m.withDO(m.DO.Debug())
}

func (m miniProgramDo) WithContext(ctx context.Context) *miniProgramDo {
	return m.withDO(m.DO.WithContext(ctx))
}

func (m miniProgramDo) ReadDB() *miniProgramDo {
	return m.Clauses(dbresolver.Read)
}

func (m miniProgramDo) WriteDB() *miniProgramDo {
	return m.Clauses(dbresolver.Write)
}

func (m miniProgramDo) Session(config *gorm.Session) *miniProgramDo {
	return m.withDO(m.DO.Session(config))
}

func (m miniProgramDo) Clauses(conds ...clause.Expression) *miniProgramDo {
	return m.withDO(m.DO.Clauses(conds...))
}

func (m miniProgramDo) Returning(value interface{}, columns ...string) *miniProgramDo {
	return m.withDO(m.DO.Returning(value, columns...))
}

func (m miniProgramDo) Not(conds ...gen.Condition) *miniProgramDo {
	return m.withDO(m.DO.Not(conds...))
}

func (m miniProgramDo) Or(conds ...gen.Condition) *miniProgramDo {
	return m.withDO(m.DO.Or(conds...))
}

func (m miniProgramDo) Select(conds ...field.Expr) *miniProgramDo {
	return m.withDO(m.DO.Select(conds...))
}

func (m miniProgramDo) Where(conds ...gen.Condition) *miniProgramDo {
	return m.withDO(m.DO.Where(conds...))
}

func (m miniProgramDo) Order(conds ...field.Expr) *miniProgramDo {
	return m.withDO(m.DO.Order(conds...))
}

func (m miniProgramDo) Distinct(cols ...field.Expr) *miniProgramDo {
	return m.withDO(m.DO.Distinct(cols...))
}

func (m miniProgramDo) Omit(cols ...field.Expr) *miniProgramDo {
	return m.withDO(m.DO.Omit(cols...))
}

func (m miniProgramDo) Join(table schema.Tabler, on ...field.Expr) *miniProgramDo {
	return m.withDO(m.DO.Join(table, on...))
}

func (m miniProgramDo) LeftJoin(table schema.Tabler, on ...field.Expr) *miniProgramDo {
	return m.withDO(m.DO.LeftJoin(table, on...))
}

func (m miniProgramDo) RightJoin(table schema.Tabler, on ...field.Expr) *miniProgramDo {
	return m.withDO(m.DO.RightJoin(table, on...))
}

func (m miniProgramDo) Group(cols ...field.Expr) *miniProgramDo {
	return m.withDO(m.DO.Group(cols...))
}

func (m miniProgramDo) Having(conds ...gen.Condition) *miniProgramDo {
	return m.withDO(m.DO.Having(conds...))
}

func (m miniProgramDo) Limit(limit int) *miniProgramDo {
	return m.withDO(m.DO.Limit(limit))
}

func (m miniProgramDo) Offset(offset int) *miniProgramDo {
	return m.withDO(m.DO.Offset(offset))
}

func (m miniProgramDo) Scopes(funcs ...func(gen.Dao) gen.Dao) *miniProgramDo {
	return m.withDO(m.DO.Scopes(funcs...))
}

func (m miniProgramDo) Unscoped() *miniProgramDo {
	return m.withDO(m.DO.Unscoped())
}

func (m miniProgramDo) Create(values ...*model.MiniProgram) error {
	if len(values) == 0 {
		return nil
	}
	return m.DO.Create(values)
}

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

func (m miniProgramDo) First() (*model.MiniProgram, error) {
	if result, err := m.DO.First(); err != nil {
		return nil, err
	} else {
		return result.(*model.MiniProgram), nil
	}
}

func (m miniProgramDo) Take() (*model.MiniProgram, error) {
	if result, err := m.DO.Take(); err != nil {
		return nil, err
	} else {
		return result.(*model.MiniProgram), nil
	}
}

func (m miniProgramDo) Last() (*model.MiniProgram, error) {
	if result, err := m.DO.Last(); err != nil {
		return nil, err
	} else {
		return result.(*model.MiniProgram), nil
	}
}

func (m miniProgramDo) Find() ([]*model.MiniProgram, error) {
	result, err := m.DO.Find()
	return result.([]*model.MiniProgram), err
}

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

func (m miniProgramDo) Attrs(attrs ...field.AssignExpr) *miniProgramDo {
	return m.withDO(m.DO.Attrs(attrs...))
}

func (m miniProgramDo) Assign(attrs ...field.AssignExpr) *miniProgramDo {
	return m.withDO(m.DO.Assign(attrs...))
}

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

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

func (m miniProgramDo) FirstOrInit() (*model.MiniProgram, error) {
	if result, err := m.DO.FirstOrInit(); err != nil {
		return nil, err
	} else {
		return result.(*model.MiniProgram), nil
	}
}

func (m miniProgramDo) FirstOrCreate() (*model.MiniProgram, error) {
	if result, err := m.DO.FirstOrCreate(); err != nil {
		return nil, err
	} else {
		return result.(*model.MiniProgram), nil
	}
}

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

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

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

func (m miniProgramDo) Scan(result interface{}) (err error) {
	return m.DO.Scan(result)
}

func (m miniProgramDo) Delete(models ...*model.MiniProgram) (result gen.ResultInfo, err error) {
	return m.DO.Delete(models)
}

func (m *miniProgramDo) withDO(do gen.Dao) *miniProgramDo {
	m.DO = *do.(*gen.DO)
	return m
}
