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

	_sopStage.sopStageDo.UseDB(db, opts...)
	_sopStage.sopStageDo.UseModel(&model.SopStage{})

	tableName := _sopStage.sopStageDo.TableName()
	_sopStage.ALL = field.NewAsterisk(tableName)
	_sopStage.ID = field.NewInt64(tableName, "id")
	_sopStage.CreatedAt = field.NewTime(tableName, "created_at")
	_sopStage.UpdatedAt = field.NewTime(tableName, "updated_at")
	_sopStage.Status = field.NewInt64(tableName, "status")
	_sopStage.DeletedAt = field.NewField(tableName, "deleted_at")
	_sopStage.TaskID = field.NewInt64(tableName, "task_id")
	_sopStage.Name = field.NewString(tableName, "name")
	_sopStage.ConditionType = field.NewInt64(tableName, "condition_type")
	_sopStage.ConditionOperator = field.NewInt64(tableName, "condition_operator")
	_sopStage.ConditionList = field.NewString(tableName, "condition_list")
	_sopStage.ActionMessage = field.NewString(tableName, "action_message")
	_sopStage.ActionLabelAdd = field.NewString(tableName, "action_label_add")
	_sopStage.ActionForward = field.NewString(tableName, "action_forward")
	_sopStage.IndexSort = field.NewInt64(tableName, "index_sort")
	_sopStage.ActionLabelDel = field.NewString(tableName, "action_label_del")

	_sopStage.fillFieldMap()

	return _sopStage
}

type sopStage struct {
	sopStageDo

	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 | 删除日期
	TaskID            field.Int64  // SOP 任务 ID
	Name              field.String // 阶段名称
	ConditionType     field.Int64  // 客群筛选条件类型  1 按标签筛选 2 按客户基本信息筛选
	ConditionOperator field.Int64  // 筛选条件关系  1 满足所有条件（and） 2 满足任意条件（or）
	ConditionList     field.String // 筛选条件列表
	ActionMessage     field.String // 命中后发送的消息内容
	ActionLabelAdd    field.String // 命中后需要打的标签
	ActionForward     field.String // 命中后转发的消息
	IndexSort         field.Int64  // 阶段顺序
	ActionLabelDel    field.String // 命中后需要打的标签

	fieldMap map[string]field.Expr
}

func (s sopStage) Table(newTableName string) *sopStage {
	s.sopStageDo.UseTable(newTableName)
	return s.updateTableName(newTableName)
}

func (s sopStage) As(alias string) *sopStage {
	s.sopStageDo.DO = *(s.sopStageDo.As(alias).(*gen.DO))
	return s.updateTableName(alias)
}

func (s *sopStage) updateTableName(table string) *sopStage {
	s.ALL = field.NewAsterisk(table)
	s.ID = field.NewInt64(table, "id")
	s.CreatedAt = field.NewTime(table, "created_at")
	s.UpdatedAt = field.NewTime(table, "updated_at")
	s.Status = field.NewInt64(table, "status")
	s.DeletedAt = field.NewField(table, "deleted_at")
	s.TaskID = field.NewInt64(table, "task_id")
	s.Name = field.NewString(table, "name")
	s.ConditionType = field.NewInt64(table, "condition_type")
	s.ConditionOperator = field.NewInt64(table, "condition_operator")
	s.ConditionList = field.NewString(table, "condition_list")
	s.ActionMessage = field.NewString(table, "action_message")
	s.ActionLabelAdd = field.NewString(table, "action_label_add")
	s.ActionForward = field.NewString(table, "action_forward")
	s.IndexSort = field.NewInt64(table, "index_sort")
	s.ActionLabelDel = field.NewString(table, "action_label_del")

	s.fillFieldMap()

	return s
}

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

func (s *sopStage) fillFieldMap() {
	s.fieldMap = make(map[string]field.Expr, 15)
	s.fieldMap["id"] = s.ID
	s.fieldMap["created_at"] = s.CreatedAt
	s.fieldMap["updated_at"] = s.UpdatedAt
	s.fieldMap["status"] = s.Status
	s.fieldMap["deleted_at"] = s.DeletedAt
	s.fieldMap["task_id"] = s.TaskID
	s.fieldMap["name"] = s.Name
	s.fieldMap["condition_type"] = s.ConditionType
	s.fieldMap["condition_operator"] = s.ConditionOperator
	s.fieldMap["condition_list"] = s.ConditionList
	s.fieldMap["action_message"] = s.ActionMessage
	s.fieldMap["action_label_add"] = s.ActionLabelAdd
	s.fieldMap["action_forward"] = s.ActionForward
	s.fieldMap["index_sort"] = s.IndexSort
	s.fieldMap["action_label_del"] = s.ActionLabelDel
}

func (s sopStage) clone(db *gorm.DB) sopStage {
	s.sopStageDo.ReplaceConnPool(db.Statement.ConnPool)
	return s
}

func (s sopStage) replaceDB(db *gorm.DB) sopStage {
	s.sopStageDo.ReplaceDB(db)
	return s
}

type sopStageDo struct{ gen.DO }

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

func (s sopStageDo) Debug() ISopStageDo {
	return s.withDO(s.DO.Debug())
}

func (s sopStageDo) WithContext(ctx context.Context) ISopStageDo {
	return s.withDO(s.DO.WithContext(ctx))
}

func (s sopStageDo) ReadDB() ISopStageDo {
	return s.Clauses(dbresolver.Read)
}

func (s sopStageDo) WriteDB() ISopStageDo {
	return s.Clauses(dbresolver.Write)
}

func (s sopStageDo) Session(config *gorm.Session) ISopStageDo {
	return s.withDO(s.DO.Session(config))
}

func (s sopStageDo) Clauses(conds ...clause.Expression) ISopStageDo {
	return s.withDO(s.DO.Clauses(conds...))
}

func (s sopStageDo) Returning(value interface{}, columns ...string) ISopStageDo {
	return s.withDO(s.DO.Returning(value, columns...))
}

func (s sopStageDo) Not(conds ...gen.Condition) ISopStageDo {
	return s.withDO(s.DO.Not(conds...))
}

func (s sopStageDo) Or(conds ...gen.Condition) ISopStageDo {
	return s.withDO(s.DO.Or(conds...))
}

func (s sopStageDo) Select(conds ...field.Expr) ISopStageDo {
	return s.withDO(s.DO.Select(conds...))
}

func (s sopStageDo) Where(conds ...gen.Condition) ISopStageDo {
	return s.withDO(s.DO.Where(conds...))
}

func (s sopStageDo) Order(conds ...field.Expr) ISopStageDo {
	return s.withDO(s.DO.Order(conds...))
}

func (s sopStageDo) Distinct(cols ...field.Expr) ISopStageDo {
	return s.withDO(s.DO.Distinct(cols...))
}

func (s sopStageDo) Omit(cols ...field.Expr) ISopStageDo {
	return s.withDO(s.DO.Omit(cols...))
}

func (s sopStageDo) Join(table schema.Tabler, on ...field.Expr) ISopStageDo {
	return s.withDO(s.DO.Join(table, on...))
}

func (s sopStageDo) LeftJoin(table schema.Tabler, on ...field.Expr) ISopStageDo {
	return s.withDO(s.DO.LeftJoin(table, on...))
}

func (s sopStageDo) RightJoin(table schema.Tabler, on ...field.Expr) ISopStageDo {
	return s.withDO(s.DO.RightJoin(table, on...))
}

func (s sopStageDo) Group(cols ...field.Expr) ISopStageDo {
	return s.withDO(s.DO.Group(cols...))
}

func (s sopStageDo) Having(conds ...gen.Condition) ISopStageDo {
	return s.withDO(s.DO.Having(conds...))
}

func (s sopStageDo) Limit(limit int) ISopStageDo {
	return s.withDO(s.DO.Limit(limit))
}

func (s sopStageDo) Offset(offset int) ISopStageDo {
	return s.withDO(s.DO.Offset(offset))
}

func (s sopStageDo) Scopes(funcs ...func(gen.Dao) gen.Dao) ISopStageDo {
	return s.withDO(s.DO.Scopes(funcs...))
}

func (s sopStageDo) Unscoped() ISopStageDo {
	return s.withDO(s.DO.Unscoped())
}

func (s sopStageDo) Create(values ...*model.SopStage) error {
	if len(values) == 0 {
		return nil
	}
	return s.DO.Create(values)
}

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

func (s sopStageDo) First() (*model.SopStage, error) {
	if result, err := s.DO.First(); err != nil {
		return nil, err
	} else {
		return result.(*model.SopStage), nil
	}
}

func (s sopStageDo) Take() (*model.SopStage, error) {
	if result, err := s.DO.Take(); err != nil {
		return nil, err
	} else {
		return result.(*model.SopStage), nil
	}
}

func (s sopStageDo) Last() (*model.SopStage, error) {
	if result, err := s.DO.Last(); err != nil {
		return nil, err
	} else {
		return result.(*model.SopStage), nil
	}
}

func (s sopStageDo) Find() ([]*model.SopStage, error) {
	result, err := s.DO.Find()
	return result.([]*model.SopStage), err
}

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

func (s sopStageDo) Attrs(attrs ...field.AssignExpr) ISopStageDo {
	return s.withDO(s.DO.Attrs(attrs...))
}

func (s sopStageDo) Assign(attrs ...field.AssignExpr) ISopStageDo {
	return s.withDO(s.DO.Assign(attrs...))
}

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

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

func (s sopStageDo) FirstOrInit() (*model.SopStage, error) {
	if result, err := s.DO.FirstOrInit(); err != nil {
		return nil, err
	} else {
		return result.(*model.SopStage), nil
	}
}

func (s sopStageDo) FirstOrCreate() (*model.SopStage, error) {
	if result, err := s.DO.FirstOrCreate(); err != nil {
		return nil, err
	} else {
		return result.(*model.SopStage), nil
	}
}

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

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

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

func (s sopStageDo) Scan(result interface{}) (err error) {
	return s.DO.Scan(result)
}

func (s sopStageDo) Delete(models ...*model.SopStage) (result gen.ResultInfo, err error) {
	return s.DO.Delete(models)
}

func (s *sopStageDo) withDO(do gen.Dao) *sopStageDo {
	s.DO = *do.(*gen.DO)
	return s
}
