// Code generated by gen-table. DO NOT EDIT.
// Code generated by gen-table. DO NOT EDIT.
// Code generated by gen-table. DO NOT EDIT.

package repository

import (
	"time"

	"github.com/ch3nnn/webstack-go/internal/dal/model"
	"github.com/ch3nnn/webstack-go/internal/dal/query"

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

var _ iStCategoryDao = (*stCategoryDao)(nil)

// ------------------------------------
// StCategory  ColumnName
// ------------------------------------
type iWhereStCategoryFunc interface {
	WhereByID(id int) func(dao gen.Dao) gen.Dao
	WhereByParentID(parentId int) func(dao gen.Dao) gen.Dao
	WhereBySort(sort int) func(dao gen.Dao) gen.Dao
	WhereByTitle(title string) func(dao gen.Dao) gen.Dao
	WhereByIcon(icon string) func(dao gen.Dao) gen.Dao
	WhereByLevel(level int32) func(dao gen.Dao) gen.Dao
	WhereByIsUsed(isUsed bool) func(dao gen.Dao) gen.Dao
	WhereByCreatedAt(createdAt time.Time) func(dao gen.Dao) gen.Dao
	WhereByUpdatedAt(updatedAt time.Time) func(dao gen.Dao) gen.Dao
	WhereByDeletedAt(deletedAt time.Time) func(dao gen.Dao) gen.Dao
}

// ------------------------------------
// StCategory  Generate Function
// ------------------------------------
type iGenStCategoryFunc interface {
	Create(m *model.StCategory) (*model.StCategory, error)
	Delete(whereFunc ...func(dao gen.Dao) gen.Dao) error
	DeletePhysical(whereFunc ...func(dao gen.Dao) gen.Dao) error
	Update(v interface{}, whereFunc ...func(dao gen.Dao) gen.Dao) (rowsAffected int64, err error)
	FindCount(whereFunc ...func(dao gen.Dao) gen.Dao) (int64, error)
	FindOne(whereFunc ...func(dao gen.Dao) gen.Dao) (*model.StCategory, error)
	FindAll(whereFunc ...func(dao gen.Dao) gen.Dao) ([]*model.StCategory, error)
	FindPage(page int, pageSize int, orderColumns []field.Expr, whereFunc ...func(dao gen.Dao) gen.Dao) ([]*model.StCategory, int64, error)
	Scan(result interface{}, whereFunc ...func(dao gen.Dao) gen.Dao) (err error)
	ScanPage(page int, pageSize int, orderColumns []field.Expr, result interface{}, whereFunc ...func(dao gen.Dao) gen.Dao) (count int64, err error)
}

type iStCategoryDao interface {
	iWhereStCategoryFunc
	iGenStCategoryFunc
}

type stCategoryDao struct {
	stCategoryDo query.IStCategoryDo
}

func (s *stCategoryDao) WhereByID(id int) func(dao gen.Dao) gen.Dao {
	return func(dao gen.Dao) gen.Dao {
		return dao.Where(query.StCategory.ID.Eq(id))
	}
}

func (s *stCategoryDao) WhereByParentID(parentId int) func(dao gen.Dao) gen.Dao {
	return func(dao gen.Dao) gen.Dao {
		return dao.Where(query.StCategory.ParentID.Eq(parentId))
	}
}

func (s *stCategoryDao) WhereBySort(sort int) func(dao gen.Dao) gen.Dao {
	return func(dao gen.Dao) gen.Dao {
		return dao.Where(query.StCategory.Sort.Eq(sort))
	}
}

func (s *stCategoryDao) WhereByTitle(title string) func(dao gen.Dao) gen.Dao {
	return func(dao gen.Dao) gen.Dao {
		return dao.Where(query.StCategory.Title.Eq(title))
	}
}

func (s *stCategoryDao) WhereByIcon(icon string) func(dao gen.Dao) gen.Dao {
	return func(dao gen.Dao) gen.Dao {
		return dao.Where(query.StCategory.Icon.Eq(icon))
	}
}

func (s *stCategoryDao) WhereByLevel(level int32) func(dao gen.Dao) gen.Dao {
	return func(dao gen.Dao) gen.Dao {
		return dao.Where(query.StCategory.Level.Eq(level))
	}
}

func (s *stCategoryDao) WhereByIsUsed(isUsed bool) func(dao gen.Dao) gen.Dao {
	return func(dao gen.Dao) gen.Dao {
		return dao.Where(query.StCategory.IsUsed.Is(isUsed))
	}
}

func (s *stCategoryDao) WhereByCreatedAt(createdAt time.Time) func(dao gen.Dao) gen.Dao {
	return func(dao gen.Dao) gen.Dao {
		return dao.Where(query.StCategory.CreatedAt.Eq(createdAt))
	}
}

func (s *stCategoryDao) WhereByUpdatedAt(updatedAt time.Time) func(dao gen.Dao) gen.Dao {
	return func(dao gen.Dao) gen.Dao {
		return dao.Where(query.StCategory.UpdatedAt.Eq(updatedAt))
	}
}

func (s *stCategoryDao) WhereByDeletedAt(deletedAt time.Time) func(dao gen.Dao) gen.Dao {
	return func(dao gen.Dao) gen.Dao {
		return dao.Where(query.StCategory.DeletedAt.Eq(deletedAt))
	}
}

func (s *stCategoryDao) Create(m *model.StCategory) (*model.StCategory, error) {
	if err := s.stCategoryDo.Create(m); err != nil {
		return nil, err
	}
	return s.FindOne(s.WhereByID(m.ID))
}

func (s *stCategoryDao) FindCount(whereFunc ...func(dao gen.Dao) gen.Dao) (int64, error) {
	return s.stCategoryDo.Scopes(whereFunc...).Count()
}

func (s *stCategoryDao) FindOne(whereFunc ...func(dao gen.Dao) gen.Dao) (*model.StCategory, error) {
	return s.stCategoryDo.Scopes(whereFunc...).First()
}

func (s *stCategoryDao) FindAll(whereFunc ...func(dao gen.Dao) gen.Dao) ([]*model.StCategory, error) {
	return s.stCategoryDo.Scopes(whereFunc...).Find()
}

func (s *stCategoryDao) FindPage(page int, pageSize int, orderColumns []field.Expr, whereFunc ...func(dao gen.Dao) gen.Dao) ([]*model.StCategory, int64, error) {
	return s.stCategoryDo.Scopes(whereFunc...).Order(orderColumns...).FindByPage((page-1)*pageSize, pageSize)
}

// 注意 当通过 struct 更新时，GORM 只会更新非零字段
// 如果想确保指定字段被更新，使用 map 来完成更新操作
func (s *stCategoryDao) Update(v interface{}, whereFunc ...func(dao gen.Dao) gen.Dao) (rowsAffected int64, err error) {
	info, err := s.stCategoryDo.Scopes(whereFunc...).Updates(v)
	if err != nil {
		return rowsAffected, err
	}

	return info.RowsAffected, nil
}

func (s *stCategoryDao) Delete(whereFunc ...func(dao gen.Dao) gen.Dao) error {
	if _, err := s.stCategoryDo.Scopes(whereFunc...).Delete(); err != nil {
		return err
	}
	return nil
}

func (s *stCategoryDao) DeletePhysical(whereFunc ...func(dao gen.Dao) gen.Dao) error {
	if _, err := s.stCategoryDo.Unscoped().Scopes(whereFunc...).Delete(); err != nil {
		return err
	}
	return nil
}

func (s *stCategoryDao) Scan(result interface{}, whereFunc ...func(dao gen.Dao) gen.Dao) (err error) {
	return s.stCategoryDo.Scopes(whereFunc...).Scan(result)
}

func (s *stCategoryDao) ScanPage(page int, pageSize int, orderColumns []field.Expr, result interface{}, whereFunc ...func(dao gen.Dao) gen.Dao) (count int64, err error) {
	return s.stCategoryDo.Scopes(whereFunc...).Order(orderColumns...).ScanByPage(result, (page-1)*pageSize, pageSize)
}
