mirror of
https://gitee.com/dromara/mayfly-go
synced 2025-11-03 16:00:25 +08:00
fix: 机器计划任务、数据库迁移任务初始化问题修复
This commit is contained in:
@@ -62,6 +62,9 @@ type App[T model.ModelI] interface {
|
||||
// @param cond 可为*model.QueryCond也可以为普通查询model
|
||||
CountByCond(cond any) int64
|
||||
|
||||
// CursorByCond 根据指定条件遍历model表数据
|
||||
CursorByCond(cond any, handler func(T) error) error
|
||||
|
||||
// Tx 执行事务操作
|
||||
Tx(ctx context.Context, funcs ...func(context.Context) error) (err error)
|
||||
}
|
||||
@@ -152,6 +155,29 @@ func (ai *AppImpl[T, R]) CountByCond(cond any) int64 {
|
||||
return ai.GetRepo().CountByCond(cond)
|
||||
}
|
||||
|
||||
func (ai *AppImpl[T, R]) CursorByCond(cond any, handler func(T) error) error {
|
||||
offset := 0
|
||||
batchSize := 200
|
||||
for {
|
||||
data, err := ai.GetRepo().SelectByCondWithOffset(cond, batchSize, offset)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if len(data) == 0 {
|
||||
break
|
||||
}
|
||||
|
||||
for _, item := range data {
|
||||
if err := handler(item); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
offset += len(data)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Tx 执行事务操作
|
||||
func (ai *AppImpl[T, R]) Tx(ctx context.Context, funcs ...func(context.Context) error) (err error) {
|
||||
dbCtx := ctx
|
||||
|
||||
@@ -89,6 +89,9 @@ type Repo[T model.ModelI] interface {
|
||||
|
||||
// CountByCond 根据指定条件统计model表的数量
|
||||
CountByCond(cond any) int64
|
||||
|
||||
// SelectByCondWithOffset 根据条件查询数据并支持 offset + limit 分页
|
||||
SelectByCondWithOffset(cond any, limit int, offset int) ([]T, error)
|
||||
}
|
||||
|
||||
var _ (Repo[*model.Model]) = (*RepoImpl[*model.Model])(nil)
|
||||
@@ -251,6 +254,15 @@ func (br *RepoImpl[T]) CountByCond(cond any) int64 {
|
||||
return gormx.CountByCond(br.GetModel(), toQueryCond(cond))
|
||||
}
|
||||
|
||||
func (br *RepoImpl[T]) SelectByCondWithOffset(cond any, limit int, offset int) ([]T, error) {
|
||||
var models []T
|
||||
err := gormx.NewQuery(br.GetModel(), toQueryCond(cond)).GenGdb().Limit(limit).Offset(offset).Find(&models).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return models, nil
|
||||
}
|
||||
|
||||
// NewModel 新建模型实例
|
||||
func (br *RepoImpl[T]) NewModel() T {
|
||||
newModel := reflect.New(br.getModelType()).Interface()
|
||||
|
||||
Reference in New Issue
Block a user