2024-07-29 18:09:57 +08:00
|
|
|
package repo
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-08-22 18:48:55 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/agent/constant"
|
|
|
|
"github.com/1Panel-dev/1Panel/agent/global"
|
2024-08-21 18:04:51 +08:00
|
|
|
|
2024-07-29 18:09:57 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/agent/app/model"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
)
|
|
|
|
|
|
|
|
type TaskRepo struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
type ITaskRepo interface {
|
|
|
|
Create(ctx context.Context, task *model.Task) error
|
|
|
|
GetFirst(opts ...DBOption) (model.Task, error)
|
|
|
|
Page(page, size int, opts ...DBOption) (int64, []model.Task, error)
|
|
|
|
Update(ctx context.Context, task *model.Task) error
|
|
|
|
|
|
|
|
WithByID(id string) DBOption
|
2024-08-01 15:31:44 +08:00
|
|
|
WithResourceID(id uint) DBOption
|
2024-08-02 16:00:15 +08:00
|
|
|
WithOperate(taskOperate string) DBOption
|
2024-07-29 18:09:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewITaskRepo() ITaskRepo {
|
|
|
|
return &TaskRepo{}
|
|
|
|
}
|
|
|
|
|
2024-08-22 18:48:55 +08:00
|
|
|
func getTaskDb(opts ...DBOption) *gorm.DB {
|
|
|
|
db := global.TaskDB
|
|
|
|
for _, opt := range opts {
|
|
|
|
db = opt(db)
|
|
|
|
}
|
|
|
|
return db
|
|
|
|
}
|
|
|
|
|
|
|
|
func getTaskTx(ctx context.Context, opts ...DBOption) *gorm.DB {
|
|
|
|
tx, ok := ctx.Value(constant.DB).(*gorm.DB)
|
|
|
|
if ok {
|
|
|
|
for _, opt := range opts {
|
|
|
|
tx = opt(tx)
|
|
|
|
}
|
|
|
|
return tx
|
|
|
|
}
|
|
|
|
return getTaskDb(opts...)
|
|
|
|
}
|
|
|
|
|
2024-07-29 18:09:57 +08:00
|
|
|
func (t TaskRepo) WithByID(id string) DBOption {
|
|
|
|
return func(g *gorm.DB) *gorm.DB {
|
|
|
|
return g.Where("id = ?", id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-02 16:00:15 +08:00
|
|
|
func (t TaskRepo) WithOperate(taskOperate string) DBOption {
|
|
|
|
return func(g *gorm.DB) *gorm.DB {
|
|
|
|
return g.Where("operate = ?", taskOperate)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-01 15:31:44 +08:00
|
|
|
func (t TaskRepo) WithResourceID(id uint) DBOption {
|
|
|
|
return func(g *gorm.DB) *gorm.DB {
|
|
|
|
return g.Where("resource_id = ?", id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-29 18:09:57 +08:00
|
|
|
func (t TaskRepo) Create(ctx context.Context, task *model.Task) error {
|
2024-08-22 18:48:55 +08:00
|
|
|
return getTaskTx(ctx).Create(&task).Error
|
2024-07-29 18:09:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t TaskRepo) GetFirst(opts ...DBOption) (model.Task, error) {
|
|
|
|
var task model.Task
|
2024-08-22 18:48:55 +08:00
|
|
|
db := getTaskDb(opts...).Model(&model.Task{})
|
2024-07-29 18:09:57 +08:00
|
|
|
if err := db.First(&task).Error; err != nil {
|
|
|
|
return task, err
|
|
|
|
}
|
|
|
|
return task, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t TaskRepo) Page(page, size int, opts ...DBOption) (int64, []model.Task, error) {
|
|
|
|
var tasks []model.Task
|
2024-08-22 18:48:55 +08:00
|
|
|
db := getTaskDb(opts...).Model(&model.Task{})
|
2024-07-29 18:09:57 +08:00
|
|
|
count := int64(0)
|
|
|
|
db = db.Count(&count)
|
|
|
|
err := db.Limit(size).Offset(size * (page - 1)).Find(&tasks).Error
|
|
|
|
return count, tasks, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t TaskRepo) Update(ctx context.Context, task *model.Task) error {
|
2024-08-22 18:48:55 +08:00
|
|
|
return getTaskTx(ctx).Save(&task).Error
|
2024-07-29 18:09:57 +08:00
|
|
|
}
|