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

	_sopTask.sopTaskDo.UseDB(db, opts...)
	_sopTask.sopTaskDo.UseModel(&model.SopTask{})

	tableName := _sopTask.sopTaskDo.TableName()
	_sopTask.ALL = field.NewAsterisk(tableName)
	_sopTask.ID = field.NewInt64(tableName, "id")
	_sopTask.CreatedAt = field.NewTime(tableName, "created_at")
	_sopTask.UpdatedAt = field.NewTime(tableName, "updated_at")
	_sopTask.Status = field.NewInt64(tableName, "status")
	_sopTask.DeletedAt = field.NewField(tableName, "deleted_at")
	_sopTask.Name = field.NewString(tableName, "name")
	_sopTask.BotWxidList = field.NewString(tableName, "bot_wxid_list")
	_sopTask.Type = field.NewInt64(tableName, "type")
	_sopTask.PlanStartTime = field.NewTime(tableName, "plan_start_time")
	_sopTask.PlanEndTime = field.NewTime(tableName, "plan_end_time")
	_sopTask.CreatorID = field.NewString(tableName, "creator_id")
	_sopTask.OrganizationID = field.NewInt64(tableName, "organization_id")
	_sopTask.Token = field.NewString(tableName, "token")

	_sopTask.fillFieldMap()

	return _sopTask
}

type sopTask struct {
	sopTaskDo

	ALL            field.Asterisk
	ID             field.Int64
	CreatedAt      field.Time   // Create Time | 创建日期
	UpdatedAt      field.Time   // Update Time | 修改日期
	Status         field.Int64  // 状态 1 草稿 2 未开始 3 已开始 4 已结束 5 禁用
	DeletedAt      field.Field  // Delete Time | 删除日期
	Name           field.String // SOP 任务名称
	BotWxidList    field.String // 机器人微信 id 列表
	Type           field.Int64  // 标签类型：1好友，2群组，3企业微信联系人
	PlanStartTime  field.Time   // 任务计划开始时间
	PlanEndTime    field.Time   // 任务计划结束时间
	CreatorID      field.String // 创建者 id
	OrganizationID field.Int64  // 机构 ID
	Token          field.String

	fieldMap map[string]field.Expr
}

func (s sopTask) Table(newTableName string) *sopTask {
	s.sopTaskDo.UseTable(newTableName)
	return s.updateTableName(newTableName)
}

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

func (s *sopTask) updateTableName(table string) *sopTask {
	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.Name = field.NewString(table, "name")
	s.BotWxidList = field.NewString(table, "bot_wxid_list")
	s.Type = field.NewInt64(table, "type")
	s.PlanStartTime = field.NewTime(table, "plan_start_time")
	s.PlanEndTime = field.NewTime(table, "plan_end_time")
	s.CreatorID = field.NewString(table, "creator_id")
	s.OrganizationID = field.NewInt64(table, "organization_id")
	s.Token = field.NewString(table, "token")

	s.fillFieldMap()

	return s
}

func (s *sopTask) 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 *sopTask) fillFieldMap() {
	s.fieldMap = make(map[string]field.Expr, 13)
	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["name"] = s.Name
	s.fieldMap["bot_wxid_list"] = s.BotWxidList
	s.fieldMap["type"] = s.Type
	s.fieldMap["plan_start_time"] = s.PlanStartTime
	s.fieldMap["plan_end_time"] = s.PlanEndTime
	s.fieldMap["creator_id"] = s.CreatorID
	s.fieldMap["organization_id"] = s.OrganizationID
	s.fieldMap["token"] = s.Token
}

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

func (s sopTask) replaceDB(db *gorm.DB) sopTask {
	s.sopTaskDo.ReplaceDB(db)
	return s
}

type sopTaskDo struct{ gen.DO }

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

func (s sopTaskDo) Debug() ISopTaskDo {
	return s.withDO(s.DO.Debug())
}

func (s sopTaskDo) WithContext(ctx context.Context) ISopTaskDo {
	return s.withDO(s.DO.WithContext(ctx))
}

func (s sopTaskDo) ReadDB() ISopTaskDo {
	return s.Clauses(dbresolver.Read)
}

func (s sopTaskDo) WriteDB() ISopTaskDo {
	return s.Clauses(dbresolver.Write)
}

func (s sopTaskDo) Session(config *gorm.Session) ISopTaskDo {
	return s.withDO(s.DO.Session(config))
}

func (s sopTaskDo) Clauses(conds ...clause.Expression) ISopTaskDo {
	return s.withDO(s.DO.Clauses(conds...))
}

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

func (s sopTaskDo) Not(conds ...gen.Condition) ISopTaskDo {
	return s.withDO(s.DO.Not(conds...))
}

func (s sopTaskDo) Or(conds ...gen.Condition) ISopTaskDo {
	return s.withDO(s.DO.Or(conds...))
}

func (s sopTaskDo) Select(conds ...field.Expr) ISopTaskDo {
	return s.withDO(s.DO.Select(conds...))
}

func (s sopTaskDo) Where(conds ...gen.Condition) ISopTaskDo {
	return s.withDO(s.DO.Where(conds...))
}

func (s sopTaskDo) Order(conds ...field.Expr) ISopTaskDo {
	return s.withDO(s.DO.Order(conds...))
}

func (s sopTaskDo) Distinct(cols ...field.Expr) ISopTaskDo {
	return s.withDO(s.DO.Distinct(cols...))
}

func (s sopTaskDo) Omit(cols ...field.Expr) ISopTaskDo {
	return s.withDO(s.DO.Omit(cols...))
}

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

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

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

func (s sopTaskDo) Group(cols ...field.Expr) ISopTaskDo {
	return s.withDO(s.DO.Group(cols...))
}

func (s sopTaskDo) Having(conds ...gen.Condition) ISopTaskDo {
	return s.withDO(s.DO.Having(conds...))
}

func (s sopTaskDo) Limit(limit int) ISopTaskDo {
	return s.withDO(s.DO.Limit(limit))
}

func (s sopTaskDo) Offset(offset int) ISopTaskDo {
	return s.withDO(s.DO.Offset(offset))
}

func (s sopTaskDo) Scopes(funcs ...func(gen.Dao) gen.Dao) ISopTaskDo {
	return s.withDO(s.DO.Scopes(funcs...))
}

func (s sopTaskDo) Unscoped() ISopTaskDo {
	return s.withDO(s.DO.Unscoped())
}

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

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

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

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

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

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

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

func (s sopTaskDo) Attrs(attrs ...field.AssignExpr) ISopTaskDo {
	return s.withDO(s.DO.Attrs(attrs...))
}

func (s sopTaskDo) Assign(attrs ...field.AssignExpr) ISopTaskDo {
	return s.withDO(s.DO.Assign(attrs...))
}

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

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

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

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

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

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

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