2024-07-23 14:48:37 +08:00
|
|
|
package repo
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/1Panel-dev/1Panel/agent/constant"
|
|
|
|
"github.com/1Panel-dev/1Panel/agent/global"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
)
|
|
|
|
|
|
|
|
type DBOption func(*gorm.DB) *gorm.DB
|
|
|
|
|
2025-01-02 17:20:03 +08:00
|
|
|
func WithByID(id uint) DBOption {
|
2024-07-23 14:48:37 +08:00
|
|
|
return func(g *gorm.DB) *gorm.DB {
|
|
|
|
return g.Where("id = ?", id)
|
|
|
|
}
|
|
|
|
}
|
2025-01-02 17:20:03 +08:00
|
|
|
|
|
|
|
func WithByIDs(ids []uint) DBOption {
|
2024-08-21 18:04:51 +08:00
|
|
|
return func(g *gorm.DB) *gorm.DB {
|
|
|
|
return g.Where("id in (?)", ids)
|
|
|
|
}
|
|
|
|
}
|
2024-07-23 14:48:37 +08:00
|
|
|
|
2025-01-02 17:20:03 +08:00
|
|
|
func WithByName(name string) DBOption {
|
2024-07-23 14:48:37 +08:00
|
|
|
return func(g *gorm.DB) *gorm.DB {
|
|
|
|
return g.Where("name = ?", name)
|
|
|
|
}
|
|
|
|
}
|
2025-01-02 17:20:03 +08:00
|
|
|
|
|
|
|
func WithByLikeName(name string) DBOption {
|
2024-08-05 18:22:25 +08:00
|
|
|
return func(g *gorm.DB) *gorm.DB {
|
2024-08-21 18:04:51 +08:00
|
|
|
if len(name) == 0 {
|
|
|
|
return g
|
|
|
|
}
|
|
|
|
return g.Where("name like ?", "%"+name+"%")
|
2024-08-05 18:22:25 +08:00
|
|
|
}
|
|
|
|
}
|
2025-01-02 17:20:03 +08:00
|
|
|
|
|
|
|
func WithByDetailName(detailName string) DBOption {
|
2024-07-23 14:48:37 +08:00
|
|
|
return func(g *gorm.DB) *gorm.DB {
|
2024-08-21 18:04:51 +08:00
|
|
|
if len(detailName) == 0 {
|
|
|
|
return g
|
|
|
|
}
|
|
|
|
return g.Where("detail_name = ?", detailName)
|
2024-07-23 14:48:37 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-01-02 17:20:03 +08:00
|
|
|
func WithByType(tp string) DBOption {
|
2024-07-23 14:48:37 +08:00
|
|
|
return func(g *gorm.DB) *gorm.DB {
|
|
|
|
return g.Where("type = ?", tp)
|
|
|
|
}
|
|
|
|
}
|
2025-01-02 17:20:03 +08:00
|
|
|
|
|
|
|
func WithTypes(types []string) DBOption {
|
2024-08-21 18:04:51 +08:00
|
|
|
return func(db *gorm.DB) *gorm.DB {
|
|
|
|
return db.Where("type in (?)", types)
|
2024-07-23 14:48:37 +08:00
|
|
|
}
|
|
|
|
}
|
2025-01-02 17:20:03 +08:00
|
|
|
|
|
|
|
func WithByStatus(status string) DBOption {
|
2024-07-23 14:48:37 +08:00
|
|
|
return func(g *gorm.DB) *gorm.DB {
|
|
|
|
if len(status) == 0 {
|
|
|
|
return g
|
|
|
|
}
|
|
|
|
return g.Where("status = ?", status)
|
|
|
|
}
|
|
|
|
}
|
2025-01-02 17:20:03 +08:00
|
|
|
func WithByFrom(from string) DBOption {
|
2024-07-23 14:48:37 +08:00
|
|
|
return func(g *gorm.DB) *gorm.DB {
|
|
|
|
return g.Where("`from` = ?", from)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-01-02 17:20:03 +08:00
|
|
|
func WithByDate(startTime, endTime time.Time) DBOption {
|
2024-07-23 14:48:37 +08:00
|
|
|
return func(g *gorm.DB) *gorm.DB {
|
2024-08-21 18:04:51 +08:00
|
|
|
return g.Where("start_time > ? AND start_time < ?", startTime, endTime)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-01-02 17:20:03 +08:00
|
|
|
func WithByCreatedAt(startTime, endTime time.Time) DBOption {
|
2024-08-21 18:04:51 +08:00
|
|
|
return func(g *gorm.DB) *gorm.DB {
|
|
|
|
return g.Where("created_at > ? AND created_at < ?", startTime, endTime)
|
2024-07-23 14:48:37 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-01-02 17:20:03 +08:00
|
|
|
func WithOrderBy(orderStr string) DBOption {
|
2024-12-17 15:59:21 +08:00
|
|
|
if orderStr == "createdAt" {
|
|
|
|
orderStr = "created_at"
|
|
|
|
}
|
2024-07-23 14:48:37 +08:00
|
|
|
return func(g *gorm.DB) *gorm.DB {
|
|
|
|
return g.Order(orderStr)
|
|
|
|
}
|
|
|
|
}
|
2025-01-02 17:20:03 +08:00
|
|
|
func WithOrderRuleBy(orderBy, order string) DBOption {
|
2024-12-17 15:59:21 +08:00
|
|
|
if orderBy == "createdAt" {
|
|
|
|
orderBy = "created_at"
|
|
|
|
}
|
2024-07-23 14:48:37 +08:00
|
|
|
switch order {
|
|
|
|
case constant.OrderDesc:
|
|
|
|
order = "desc"
|
|
|
|
case constant.OrderAsc:
|
|
|
|
order = "asc"
|
|
|
|
default:
|
|
|
|
orderBy = "created_at"
|
|
|
|
order = "desc"
|
|
|
|
}
|
|
|
|
return func(g *gorm.DB) *gorm.DB {
|
|
|
|
return g.Order(fmt.Sprintf("%s %s", orderBy, order))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func getTx(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 getDb(opts...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func getDb(opts ...DBOption) *gorm.DB {
|
|
|
|
db := global.DB
|
|
|
|
for _, opt := range opts {
|
|
|
|
db = opt(db)
|
|
|
|
}
|
|
|
|
return db
|
|
|
|
}
|